question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I call Contract methods with overloaded signatures?

See original GitHub issue

The contract https://etherscan.io/address/0x226159d592E2b063810a10Ebf6dcbADA94Ed68b8#code has a function addr where second argument is optional. It generates the following ABI (fragment):

const abi = [
  {
    "constant":true,
    "inputs":[{"internalType":"bytes32","name":"node","type":"bytes32"}],
    "name":"addr",
    "outputs":[{"internalType":"address","name":"","type":"address"}],
    "payable":false,
    "stateMutability":"view",
    "type":"function",
  },

  {
    "constant":true,
    "inputs":[
       {"internalType":"bytes32","name":"node","type":"bytes32"},
       {"internalType":"uint256","name":"coinType","type":"uint256"}],
    "name":"addr",
    "outputs":[{"internalType":"bytes","name":"","type":"bytes"}],
    "payable":false,
    "stateMutability":"view",
    "type":"function",
  },
]
new ethers.utils.Interface(abi).encodeFunctionData('addr', [
  '0x981bb41ff8d6a8d71f71f2f44261af41c3279ad75a57b303f263b11646b28b05',
  144,
]);

Which causes an error when encoding arguments:


/Users/bogdan/makabu/unstoppable/resolution/node_modules/@ethersproject/logger/lib/index.js:179
        var error = new Error(message);
                    ^
Error: multiple matching functions (argument="name", value="addr", code=INVALID_ARGUMENT, version=abi/5.0.1)
    at Logger.makeError (/Users/bogdan/makabu/unstoppable/resolution/node_modules/@ethersproject/logger/lib/index.js:179:21)
    at Logger.throwError (/Users/bogdan/makabu/unstoppable/resolution/node_modules/@ethersproject/logger/lib/index.js:188:20)
    at Logger.throwArgumentError (/Users/bogdan/makabu/unstoppable/resolution/node_modules/@ethersproject/logger/lib/index.js:191:21)
    at Interface.getFunction (/Users/bogdan/makabu/unstoppable/resolution/node_modules/@ethersproject/abi/lib/interface.js:179:24)
    at Interface.encodeFunctionData (/Users/bogdan/makabu/unstoppable/resolution/node_modules/@ethersproject/abi/lib/interface.js:257:37)
    at Object.<anonymous> (/Users/bogdan/makabu/unstoppable/resolution/tmp/test2.ts:27:33)
    at Module._compile (internal/modules/cjs/loader.js:1201:30)
    at Module.m._compile (/Users/bogdan/.nvm/versions/node/v14.5.0/lib/node_modules/ts-node/src/index.ts:858:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:1221:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/bogdan/.nvm/versions/node/v14.5.0/lib/node_modules/ts-node/src/index.ts:861:12)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
zemsecommented, Jul 14, 2020

@ricmoo Was just wondering if it’s possible for methods like foo(bytes2) and foo(bytes) in same contract can be called in this fashion or am I missing something:

contractInstance.functions['foo(bytes2)']('0x1234');
contractInstance.functions['foo(bytes)']('0x1234567890');

This way I think it could be explicit which method will be called and ethers.js would not require to guess (and I agree guessing would lead to unintended behavior).

1reaction
ricmoocommented, Oct 8, 2020

I think this has been answered to everyone’s satisfaction? So, I’m going to close it, but if not, please feel free to re-open.

Thanks! 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

ethers - call an overloaded function with a specific signer
assuming a signer called 'user' and a contract called 'Foo' with function 'bar' await Foo.connect(user).bar();.
Read more >
How to use overloaded function from smart contract with wagmi?
This should pass the function signature to the underlying ethers call. Share.
Read more >
Learn Solidity lesson 24. Function overloading, fallback and ...
The data we send to a contract in a transaction (or call) is called payload. The payload usually contains the signature of the...
Read more >
Polymorphism and Function Overloading in Solidity
Contract polymorphism supports calling the child contract function by using the instance of parent contracts. pragma solidity ^0.4.19;contract ...
Read more >
A Simple Explanation of Function Overloading in TypeScript
The second approach is to use the function overloading feature. I recommend it when the function signature is relatively complex and has ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found