Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 14 additions & 10 deletions packages/web3-eth-abi/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,38 +177,42 @@ export type MatchPrimitiveType<
| PrimitiveTupleType<Type, Components>
| never;

export type ContractMethodOutputParameters<Params extends Array<unknown> | undefined> =
Params extends []
export type ContractMethodOutputParameters<Params extends ReadonlyArray<unknown> | undefined> =
Params extends readonly []
? []
: Params extends [infer H, ...infer R]
: Params extends readonly [infer H, ...infer R]
? H extends AbiParameter
? // TODO: Find a way to set name for tuple item
[MatchPrimitiveType<H['type'], H['components']>, ...ContractMethodOutputParameters<R>]
: ContractMethodOutputParameters<R>
: Params extends undefined | unknown
? []
: Params;

export type ContractMethodInputParameters<Params extends Array<unknown> | undefined> =
Params extends []
export type ContractMethodInputParameters<Params extends ReadonlyArray<unknown> | undefined> =
Params extends readonly []
? []
: Params extends [infer H, ...infer R]
: Params extends readonly [infer H, ...infer R]
? H extends AbiParameter
? // TODO: Find a way to set name for tuple item
[MatchPrimitiveType<H['type'], H['components']>, ...ContractMethodInputParameters<R>]
: ContractMethodInputParameters<R>
: Params extends undefined | unknown
? []
: Params;

export type ContractConstructor<Abis extends ContractAbi> = {
[Abi in FilterAbis<Abis, AbiConstructorFragment> as 'constructor']: {
readonly Abi: Abi;
readonly Inputs: ContractMethodInputParameters<[...NonNullable<Abi['inputs']>]>;
readonly Inputs: ContractMethodInputParameters<Abi['inputs']>;
};
}['constructor'];

export type ContractMethod<Abi extends AbiFunctionFragment> = {
readonly Abi: Abi;

readonly Inputs?: ContractMethodInputParameters<[...NonNullable<Abi['inputs']>]>;
readonly Outputs?: ContractMethodOutputParameters<[...NonNullable<Abi['outputs']>]>;
readonly Inputs: ContractMethodInputParameters<Abi['inputs']>;
readonly Outputs: ContractMethodOutputParameters<Abi['outputs']>;
};

export type ContractMethods<Abis extends ContractAbi> = {
Expand All @@ -220,7 +224,7 @@ export type ContractMethods<Abis extends ContractAbi> = {

export type ContractEvent<Abi extends AbiEventFragment> = {
readonly Abi: Abi;
readonly Inputs: ContractMethodInputParameters<[...NonNullable<Abi['inputs']>]>;
readonly Inputs: ContractMethodInputParameters<Abi['inputs']>;
};

export type ContractEvents<Abis extends ContractAbi> = {
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 @@ -92,7 +92,7 @@ import {

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type ContractBoundMethod<Method extends ContractMethod<any>> = (
...args: NonNullable<Method['Inputs']>
...args: Method['Inputs']
) => Method['Abi']['stateMutability'] extends 'payable' | 'pure'
? PayableMethodObject<Method['Inputs'], Method['Outputs']>
: NonPayableMethodObject<Method['Inputs'], Method['Outputs']>;
Expand Down
6 changes: 3 additions & 3 deletions packages/web3-eth-contract/test/unit/contract_typing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ describe('contract typing', () => {
expectTypeOf<keyof typeof contract.methods>().toBe<keyof Erc20Interface['methods']>(),
);

// typecheck.skip('should have interface compliance methods', () =>
// expectTypeOf(contract.methods).toExtend<Erc20Interface['methods']>(),
// );
typecheck('should have interface compliance methods', () =>
expectTypeOf(contract.methods).toExtend<Erc20Interface['methods']>(),
);

typecheck('should have all events', () =>
expectTypeOf<keyof typeof contract.events>().toBe<keyof Erc20Interface['events']>(),
Expand Down