Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bignumber issue in web3 invalid number value (arg="_value", coderType="uint256", value=10000000000000000) #2077

Open
patidarmanoj10 opened this issue Nov 29, 2018 · 43 comments

Comments

@patidarmanoj10
Copy link

commented Nov 29, 2018

Getting invalid number error when calling a method of a contract which take uint256 argument.

Example:
myContract.methods.transfer(accounts[1], 1e16).send({ from: accounts[0] })

  1. When sending value 1e16 it throw error
    web3 invalid number value (arg="_value", coderType="uint256", value=10000000000000000)
  2. When sending value 10000000000000000 throw same error
  3. When sending value 1e16 as hex it work okay. No error.
  4. When sending value 1e15 or less value as number like 100000000000000 if work okay. No error

I am using web3 1.0.0-beta.36

@nivida nivida added the bug label Nov 30, 2018

@nivida

This comment has been minimized.

Copy link
Member

commented Nov 30, 2018

Thanks for submitting this issue!
I think this an issue of the new AbiCoder from ethers.js I will update the ethers.js package and test it.

@nivida nivida added this to To do in 1.0 Nov 30, 2018

@nivida nivida added this to the 1.0 milestone Nov 30, 2018

@patidarmanoj10

This comment has been minimized.

Copy link
Author

commented Nov 30, 2018

@nivida . More details- I tested in old version of web3 . This is not in issue in beta.35 and beta.34 . Seems like breaking in beta.36 only.

@nivida

This comment has been minimized.

Copy link
Member

commented Nov 30, 2018

@patidarmanoj10 Yes this is because we use the new AbiCoder of ethers.js since beta36. This should be fixed quickly and released with the next build thanks for testing it. My plan is to write the AbiCoder by my self after the 1.0 stable release because it's dangerous to have such an important part of this lib as external dep. :-)

@gabmontes

This comment has been minimized.

Copy link
Contributor

commented Nov 30, 2018

@nivida what version of ethers.js do you think is good?

@nivida

This comment has been minimized.

Copy link
Member

commented Dec 1, 2018

@gabmontes Hopefully the latest version has fixed it. (4.0.15)
I will test it by own asap.

@markerdmann

This comment has been minimized.

Copy link

commented Dec 3, 2018

I was able to reproduce the bug in version 4.0.15:

> ethersAbiCoder.encode(['uint256'], [1e+14])
'0x00000000000000000000000000000000000000000000000000005af3107a4000'
> ethersAbiCoder.encode(['uint256'], [1e+16])
Error: invalid number value (arg="", coderType="uint256", value=10000000000000000, version=4.0.15)
    at Object.throwError (/private/tmp/node_modules/ethers/errors.js:76:17)
    at CoderNumber.encode (/private/tmp/node_modules/ethers/utils/abi-coder.js:390:20)
    at /private/tmp/node_modules/ethers/utils/abi-coder.js:642:59
    at Array.forEach (<anonymous>)
    at pack (/private/tmp/node_modules/ethers/utils/abi-coder.js:641:12)
    at CoderTuple.encode (/private/tmp/node_modules/ethers/utils/abi-coder.js:801:16)
    at AbiCoder.encode (/private/tmp/node_modules/ethers/utils/abi-coder.js:938:77)
> ethersAbiCoder.encode(['uint256'], [1e+15])
'0x00000000000000000000000000000000000000000000000000038d7ea4c68000'
@eyezick

This comment has been minimized.

Copy link
Contributor

commented Dec 4, 2018

This is also referenced in #1920 just fyi

@ecouffes

This comment has been minimized.

Copy link

commented Dec 17, 2018

The same issue in beta.37.

@yueawang

This comment has been minimized.

Copy link

commented Dec 25, 2018

Any update on this issue??
I just met in beta.37.

@patidarmanoj10 , you mentioned

When sending value 1e16 as hex it work okay. No error.

Does that mean a workaround for this issue?? If I convert my number to that format.

Thanks!

@patidarmanoj10

This comment has been minimized.

Copy link
Author

commented Dec 26, 2018

@yueawang yes.

@mattcrooks

This comment has been minimized.

Copy link

commented Jan 8, 2019

In addition to the BigNumber issue we are also getting an error with Bytes3:
Error: invalid bytes3 value (arg="ccy", coderType="bytes3", value="USD")

Workaround:
web3.utils.asciiToHex('USD')

We are using truffle 5.0.1 and most unit tests are failing due to this error.

Any update on if/when this issue will be resolved?

Many thanks!

@kombos

This comment has been minimized.

Copy link

commented Jan 17, 2019

I'm using the following truffle version and this same issue is cropping up:
Truffle v5.0.2 (core: 5.0.2)
Solidity v0.5.0 (solc-js)
Node v11.6.0
web3@1.0.0-beta.37

once I allocate a value greater than 1e15 to uint256 (like 1e16, or 1e17), it breaks and throws an error during truffle deployment of contract (using constructor arguments):

Error: invalid number value (arg="_var", coderType="uint256", value=10000000000000000)

@0x-genesys

This comment has been minimized.

Copy link

commented Jan 22, 2019

I am facing the same issue for latest web3 1.0.0 version

@nivida

This comment has been minimized.

Copy link
Member

commented Jan 22, 2019

This should be solved in this git tag and will be released within the next week:
https://github.com/ethereum/web3.js/tree/v1.0.0-beta.38-rc2

Would be great if someone could quickly test it for me.

@markerdmann

This comment has been minimized.

Copy link

commented Jan 22, 2019

@nivida It looks like I'm still getting the error:

Error: invalid BigNumber string value (arg="value", value="1e+24", version=4.0.21)

I cloned Truffle, checked out v5.0.2, cloned web3 inside truffle/packages and checked out v1.0.0-beta.38-rc2, and then updated relevant package.json files to point everything in my project and in truffle to that version of web3. Not sure if that's the easiest way to do it, but the stack trace for the error above is pointing to mytruffle/packages/truffle-contract/node_modules/ethers/errors.js:76:17.

Edit: I think the problem is just that it's still pointing to the wrong version of web3-eth-abi and ethers. I'll look for a better way to replace web3 in my local clone of truffle.

@GuthL

This comment has been minimized.

Copy link

commented Jan 23, 2019

I have the same error on Remix, after generating a smart contract from circom (0k knowledge proof).

@nivida

This comment has been minimized.

Copy link
Member

commented Jan 25, 2019

@markerdmann I will release a Web3.js version with the latest release of ethers.js. But as I saw are you already using the latest version of ethers.js version=4.0.21. Because of this would I recommend to open an issue in the ethers.js repository for fixing this.

@inyutin

This comment has been minimized.

Copy link

commented Jan 29, 2019

The same issue on 1.0.0-beta.40

caffeinum added a commit to caffeinum/swap.core that referenced this issue Jan 30, 2019

@leonprou

This comment has been minimized.

Copy link
Contributor

commented Feb 4, 2019

Same issue also on 1.0.0-beta.41.

@nivida
I made a little dig into code the of ethers.js and found that they are using their own implementation of bignumber. So when I (and the others I suppose) talk about bignumber.js, ethers got their own implementation.

In beta.36 ethers.js was introduced to web3-eth-abi and since than it's not functioning with bignumber.js.

@chrsengel

This comment has been minimized.

Copy link

commented Feb 6, 2019

I had the same error when passing wei values in truffle tests and my temporary solution is using web3.utils.toWei which seems to work. Maybe someone will find this helpful as an interim solution.

@leonprou

This comment has been minimized.

Copy link
Contributor

commented Feb 7, 2019

@chrsengel yeah it works cause it converts the bignumber to string, and ethers accepts strings.

It's actually may be a good idea to accepts less types to eliminate errors. I think it's ok to translate every number to a string.

lbeder added a commit to lbeder/web3-abi-helper that referenced this issue Feb 7, 2019

@nivida

This comment has been minimized.

Copy link
Member

commented Feb 7, 2019

Web3.js does have currently two other issues with the abi coder of ethers.js the possible quick fixes are:

  • back to an older version of ethers.js
  • using of the ethereumjs abi coder
  • quick fixing of web3-utils and web3-core-helpers
  • Creation of an web3-types module with all possible types (Hex, Iban, Address etc.)

I will check all the possibilities tomorrow and will choose the fastest end best solution of all.

@nivida

This comment has been minimized.

Copy link
Member

commented Feb 8, 2019

I've tested it now and this is an issue of ethers.js and will also occur in the latest version of ethers.js. I've created an issue in the ethers.js GitHub repository: ethers-io/ethers.js#418

@leonprou

This comment has been minimized.

Copy link
Contributor

commented Feb 8, 2019

@nivida Yes that makes sense, ethers.js using now their own implementation of BigNumber (or maybe doing some wrapping), wrote it in my last comment 👀 .

@GaJain

This comment has been minimized.

Copy link

commented Feb 22, 2019

@nivida - The issue persists with 1.0.0-beta.46 . Any workaround for this ?

@pjonson2

This comment has been minimized.

Copy link

commented Feb 28, 2019

@nivida - I'm having the same issue. The numbers I'm trying to pass are 5*10^18 and 10^18. I have tried the following and have had the same error as what is within the header of this issue.

Example:
myContract.methods.transfer(accounts[1], 1e16).send({ from: accounts[0] })

  1. When sending value 1e18 an error is thrown.
    web3 invalid number value (arg="_value", coderType="uint256", value=1000000000000000000)
  2. When sending value 1000000000000000000 the same error is thrown
  3. When sending value 1e18 as hex through ...

amount = number(10**18);

myContract.methods.transfer(accounts[1], amount.toString(16)
).send({ from: accounts[0] })

  1. When sending value 1e16 - 1 or less works.

I am using "web3": "^1.0.0-beta.37"

If anyone knows a workaround for this please let me know via email, pjonson2@gmail.com ,or through github.

@leonprou

This comment has been minimized.

Copy link
Contributor

commented Feb 28, 2019

@pjonson2 Did you tried the solution suggested by chrsengel here #2077 (comment) ? Passing just a string like "1000000000000000000" as amount should work.

@kingdido999

This comment has been minimized.

Copy link

commented Mar 1, 2019

web3.utils.toHex and web3.utils.toWei does not work for me. As the convert-to-string workaround mentioned above, I tried the following and it works:

1e18.toString()
@rmindo

This comment has been minimized.

Copy link

commented Mar 1, 2019

I have the same error before but it works now, You can try this.

// Decimal
const decimals = web3.utils.toBN(18);

// Amount of token
const tokenAmount = web3.utils.toBN(10000000000);

// Amount as Hex - contract.methods.transfer(toAddress, tokenAmountHex).encodeABI();
const tokenAmountHex = '0x' + tokenAmount.mul(web3.utils.toBN(10).pow(decimals)).toString('hex');

@leonprou

This comment has been minimized.

Copy link
Contributor

commented Mar 1, 2019

@kingdido999 web3.utils.toWei should receive a price in ether. If you pass 1e18 to it you already pass it in wei, so that must be the reason it fails.

When I looked into ethers source code I've found that passing the values as strings in wei should work. No matter which transform function you use.

@PendicGordan

This comment has been minimized.

Copy link

commented Mar 27, 2019

Any solutions right now? Got the error in 1.0.0-beta.50. Tried also with 1.0.0-beta.34, 1.0.0-beta.35, without success.

@alexbabichev

This comment has been minimized.

Copy link

commented Apr 1, 2019

downgraded to ^1.0.0-beta.35 to get it work

@sowmyakannan

This comment has been minimized.

Copy link

commented Apr 14, 2019

Any updates on this. I have been getting this error too for a while now with
MyToken.transfer(_toCrowdsaleContractAddress, web3.utils.toBN(value)) where value = Mytoken.totalSupply().

Also tried using this web3.utils.toBN(value.toString()) and this Number(value.toString()).

Any suggestions on this would be appreciated. Have been stuck on this for quite sometime now.

@leonprou

This comment has been minimized.

Copy link
Contributor

commented Apr 14, 2019

@sowmyakannan try just to pass the string in wei. it Works for me with web3@1.0.0-beta.37

@nivida nivida moved this from Proposed To Do's to Accepted To Do's in 1.0 Apr 15, 2019

@Solexplorer

This comment has been minimized.

Copy link

commented May 5, 2019

const tokenAmountHex = '0x' + tokenAmount.mul(web3.utils.toBN(10).pow(decimals)).toString('hex');

This actually worked for me, thanks for posting

@PendicGordan

This comment has been minimized.

Copy link

commented May 5, 2019

Just to be aware of, there are some problems/bugs with BN, so whenever you can, pass strings(like in @rmindo case) and not BNs. So it's more BN problem, than web3. Caused me a lot of trouble..

@Alexintosh

This comment has been minimized.

Copy link

commented May 6, 2019

Got the same issue with 1.0.0-beta.53 downgrading.

@christianascone

This comment has been minimized.

Copy link

commented May 7, 2019

@sowmyakannan try just to pass the string in wei. it Works for me with web3@1.0.0-beta.37

This worked for me

@marsrobertson

This comment has been minimized.

Copy link

commented May 7, 2019

It is annoying.

Using truffle test and 1.0.0-beta.37

Works for me: #2077 (comment)

try just to pass the string in wei

In case you want to check things out...

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>web3.js 1.0.0-beta TESTING (open console)</title>
    <script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.34/dist/web3.js"></script>
  </head>
  <body>
  	<h1>web3.js 1.0.0-beta TESTING (open console)</h1>
  	<script>
      const web3 = new Web3(Web3.givenProvider || 'ws://localhost:8546', null, {}); // initialising it: https://web3js.readthedocs.io/en/1.0/getting-started.html   
      console.log(web3.version);
      console.log("toWei: " + web3.utils.toWei("1"));
      console.log("fromWei:" + web3.utils.fromWei("1000000000000000001"));
  	</script>
  
  </body>
</html>
@ilanolkies

This comment has been minimized.

Copy link
Contributor

commented May 29, 2019

Should uint256 work with web3.utils.toBN(...)? I have to call methods with uint256 parameters using a hexa expression. I think that's wrong.

This Solidity method:

function shaBid(bytes32 _hash, address _owner, uint _value, bytes32 _salt) public pure returns (bytes32)
Called with Output
contract.methods.shaBid(hash, owner, web3.utils.toBN(1), '0x00') Error: invalid number value (arg="_value", coderType="uint256", value="1")
contract.methods.shaBid(hash, owner, '0x00', '0x00') No error
@Karlus44

This comment has been minimized.

Copy link

commented Jun 30, 2019

Same issue here
I'm using

Truffle v5.0.25 (core: 5.0.25)
Solidity v0.5.0 (solc-js)
Node v8.10.0
Web3.js v1.0.0-beta.37

and I'm trying to give a "price" value from web3 ($('#article_price').val() in Wei) to a uint256 in Solidity in ether.
Here are some examples I tested

var _price = $('#article_price').val()*1e18;
Error: "invalid number value (arg="_price", coderType="uint256", value=10500000000000000000)"
var _price = web3.utils.toWei($('#article_price').val(),'ether');
Error: "Error: [number-to-bn] while converting number 1e+22 to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported. Given value: "1e+22""
var _price = $('#article_price').val()*1e18.toString();
Error: "Error: [number-to-bn] while converting number 1e+22 to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported. Given value: "1e+22""

I tried also BN conversions, Hex conversions ... to the same disappointing answer.
I don't know what else I should try ...

@Karlus44

This comment has been minimized.

Copy link

commented Jul 1, 2019

Also a different error here :

var _price = web3.utils.toWei($('#article_price').val() || 0,"ether");
Error: "Please pass numbers as strings or BigNumber objects to avoid precision errors."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
You can’t perform that action at this time.