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

Commit 3904a46

Browse files
author
Alex
authored
allow fromwei and toWei to accept numbers as units (#7052)
* allow fromwei and toWei to accept numbers as units * add error and testcases
1 parent 553f270 commit 3904a46

File tree

6 files changed

+77
-14
lines changed

6 files changed

+77
-14
lines changed

packages/web3-errors/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,8 @@ Documentation:
166166

167167
- Fixed grammar and spelling in `transactionTimeoutHint` (#6559)
168168

169-
## [Unreleased]
169+
## [Unreleased]
170+
171+
### Added
172+
173+
- Added `InvalidIntegerError` error for fromWei and toWei (#7052)

packages/web3-errors/src/error_codes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ export const ERR_INVALID_LARGE_VALUE = 1011;
155155
export const ERR_INVALID_BLOCK = 1012;
156156
export const ERR_INVALID_TYPE_ABI = 1013;
157157
export const ERR_INVALID_NIBBLE_WIDTH = 1014;
158+
export const ERR_INVALID_INTEGER = 1015;
158159

159160
// Validation error codes
160161
export const ERR_VALIDATION = 1100;

packages/web3-errors/src/errors/utils_errors.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
ERR_INVALID_TYPE,
3232
ERR_INVALID_TYPE_ABI,
3333
ERR_INVALID_UNIT,
34+
ERR_INVALID_INTEGER,
3435
ERR_INVALID_UNSIGNED_INTEGER,
3536
} from '../error_codes.js';
3637
import { InvalidValueError } from '../web3_error_base.js';
@@ -75,6 +76,15 @@ export class InvalidUnitError extends InvalidValueError {
7576
}
7677
}
7778

79+
export class InvalidIntegerError extends InvalidValueError {
80+
public code = ERR_INVALID_INTEGER;
81+
82+
public constructor(value: unknown) {
83+
super(value, 'not a valid unit. Must be a positive integer');
84+
85+
}
86+
}
87+
7888
export class HexProcessingError extends InvalidValueError {
7989
public code = ERR_INVALID_HEX;
8090

packages/web3-utils/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,9 @@ Documentation:
221221
### Added
222222

223223
- `toWei` add warning when using large numbers or large decimals that may cause precision loss (#6908)
224+
- `toWei` and `fromWei` now supports integers as a unit. (#7053)
224225

225226
### Fixed
226227

227228
- `toWei` support numbers in scientific notation (#6908)
228-
- `toWei` and `fromWei` trims according to ether unit successfuly (#7044)
229+
- `toWei` and `fromWei` trims according to ether unit successfuly (#7044)

packages/web3-utils/src/converters.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
InvalidBytesError,
4242
InvalidNumberError,
4343
InvalidUnitError,
44+
InvalidIntegerError,
4445
} from 'web3-errors';
4546
import { isUint8Array } from './uint8array.js';
4647

@@ -492,13 +493,22 @@ export const toBigInt = (value: unknown): bigint => {
492493
* > 0.000000001
493494
* ```
494495
*/
495-
export const fromWei = (number: Numbers, unit: EtherUnits): string => {
496-
const denomination = ethUnitMap[unit];
496+
export const fromWei = (number: Numbers, unit: EtherUnits | number): string => {
497+
let denomination;
498+
if (typeof unit === 'string') {
499+
denomination = ethUnitMap[unit];
497500

498-
if (!denomination) {
499-
throw new InvalidUnitError(unit);
501+
if (!denomination) {
502+
throw new InvalidUnitError(unit);
503+
}
504+
} else {
505+
if (unit < 0 || !Number.isInteger(unit)) {
506+
throw new InvalidIntegerError(unit);
507+
}
508+
denomination = BigInt(10)**BigInt(unit);
500509
}
501510

511+
502512
// value in wei would always be integer
503513
// 13456789, 1234
504514
const value = String(toNumber(number));
@@ -551,14 +561,23 @@ export const fromWei = (number: Numbers, unit: EtherUnits): string => {
551561
* ```
552562
*/
553563
// todo in 1.x unit defaults to 'ether'
554-
export const toWei = (number: Numbers, unit: EtherUnits): string => {
564+
export const toWei = (number: Numbers, unit: EtherUnits | number): string => {
555565
validator.validate(['number'], [number]);
556566

557-
const denomination = ethUnitMap[unit];
558-
559-
if (!denomination) {
560-
throw new InvalidUnitError(unit);
567+
let denomination;
568+
if (typeof unit === 'string') {
569+
denomination = ethUnitMap[unit];
570+
if (!denomination) {
571+
throw new InvalidUnitError(unit);
572+
}
573+
} else {
574+
if (unit < 0 || !Number.isInteger(unit)) {
575+
throw new InvalidIntegerError(unit);
576+
}
577+
578+
denomination = BigInt(10)**BigInt(unit);
561579
}
580+
562581
let parsedNumber = number;
563582
if (typeof parsedNumber === 'number') {
564583
if (parsedNumber < 1e-15) {

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

Lines changed: 31 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,9 +290,35 @@ const conversionBaseData: [[Numbers, EtherUnits], string][] = [
290290
[['178373938391829348', 'ether'], '0.178373938391829348'],
291291
[['879123456788877661', 'gwei'], '879123456.788877661'],
292292
[['879123456788877661', 'tether'], '0.000000000000879123456788877661'],
293+
[['1', 0], '1'],
294+
[['1', 1], '0.1'],
295+
[['1', 2], '0.01'],
296+
[['1', 3], '0.001'],
297+
[['1', 4], '0.0001'],
298+
[['1', 5], '0.00001'],
299+
[['1', 6], '0.000001'],
300+
[['1', 7], '0.0000001'],
301+
[['1', 8], '0.00000001'],
302+
[['1', 9], '0.000000001'],
303+
[['1', 10], '0.0000000001'],
304+
[[1, 18], '0.000000000000000001'],
305+
[[100, 2], '1'],
306+
[['100', 2], '1'],
307+
[['1000', 3], '1'],
308+
[['10000', 4], '1'],
309+
[['100000', 5], '1'],
310+
[['1000000', 6], '1'],
311+
[['10000000', 7], '1'],
312+
[['100000000', 8], '1'],
313+
[['1000000000', 9], '1'],
314+
[['10000000000', 10], '1'],
315+
[['100000000000', 11], '1'],
316+
[['1000000000000', 12], '1'],
317+
[['10000000000000', 13], '1'],
318+
[['1000000000000000000', 18], '1'],
293319
];
294320

295-
export const fromWeiValidData: [[Numbers, EtherUnits], Numbers][] = [
321+
export const fromWeiValidData: [[Numbers, EtherUnits | number], Numbers][] = [
296322
...conversionBaseData,
297323
[['0xff', 'wei'], '255'],
298324
[[1e+22, 'ether'], '10000'],
@@ -305,7 +331,7 @@ export const fromWeiValidData: [[Numbers, EtherUnits], Numbers][] = [
305331
[['3308685546611893', 'ether'], '0.003308685546611893']
306332
];
307333

308-
export const toWeiValidData: [[Numbers, EtherUnits], Numbers][] = [
334+
export const toWeiValidData: [[Numbers, EtherUnits | number], Numbers][] = [
309335
...conversionBaseData,
310336
[['255', 'wei'], '0xFF'],
311337
[['100000000000', 'ether'], 0.0000001],
@@ -336,6 +362,8 @@ export const fromWeiInvalidData: [[any, any], string][] = [
336362
[[{}, 'kwei'], 'Invalid value given "{}". Error: can not parse as number data'],
337363
[['data', 'kwei'], 'Invalid value given "data". Error: can not parse as number data.'],
338364
[['1234', 'uwei'], 'Invalid value given "uwei". Error: invalid unit.'],
365+
[['1234', -1], 'Invalid value given "-1". Error: not a valid unit. Must be a positive integer.'],
366+
[['1234', 3.3], 'Invalid value given "3.3". Error: not a valid unit. Must be a positive integer.']
339367
];
340368

341369
export const toWeiInvalidData: [[any, any], string][] = [

0 commit comments

Comments
 (0)