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

[DRAFT] Allow smart contract methods to accept parameters when ABI was provided not as const #6636

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: Abi extends AbiFunctionFragment ? Method['Inputs'] : any
) => Method['Abi']['stateMutability'] extends 'payable' | 'pure'
? PayableMethodObject<Method['Inputs'], Method['Outputs']>
: NonPayableMethodObject<Method['Inputs'], Method['Outputs']>;
Expand Down
33 changes: 33 additions & 0 deletions packages/web3-eth-contract/test/unit/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1595,6 +1595,39 @@ describe('Contract', () => {
spyEstimateGas.mockClear();
});

it('estimateGas should work for contract method even with no ABI', async () => {
const contract = new Contract([], deployedAddr);

const spyTx = jest.spyOn(eth, 'sendTransaction').mockImplementation(() => {
const newContract = contract.clone();
newContract.options.address = deployedAddr;
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return Promise.resolve(newContract) as any;
});

const spyEstimateGas = jest
.spyOn(eth, 'estimateGas')
.mockImplementationOnce((_objInstance, _tx, _block) => {
expect(_block).toBe('latest');
expect(_tx.to).toStrictEqual(deployedAddr);
expect(_tx.from).toStrictEqual(sendOptions.from);
expect(_tx.data).toBe(
'0xa41368620000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000',
);

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return Promise.resolve(BigInt(36916)) as any;
});

// This produces an error at runtime.
// This needs investigation and fixing.
const result = await contract.methods.setGreeting().estimateGas(sendOptions);
expect(result).toStrictEqual(BigInt(36916));

spyTx.mockClear();
spyEstimateGas.mockClear();
});

it('encodeABI should work for contract method', async () => {
const arg = 'Hello';

Expand Down