0

I'm deploying a contract usign ethers.js I do it as follows:

const tokenWeiPrice = (answers.priceEur / ethEur) * Math.pow(10, 18);

const contract = await factory.deploy(
   answers.maxSupply,
   answers.eventName,
   answers.symbol,
   0,
   expireAfter,
   tokenWeiPrice
);

While deploying it throws an error

(node:11654) UnhandledPromiseRejectionWarning: Error: invalid constructor argument (arg="_ticketPrice", reason="invalid number value", value=43478260869565220, version=4.0.20)

the problem stays in tokenWeiPrice I would like to know why, in my contract this is a uint256 constructor's parameter.

  • You're using integer values way to large for javascript (larger than Number.MAX_SAFE_INTEGER). Use BigNumber conclusively. – goodvibration Jan 10 '19 at 13:36
  • Also, tokenWeiPrice is likely not even an integer, so you should by the least use Math.floor(tokenWeiPrice). But like I said, require("bignumber.js") and use it everywhere. Of course, you'll need to make sure you round intermediate results before passing them to your contract. Ideally, given a BigNumber named x, use x.toFixed() in order to pass it as a proper string. – goodvibration Jan 10 '19 at 13:37
  • Hi goodvibration thanks for your answer I corrected it and it works – Marcello Bardus Jan 10 '19 at 13:43
  • NP, I just wrote an answer to clarify these two comments properly. Check it out. – goodvibration Jan 10 '19 at 13:47
2

const tokenWeiPrice = (answers.priceEur / ethEur) * Math.pow(10, 18);

Problem #1: 10 ^ 18 > Number.MAX_SAFE_INTEGER

Problem #2: tokenWeiPrice is not necessarily an integer.

You should use BigNumber conclusively for the purpose of interacting with contracts.

If ethers.js doesn't support this type of input, then before passing variable x of type BigNumber to your contract, use x.toFixed() in order to convert it into a proper string.

For example:

let BigNumber = require("bignumber.js");

const tokenWeiPrice = new BigNumber(10).pow(18).times(priceEur).dividedToIntegerBy(ethEur);

const contract = await factory.deploy(
   answers.maxSupply,
   answers.eventName,
   answers.symbol,
   0,
   expireAfter,
   tokenWeiPrice.toFixed()
);
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.