-
Notifications
You must be signed in to change notification settings - Fork 5k
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
fix: fromWei() parsing of large numbers > 10^20 #6882
fix: fromWei() parsing of large numbers > 10^20 #6882
Conversation
Prevent automatic conversion to scientific notation for numbers >= 10^21 by using BigInt, ensuring accurate string representations and avoiding parsing errors in fromWei function.
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## 4.x #6882 +/- ##
=======================================
Coverage 91.98% 91.98%
=======================================
Files 215 215
Lines 8174 8176 +2
Branches 2207 2208 +1
=======================================
+ Hits 7519 7521 +2
Misses 655 655
Flags with carried forward coverage won't be shown. Click here to find out more.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution. I found that this solution does not cover all cases because of JavaScript problem
@avkos Suddenly there is nothing the library, me or you can do about it. The precision is lost alredy before the number enters any function of the web3 library. If the user choses to use javascript numbers (instead of bigints or strings) then he will deal with precision issues because the javascript numbers system itself is not precise. Here is an example. You can see that the precision is already lost while initializing the number, not in the web3 library function.
logs:
Here is also a warning from my IDE that denotes exactly that issue. These issues you have mentioned were already there before my changes. My commits have not changed any behaviour related to that. If the user chooses to use the javascript 'number' type the value arrives in the function imprecise and there is no way the function can fix that. |
Hey there, thanks for submitting this PR :) we talked about this with the team and I think that theres limitations using number which is why this is headache dealing with large/decimal numbers. My suggestion is that if a number is at the max that javascript will allow it (which i believe in this case its 100000000000000000000), an error should produce telling the user that their number is too large, and should use string/bigInts. |
@luu-alex This is indeed a viable approach to handle this situation. But this would probably break a lot of application that already use this library. There is really big amount of application already using this library. Backwards compatibility is a real concern here. I can of course implement that, but are you sure that the error solution is good for the library? |
@luu-alex Anyways, I propose you to merge this request, because this PR is not about precision, it is about a real bug in parsing. We can open a new ticket with a PR to discuss and potentially implement something for the precision problem. These two problems are linked but still different. And the initial Ticket was not aimed to solve the precision problem. |
@sgerodes I think you bring up some good points, we can move forward with this PR. I'll create another issue on precision for discussion |
https://github.com/web3/web3.js/blob/4.x/packages/web3-utils/CHANGELOG.md include this fix in the changelog with issue # |
@luu-alex done. I have assumed this fix would be released in version 4.6.1 |
@sgerodes we'll change that number when we plan to do next release, i changed it for you. :) thanks for this! |
Prevent automatic conversion to scientific notation for numbers >= 10^21 by using BigInt, ensuring accurate string representations and avoiding parsing errors in fromWei function.
Fixes #6880
Description
Current errornous behaviour examples:
The following code performs silently a wrong parsing because of this Issue
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".
Explanation of the behaviour
When converting large numerical values from wei to ether using fromWei, we encounter an issue where numbers greater than or equal to 10^21 are automatically represented in scientific notation by JavaScript. This conversion introduces an additional decimal point into the string representation (1.9.99999999999999e+22 for 19999999999999990000000), leading to significant parsing errors and value distortion upon further processing (e.g., when using parseFloat, 19999 ETH incorrectly becomes 1.9 ETH).
JavaScript's default behavior of converting large numbers to scientific notation when stringified is the underlying cause. This automatic conversion impacts the string manipulation logic within the fromWei function, resulting in malformed outputs for large wei values.
Solution
I have implemented a conditional check to utilize BigInt for numbers that are at risk of being automatically converted to scientific notation (>= 10^21).
Type of change
Checklist:
npm run lint
with success and extended the tests and types if necessary.npm run test:unit
with success.npm run test:coverage
and my test cases cover all the lines and branches of the added code.npm run build
and testeddist/web3.min.js
in a browser.CHANGELOG.md
file in the root folder.