Skip to content

Commit

Permalink
fix: fromWei() parsing of large numbers > 10^20 (#6882)
Browse files Browse the repository at this point in the history
* Fix scientific notation parsing for large numbers with BigInt

Prevent automatic conversion to scientific notation for numbers >= 10^21 by using BigInt, ensuring accurate string representations and avoiding parsing errors in fromWei function.

* Update Tests for scientific notation number parsing.

* refactor: minor code cleaning

* docs: Update changelog for #6882

* Update CHANGELOG.md

---------

Co-authored-by: Alex <alex.luu@mail.utoronto.ca>
Co-authored-by: Alex <luu.alex98@gmail.com>
  • Loading branch information
3 people authored Mar 25, 2024
1 parent 1f81ff0 commit a83e9d5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
7 changes: 6 additions & 1 deletion packages/web3-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,9 @@ Documentation:

- replaced our eventEmitter to EventEmitter3 to support react native builds (#6253)

## [Unreleased]
## [Unreleased]

### Fixed

- fixed erroneous parsing of big numbers in the `fromWei(...)` function (#6880)

7 changes: 7 additions & 0 deletions packages/web3-utils/src/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,13 @@ export const toHex = (
*/
export const toNumber = (value: Numbers): number | bigint => {
if (typeof value === 'number') {
if (value > 1e+20) {
// JavaScript converts numbers >= 10^21 to scientific notation when coerced to strings,
// leading to potential parsing errors and incorrect representations.
// For instance, String(10000000000000000000000) yields '1e+22'.
// Using BigInt prevents this
return BigInt(value);
}
return value;
}

Expand Down
5 changes: 4 additions & 1 deletion packages/web3-utils/test/fixtures/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,10 @@ const conversionBaseData: [[Numbers, EtherUnits], string][] = [

export const fromWeiValidData: [[Numbers, EtherUnits], string][] = [
...conversionBaseData,
[['0xff', 'wei'], '255'],
[['0xff', 'wei'], '255'],
[[1e+22, 'ether'], '10000'],
[[19999999999999991611392, 'ether'], '19999.999999999991611392'],
[[1.9999999999999991611392e+22, 'ether'], '19999.999999999991611392'],
];

export const toWeiValidData: [[Numbers, EtherUnits], string][] = [
Expand Down

1 comment on commit a83e9d5

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: a83e9d5 Previous: 6c075db Ratio
processingTx 9428 ops/sec (±3.62%) 9301 ops/sec (±4.81%) 0.99
processingContractDeploy 38723 ops/sec (±8.01%) 39129 ops/sec (±7.62%) 1.01
processingContractMethodSend 19502 ops/sec (±7.96%) 19443 ops/sec (±5.19%) 1.00
processingContractMethodCall 40304 ops/sec (±5.83%) 38971 ops/sec (±6.34%) 0.97
abiEncode 44499 ops/sec (±7.75%) 44252 ops/sec (±6.92%) 0.99
abiDecode 31377 ops/sec (±6.77%) 30419 ops/sec (±8.89%) 0.97
sign 1636 ops/sec (±0.62%) 1656 ops/sec (±4.08%) 1.01
verify 378 ops/sec (±0.43%) 373 ops/sec (±0.78%) 0.99

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.