Contract with function overloading
Hi @ricmoo thanks for response,
contract.functions works and using your code I’ve was able to call them. Maybe it’ll solve my specific problem. Thanks.
I’m not sure if is a javascript limitation because web3js works on the same scenario. In the case where we have two functions with the same name, the same number of params and different types, I agree with you, but itsn’t the case.
I’ve written a sample contract with some test cases:
SampleContract.sol
pragma solidity ^0.5.0;
contract SampleContract {
function overloading() public pure returns(uint) {
return 1;
}
function overloading(string memory a) public pure returns(uint) {
return 2;
}
function overloading(string memory a, string memory b) public pure returns(uint) {
return 3;
}
}
SampleContrat.test.js
describe.only("", () => {
// OK
it("should call overloading functions - web3js", async function() {
const sampleContractWeb3 = new web3.eth.Contract(abi, address);
const f1 = await sampleContractWeb3.methods.overloading().call();
const f2 = await sampleContractWeb3.methods.overloading("a").call();
const f3 = await sampleContractWeb3.methods.overloading("a", "b").call();
expect(f1).to.equal("1");
expect(f2).to.equal("2");
expect(f3).to.equal("3");
});
// OK
it("should call overloading functions - ethers", async function() {
const provider = new ethers.providers.JsonRpcProvider();
const sampleContractEthers = new ethers.Contract(address, abi, provider);
const f1 = await sampleContractEthers["overloading()"]();
const f2 = await sampleContractEthers["overloading(string)"]("a");
const f3 = await sampleContractEthers["overloading(string,string)"](
"a",
"b"
);
expect(f1.toNumber()).to.equal(1);
expect(f2.toNumber()).to.equal(2);
expect(f3.toNumber()).to.equal(3);
});
// FAIL
it("should call overloading functions - ethers", async function() {
const provider = new ethers.providers.JsonRpcProvider();
const sampleContractEthers = new ethers.Contract(address, abi, provider);
const f1 = await sampleContractEthers.overloading(); // Error: incorrect number of arguments
const f2 = await sampleContractEthers.overloading("a");
const f3 = await sampleContractEthers.overloading("a", "b");
expect(f1.toNumber()).to.equal(1);
expect(f2.toNumber()).to.equal(2);
expect(f3.toNumber()).to.equal(3);
});
});
https://github.com/ethers-io/ethers.js/issues/407