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

bug: toWei fail to convert number beyond 8th decimal point on a number #6894

Closed
BeroBurny opened this issue Mar 13, 2024 · 1 comment
Closed
Assignees
Labels
4.x 4.0 related Bug Addressing a bug

Comments

@BeroBurny
Copy link
Contributor

BeroBurny commented Mar 13, 2024

Expected behavior

To manage to convert value to wei

Actual behavior

Throw error while converting value beyond 8th decimal point

Steps to reproduce the behavior

utils.toWei(0.0000001, 'ether');

Logs

Created Script to test scenarios
https://github.com/BeroBurny/web3js-v4-inting/blob/58c2389176c12c297e029f8e716d70ce8db3c637/src/index.ts#L5-L46

and this produced logs

 [toWei] Number works with: 1
 [toWei] Number works with: 0.1
 [toWei] Number works with: 0.01
 [toWei] Number works with: 0.001
 [toWei] Number works with: 0.0001
 [toWei] Number works with: 0.00001
 [toWei] Number works with: 0.000001
 [toWei] Number failed with: 0.0000001
 -  Cannot convert 1e-7 to a BigInt
 [toWei] Number failed with: 0.00000001
 -  Cannot convert 1e-8 to a BigInt
 [toWei] Number failed with: 0.000000001
 -  Cannot convert 1e-9 to a BigInt
 [toWei] String works with: 1
 [toWei] String works with: 0.1
 [toWei] String works with: 0.01
 [toWei] String works with: 0.001
 [toWei] String works with: 0.0001
 [toWei] String works with: 0.00001
 [toWei] String works with: 0.000001
 [toWei] String works with: 0.0000001
 [toWei] String works with: 0.00000001
 [toWei] String works with: 0.000000001

Environment

Web3 4.6.0
Node 20

@luu-alex
Copy link
Contributor

Looks like at some point when decimals become larger js turns it into scientific notation, creating a PR to handle this case. thanks

@luu-alex luu-alex self-assigned this Mar 18, 2024
@mconnelly8 mconnelly8 added 4.x 4.0 related Bug Addressing a bug labels Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.x 4.0 related Bug Addressing a bug
Projects
None yet
Development

No branches or pull requests

3 participants