1

This is my solidity codes, I am trying to compare the variables passed with different variables. Because float data type is not supported by solidity and function input is default tailed with 18 zeros, I use the timeAmount to multiply 10^18.

function setUriToUpdate(uint256 temperature) public {
    uint256 timesAmount = 10 ** 18;
    if (temperature < 10 * timesAmount) {
       uriToUpdate = METADATA_BELOW;
    } else if (temperature > 10 * timesAmount && temperature < 20 * timesAmount) {
       uriToUpdate = METADATA_AVERAGE;
    } else if (temperature >= 20 * timesAmount && temperature < 50 * timesAmount) {
       uriToUpdate = METADATA_ABOVE;
    }
}

When I write my unit test in js code, I don't know to pass a variable tailed with 18 zeros.

await nft.connect(deployer).setUriToUpdate(15 * 10 ** 18)

The error is:

Error: overflow [ See: https://links.ethers.org/v5-errors-NUMERIC_FAULT-overflow ] (fault="overflow", operation="BigNumber.from", value=15000000000000000000, code=NUMERIC_FAULT, version=bignumber/5.7.0)

Can anyone tell me how to pass big number to the function?

thanks in advance!

1
  • Convert the number into String and send it to smart contract. This happened because Javascript doesn't support number more than 53 bits. Dec 30, 2022 at 11:48

2 Answers 2

0

The reason why is failing is because of the result of 15 * 10 ** 18 is beyond what is considered a safe range in Javascript.

If you print the result of this in Javascript:

console.log("Number.MAX_SAFE_INTEGER: ", Number.MAX_SAFE_INTEGER);

You'll get:

9007199254740991

Which is a 53-bit number. It has 16 digits.

If you print the result of 15 * 10 ** 18:

15000000000000000000

Way bigger than the Number.MAX_SAFE_INTEGER with 20 digits.

Check the ethersjs docs here: https://docs.ethers.org/v5/api/utils/bignumber/

To solve this, you have multiple options:

  1. Convert each of the numbers to the built-in Javascript BigInt type, appending an n at the end of the number:
...setUriToUpdate(15n * 10n ** 18n)...

Convert the result to a string:

contract.setUriToUpdate("15000000000000000000").

Use ethers.BigNumber.from to convert each of the factors before multiplying:

const fifteen = ethers.BigNumber.from(15);
const ten = ethers.BigNumber.from(10);
const eighteen = ethers.BigNumber.from(18);

contract.setUriToUpdate(fifteen.mul(ten).mul(eighteen)).then(response => {
    console.log("response2: ", response) 
});

And so on.

0

The error is due to the overflow and you may use BigInt data type. To use it just include

/* global BigInt */

at the top of your code file. Then use BigInt to calculate instead of the Number data type.

const bigNumber = BigInt(/*smthing*/).toString(10)

I hope it helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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