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

Change default return type for integer values to BigInt #5137

Merged
merged 29 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1261545
:art: Update the code for small fixes
nazarhussain Jun 13, 2022
24b54df
Merge branch '4.x' into nh/5125-4.x-refactor
nazarhussain Jun 13, 2022
2e8eb6e
:white_check_mark: Update failing tests
nazarhussain Jun 13, 2022
b527ca8
:white_check_mark: Fix failing tests
nazarhussain Jun 13, 2022
57e6755
:art: Add version attribute to 'Web3' class
nazarhussain Jun 14, 2022
c0757de
:truck: Rename the misused promievent names
nazarhussain Jun 14, 2022
f785298
:pencil2: Fix the typos in tests
nazarhussain Jun 14, 2022
da0e926
Merge branch '4.x' into nh/5125-4.x-refactor
nazarhussain Jun 14, 2022
ed1708c
:pencil2: Fix some typo in tests files
nazarhussain Jun 14, 2022
6e74b6c
:art: Improve the code as per feedback
nazarhussain Jun 14, 2022
c9f593a
:art: Improve code with feedback
nazarhussain Jun 14, 2022
257f127
:art: Fix some typo in refactoring
nazarhussain Jun 14, 2022
15b8640
:building_construction: Update the default return format to bigint
nazarhussain Jun 14, 2022
ec3716c
:test_tube: Fix few eth tests
nazarhussain Jun 14, 2022
3321461
:white_check_mark: Fix all tests for web3-eth
nazarhussain Jun 15, 2022
3bfa8a5
:art: Add more test cases for the validator
nazarhussain Jun 15, 2022
d080142
:white_check_mark: Fix tests for eth-personal
nazarhussain Jun 15, 2022
baed875
:white_check_mark: Fix contracts tests
nazarhussain Jun 15, 2022
b715ff4
Merge branch '4.x' into nh/5128-default-return-type
nazarhussain Jun 16, 2022
7acc2e6
:test_tube: Fix failing tests for 'web3-eth'
nazarhussain Jun 20, 2022
61e5dd3
:rotating_light: Fix linter warnings
nazarhussain Jun 20, 2022
6143933
:white_check_mark: Fix integration tests for 'web3-eth'
nazarhussain Jun 20, 2022
78cdf04
:white_check_mark: Fix failing tests
nazarhussain Jun 20, 2022
8b9b140
:white_check_mark: Fix ws tests
nazarhussain Jun 20, 2022
090cee7
:white_check_mark: Fix some expecations for the integration tests
nazarhussain Jun 21, 2022
af331a5
Merge branch '4.x' into nh/5128-default-return-type
nazarhussain Jun 21, 2022
dd08f8b
:art: Improve validation logic
nazarhussain Jun 21, 2022
79d7411
:art: Update few expectations
nazarhussain Jun 21, 2022
a0846c5
:white_check_mark: Update contracts integration tests
nazarhussain Jun 21, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/web3-common/src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ export type DataFormat = {
readonly bytes: FMT_BYTES;
};

export const DEFAULT_RETURN_FORMAT = { number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX } as const;
export const DEFAULT_RETURN_FORMAT = { number: FMT_NUMBER.BIGINT, bytes: FMT_BYTES.HEX } as const;
export const ETH_DATA_FORMAT = { number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX } as const;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
export const ETH_DATA_FORMAT = { number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX } as const;
export const ETH_INPUT_DATA_FORMAT = { number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX } as const;

I think should rename its all occurrences for better readability.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jdevcs As per my knowledge, Eth implementation follows the same format for input/output. In that aspect I named this value as ETH_DATA_FORMAT. Please suggest if you think ETH_INPUT_DATA_FORMAT is better usage?


// Added `undefined` to cover optional type
export type FormatType<T, F extends DataFormat> = number extends Extract<T, Numbers>
? NumberTypes[F['number']] | Exclude<T, Numbers>
: Buffer extends Extract<T, Bytes>
Expand Down
6 changes: 3 additions & 3 deletions packages/web3-common/src/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,14 +359,14 @@ export const outputLogFormatter = (log: Partial<LogsInput>): LogsOutput => {
modifiedLog.id = undefined;
}

if (log.blockNumber) {
if (log.blockNumber && isHexStrict(log.blockNumber)) {
modifiedLog.blockNumber = hexToNumber(log.blockNumber);
}
if (log.transactionIndex) {
if (log.transactionIndex && isHexStrict(log.transactionIndex)) {
modifiedLog.transactionIndex = hexToNumber(log.transactionIndex);
}

if (log.logIndex) {
if (log.logIndex && isHexStrict(log.logIndex)) {
modifiedLog.logIndex = hexToNumber(log.logIndex);
}

Expand Down
2 changes: 0 additions & 2 deletions packages/web3-common/src/web3_base_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export abstract class Web3BaseProvider<API extends Web3APISpec = EthExecutionAPI

/**
* @deprecated Please use `.request` instead.
*
* @param payload - Request Payload
* @param cb - Callback
*/
Expand All @@ -75,7 +74,6 @@ export abstract class Web3BaseProvider<API extends Web3APISpec = EthExecutionAPI

/**
* @deprecated Please use `.request` instead.
*
* @param payload - Request Payload
* @param cb - Callback
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth-contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ export class Contract<Abi extends ContractAbi>

return sendTransaction(this, tx, DEFAULT_RETURN_FORMAT, {
transactionResolver: receipt => {
if (receipt.status === '0x0') {
if (receipt.status === BigInt(0)) {
throw new Web3ContractError(
'contract deployment error',
receipt as ReceiptInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,15 @@ describe('contract', () => {
.send(sendOptions);

expect(receipt).toEqual(
expect.objectContaining({ status: '0x1', transactionHash: expect.any(String) }),
expect.objectContaining({
// status: BigInt(1),
transactionHash: expect.any(String),
}),
);

// To avoid issue with the `objectContaining` and `cypress` had to add
// these expectations explicitly on each attribute
expect(receipt.status).toEqual(BigInt(1));
});

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

expect(receipt).toEqual(
expect.objectContaining({ status: '0x1', transactionHash: expect.any(String) }),
expect.objectContaining({
// status: BigInt(1),
transactionHash: expect.any(String),
}),
);

// To avoid issue with the `objectContaining` and `cypress` had to add
// these expectations explicitly on each attribute
expect(receipt.status).toEqual(BigInt(1));
});

// TODO: Get and match the revert error message
it('should returns errors on reverts', async () => {
return expect(contract.methods.reverts().send(sendOptions)).rejects.toEqual(
expect.objectContaining({ status: '0x0', transactionHash: expect.any(String) }),
);
try {
await contract.methods.reverts().send(sendOptions);
} catch (receipt: any) {
// eslint-disable-next-line jest/no-conditional-expect
expect(receipt).toEqual(
expect.objectContaining({
transactionHash: expect.any(String),
}),
);

// To avoid issue with the `objectContaining` and `cypress` had to add
// these expectations explicitly on each attribute
// eslint-disable-next-line jest/no-conditional-expect
expect(receipt.status).toEqual(BigInt(0));
}

expect.assertions(2);
});
});
});
Expand Down
12 changes: 9 additions & 3 deletions packages/web3-eth-ens/src/ens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
import { Web3Context, SupportedProviders, Web3ContextObject } from 'web3-core';
import { getId, Web3NetAPI } from 'web3-net';
import { Address } from 'web3-utils';
import { EthExecutionAPI, DEFAULT_RETURN_FORMAT } from 'web3-common';
import { EthExecutionAPI, DEFAULT_RETURN_FORMAT, FormatType, FMT_NUMBER } from 'web3-common';
import { NonPayableCallOptions, Contract } from 'web3-eth-contract';
import { RESOLVER } from './abi/resolver';
import { Registry } from './registry';
Expand Down Expand Up @@ -71,7 +71,10 @@ export class ENS extends Web3Context<EthExecutionAPI & Web3NetAPI> {
name: string,
address: Address,
txConfig: NonPayableCallOptions,
): Promise<ReceiptInfo | RevertInstructionError> {
): Promise<
| FormatType<ReceiptInfo, typeof DEFAULT_RETURN_FORMAT>
| FormatType<RevertInstructionError, typeof DEFAULT_RETURN_FORMAT>
> {
return this._registry.setResolver(name, address, txConfig);
}

Expand Down Expand Up @@ -289,7 +292,10 @@ export class ENS extends Web3Context<EthExecutionAPI & Web3NetAPI> {
if (this._detectedAddress) {
return this._detectedAddress;
}
const networkType = await getId(this, DEFAULT_RETURN_FORMAT); // get the network from provider
const networkType = await getId(this, {
...DEFAULT_RETURN_FORMAT,
number: FMT_NUMBER.HEX,
Copy link
Contributor

Choose a reason for hiding this comment

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

If we always want to have number:FMT_NUMBER.HEX here why we are using default format?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To not redefine bytes format. You can't define just one format while declaring one data format.

}); // get the network from provider
const addr = registryAddresses[networkType];

if (typeof addr === 'undefined') {
Expand Down
6 changes: 3 additions & 3 deletions packages/web3-eth-personal/src/rpc_method_wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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/>.
*/

import { DEFAULT_RETURN_FORMAT } from 'web3-common';
import { ETH_DATA_FORMAT } from 'web3-common';
import { formatTransaction, Transaction } from 'web3-eth';
import { Address, HexString, isHexStrict, toChecksumAddress, utf8ToHex } from 'web3-utils';
import { validator } from 'web3-validator';
Expand Down Expand Up @@ -78,7 +78,7 @@ export const sendTransaction = async (
tx: Transaction,
passphrase: string,
) => {
const formattedTx = formatTransaction(tx, DEFAULT_RETURN_FORMAT);
const formattedTx = formatTransaction(tx, ETH_DATA_FORMAT);

return rpcSendTransaction(requestManager, formattedTx, passphrase);
};
Expand All @@ -88,7 +88,7 @@ export const signTransaction = async (
tx: Transaction,
passphrase: string,
) => {
const formattedTx = formatTransaction(tx, DEFAULT_RETURN_FORMAT);
const formattedTx = formatTransaction(tx, ETH_DATA_FORMAT);

return rpcSignTransaction(requestManager, formattedTx, passphrase);
};
Expand Down
11 changes: 3 additions & 8 deletions packages/web3-eth-personal/test/unit/eth_personal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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/>.
*/

import { ETH_DATA_FORMAT } from 'web3-common';
import * as utils from 'web3-utils';
import * as eth from 'web3-eth';
import { validator } from 'web3-validator';
Expand Down Expand Up @@ -185,10 +186,7 @@ describe('Personal', () => {
await personal.sendTransaction(tx, 'password');

expect(eth.formatTransaction).toHaveBeenCalledTimes(1);
expect(eth.formatTransaction).toHaveBeenCalledWith(tx, {
bytes: 'BYTES_HEX',
number: 'NUMBER_HEX',
});
expect(eth.formatTransaction).toHaveBeenCalledWith(tx, ETH_DATA_FORMAT);
});
});

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

expect(eth.formatTransaction).toHaveBeenCalledTimes(1);
expect(eth.formatTransaction).toHaveBeenCalledWith(tx, {
bytes: 'BYTES_HEX',
number: 'NUMBER_HEX',
});
expect(eth.formatTransaction).toHaveBeenCalledWith(tx, ETH_DATA_FORMAT);
});
});

Expand Down
19 changes: 19 additions & 0 deletions packages/web3-eth/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
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/>.
*/
import { FMT_BYTES, FMT_NUMBER } from 'web3-common';

export const NUMBER_DATA_FORMAT = { bytes: FMT_BYTES.HEX, number: FMT_NUMBER.NUMBER } as const;
Loading