Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 0306e72

Browse files
authored
Merge branch '4.x' into ok/6459-4.x]-Enable-plugins-to-register-builder-for-new-transaction-types
2 parents 8f98309 + 42502b6 commit 0306e72

File tree

8 files changed

+93
-57
lines changed

8 files changed

+93
-57
lines changed

packages/web3-eth-abi/CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,8 @@ Documentation:
142142

143143
- Dependencies updated
144144

145-
## [Unreleased]
145+
## [Unreleased]
146+
147+
### Fixed
148+
149+
- Fix issue with default config with babel (and React): "TypeError: Cannot convert a BigInt value to a number #6187" (#6506)

packages/web3-eth-abi/src/coders/base/number.ts

+1-19
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { padLeft, toBigInt } from 'web3-utils';
2121
import { utils } from 'web3-validator';
2222
import { DecoderResult, EncoderResult } from '../types.js';
2323
import { WORD_SIZE } from '../utils.js';
24+
import { numberLimits } from './numbersLimits.js';
2425

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

46-
const numberLimits = new Map<string, { min: bigint; max: bigint }>();
47-
48-
// precalculate all the limits
49-
for (let i = 8; i <= 256; i += 8) {
50-
numberLimits.set(`uint${i}`, {
51-
min: BigInt(0),
52-
max: BigInt(2) ** BigInt(i) - BigInt(1),
53-
});
54-
numberLimits.set(`int${i}`, {
55-
min: -(BigInt(2) ** BigInt(i - 1)),
56-
max: BigInt(2) ** BigInt(i - 1) - BigInt(1),
57-
});
58-
}
59-
60-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
61-
numberLimits.set(`int`, numberLimits.get('int256')!);
62-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
63-
numberLimits.set(`uint`, numberLimits.get('uint256')!);
64-
6547
export function encodeNumber(param: AbiParameter, input: unknown): EncoderResult {
6648
let value;
6749
try {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
This file is part of web3.js.
3+
4+
web3.js is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU Lesser General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
web3.js is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public License
15+
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
/*
19+
* this variable contains the precalculated limits for all the numbers for uint and int types
20+
*/
21+
export const numberLimits = new Map<string, { min: bigint; max: bigint }>();
22+
23+
let base = BigInt(256); // 2 ^ 8 = 256
24+
for (let i = 8; i <= 256; i += 8) {
25+
numberLimits.set(`uint${i}`, {
26+
min: BigInt(0),
27+
max: base - BigInt(1),
28+
});
29+
numberLimits.set(`int${i}`, {
30+
min: -base / BigInt(2),
31+
max: base / BigInt(2) - BigInt(1),
32+
});
33+
base *= BigInt(256);
34+
}
35+
36+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
37+
numberLimits.set(`int`, numberLimits.get('int256')!);
38+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
39+
numberLimits.set(`uint`, numberLimits.get('uint256')!);

packages/web3-utils/CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,5 @@ Documentation:
162162

163163
### Added
164164

165-
- As a replacment of the node EventEmitter, a custom `EventEmitter` has been implemented and exported. (#6398)
165+
- As a replacement of the node EventEmitter, a custom `EventEmitter` has been implemented and exported. (#6398)
166+
- Fix issue with default config with babel (and React): "TypeError: Cannot convert a BigInt value to a number #6187" (#6506)

packages/web3-utils/src/converters.ts

+27-29
Original file line numberDiff line numberDiff line change
@@ -38,39 +38,37 @@ import {
3838
InvalidUnitError,
3939
} from 'web3-errors';
4040

41-
const base = BigInt(10);
42-
const expo10 = (expo: number) => base ** BigInt(expo);
43-
4441
// Ref: https://ethdocs.org/en/latest/ether.html
42+
// Note: this could be simplified using ** operator, but babel does not handle it well (https://github.com/babel/babel/issues/13109)
4543
/** @internal */
4644
export const ethUnitMap = {
47-
noether: BigInt('0'),
45+
noether: BigInt(0),
4846
wei: BigInt(1),
49-
kwei: expo10(3),
50-
Kwei: expo10(3),
51-
babbage: expo10(3),
52-
femtoether: expo10(3),
53-
mwei: expo10(6),
54-
Mwei: expo10(6),
55-
lovelace: expo10(6),
56-
picoether: expo10(6),
57-
gwei: expo10(9),
58-
Gwei: expo10(9),
59-
shannon: expo10(9),
60-
nanoether: expo10(9),
61-
nano: expo10(9),
62-
szabo: expo10(12),
63-
microether: expo10(12),
64-
micro: expo10(12),
65-
finney: expo10(15),
66-
milliether: expo10(15),
67-
milli: expo10(15),
68-
ether: expo10(18),
69-
kether: expo10(21),
70-
grand: expo10(21),
71-
mether: expo10(24),
72-
gether: expo10(27),
73-
tether: expo10(30),
47+
kwei: BigInt(1000),
48+
Kwei: BigInt(1000),
49+
babbage: BigInt(1000),
50+
femtoether: BigInt(1000),
51+
mwei: BigInt(1000000),
52+
Mwei: BigInt(1000000),
53+
lovelace: BigInt(1000000),
54+
picoether: BigInt(1000000),
55+
gwei: BigInt(1000000000),
56+
Gwei: BigInt(1000000000),
57+
shannon: BigInt(1000000000),
58+
nanoether: BigInt(1000000000),
59+
nano: BigInt(1000000000),
60+
szabo: BigInt(1000000000000),
61+
microether: BigInt(1000000000000),
62+
micro: BigInt(1000000000000),
63+
finney: BigInt(1000000000000000),
64+
milliether: BigInt(1000000000000000),
65+
milli: BigInt(1000000000000000),
66+
ether: BigInt('1000000000000000000'),
67+
kether: BigInt('1000000000000000000000'),
68+
grand: BigInt('1000000000000000000000'),
69+
mether: BigInt('1000000000000000000000000'),
70+
gether: BigInt('1000000000000000000000000000'),
71+
tether: BigInt('1000000000000000000000000000000'),
7472
};
7573

7674
export type EtherUnits = keyof typeof ethUnitMap;

packages/web3-utils/src/string_manipulation.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
1717

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

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

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

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

159-
const complement = BigInt(2) ** (BigInt(nibbleWidth) * BigInt(4));
159+
const complement = bigintPower(BigInt(2), BigInt(nibbleWidth) * BigInt(4));
160160

161161
return toNumber(BigInt(val) - complement);
162162
};

packages/web3-validator/CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,5 @@ Documentation:
151151

152152
### Fixed
153153

154-
- Multi-dimensional arrays are now handled properly when parsing ABIs
154+
- Multi-dimensional arrays are now handled properly when parsing ABIs (#6435)
155+
- Fix issue with default config with babel (and React): "TypeError: Cannot convert a BigInt value to a number #6187" (#6506)

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

+14-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ import { isHexStrict } from './string.js';
2424
*/
2525
export const isBigInt = (value: ValidInputTypes): boolean => typeof value === 'bigint';
2626

27+
// Note: this could be simplified using ** operator, but babel does not handle it well
28+
// you can find more at: https://github.com/babel/babel/issues/13109 and https://github.com/web3/web3.js/issues/6187
29+
/** @internal */
30+
export const bigintPower = (base: bigint, expo: bigint) => {
31+
let res = base;
32+
for (let index = 1; index < expo; index += 1) {
33+
res *= base;
34+
}
35+
return res;
36+
};
37+
2738
export const isUInt = (
2839
value: ValidInputTypes,
2940
options: { abiType: string; bitSize?: never } | { bitSize: number; abiType?: never } = {
@@ -49,7 +60,7 @@ export const isUInt = (
4960
size = options.bitSize;
5061
}
5162

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

5465
try {
5566
const valueToCheck =
@@ -94,8 +105,8 @@ export const isInt = (
94105
size = options.bitSize;
95106
}
96107

97-
const maxSize = BigInt(2) ** BigInt((size ?? 256) - 1);
98-
const minSize = BigInt(-1) * BigInt(2) ** BigInt((size ?? 256) - 1);
108+
const maxSize = bigintPower(BigInt(2), BigInt((size ?? 256) - 1));
109+
const minSize = BigInt(-1) * bigintPower(BigInt(2), BigInt((size ?? 256) - 1));
99110

100111
try {
101112
const valueToCheck =

0 commit comments

Comments
 (0)