Skip to content

Commit

Permalink
update converters (#7054)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex authored May 22, 2024
1 parent 3904a46 commit ac2e180
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);
}
let res = base;
for (let index = 1; index < expo; index += 1) {
res *= base;
Expand Down

1 comment on commit ac2e180

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: ac2e180 Previous: 3904a46 Ratio
processingTx 8800 ops/sec (±4.35%) 9074 ops/sec (±5.00%) 1.03
processingContractDeploy 39867 ops/sec (±6.83%) 40926 ops/sec (±5.60%) 1.03
processingContractMethodSend 19416 ops/sec (±7.11%) 20386 ops/sec (±7.30%) 1.05
processingContractMethodCall 39762 ops/sec (±5.77%) 40611 ops/sec (±6.33%) 1.02
abiEncode 45021 ops/sec (±7.04%) 45322 ops/sec (±7.04%) 1.01
abiDecode 30398 ops/sec (±8.02%) 30998 ops/sec (±7.93%) 1.02
sign 1559 ops/sec (±0.80%) 1602 ops/sec (±3.59%) 1.03
verify 367 ops/sec (±3.03%) 379 ops/sec (±0.71%) 1.03

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.