0

Using web3js v1.2.1

const contract = new web3.eth.Contract(abi, '0x29...');
const amount = 1 * 10 ** 18;
const data = contract.methods.transfer(recipient, amount).encodeABI();

I get error :

invalid number value (arg="value", coderType="uint256", val


 reason: 'invalid number value',
  code: 'INVALID_ARGUMENT',
  arg: 'value',
  coderType: 'uint256',
  value: 1000000000000000000 }

This should work:

const amount = web3.utils.toHex(1e18)
  • use web3 method to convert ether to wei – Majd TL Oct 17 '19 at 6:44
1

javascript cannot handle those big numbers, Use the method web3.utils.toWei(number [, unit]).

const amount = web3.utils.toWei(1, 'ether');
| improve this answer | |
  • Error: Please pass numbers as strings or BigNumber objects to avoid precision errors. – Alvin Oct 17 '19 at 9:58
  • vlt web3.utils.toBN(web3.utils.toWei(1, 'ether')); – Majd TL Oct 17 '19 at 13:35
  • oder (web3.utils.toWei(1, 'ether')).toString()); – Majd TL Oct 17 '19 at 13:37
0

You should pass a string or a BN object, not an integer.

Also note that this specific integer (10 ** 18) is larger than Number.MAX_SAFE_INTEGER and should therefore not be used for any other purpose.

Here are a few options which you can choose from:

const amount = "1e18";
const amount = Web3.utils.toBN("1e18");
const amount = Web3.utils.toBN(10).pow(18);

Or with a variable x = 18 (which you can change of course):

const amount = `1e${x}`;
const amount = Web3.utils.toBN(`1e${x}`);
const amount = Web3.utils.toBN(10).pow(x);
| improve this answer | |
  • All the above give me the same error. or error : num.bitLength is not a function – Alvin Oct 17 '19 at 9:59
  • @Alvin: Well maybe you are passing recipient and amount in the wrong order. It's hard to say, because you haven't posted your implementation of function transfer (the standard function indeed uses this order of parameters, but perhaps you've implemented your own version, who knows...). – goodvibration Oct 17 '19 at 10:14

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.