Skip to content

Commit

Permalink
fix: contract method types should allow any param if abi type not sup…
Browse files Browse the repository at this point in the history
…plied (#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>
  • Loading branch information
mpetrunic committed Jan 9, 2024
1 parent 762c298 commit 3cfe56f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
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 @@ -113,7 +113,7 @@ type ContractBoundMethod<
Abi extends AbiFunctionFragment,
Method extends ContractMethod<Abi> = ContractMethod<Abi>,
> = (
...args: Method['Inputs']
...args: Method['Inputs'] extends undefined|unknown ? any[] : Method['Inputs']
) => Method['Abi']['stateMutability'] extends 'payable' | 'pure'
? PayableMethodObject<Method['Inputs'], Method['Outputs']>
: NonPayableMethodObject<Method['Inputs'], Method['Outputs']>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,12 @@ describe('contract ERC721 overloaded functions', () => {
it('transferFrom with 3 invalid arguments', () => {
expect(() =>
contractDeployed.methods
// @ts-expect-error-next-line
.safeTransferFrom(1, 2, 3),
).toThrow('Web3 validator');
});

it('transferFrom with 2 arguments', () => {
expect(() =>
// @ts-expect-error-next-line
contractDeployed.methods.safeTransferFrom(localAccount.address, localAccount.address),
).toThrow('Web3 validator');
});
Expand Down
20 changes: 19 additions & 1 deletion packages/web3-eth-contract/test/unit/contract_typing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,31 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
/* eslint-disable jest/expect-expect */

import { expectTypeOf, typecheck } from '@humeris/espresso-shot';
import { Numbers } from 'web3-types';
import { ContractConstructorArgs, Numbers } from 'web3-types';
import { Contract } from '../../src/contract';
import { erc20Abi, Erc20Interface } from '../fixtures/erc20';
import { erc721Abi, Erc721Interface } from '../fixtures/erc721';
import { NonPayableMethodObject, PayableMethodObject } from '../../src';

describe('contract typing', () => {

describe('no abi type', () => {
const defaultContractInstance = new Contract([]);
// when using new web3.eth.Contract generic is any[] instead of never
const web3ContractInstance = new Contract<any[]>([]);

typecheck('should allow any contract init params', () => [
expectTypeOf<ContractConstructorArgs<any[]>>().not.toBe<[]>(),
expectTypeOf<ContractConstructorArgs<never>>().not.toBe<[]>(),
expectTypeOf<ContractConstructorArgs<[]>>().not.toBe<[]>(),
]);

typecheck('should allow any input params', () => [
expectTypeOf<Parameters<typeof defaultContractInstance.methods.test>>().toBe<any[]>(),
expectTypeOf<Parameters<typeof web3ContractInstance.methods.test>>().toBe<any[]>(),
]);

})
describe('custom abi', () => {
const abi = [
{
Expand Down

1 comment on commit 3cfe56f

@github-actions
Copy link

Choose a reason for hiding this comment

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

Benchmark

Benchmark suite Current: 3cfe56f Previous: 6c075db Ratio
processingTx 9089 ops/sec (±5.02%) 9301 ops/sec (±4.81%) 1.02
processingContractDeploy 40703 ops/sec (±6.98%) 39129 ops/sec (±7.62%) 0.96
processingContractMethodSend 19807 ops/sec (±6.94%) 19443 ops/sec (±5.19%) 0.98
processingContractMethodCall 38996 ops/sec (±6.69%) 38971 ops/sec (±6.34%) 1.00
abiEncode 45722 ops/sec (±6.51%) 44252 ops/sec (±6.92%) 0.97
abiDecode 31812 ops/sec (±8.11%) 30419 ops/sec (±8.89%) 0.96
sign 1685 ops/sec (±1.01%) 1656 ops/sec (±4.08%) 0.98
verify 375 ops/sec (±0.53%) 373 ops/sec (±0.78%) 0.99

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.