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

Avoid using ** with BigInt #6506

Merged
Merged
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),
Copy link
Contributor

Choose a reason for hiding this comment

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

Exponentiation Operator usage issue was solved by specifying min browser versions in one of issue?
Its supported in ECMAScript 2016 and

Chrome 52 | Edge 14 | Firefox 52 | Safari 10.1 | Opera 39
Jul 2016 | Aug 2016 | Mar 2017 | Mar 2017 | Aug 2016

so react with older browsers is causing this problem only, and if some one specify min browsers list in react config and use on ECMAScript 2016 supporting browser there is no issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, the issue is present with the latest as well as any older version of React . And any other framework that uses babel (the latest version of babel and any older version as well).

And this is what happens:

  1. When building React app (to generate the build folder to be used for deployment), babel is used to tans-compile the code.
  2. Babel will convert every ** to Math.pow.
  3. When browsing the page, an exception will be thrown because Math.pow does not work with BigInt.

Currently, the only way to avoid this behavior is to add configuration to package.json as mentioned in: babel/babel#13109 (comment) and applied in #6187 (comment)

And this MR propose changes so the users do not need to add configurations to their package.json.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is better to have our own bigint pow function right now. and wait while Babel starts supporting it

});
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
Loading
Loading