Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 5eeb2d6

Browse files
authored
chore: fix 0 kwei bug (#7387)
1 parent 6229f4d commit 5eeb2d6

File tree

4 files changed

+12
-1
lines changed

4 files changed

+12
-1
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2809,3 +2809,7 @@ If there are any bugs, improvements, optimizations or any new feature proposal f
28092809
#### web3
28102810

28112811
- Export Web3Account, Wallet and signature related types. (#7374)
2812+
2813+
#### web3-utils
2814+
2815+
- Make `fromWei` return "0" when input is `0` (#7387)

packages/web3-utils/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,7 @@ Documentation:
241241
- fix `padRight` validation failure on large `uint` (#7265)
242242

243243
## [Unreleased]
244+
245+
### Fixed
246+
247+
- Make `fromWei` return "0" when input is `0` (#7387)

packages/web3-utils/src/converters.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ export const fromWei = (number: Numbers, unit: EtherUnits | number): string => {
546546
const fraction = zeroPaddedValue.slice(-numberOfZerosInDenomination).replace(/\.?0+$/, '');
547547

548548
if (integer === '') {
549-
return `0.${fraction}`;
549+
return fraction ? `0.${fraction}` : '0';
550550
}
551551

552552
if (fraction === '') {

packages/web3-utils/test/fixtures/converters.ts

+3
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,9 @@ export const fromWeiValidData: [[Numbers, EtherUnits | number], Numbers][] = [
328328
[['1123', 'kwei'], '1.123'],
329329
[['1234100', 'kwei'], '1234.1'],
330330
[['3308685546611893', 'ether'], '0.003308685546611893'],
331+
[['0', 'wei'], '0'],
332+
[['0', 'kwei'], '0'],
333+
[['0', 'ether'], '0'],
331334
];
332335

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

0 commit comments

Comments
 (0)