Unable to call contract function from web3 with big number as parameter

16,817

Solution 1

I tried sending the parameter as String and it worked.

Posting this answer, so might be helpful for somebody.

Solution 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'
    );
  });
Share:
16,817
Fariha Abbasi
Author by

Fariha Abbasi

Updated on June 05, 2022

Comments

  • Fariha Abbasi
    Fariha Abbasi almost 2 years

    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)

  • a.t.
    a.t. over 2 years
    I got this error on running a waffle test: expect(await vrfContract.getHardcodedUintNumber()).to.be.equal(1844620511‌​01657558340059482045‌​46580960626098221936‌​40317320895988530009‌​4367089); and was a bit confused by the answer as I thought (and think) it implied: "return the uint256 as a string from the function", even though, in my case, it should return a uint256. However, putting the expected number in double quotes resolved (as a string) it: expect(await vrfContract.getHardcodedUintNumber()).to.be.equal("184462051‌​10165755834005948204‌​54658096062609822193‌​64031732089598853000‌​94367089");
  • zozzancs
    zozzancs about 2 years
    thank you, I can't believe a simple toString() would solve this, I spent couple of hours on this :)
  • Davide Arcinotti
    Davide Arcinotti about 2 years
    I don't understand what is the resolution you are suggesting