Skip to content

Commit

Permalink
Merge branch '4.x' into ok/6459-4.x]-Enable-plugins-to-register-build…
Browse files Browse the repository at this point in the history
…er-for-new-transaction-types
  • Loading branch information
avkos committed Oct 16, 2023
2 parents 8f98309 + 42502b6 commit 0306e72
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 57 deletions.
6 changes: 5 additions & 1 deletion packages/web3-eth-abi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,8 @@ Documentation:

- Dependencies updated

## [Unreleased]
## [Unreleased]

### Fixed

- Fix issue with default config with babel (and React): "TypeError: Cannot convert a BigInt value to a number #6187" (#6506)
20 changes: 1 addition & 19 deletions packages/web3-eth-abi/src/coders/base/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { padLeft, toBigInt } from 'web3-utils';
import { utils } from 'web3-validator';
import { DecoderResult, EncoderResult } from '../types.js';
import { WORD_SIZE } from '../utils.js';
import { numberLimits } from './numbersLimits.js';

// eslint-disable-next-line no-bitwise
const mask = BigInt(1) << BigInt(256);
Expand All @@ -43,25 +44,6 @@ function uint8ArrayToBigInt(value: Uint8Array, max: bigint): bigint {
return result - mask;
}

const numberLimits = new Map<string, { min: bigint; max: bigint }>();

// precalculate all the limits
for (let i = 8; i <= 256; i += 8) {
numberLimits.set(`uint${i}`, {
min: BigInt(0),
max: BigInt(2) ** BigInt(i) - BigInt(1),
});
numberLimits.set(`int${i}`, {
min: -(BigInt(2) ** BigInt(i - 1)),
max: BigInt(2) ** BigInt(i - 1) - BigInt(1),
});
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
numberLimits.set(`int`, numberLimits.get('int256')!);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
numberLimits.set(`uint`, numberLimits.get('uint256')!);

export function encodeNumber(param: AbiParameter, input: unknown): EncoderResult {
let value;
try {
Expand Down
39 changes: 39 additions & 0 deletions packages/web3-eth-abi/src/coders/base/numbersLimits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

/*
* this variable contains the precalculated limits for all the numbers for uint and int types
*/
export const numberLimits = new Map<string, { min: bigint; max: bigint }>();

let base = BigInt(256); // 2 ^ 8 = 256
for (let i = 8; i <= 256; i += 8) {
numberLimits.set(`uint${i}`, {
min: BigInt(0),
max: base - BigInt(1),
});
numberLimits.set(`int${i}`, {
min: -base / BigInt(2),
max: base / BigInt(2) - BigInt(1),
});
base *= BigInt(256);
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
numberLimits.set(`int`, numberLimits.get('int256')!);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
numberLimits.set(`uint`, numberLimits.get('uint256')!);
3 changes: 2 additions & 1 deletion packages/web3-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,5 @@ Documentation:

### Added

- As a replacment of the node EventEmitter, a custom `EventEmitter` has been implemented and exported. (#6398)
- As a replacement of the node EventEmitter, a custom `EventEmitter` has been implemented and exported. (#6398)
- Fix issue with default config with babel (and React): "TypeError: Cannot convert a BigInt value to a number #6187" (#6506)
56 changes: 27 additions & 29 deletions packages/web3-utils/src/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,39 +38,37 @@ import {
InvalidUnitError,
} from 'web3-errors';

const base = BigInt(10);
const expo10 = (expo: number) => base ** BigInt(expo);

// Ref: https://ethdocs.org/en/latest/ether.html
// Note: this could be simplified using ** operator, but babel does not handle it well (https://github.com/babel/babel/issues/13109)
/** @internal */
export const ethUnitMap = {
noether: BigInt('0'),
noether: BigInt(0),
wei: BigInt(1),
kwei: expo10(3),
Kwei: expo10(3),
babbage: expo10(3),
femtoether: expo10(3),
mwei: expo10(6),
Mwei: expo10(6),
lovelace: expo10(6),
picoether: expo10(6),
gwei: expo10(9),
Gwei: expo10(9),
shannon: expo10(9),
nanoether: expo10(9),
nano: expo10(9),
szabo: expo10(12),
microether: expo10(12),
micro: expo10(12),
finney: expo10(15),
milliether: expo10(15),
milli: expo10(15),
ether: expo10(18),
kether: expo10(21),
grand: expo10(21),
mether: expo10(24),
gether: expo10(27),
tether: expo10(30),
kwei: BigInt(1000),
Kwei: BigInt(1000),
babbage: BigInt(1000),
femtoether: BigInt(1000),
mwei: BigInt(1000000),
Mwei: BigInt(1000000),
lovelace: BigInt(1000000),
picoether: BigInt(1000000),
gwei: BigInt(1000000000),
Gwei: BigInt(1000000000),
shannon: BigInt(1000000000),
nanoether: BigInt(1000000000),
nano: BigInt(1000000000),
szabo: BigInt(1000000000000),
microether: BigInt(1000000000000),
micro: BigInt(1000000000000),
finney: BigInt(1000000000000000),
milliether: BigInt(1000000000000000),
milli: BigInt(1000000000000000),
ether: BigInt('1000000000000000000'),
kether: BigInt('1000000000000000000000'),
grand: BigInt('1000000000000000000000'),
mether: BigInt('1000000000000000000000000'),
gether: BigInt('1000000000000000000000000000'),
tether: BigInt('1000000000000000000000000000000'),
};

export type EtherUnits = keyof typeof ethUnitMap;
Expand Down
6 changes: 3 additions & 3 deletions packages/web3-utils/src/string_manipulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.

import { Numbers } from 'web3-types';
import { NibbleWidthError } from 'web3-errors';
import { isHexStrict, validator, utils as validatorUtils } from 'web3-validator';
import { isHexStrict, validator, utils as validatorUtils, bigintPower } from 'web3-validator';
import { numberToHex, toHex, toNumber } from './converters.js';

/**
Expand Down Expand Up @@ -115,7 +115,7 @@ export const toTwosComplement = (value: Numbers, nibbleWidth = 64): string => {

if (val >= 0) return padLeft(toHex(val), nibbleWidth);

const largestBit = BigInt(2) ** BigInt(nibbleWidth * 4);
const largestBit = bigintPower(BigInt(2), BigInt(nibbleWidth * 4));
if (-val >= largestBit) {
throw new NibbleWidthError(`value: ${value}, nibbleWidth: ${nibbleWidth}`);
}
Expand Down Expand Up @@ -156,7 +156,7 @@ export const fromTwosComplement = (value: Numbers, nibbleWidth = 64): number | b
// check the largest bit to see if negative
if (nibbleWidth * 4 !== largestBit) return val;

const complement = BigInt(2) ** (BigInt(nibbleWidth) * BigInt(4));
const complement = bigintPower(BigInt(2), BigInt(nibbleWidth) * BigInt(4));

return toNumber(BigInt(val) - complement);
};
3 changes: 2 additions & 1 deletion packages/web3-validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,5 @@ Documentation:

### Fixed

- Multi-dimensional arrays are now handled properly when parsing ABIs
- Multi-dimensional arrays are now handled properly when parsing ABIs (#6435)
- Fix issue with default config with babel (and React): "TypeError: Cannot convert a BigInt value to a number #6187" (#6506)
17 changes: 14 additions & 3 deletions packages/web3-validator/src/validation/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ import { isHexStrict } from './string.js';
*/
export const isBigInt = (value: ValidInputTypes): boolean => typeof value === 'bigint';

// Note: this could be simplified using ** operator, but babel does not handle it well
// 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) => {
let res = base;
for (let index = 1; index < expo; index += 1) {
res *= base;
}
return res;
};

export const isUInt = (
value: ValidInputTypes,
options: { abiType: string; bitSize?: never } | { bitSize: number; abiType?: never } = {
Expand All @@ -49,7 +60,7 @@ export const isUInt = (
size = options.bitSize;
}

const maxSize = BigInt(2) ** BigInt(size ?? 256) - BigInt(1);
const maxSize = bigintPower(BigInt(2), BigInt(size ?? 256)) - BigInt(1);

try {
const valueToCheck =
Expand Down Expand Up @@ -94,8 +105,8 @@ export const isInt = (
size = options.bitSize;
}

const maxSize = BigInt(2) ** BigInt((size ?? 256) - 1);
const minSize = BigInt(-1) * BigInt(2) ** BigInt((size ?? 256) - 1);
const maxSize = bigintPower(BigInt(2), BigInt((size ?? 256) - 1));
const minSize = BigInt(-1) * bigintPower(BigInt(2), BigInt((size ?? 256) - 1));

try {
const valueToCheck =
Expand Down

0 comments on commit 0306e72

Please sign in to comment.