Smart contract has multiple functions with the same name, how to access them with ethers.js?

208 Views Asked by At

I tried to access "multicall" on pancakeswap smart router v3. But it has all 3 functions named "multicall"

when I use the code:

const routerContract_bsc = new ethers.Contract(
    router_bsc,
    [
        'function multicall(uint deadline, bytes data) external returns (uint[] memory amounts)',   
    ],
    signer_bsc
  );

It sends a method ID: "0xd6a0e487" which doesn't match the ID of any of the 3 "multicall" functions.

Router v3 bscscan: https://bscscan.com/address/0x13f4ea83d0bd40e75c8222255bc855a974568dd4#code

1

There are 1 best solutions below

0
Worthy On

When a smart contract has multiple functions with the same name but different parameter types (overloaded functions), you need to specify the function's complete signature to access the intended version. Generate the function signature using the keccak256 hash of the function name and its parameter types. Then, use this signature in ethers.js to interact with the specific function you want to call.