Loading...
While dealing with smart contracts users have to work with large numbers . When we interact with smart contract using javascript handling the large numbers is sometimes troublesome . To deal with those large numbers we can use BN from web3.utils .
var BN = web3.utils.BN;
new BN(1231238019830)
>"1231238019830"
We can do all calculations on the big numbers using web3.utils.BN
var number = new BigNumber(321323);
web3.utils.isBigNumber(number);
> true
web3.utils.toBN(3123231).toString();
Now lets look at the example of converting to big number to send argument to contract methods ,
var decimals = 20;
myContract.methods.set(web3.utils.toBN(10 ** (decimals))).send({ from: `${user_address}` },
function (err, result) {if (!err) {console.log(result);}
});
here users transacts the set method and sends big number converted argument to the smart contract.