ethereumweb3jstrufflebignumber.js

Calling web3.utils.BN.add gives error: Cannot create property 'negative' on number


At truffle test, I am trying to add two Big Numbers together using the following code:

myBignumber = new web3.utils.BN(1);
myBignumber.add(2)

But it caused this error:

Cannot create property 'negative' on number 2

Versions:


Solution

  • The BN used at Web3 has some open issues (ref)

    Suggested Alternative:

    Try using another BigNumber library that has a working addition function (like this).

    To use the library install it:

    npm install bignumber.js
    

    Now at your truffle test:

        var BigNumber = require("bignumber.js");
    
        ...
        // Instead of the commented lines, use the next, uncommitted, ones:
        // myBignumber = new web3.utils.BN(1);
        // myBignumber.add(2)
        myBignumber = new BigNumber(1); 
        myBignumber.plus(2);