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

11 changes: 5 additions & 6 deletions packages/web3-eth-contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,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 Expand Up @@ -371,9 +371,8 @@ export class Contract<Abi extends ContractAbi>
: returnFormat ?? DEFAULT_RETURN_FORMAT;
const address =
typeof addressOrOptionsOrContext === 'string' ? addressOrOptionsOrContext : undefined;
this.config.contractDataInputFill =
(options as ContractInitOptions)?.dataInputFill ??
this.config.contractDataInputFill;
this.config.contractDataInputFill =
(options as ContractInitOptions)?.dataInputFill ?? this.config.contractDataInputFill;
this._parseAndSetJsonInterface(jsonInterface, returnDataFormat);

if (!isNullish(address)) {
Expand Down Expand Up @@ -1083,13 +1082,13 @@ export class Contract<Abi extends ContractAbi>
options: { ...options, dataInputFill: this.config.contractDataInputFill },
contractOptions: modifiedContractOptions,
});

const transactionToSend = sendTransaction(this, tx, DEFAULT_RETURN_FORMAT, {
// TODO Should make this configurable by the user
checkRevertBeforeSending: false,
contractAbi: this._jsonInterface,
});

// eslint-disable-next-line no-void
void transactionToSend.on('error', (error: unknown) => {
if (error instanceof ContractExecutionError) {
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 @@ -1378,6 +1378,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