ERC-165

Standard Interface Detection

ERC165 is a standard to detect and publish what interfaces a smart contract implements.

Motivation

  • Creates a standard method to publish and detect what interfaces a smart contract implements.

  • This helps prevent tokens from getting stuck, when they are sent to contracts which do not support that token

  • Like sending ERC20 to contracts that does not support ERC20, so they get stuck.

  • We need to know if the necessary methods exist on the target function

Solution

  • Keccak256 and XOR

  • Keccak256(method_1) ^ Keccak256(method_2) ^ Keccak256(method_3)

  • we will hash all the methods of an interface and XOR them -> with XOR the order will not matter

  • you can think of this hexadecimal as the bytes4 fingerprint of an interface or group of methods.

This gives us a way to ask a contract "Do you support 0x780e9d63 (ERC721 Enumerable)?"

Calculating Interface ID

  • interfaceid: type(IERC721).interfaceId

Example Implementation

  • Inherit ERC165.sol

  • override the supportsInterface function accordingly

In ERC165:

In ERC721:

  • we override the inherited function on ERC165 and IERC165

  • the child implementation checks for support on ERC712, ERC712Metadata, and ERC165 (super.supportsInterface)

Last updated