我正在尝试调用期望参数为 unit256 的合约的自定义函数。
我从 web3 调用这个函数,这个值作为参数:10000000000000000000(10 有 18 个零) 一旦这个调用被 web3 命中,我就面临以下大数错误:
错误:溢出(故障=“溢出”,操作=“BigNumber.from”,值=10000000000000000000,代码=NUMERIC_FAULT,版本=bignumber/5.0.0-beta.138)**
有谁知道原因吗?
这里是我调用的合约函数:
function lock(
address tokenAddress,
uint256 amount
)
这里是 web3 代码片段:
Contract.methods.lock(0x57AA33D53351eA4BF00C6F10c816B3037E268b7a, 10000000000000000000,
).send({
from: accounts[0],
gasLimit: 500000,
value: 0
});
我尝试了相同的函数,使用较小的 amount 值,它起作用了,例如1(有 18 个零)
我尝试将参数作为字符串发送,它成功了。
我在我的 Truffle UnitTest 中使用 BigInt(这是打字稿代码)
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'
);
});
要启用 BigInt 全局,您可以在代码中添加注释:
/* global BigInt */
这里是 web3 代码片段:
Contract.methods.lock(0x57AA33D53351eA4BF00C6F10c816B3037E268b7a,
BigInt(10000000000000000000)).send({
from: accounts[0],
gasLimit: 500000,
value: 0
});
对我来说,我试图在 BigNumber 上调用 .toNumber() 并出现错误。当我改用 .toString() 时它消失了
x.toString();