-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Web3.utils.fromWei silently performs unsafe conversion for big numbers because of scientific notation #6880
Comments
Thanks for posting this @sgerodes , our devs will take a look at it and reply asap!💪 |
The Issue comes from very large numbers (>= 10^21) are incorrectly converted into a format with an additional dot due to JavaScript's automatic scientific notation for large numbers. Example: |
@SantiagoDevRel I have created a PR that fixes this Bug #6882 |
Amazing @sgerodes , the devs will review it tomorrow! |
@sgerodes if you want to use
|
@jdevcs Thank you. That will fix my code. But there is still a bug to be fixed. The following is clearly an errornous behaviour:
The problem is this function accepts js non-bigint numbers greater than 10^21 and parses them incorrectly without errors or warnings. This can create errornous behaviour. And it will occur silently.
19999 ETH was parsed as 1.9 ETH with little code, that felt very intutive. Just using parseFloat. Which probably many of the devs use after they receive a string from "fromWei". |
Expected behavior
Web3.utils.fromWei is parsing big numbers (>=10^21) without adding an unnecessary dot
Actual behavior
Web3.utils.fromWei insert an additional dot in the scientific notation when dealing with Big numbers
In the example below the bumber 19999999999999990000000 is parsed to ether. this correcponds to 19999.99999999999 ETH.
When parsed we can see that there is an additional dot in the output: 1.9.99999999999999e+22.
If we parse this string to a float then a 1.9 will be produced.
This behaviour silently without errors converted a number representing ca 19999 ETH to 1.9 ETH.
Steps to reproduce the behavior
Logs
Environment
node version 20
web3.js version 4.6.0
Reason of the error
The internal logic of fromWei is behaving wrongly because the number is parsed into a scientific notation string like "1.999999999999999e+22". This inroduceses errors into the output
The text was updated successfully, but these errors were encountered: