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

Commit daaaff7

Browse files
authoredMay 31, 2023
Autotype for contract methods (#6137)
* autotype for contract methods * add method types tests * add changelog * changelog sync
1 parent ab80131 commit daaaff7

File tree

5 files changed

+122
-11
lines changed

5 files changed

+122
-11
lines changed
 

‎CHANGELOG.md

+58-6
Original file line numberDiff line numberDiff line change
@@ -1462,44 +1462,96 @@ should use 4.0.1-alpha.0 for testing.
14621462

14631463
## [Unreleased]
14641464

1465-
### Breaking Changes
1465+
### Added
14661466

1467-
- dropped support for NodeJs@14
1468-
- removed non read-only methods from ens package
1467+
#### web3-errors
14691468

1470-
### Added
1469+
- `InvalidPropertiesForTransactionTypeError` with error code `429` (#6102)
14711470

14721471
#### web3-eth-contract
14731472

14741473
- Added support for `getPastEvents` method to filter `allEvents` and specific event (#6010)
1474+
- Added `maxPriorityFeePerGas` and `maxFeePerGas` in `ContractOptions` type and updated function using it in utils (#6118)
1475+
- Added method's type autodetection by ABI param (#6137)
14751476

14761477
#### web3-types
14771478

14781479
- Added `filters` param to the `Filter` type (#6010)
1480+
- Added types `JsonRpcSubscriptionResultOld`, `Web3ProviderMessageEventCallback`. Added `.on('data')` type support for old providers (#6082)
14791481
- Export for `HardforksOrdered` enum (#6102)
14801482
- Export for `Web3ValidationErrorObject` type (#6102)
14811483

1482-
#### web3-errors
1484+
#### web3-utils
14831485

1484-
- `InvalidPropertiesForTransactionTypeError` with error code `429` (#6102)
1486+
- Optional `hexstrict` parameter added to numberToHex (#6004)
14851487

14861488
### Fixed
14871489

1490+
#### web3-eth
1491+
1492+
- Fixed `ignoreGasPricing` bug with wallet in context (#6071)
1493+
14881494
#### web3-eth-accounts
14891495

14901496
- Fixed ESM import bugs reported in (#6032) and (#6034)
14911497
- ESM projects will not need to run --experimental-specifier-resolution=node (#6127)
14921498

14931499
### Changed
14941500

1501+
#### web3-core
1502+
1503+
- Replaced Buffer for Uint8Array (#6004)
1504+
1505+
#### web3-errors
1506+
1507+
- Nested Smart Contract error data is extracted at `Eip838ExecutionError` constructor and the nested error is set at `innerError` (#6045)
1508+
14951509
#### web3-eth
14961510

14971511
- `formatTransaction` no longer throws a `TransactionDataAndInputError` if it's passed a transaction object with both `data` and `input` properties set (as long as they are the same value) (#6064)
14981512
- Refactored documentation for `rpc_method_wrappers` to point to the previously duplicated documentation found under the `Web3Eth` class documentation (#6054)
1513+
- Replaced Buffer for Uint8Array (#6004)
14991514
- Refactored `defaultTransactionTypeParser` to return correct EIP-2718 types, prior implementation was prioritizing `transaction.hardfork` and ignoring the use of `transaction.gasLimit`. `defaultTransactionTypeParser` will now throw `InvalidPropertiesForTransactionTypeError`s for properties are used that are incompatible with `transaction.type` (#6102)
1515+
- `prepareTransactionForSigning` and `defaultTransactionBuilder` now accepts optional `fillGasPrice` flag and by default will not fill gas(#6071)
1516+
1517+
#### web3-eth-abi
1518+
1519+
- Nested Smart Contract error data hex string is decoded when the error contains the data as object (when the data hex string is inside data.originalError.data or data.data) (#6045)
1520+
1521+
#### web3-eth-accounts
1522+
1523+
- Replaced `Buffer` for `Uint8Array` (#6004)
1524+
- The methods `recover`, `encrypt`, `privateKeyToAddress` does not support type `Buffer` but supports type `Uint8Array` (#6004)
1525+
- The method `parseAndValidatePrivateKey` returns a type `Uint8Array` instead of type `Buffer` (#6004)
1526+
1527+
#### web3-providers-ipc
1528+
1529+
- Replaced Buffer for Uint8Array (#6004)
1530+
1531+
#### web3-types
1532+
1533+
- Removed chainId, to, data & input properties from NonPayableCallOptions
1534+
- Replaced Buffer for Uint8Array (#6004)
1535+
- types `FMT_BYTES.BUFFER`, `Bytes` and `FormatType` and encryption option types for `salt` and `iv` has replaced support for `Buffer` for `Uint8Array` (#6004)
1536+
- Added `internalType` property to the `AbiParameter` type.
1537+
1538+
#### web3-utils
1539+
1540+
- Replaced Buffer for Uint8Array (#6004)
1541+
- The methods `hexToBytes`, `randomBytes` does not return type `Buffer` but type `Uint8Array` (#6004)
1542+
- The methods `sha3` and `keccak256Wrapper` does not accept type `Buffer` but type `Uint8Array` (#6004)
1543+
- The method `bytesToBuffer` has been removed for the usage of `bytesToUint8Array` (#6004)
1544+
1545+
#### web3-validator
1546+
1547+
- Replaced Buffer for Uint8Array (#6004)
15001548

15011549
### Removed
15021550

1551+
#### web3-eth-ens
1552+
1553+
- Removed non read-only methods (#6084)
1554+
15031555
#### web3-validator
15041556

15051557
- `Web3ValidationErrorObject` type is now exported from `web3-types` package (#6102)

‎packages/web3-eth-contract/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,4 @@ const transactionHash = receipt.transactionHash;
259259

260260
- Added support for `getPastEvents` method to filter `allEvents` and specific event (#6010)
261261
- Added `maxPriorityFeePerGas` and `maxFeePerGas` in `ContractOptions` type and updated function using it in utils (#6118)
262+
- Added method's type autodetection by ABI param (#6137)

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -797,11 +797,11 @@ export class Contract<Abi extends ContractAbi>
797797
...(this._overloadedMethodAbis.get(abi.name) ?? []),
798798
abi,
799799
]);
800-
801-
const contractMethod = this._createContractMethod(
802-
this._overloadedMethodAbis.get(abi.name) ?? [],
803-
errorsAbi,
804-
);
800+
const abiFragment = this._overloadedMethodAbis.get(abi.name) ?? [];
801+
const contractMethod = this._createContractMethod<
802+
typeof abiFragment,
803+
AbiErrorFragment
804+
>(abiFragment, errorsAbi);
805805

806806
this._functions[methodName] = {
807807
signature: methodSignature,

‎packages/web3-eth-contract/test/unit/contract.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,28 @@ describe('Contract', () => {
6262
expect(contract).toBeInstanceOf(Contract);
6363
});
6464

65+
it('method should have correct type by ABI', () => {
66+
const contractInstance = new Contract([
67+
{
68+
inputs: [
69+
{
70+
internalType: 'uint256',
71+
name: 'tokenId',
72+
type: 'uint256',
73+
},
74+
],
75+
name: 'tokenURI',
76+
outputs: [{ internalType: 'string', name: '', type: 'string' }],
77+
stateMutability: 'view',
78+
type: 'function',
79+
},
80+
] as const);
81+
82+
const method = contractInstance.methods.tokenURI(123);
83+
84+
expect(method).toBeDefined();
85+
});
86+
6587
it('should init with abi, options and context', () => {
6688
const contract = new Contract(
6789
[],

‎packages/web3-eth-contract/test/unit/contract_typing.test.ts

+36
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,47 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
1818
/* eslint-disable jest/expect-expect */
1919

2020
import { expectTypeOf, typecheck } from '@humeris/espresso-shot';
21+
import { Numbers } from 'web3-types';
2122
import { Contract } from '../../src/contract';
2223
import { erc20Abi, Erc20Interface } from '../fixtures/erc20';
2324
import { erc721Abi, Erc721Interface } from '../fixtures/erc721';
25+
import { NonPayableMethodObject, PayableMethodObject } from '../../src';
2426

2527
describe('contract typing', () => {
28+
describe('custom abi', () => {
29+
const abi = [
30+
{
31+
inputs: [
32+
{
33+
internalType: 'string',
34+
name: 'tokenId',
35+
type: 'string',
36+
},
37+
],
38+
name: 'tokenURI',
39+
outputs: [{ internalType: 'string', name: '', type: 'string' }],
40+
stateMutability: 'view',
41+
type: 'function',
42+
},
43+
] as const;
44+
const contractInstance = new Contract(abi);
45+
interface CustomInterface {
46+
methods: {
47+
[key: string]: (
48+
...args: ReadonlyArray<any>
49+
) =>
50+
| PayableMethodObject<ReadonlyArray<unknown>, ReadonlyArray<unknown>>
51+
| NonPayableMethodObject<ReadonlyArray<unknown>, ReadonlyArray<unknown>>;
52+
tokenURI: (tokenId: Numbers) => NonPayableMethodObject<[Numbers], [string]>;
53+
};
54+
}
55+
56+
typecheck('should contain all methods', () =>
57+
expectTypeOf<keyof typeof contractInstance.methods>().toBe<
58+
keyof CustomInterface['methods']
59+
>(),
60+
);
61+
});
2662
describe('erc20', () => {
2763
const contract = new Contract(erc20Abi);
2864

0 commit comments

Comments
 (0)
This repository has been archived.