3

I have token contract which implements IERC2981 interface and I've overridden the supportsInterface function like so.

function supportsInterface(bytes4 interfaceId) public view virtual override(ERC721,
        IERC165) returns (bool) {
    return (interfaceId == type(IERC2981).interfaceId || super.supportsInterface(interfaceId));
}

How will I check or call the supportsInterface function from another contract? let's say from my sales contract I want to check If the token contract supports IERC2981 If its does call the royaltyInfo function.

2 Answers 2

4

If you have the address of your target contract and want to enforce that the IERC2981 is supported you would do the following:

require(
  IEIP165(target).supportsInterface(type(IERC2981).interfaceId),
  "Contract does not support IERC2981" interface.
);

The IEIP165 interface can be found in the OpenZepplin contracts.

0

What if the contract does not support ERC165, i.e. the supportsInterface function does not exist; will it revert?

I'm looking for a safe way to check interface support without reverting, eg:

 if ( IERC165(target).supportsInterface(0x2a55205a) ) {
   ...
 } else {...}
1

Your Answer

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

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