'Unable to call contract function from web3 with big number as parameter
Hi I am trying to call a custom function of a contract that expects a parameter of unit256.
Im calling this function from web3 with this value as parameter: 10000000000000000000 (10 with 18 zeros) As soon as this call is hit by web3, I faced following Big number error:
Error: overflow (fault="overflow", operation="BigNumber.from", value=10000000000000000000, code=NUMERIC_FAULT, version=bignumber/5.0.0-beta.138)
Does any one know the cause?
Here is the function of the contract I'm calling:
function lock(
address tokenAddress,
uint256 amount
)
and here is the web3 code snippet:
Contract.methods.lock(0x57AA33D53351eA4BF00C6F10c816B3037E268b7a, 10000000000000000000,
).send({
from: accounts[0],
gasLimit: 500000,
value: 0
});
I tried the same function with small values for amount and it worked e.g. 1(with 18 zeros)
Solution 1:[1]
I tried sending the parameter as String and it worked.
Posting this answer, so might be helpful for somebody.
Solution 2:[2]
I'm using BigInt in my Truffle UnitTest
it('should return correct balances when transfer', async () => {
const receiver = accounts[2];
const balanceOfOwner = await contractInstance.balanceOf.call(owner);
assert.equal(balanceOfOwner, totalSupply * 10 ** decimals, 'Total balance');
const sendAmount = 69 * 10 ** decimals;
await contractInstance.transfer(receiver, BigInt(sendAmount), {
from: owner,
});
const balanceOfReceiver = await contractInstance.balanceOf.call(receiver);
assert.equal(balanceOfReceiver, sendAmount, 'Received sendAmount');
assert.equal(
await contractInstance.balanceOf.call(owner),
balanceOfOwner - sendAmount,
'Decreased to'
);
});
Solution 3:[3]
The reason why can be found in the documentation of the ethers package:
Why can't I just use numbers? The first problem many encounter when dealing with Ethereum is the concept of numbers. Most common currencies are broken down with very little granularity. For example, there are only 100 cents in a single dollar. However, there are 1018 wei in a single ether. JavaScript uses IEEE 754 double-precision binary floating point numbers to represent numeric values. As a result, there are holes in the integer set after 9,007,199,254,740,991; which is problematic for Ethereum because that is only around 0.009 ether (in wei), which means any value over that will begin to experience rounding errors. (...) To remedy this, all numbers (which can be large) are stored and manipulated as Big Numbers.
Solution 4:[4]
For me, I was trying to call .toNumber() on a BigNumber and the error came up. It went away when I used .toString() instead
x.toString();
Solution 5:[5]
To enable the BigInt global you can add a comment to your code:
/* global BigInt */
and here is the web3 code snippet:
Contract.methods.lock(0x57AA33D53351eA4BF00C6F10c816B3037E268b7a,
BigInt(10000000000000000000)).send({
from: accounts[0],
gasLimit: 500000,
value: 0
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Fariha Abbasi |
| Solution 2 | vanduc1102 |
| Solution 3 | GongFu |
| Solution 4 | Ogubuike Alex |
| Solution 5 | Aun Shahbaz Awan |
