Function Selector – Understanding Function Signature with Custom Types in Solidity

enumfunction-selector

I have the following Enum that is an input to a function

enum Cat {
    Tabby,
    Rascal,
    Cool
}

function hiMom(Cat myCat) public {

What is the function signature of hiMom(Cat)?

Best Answer

You'll need to know the functions parameters elementary type and use that.

For an enum, the type is uint8.

You can determine this using the --abi option to solc (or other compilers):

❯ solc --abi - <<EOF | tail -1 | jq 
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

enum Cat {
    Tabby,
    Rascal,
    Cool
}

contract Foo {
  function hiMom(Cat myCat) public {}
}
EOF
[
  {
    "inputs": [
      {
        "internalType": "enum Cat",
        "name": "myCat",
        "type": "uint8"
               # ^^^^^ <- THIS IS THE BASE TYPE <------------
      }
    ],
    "name": "hiMom",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  }
]

In cast:

cast sig "hiMom(uint8)"

Tuple/Struct Edit

Note, for a tuple or struct type, you'll need to combine the components into parentheses.

Example:

struct animal {
  string breed;
  uint256 id;
}

function getName(animal myAnimal) public {

getName would have a function selector of: 0x5e492cc7

You could find it by:

cast sig "getName((string,uint256))"