1

So I have two function with the same name but different parameter in my solidity smart contract functionName(args1) and functionName(args1, args2)

And because of this function overloading, the ABI of the smart contract has 2 function with exactly the same name, functionName with 1 argument and functionName with 2 arguments

When I try to use wagmi useContractWrite to call the first function with:

const functionWrite = useContractWrite({
  ...ContractInstance,
  functionName: 'functionName',
  args: [args1],
});

or calling second function

const functionWrite = useContractWrite({
...ContractInstance,
functionName: 'functionName',
args: [args1, args2],
});

It seems wagmi didn't detect the function because of the duplicate name and it gave me this error: 'functionName' doesn't exist in interface for contract "{my contract address}"

Does anyone know how to use overloaded function with wagmi?

1
  • Are you sure those functions are inside you ABI files? Maybe you did not copy them right, or if they are internal functions you will not be able to call them or have them in ABI's
    – Sky
    Dec 12, 2022 at 20:49

1 Answer 1

0

You need to specify the function signature in the functionName field. For instance take this mintable ERC20 contract on the Polygon Mumbai network.

It includes two versions of the mint function:

 function mint(uint256 value) public returns (bool) {
    _mint(_msgSender(), value);
    return true;
 }

 function mint(address account, uint256 value) public returns (bool) {
    _mint(account, value);
    return true;
 }

Using the first function with the useContractWrite hook:

  const { write } = useContractWrite({
    addressOrName: ADDRESS,
    contractInterface: mintableERC20Abi,
    functionName: "mint(uint256)",
    args: [amountToMint],
  });

Using the second function with the useContractWrite hook:

  const { write } = useContractWrite({
    addressOrName: ADDRESS,
    contractInterface: mintableERC20Abi,
    functionName: "mint(address,uint256)",
    args: [receiverAddress, amountToMint],
  });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.