Skip to content

Commit

Permalink
chore: fix 0 kwei bug (#7387)
Browse files Browse the repository at this point in the history
  • Loading branch information
whitemoshui authored Nov 19, 2024
1 parent 6229f4d commit 5eeb2d6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2809,3 +2809,7 @@ If there are any bugs, improvements, optimizations or any new feature proposal f
#### web3

- Export Web3Account, Wallet and signature related types. (#7374)

#### web3-utils

- Make `fromWei` return "0" when input is `0` (#7387)
4 changes: 4 additions & 0 deletions packages/web3-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,7 @@ Documentation:
- fix `padRight` validation failure on large `uint` (#7265)

## [Unreleased]

### Fixed

- Make `fromWei` return "0" when input is `0` (#7387)
2 changes: 1 addition & 1 deletion packages/web3-utils/src/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ export const fromWei = (number: Numbers, unit: EtherUnits | number): string => {
const fraction = zeroPaddedValue.slice(-numberOfZerosInDenomination).replace(/\.?0+$/, '');

if (integer === '') {
return `0.${fraction}`;
return fraction ? `0.${fraction}` : '0';
}

if (fraction === '') {
Expand Down
3 changes: 3 additions & 0 deletions packages/web3-utils/test/fixtures/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ export const fromWeiValidData: [[Numbers, EtherUnits | number], Numbers][] = [
[['1123', 'kwei'], '1.123'],
[['1234100', 'kwei'], '1234.1'],
[['3308685546611893', 'ether'], '0.003308685546611893'],
[['0', 'wei'], '0'],
[['0', 'kwei'], '0'],
[['0', 'ether'], '0'],
];

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

1 comment on commit 5eeb2d6

@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: 5eeb2d6 Previous: 6229f4d Ratio
processingTx 21473 ops/sec (±6.68%) 23397 ops/sec (±6.73%) 1.09
processingContractDeploy 38101 ops/sec (±7.72%) 41738 ops/sec (±7.66%) 1.10
processingContractMethodSend 15261 ops/sec (±8.62%) 17021 ops/sec (±7.33%) 1.12
processingContractMethodCall 26904 ops/sec (±7.20%) 27953 ops/sec (±7.87%) 1.04
abiEncode 44897 ops/sec (±6.89%) 46545 ops/sec (±6.46%) 1.04
abiDecode 29584 ops/sec (±10.53%) 31435 ops/sec (±7.33%) 1.06
sign 1532 ops/sec (±3.63%) 1578 ops/sec (±0.66%) 1.03
verify 365 ops/sec (±0.62%) 370 ops/sec (±2.74%) 1.01

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

Please sign in to comment.