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

Commit 0beb794

Browse files
authored
Change default return type for integer values to BigInt (#5137)
* 🎨 Update the code for small fixes * ✅ Update failing tests * ✅ Fix failing tests * 🎨 Add version attribute to 'Web3' class * 🚚 Rename the misused promievent names * ✏️ Fix the typos in tests * ✏️ Fix some typo in tests files * 🎨 Improve the code as per feedback * 🎨 Improve code with feedback * 🎨 Fix some typo in refactoring * 🏗️ Update the default return format to bigint * 🧪 Fix few eth tests * ✅ Fix all tests for web3-eth * 🎨 Add more test cases for the validator * ✅ Fix tests for eth-personal * ✅ Fix contracts tests * 🧪 Fix failing tests for 'web3-eth' * 🚨 Fix linter warnings * ✅ Fix integration tests for 'web3-eth' * ✅ Fix failing tests * ✅ Fix ws tests * ✅ Fix some expecations for the integration tests * 🎨 Improve validation logic * 🎨 Update few expectations * ✅ Update contracts integration tests
1 parent f498948 commit 0beb794

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+432
-442
lines changed

packages/web3-common/src/formatter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ export type DataFormat = {
6060
readonly bytes: FMT_BYTES;
6161
};
6262

63-
export const DEFAULT_RETURN_FORMAT = { number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX } as const;
63+
export const DEFAULT_RETURN_FORMAT = { number: FMT_NUMBER.BIGINT, bytes: FMT_BYTES.HEX } as const;
64+
export const ETH_DATA_FORMAT = { number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX } as const;
6465

65-
// Added `undefined` to cover optional type
6666
export type FormatType<T, F extends DataFormat> = number extends Extract<T, Numbers>
6767
? NumberTypes[F['number']] | Exclude<T, Numbers>
6868
: Buffer extends Extract<T, Bytes>

packages/web3-common/src/formatters.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,14 +359,14 @@ export const outputLogFormatter = (log: Partial<LogsInput>): LogsOutput => {
359359
modifiedLog.id = undefined;
360360
}
361361

362-
if (log.blockNumber) {
362+
if (log.blockNumber && isHexStrict(log.blockNumber)) {
363363
modifiedLog.blockNumber = hexToNumber(log.blockNumber);
364364
}
365-
if (log.transactionIndex) {
365+
if (log.transactionIndex && isHexStrict(log.transactionIndex)) {
366366
modifiedLog.transactionIndex = hexToNumber(log.transactionIndex);
367367
}
368368

369-
if (log.logIndex) {
369+
if (log.logIndex && isHexStrict(log.logIndex)) {
370370
modifiedLog.logIndex = hexToNumber(log.logIndex);
371371
}
372372

packages/web3-common/src/web3_base_provider.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ export abstract class Web3BaseProvider<API extends Web3APISpec = EthExecutionAPI
5454

5555
/**
5656
* @deprecated Please use `.request` instead.
57-
*
5857
* @param payload - Request Payload
5958
* @param cb - Callback
6059
*/
@@ -75,7 +74,6 @@ export abstract class Web3BaseProvider<API extends Web3APISpec = EthExecutionAPI
7574

7675
/**
7776
* @deprecated Please use `.request` instead.
78-
*
7977
* @param payload - Request Payload
8078
* @param cb - Callback
8179
*/

packages/web3-eth-contract/src/contract.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ export class Contract<Abi extends ContractAbi>
616616

617617
return sendTransaction(this, tx, DEFAULT_RETURN_FORMAT, {
618618
transactionResolver: receipt => {
619-
if (receipt.status === '0x0') {
619+
if (receipt.status === BigInt(0)) {
620620
throw new Web3ContractError(
621621
'contract deployment error',
622622
receipt as ReceiptInfo,

packages/web3-eth-contract/test/integration/contract_methods.test.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,15 @@ describe('contract', () => {
6565
.send(sendOptions);
6666

6767
expect(receipt).toEqual(
68-
expect.objectContaining({ status: '0x1', transactionHash: expect.any(String) }),
68+
expect.objectContaining({
69+
// status: BigInt(1),
70+
transactionHash: expect.any(String),
71+
}),
6972
);
73+
74+
// To avoid issue with the `objectContaining` and `cypress` had to add
75+
// these expectations explicitly on each attribute
76+
expect(receipt.status).toEqual(BigInt(1));
7077
});
7178

7279
it('should returns a receipt (EIP-1559, maxFeePerGas and maxPriorityFeePerGas specified)', async () => {
@@ -78,15 +85,36 @@ describe('contract', () => {
7885
});
7986

8087
expect(receipt).toEqual(
81-
expect.objectContaining({ status: '0x1', transactionHash: expect.any(String) }),
88+
expect.objectContaining({
89+
// status: BigInt(1),
90+
transactionHash: expect.any(String),
91+
}),
8292
);
93+
94+
// To avoid issue with the `objectContaining` and `cypress` had to add
95+
// these expectations explicitly on each attribute
96+
expect(receipt.status).toEqual(BigInt(1));
8397
});
8498

8599
// TODO: Get and match the revert error message
86100
it('should returns errors on reverts', async () => {
87-
return expect(contract.methods.reverts().send(sendOptions)).rejects.toEqual(
88-
expect.objectContaining({ status: '0x0', transactionHash: expect.any(String) }),
89-
);
101+
try {
102+
await contract.methods.reverts().send(sendOptions);
103+
} catch (receipt: any) {
104+
// eslint-disable-next-line jest/no-conditional-expect
105+
expect(receipt).toEqual(
106+
expect.objectContaining({
107+
transactionHash: expect.any(String),
108+
}),
109+
);
110+
111+
// To avoid issue with the `objectContaining` and `cypress` had to add
112+
// these expectations explicitly on each attribute
113+
// eslint-disable-next-line jest/no-conditional-expect
114+
expect(receipt.status).toEqual(BigInt(0));
115+
}
116+
117+
expect.assertions(2);
90118
});
91119
});
92120
});

packages/web3-eth-ens/src/ens.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
import { Web3Context, SupportedProviders, Web3ContextObject } from 'web3-core';
2525
import { getId, Web3NetAPI } from 'web3-net';
2626
import { Address } from 'web3-utils';
27-
import { EthExecutionAPI, DEFAULT_RETURN_FORMAT } from 'web3-common';
27+
import { EthExecutionAPI, DEFAULT_RETURN_FORMAT, FormatType, FMT_NUMBER } from 'web3-common';
2828
import { NonPayableCallOptions, Contract } from 'web3-eth-contract';
2929
import { RESOLVER } from './abi/resolver';
3030
import { Registry } from './registry';
@@ -71,7 +71,10 @@ export class ENS extends Web3Context<EthExecutionAPI & Web3NetAPI> {
7171
name: string,
7272
address: Address,
7373
txConfig: NonPayableCallOptions,
74-
): Promise<ReceiptInfo | RevertInstructionError> {
74+
): Promise<
75+
| FormatType<ReceiptInfo, typeof DEFAULT_RETURN_FORMAT>
76+
| FormatType<RevertInstructionError, typeof DEFAULT_RETURN_FORMAT>
77+
> {
7578
return this._registry.setResolver(name, address, txConfig);
7679
}
7780

@@ -289,7 +292,10 @@ export class ENS extends Web3Context<EthExecutionAPI & Web3NetAPI> {
289292
if (this._detectedAddress) {
290293
return this._detectedAddress;
291294
}
292-
const networkType = await getId(this, DEFAULT_RETURN_FORMAT); // get the network from provider
295+
const networkType = await getId(this, {
296+
...DEFAULT_RETURN_FORMAT,
297+
number: FMT_NUMBER.HEX,
298+
}); // get the network from provider
293299
const addr = registryAddresses[networkType];
294300

295301
if (typeof addr === 'undefined') {

packages/web3-eth-personal/src/rpc_method_wrappers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ You should have received a copy of the GNU Lesser General Public License
1515
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

18-
import { DEFAULT_RETURN_FORMAT } from 'web3-common';
18+
import { ETH_DATA_FORMAT } from 'web3-common';
1919
import { formatTransaction, Transaction } from 'web3-eth';
2020
import { Address, HexString, isHexStrict, toChecksumAddress, utf8ToHex } from 'web3-utils';
2121
import { validator } from 'web3-validator';
@@ -78,7 +78,7 @@ export const sendTransaction = async (
7878
tx: Transaction,
7979
passphrase: string,
8080
) => {
81-
const formattedTx = formatTransaction(tx, DEFAULT_RETURN_FORMAT);
81+
const formattedTx = formatTransaction(tx, ETH_DATA_FORMAT);
8282

8383
return rpcSendTransaction(requestManager, formattedTx, passphrase);
8484
};
@@ -88,7 +88,7 @@ export const signTransaction = async (
8888
tx: Transaction,
8989
passphrase: string,
9090
) => {
91-
const formattedTx = formatTransaction(tx, DEFAULT_RETURN_FORMAT);
91+
const formattedTx = formatTransaction(tx, ETH_DATA_FORMAT);
9292

9393
return rpcSignTransaction(requestManager, formattedTx, passphrase);
9494
};

packages/web3-eth-personal/test/unit/eth_personal.test.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ You should have received a copy of the GNU Lesser General Public License
1515
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

18+
import { ETH_DATA_FORMAT } from 'web3-common';
1819
import * as utils from 'web3-utils';
1920
import * as eth from 'web3-eth';
2021
import { validator } from 'web3-validator';
@@ -185,10 +186,7 @@ describe('Personal', () => {
185186
await personal.sendTransaction(tx, 'password');
186187

187188
expect(eth.formatTransaction).toHaveBeenCalledTimes(1);
188-
expect(eth.formatTransaction).toHaveBeenCalledWith(tx, {
189-
bytes: 'BYTES_HEX',
190-
number: 'NUMBER_HEX',
191-
});
189+
expect(eth.formatTransaction).toHaveBeenCalledWith(tx, ETH_DATA_FORMAT);
192190
});
193191
});
194192

@@ -218,10 +216,7 @@ describe('Personal', () => {
218216
await personal.signTransaction(tx, 'password');
219217

220218
expect(eth.formatTransaction).toHaveBeenCalledTimes(1);
221-
expect(eth.formatTransaction).toHaveBeenCalledWith(tx, {
222-
bytes: 'BYTES_HEX',
223-
number: 'NUMBER_HEX',
224-
});
219+
expect(eth.formatTransaction).toHaveBeenCalledWith(tx, ETH_DATA_FORMAT);
225220
});
226221
});
227222

packages/web3-eth/src/constants.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
import { FMT_BYTES, FMT_NUMBER } from 'web3-common';
18+
19+
export const NUMBER_DATA_FORMAT = { bytes: FMT_BYTES.HEX, number: FMT_NUMBER.NUMBER } as const;

0 commit comments

Comments
 (0)