0

Ethers generally doesn't let you call an overloaded function like this contract.overloaded(1), but it does let you call it with a fully qualified function name like this contract["overloaded(uint)"](1). However, I now have a function that takes an enum as an input, and I don't know how to do a fully qualified function call for that. Consider the example contract

pragma solidity ^0.8.6;


contract MyContract {
    enum MyEnum{ ZERO, ONE}

    function unique(MyEnum _enum) public view returns(uint) {
        if (_enum == MyEnum.ZERO) {
            return 0;
        } else {
            return 1;
        }
    }

    function overloaded() public view returns(uint) {
        return 0;
    }

    function overloaded(MyEnum _enum) public view returns(uint) {
        if (_enum == MyEnum.ZERO) {
            return 0;
        } else {
            return 1;
        }
    }
}

Now I can call non-overloaded functions or overloaded functions that don't have enums as arguments (both return 0, showing that the enum input in the unique function is parsed correctly):

const ethers = require("ethers");
const MyContract = await ethers.getContractFactory("MyContract");
myContract = await MyContract.deploy();
await myContract.deployed();

console.log(await myContract.unique(0));
console.log(await myContract["overloaded()"]());

However, if I try calling the overloaded function with an enum argument like this

console.log(await myContract["overloaded(MyEnum)"](0));

or like this

console.log(await myContract["overloaded(enum)"](0));

I always get errors like TypeError: myContract.overloaded(MyEnum) is not a function or TypeError: myContract.overloaded(enum) is not a function. I couldn't find the syntax for calling overloaded functions in the ethers documentation either

1 Answer 1

3

Ok, I found the answer - enums are treated as uint8 internally by solidity, so myContract["overloaded(uint8)"](0) works. Leaving this here in case it helps anyone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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