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

Closed
patidarmanoj10 opened this issue Nov 29, 2018 · 66 comments
Labels
1.x 1.0 related issues 2.x 2.0 related issues Bug Addressing a bug Stale Has not received enough activity

Comments

@patidarmanoj10
Copy link

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 Addressing a bug label Nov 30, 2018
@nivida
Copy link
Contributor

nivida 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 the 1.0 milestone Nov 30, 2018
@patidarmanoj10
Copy link
Author

@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
Copy link
Contributor

nivida 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
Copy link
Contributor

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

@nivida
Copy link
Contributor

nivida commented Dec 1, 2018

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

@markerdmann
Copy link

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
Copy link
Contributor

eyezick commented Dec 4, 2018

This is also referenced in #1920 just fyi

@ecouffes
Copy link

The same issue in beta.37.

@yueawang
Copy link

yueawang 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
Copy link
Author

@yueawang yes.

@mattcrooks
Copy link

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
Copy link

kombos 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
Copy link

I am facing the same issue for latest web3 1.0.0 version

@nivida
Copy link
Contributor

nivida 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
Copy link

markerdmann 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
Copy link

GuthL commented Jan 23, 2019

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

@nivida
Copy link
Contributor

nivida 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
Copy link

inyutin commented Jan 29, 2019

The same issue on 1.0.0-beta.40

@leonprou
Copy link
Contributor

leonprou 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.

@protortyp
Copy link

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
Copy link
Contributor

leonprou 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
Copy link
Contributor

nivida 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
Copy link
Contributor

nivida 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

@github-actions
Copy link

github-actions bot commented Aug 3, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. If you believe this was a mistake, please comment.

@github-actions github-actions bot added the Stale Has not received enough activity label Aug 3, 2020
@jdowning100
Copy link

Issue still has not been fixed

@github-actions github-actions bot removed the Stale Has not received enough activity label Aug 6, 2020
@trongdth
Copy link

use web3.utils.toWei('1000000000') for workaround way

@pierce403
Copy link

web3.utils.toWei(amount) seems dead. web3.toWei(amount) worked for me though.

@crameur
Copy link

crameur commented Aug 18, 2020

Quick fixes:
const num256 = 42*10**18;
const fix1 = num256.toString();
const fix2 = "0x"+num256.toString(16);

@SalahAdDin
Copy link

@crameur why must it be like that?

@github-actions
Copy link

github-actions bot commented Nov 1, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. If you believe this was a mistake, please comment.

@github-actions github-actions bot added the Stale Has not received enough activity label Nov 1, 2020
@sham789
Copy link

sham789 commented Nov 5, 2020

Rename the variable while importing

@github-actions github-actions bot removed the Stale Has not received enough activity label Nov 6, 2020
@github-actions
Copy link

github-actions bot commented Jan 5, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. If you believe this was a mistake, please comment.

@github-actions github-actions bot added the Stale Has not received enough activity label Jan 5, 2021
@scottie
Copy link

scottie commented Feb 15, 2021

Heads up 2021 still remains a issue.

@KevinH2810
Copy link

const fix2 = "0x"+num256.toString(16);

this quick fix solve my error. thank you sir!

@eliobricenov
Copy link

Fix function:

const toHex = (amount: BigNumber): string => '0x' + amount.toString(16);

michalsmiarowski added a commit to keep-network/keep-core that referenced this issue Sep 16, 2021
We are changing the amount type from BigNumber to string because it looks like
it doesn't work well that way on Ropsten.

More info: web3/web3.js#2077
r-czajkowski added a commit to keep-network/keep-core that referenced this issue Sep 16, 2021
…ction-send-big-number-to-string

Change type of amount sent when depositing

We are changing the amount type from BigNumber to string because it looks like
it transactions doesn't work with the amount as BigNumber.

More info: web3/web3.js#2077
@jjhesk
Copy link

jjhesk commented Oct 17, 2021

I have the similar issue from MikeMcl/bignumber.js#305

@zhawasolutions
Copy link

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

just add toFixed() function.
for example :

const BEP20Contract = new web3.eth.Contract(abiJson, contract);
var decimals=parseFloat(js['decimals']);
var bep20_value=(parseFloat(amount) * (10**decimals)).toFixed();
var data = BEP20Contract.methods.transfer(to_address, bep20_value).encodeABI();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.x 1.0 related issues 2.x 2.0 related issues Bug Addressing a bug Stale Has not received enough activity
Projects
None yet
Development

No branches or pull requests