无法从 web3 调用以大数字作为参数的合约函数

2022-09-01 21:56:47

我正在尝试调用一个合约的自定义函数,该函数需要一个 unit256 的参数。

我从web3调用此函数,此值作为参数:1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

错误:溢出(错误=“溢出”,操作=“BigNumber.from”,值=1000000000000000000000,代码=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
        });

我尝试了相同的函数,金额值很小,它的工作原理例如1(18个零)


答案 1

我尝试将参数作为字符串发送,它的工作原理。


答案 2

我在我的松露单元测试中使用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'
    );
  });

推荐