Skip to content

Commit

Permalink
allow fromwei and toWei to accept numbers as units
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Luu committed May 21, 2024
1 parent 32b6b29 commit c53a0a9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
24 changes: 20 additions & 4 deletions packages/web3-utils/src/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,16 @@ export const toBigInt = (value: unknown): bigint => {
* > 0.000000001
* ```
*/
export const fromWei = (number: Numbers, unit: EtherUnits): string => {
const denomination = ethUnitMap[unit];
export const fromWei = (number: Numbers, unit: EtherUnits | number): string => {
let denomination;
if (typeof unit === 'string') {
denomination = ethUnitMap[unit];
} else {
if (unit <= 0 && !Number.isInteger(unit)) {
throw new InvalidUnitError(unit);

Check warning on line 501 in packages/web3-utils/src/converters.ts

View check run for this annotation

Codecov / codecov/patch

packages/web3-utils/src/converters.ts#L501

Added line #L501 was not covered by tests
}
denomination = BigInt(10)**BigInt(unit);
}

if (!denomination) {
throw new InvalidUnitError(unit);
Expand Down Expand Up @@ -550,10 +558,18 @@ export const fromWei = (number: Numbers, unit: EtherUnits): string => {
* ```
*/
// todo in 1.x unit defaults to 'ether'
export const toWei = (number: Numbers, unit: EtherUnits): string => {
export const toWei = (number: Numbers, unit: EtherUnits | number): string => {
validator.validate(['number'], [number]);

const denomination = ethUnitMap[unit];
let denomination;
if (typeof unit === 'string') {
denomination = ethUnitMap[unit];
} else {
if (unit < 0 && !Number.isInteger(unit)) {
throw new InvalidUnitError(unit);

Check warning on line 569 in packages/web3-utils/src/converters.ts

View check run for this annotation

Codecov / codecov/patch

packages/web3-utils/src/converters.ts#L569

Added line #L569 was not covered by tests
}
denomination = BigInt(10)**BigInt(unit);
}

if (!denomination) {
throw new InvalidUnitError(unit);
Expand Down
15 changes: 12 additions & 3 deletions packages/web3-utils/test/fixtures/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export const toHexInvalidData: [any, string][] = [
[undefined, 'Invalid value given "undefined". Error: can not be converted to hex.'],
];

const conversionBaseData: [[Numbers, EtherUnits], string][] = [
const conversionBaseData: [[Numbers, EtherUnits | number], string][] = [
[[0, 'wei'], '0'],
[[123, 'wei'], '123'],
[['123', 'wei'], '123'],
Expand Down Expand Up @@ -290,17 +290,26 @@ const conversionBaseData: [[Numbers, EtherUnits], string][] = [
[['178373938391829348', 'ether'], '0.178373938391829348'],
[['879123456788877661', 'gwei'], '879123456.788877661'],
[['879123456788877661', 'tether'], '0.000000000000879123456788877661'],
[['1', 3], '0.001'],
[['1', 4], '0.0001'],
[['1', 5], '0.00001'],
[['1', 6], '0.000001'],
[['1', 7], '0.0000001'],
[['1', 8], '0.00000001'],
[['1', 9], '0.000000001'],
[['1', 10], '0.0000000001'],
[['1000000000000000000', 18], '1'],
];

export const fromWeiValidData: [[Numbers, EtherUnits], string][] = [
export const fromWeiValidData: [[Numbers, EtherUnits | number], string][] = [
...conversionBaseData,
[['0xff', 'wei'], '255'],
[[1e+22, 'ether'], '10000'],
[[19999999999999991611392, 'ether'], '19999.999999999991611392'],
[[1.9999999999999991611392e+22, 'ether'], '19999.999999999991611392'],
];

export const toWeiValidData: [[Numbers, EtherUnits], Numbers][] = [
export const toWeiValidData: [[Numbers, EtherUnits | number], Numbers][] = [
...conversionBaseData,
[['255', 'wei'], '0xFF'],
[['100000000000', 'ether'], 0.0000001],
Expand Down

0 comments on commit c53a0a9

Please sign in to comment.