Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use bigint power #7054

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
// 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
Loading