Skip to content

Commit

Permalink
update converters
Browse files Browse the repository at this point in the history
  • Loading branch information
luu-alex committed May 22, 2024
1 parent 3904a46 commit 7f7610a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/web3-utils/src/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
utils,
utils as validatorUtils,
validator,
bigintPower,
} from 'web3-validator';

import {
Expand Down Expand Up @@ -505,7 +506,7 @@ export const fromWei = (number: Numbers, unit: EtherUnits | number): string => {
if (unit < 0 || !Number.isInteger(unit)) {
throw new InvalidIntegerError(unit);
}
denomination = BigInt(10)**BigInt(unit);
denomination = bigintPower(BigInt(10),BigInt(unit));
}


Expand Down Expand Up @@ -575,7 +576,7 @@ export const toWei = (number: Numbers, unit: EtherUnits | number): string => {
throw new InvalidIntegerError(unit);
}

denomination = BigInt(10)**BigInt(unit);
denomination = bigintPower(BigInt(10),BigInt(unit));
}

let parsedNumber = number;
Expand Down Expand Up @@ -608,7 +609,6 @@ export const toWei = (number: Numbers, unit: EtherUnits | number): string => {

// join the value removing `.` from
// 24.56 -> 2456

const value = BigInt(`${integer}${fraction}`);

// multiply value with denomination
Expand Down
4 changes: 4 additions & 0 deletions packages/web3-validator/src/validation/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export const isBigInt = (value: ValidInputTypes): boolean => typeof value === 'b
// you can find more at: https://github.com/babel/babel/issues/13109 and https://github.com/web3/web3.js/issues/6187
/** @internal */
export const bigintPower = (base: bigint, expo: bigint) => {
// edge case
if (expo === BigInt(0)) {
return BigInt(1);

Check warning on line 33 in packages/web3-validator/src/validation/numbers.ts

View check run for this annotation

Codecov / codecov/patch

packages/web3-validator/src/validation/numbers.ts#L33

Added line #L33 was not covered by tests
}
let res = base;
for (let index = 1; index < expo; index += 1) {
res *= base;
Expand Down

0 comments on commit 7f7610a

Please sign in to comment.