diff --git a/packages/web3-utils/src/converters.ts b/packages/web3-utils/src/converters.ts index f525150187e..6649bef1481 100644 --- a/packages/web3-utils/src/converters.ts +++ b/packages/web3-utils/src/converters.ts @@ -32,6 +32,7 @@ import { utils, utils as validatorUtils, validator, + bigintPower, } from 'web3-validator'; import { @@ -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)); } @@ -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; @@ -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 diff --git a/packages/web3-validator/src/validation/numbers.ts b/packages/web3-validator/src/validation/numbers.ts index 276fb84dd1e..bfaa7372148 100644 --- a/packages/web3-validator/src/validation/numbers.ts +++ b/packages/web3-validator/src/validation/numbers.ts @@ -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); + } let res = base; for (let index = 1; index < expo; index += 1) { res *= base;