2

When an interface has multiple levels of inheritance, should the supportsInterface in ERC165 checks the entire entire inheritance stack of the interface or just the top-most level?

Here's an example. Let's say I have an interface IAnimal and another interface ICat which implements IAnimal like this:

interface IAnimal {
  function walk() external;
  function run() external;
}

interface ICat is IAnimal {
   function meow() external;
   function jumps() external;
}

Then, I have a ERC165 contract that uses ICat:

contract MyContract is ERC165, ICat {
  // Implements interface methods of both ICat and IAnimal because ICat implements IAnimal...

  function supportsInterface(bytes4 interfaceId) external view returns (bool) {
    // How should the check here be like?
  }
}

My question is, how should I implement the check in supportsInterface in this case?

Should the check in supportsInterface match the interface ID of only the methods in ICat excluding IAnimal? Or should the check here match the interface ID of all methods in both ICat and IAnimal since ICat extends IAnimal?

  • If the check only has to match the interface ID of the methods, I could do this:
    return interfaceId == type(ICat).interfaceId || interfaceId == type(IAnimal).interfaceId;
    
  • However, if the check has to match the interface ID of all methods in both ICat and IAnimal, how should I combine the interface ID from both interfaces?
    • I notice that type(ICat).interfaceId will ignore the methods in IAnimal when computing the interface ID. In other words, the interface ID is only computing the methods meow() and jumps() in ICat but doesn't include walk() and run() in IAnimal! This is surprising to me because type().interfaceId seems to ignore any inherited methods in the interface? So I can't use type(ICat).
    • I tried something like this but this doesn't seem correct:
      return interfaceId == type(IAnimal).interfaceId || interfaceId == (type(ICat).interfaceId ^ type(IAnimal).interfaceId);
      

I haven't found any examples on ERC165 with multiple levels of inheritance with its interface, not even in its EIP doc.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.