I am having problem using bignumber.js with web3:

const sender = '0xss';
const recipient = '0xxx';
const amount = 1000; // have to use Bignumber for more than 1000
const decimals = 18;
const amountHex = web3.utils.toHex(new BigNumber(`${amount}e+${decimals}`));
const count = await web3.eth.getTransactionCount(sender);
const data = contract.methods.transfer(recipient, amountHex).encodeABI();
const estimateGas = await web3.eth.estimateGas({
    from: sender,
    nonce: '0x' + count.toString(16),
    to: '0xff',

I get error at estimateGas :

Error: Returned error: The execution failed due to an exception.
  • 1
    It means that contract.methods.transfer would fail if you called it with the given parameters. – goodvibration Dec 17 '19 at 10:49
  • 1
    But what's wrong? – Alvin Dec 17 '19 at 12:08
  • 1
    You gotta do some debugging yourself dude. And if you're expecting any help here, then you should by the least include ALL the relevant information. For example, I have no idea what your transfer function looks like and what it could fail for, because you haven't bothered to post the code of this function! – goodvibration Dec 17 '19 at 12:27
  • okay. When I use Number instead of BigNumber, web3.utils.toHex(Number(${amount}e${decimals})); then it work. – Alvin Dec 17 '19 at 14:09
  • Hey, I use the exact same ${amount}e${decimals} trick! Where did you learn that? – goodvibration Dec 17 '19 at 14:11

Web3.js v0.x relies on BigNumber.

Web3.js v1.x relies on BN.

The fact that you're calling web3.utils.toHex implies that you're using Web3.js v1.x.

Also note that you don't need your amount as a hex string; you can use either an integer or a string which represents an integer (which can be in either decimal format or hexadecimal format) or a BN instance.

So in short, instead of this:

const amountHex = web3.utils.toHex(new BigNumber(`${amount}e+${decimals}`));

You can use either one of the following:

const amount = `${amount}e+${decimals}`;
const amount = web3.utils.toBN(`${amount}e+${decimals}`);

You may also be tempted to use this:

const amount = Number(`${amount}e+${decimals}`);

However, since the value represented by this string is most likely larger than Number.MAX_SAFE_INTEGER, you should avoid this option as it will potentially yield an innaccurate value.

  • I use Number, const amount = Number(${amount}e+${decimals});, but it does not support bignumber, when I put in 1000 for amount, it prompted bn.js error. – Alvin Dec 17 '19 at 14:26
  • @Alvin: Have you read my answer? It actually recommends NOT to use this option. Have you tried either one of the options that it does suggest? – goodvibration Dec 17 '19 at 14:32
  • Note to readers: avoid web3.utils.toBN(`${amount}e+${decimals}`), as the BN class interprets the input string as hexadecimal. – goodvibration Jun 23 '20 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.