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

chore: fix 0 kwei bug #7387

Open
wants to merge 3 commits into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading