Skip to content

Commit 3cfe56f

Browse files
authored
fix: contract method types should allow any param if abi type not supplied (web3#6688)
* Fix default contractor and method input parameter Previously type was [] (zero element tuple) instead of any[] which prevented passing parameters if contract aby type was not defined. * better solution + test Signed-off-by: Marin Petrunic <marin.petrunic@gmail.com> * improve test Signed-off-by: Marin Petrunic <marin.petrunic@gmail.com> * remove expect error directives Signed-off-by: Marin Petrunic <marin.petrunic@gmail.com> * remove extra line --------- Signed-off-by: Marin Petrunic <marin.petrunic@gmail.com>
1 parent 762c298 commit 3cfe56f

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ type ContractBoundMethod<
113113
Abi extends AbiFunctionFragment,
114114
Method extends ContractMethod<Abi> = ContractMethod<Abi>,
115115
> = (
116-
...args: Method['Inputs']
116+
...args: Method['Inputs'] extends undefined|unknown ? any[] : Method['Inputs']
117117
) => Method['Abi']['stateMutability'] extends 'payable' | 'pure'
118118
? PayableMethodObject<Method['Inputs'], Method['Outputs']>
119119
: NonPayableMethodObject<Method['Inputs'], Method['Outputs']>;

packages/web3-eth-contract/test/integration/local_account/contract_overloaded_methods.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,12 @@ describe('contract ERC721 overloaded functions', () => {
104104
it('transferFrom with 3 invalid arguments', () => {
105105
expect(() =>
106106
contractDeployed.methods
107-
// @ts-expect-error-next-line
108107
.safeTransferFrom(1, 2, 3),
109108
).toThrow('Web3 validator');
110109
});
111110

112111
it('transferFrom with 2 arguments', () => {
113112
expect(() =>
114-
// @ts-expect-error-next-line
115113
contractDeployed.methods.safeTransferFrom(localAccount.address, localAccount.address),
116114
).toThrow('Web3 validator');
117115
});

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,31 @@ 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';
21+
import { ContractConstructorArgs, Numbers } from 'web3-types';
2222
import { Contract } from '../../src/contract';
2323
import { erc20Abi, Erc20Interface } from '../fixtures/erc20';
2424
import { erc721Abi, Erc721Interface } from '../fixtures/erc721';
2525
import { NonPayableMethodObject, PayableMethodObject } from '../../src';
2626

2727
describe('contract typing', () => {
28+
29+
describe('no abi type', () => {
30+
const defaultContractInstance = new Contract([]);
31+
// when using new web3.eth.Contract generic is any[] instead of never
32+
const web3ContractInstance = new Contract<any[]>([]);
33+
34+
typecheck('should allow any contract init params', () => [
35+
expectTypeOf<ContractConstructorArgs<any[]>>().not.toBe<[]>(),
36+
expectTypeOf<ContractConstructorArgs<never>>().not.toBe<[]>(),
37+
expectTypeOf<ContractConstructorArgs<[]>>().not.toBe<[]>(),
38+
]);
39+
40+
typecheck('should allow any input params', () => [
41+
expectTypeOf<Parameters<typeof defaultContractInstance.methods.test>>().toBe<any[]>(),
42+
expectTypeOf<Parameters<typeof web3ContractInstance.methods.test>>().toBe<any[]>(),
43+
]);
44+
45+
})
2846
describe('custom abi', () => {
2947
const abi = [
3048
{

0 commit comments

Comments
 (0)