26

I am trying to call a custom function of a contract that expects a parameter of unit256.

I'm 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 anyone 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)

TylerH
  • 20,799
  • 66
  • 75
  • 101
Fariha Abbasi
  • 1,212
  • 2
  • 13
  • 19

4 Answers4

59

I tried sending the parameter as a String and it worked.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Fariha Abbasi
  • 1,212
  • 2
  • 13
  • 19
  • 1
    I got this error on running a waffle test: `expect(await vrfContract.getHardcodedUintNumber()).to.be.equal(18446205110165755834005948204546580960626098221936403173208959885300094367089);` 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("18446205110165755834005948204546580960626098221936403173208959885300094367089");` – a.t. Sep 09 '21 at 00:12
  • 1
    This works. Solidity sometimes can be really awful. – Marco Altieri Oct 23 '22 at 19:09
  • Got the error when trying to run Value.new(lovelaces) where lovelaces came in from the BE api. Resolved by converting to string and processing the BigNum from it; after hours of debugging found this post!! lovelaces = BigNum.from_str(lovelaces.toString()) – anusreemn Feb 28 '23 at 11:27
3

I'm using BigInt in my Truffle UnitTest ( this is typescript code )

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'
    );
  });
vanduc1102
  • 5,769
  • 1
  • 46
  • 43
1

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
   });
Aun Shahbaz Awan
  • 559
  • 7
  • 10
0

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();