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

Commit c53a0a9

Browse files
author
Alex Luu
committed
allow fromwei and toWei to accept numbers as units
1 parent 32b6b29 commit c53a0a9

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

packages/web3-utils/src/converters.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,16 @@ export const toBigInt = (value: unknown): bigint => {
492492
* > 0.000000001
493493
* ```
494494
*/
495-
export const fromWei = (number: Numbers, unit: EtherUnits): string => {
496-
const denomination = ethUnitMap[unit];
495+
export const fromWei = (number: Numbers, unit: EtherUnits | number): string => {
496+
let denomination;
497+
if (typeof unit === 'string') {
498+
denomination = ethUnitMap[unit];
499+
} else {
500+
if (unit <= 0 && !Number.isInteger(unit)) {
501+
throw new InvalidUnitError(unit);
502+
}
503+
denomination = BigInt(10)**BigInt(unit);
504+
}
497505

498506
if (!denomination) {
499507
throw new InvalidUnitError(unit);
@@ -550,10 +558,18 @@ export const fromWei = (number: Numbers, unit: EtherUnits): string => {
550558
* ```
551559
*/
552560
// todo in 1.x unit defaults to 'ether'
553-
export const toWei = (number: Numbers, unit: EtherUnits): string => {
561+
export const toWei = (number: Numbers, unit: EtherUnits | number): string => {
554562
validator.validate(['number'], [number]);
555563

556-
const denomination = ethUnitMap[unit];
564+
let denomination;
565+
if (typeof unit === 'string') {
566+
denomination = ethUnitMap[unit];
567+
} else {
568+
if (unit < 0 && !Number.isInteger(unit)) {
569+
throw new InvalidUnitError(unit);
570+
}
571+
denomination = BigInt(10)**BigInt(unit);
572+
}
557573

558574
if (!denomination) {
559575
throw new InvalidUnitError(unit);

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ export const toHexInvalidData: [any, string][] = [
256256
[undefined, 'Invalid value given "undefined". Error: can not be converted to hex.'],
257257
];
258258

259-
const conversionBaseData: [[Numbers, EtherUnits], string][] = [
259+
const conversionBaseData: [[Numbers, EtherUnits | number], string][] = [
260260
[[0, 'wei'], '0'],
261261
[[123, 'wei'], '123'],
262262
[['123', 'wei'], '123'],
@@ -290,17 +290,26 @@ const conversionBaseData: [[Numbers, EtherUnits], string][] = [
290290
[['178373938391829348', 'ether'], '0.178373938391829348'],
291291
[['879123456788877661', 'gwei'], '879123456.788877661'],
292292
[['879123456788877661', 'tether'], '0.000000000000879123456788877661'],
293+
[['1', 3], '0.001'],
294+
[['1', 4], '0.0001'],
295+
[['1', 5], '0.00001'],
296+
[['1', 6], '0.000001'],
297+
[['1', 7], '0.0000001'],
298+
[['1', 8], '0.00000001'],
299+
[['1', 9], '0.000000001'],
300+
[['1', 10], '0.0000000001'],
301+
[['1000000000000000000', 18], '1'],
293302
];
294303

295-
export const fromWeiValidData: [[Numbers, EtherUnits], string][] = [
304+
export const fromWeiValidData: [[Numbers, EtherUnits | number], string][] = [
296305
...conversionBaseData,
297306
[['0xff', 'wei'], '255'],
298307
[[1e+22, 'ether'], '10000'],
299308
[[19999999999999991611392, 'ether'], '19999.999999999991611392'],
300309
[[1.9999999999999991611392e+22, 'ether'], '19999.999999999991611392'],
301310
];
302311

303-
export const toWeiValidData: [[Numbers, EtherUnits], Numbers][] = [
312+
export const toWeiValidData: [[Numbers, EtherUnits | number], Numbers][] = [
304313
...conversionBaseData,
305314
[['255', 'wei'], '0xFF'],
306315
[['100000000000', 'ether'], 0.0000001],

0 commit comments

Comments
 (0)