EIPs Explained

ERC-165 · Final · ERC · 2018-01-23

ERC-165: Standard Interface Detection

A single supportsInterface(bytes4) function that lets any contract declare which interfaces it implements, so other contracts and apps can check before calling.

What problem does it solve?

A contract address on its own tells you nothing about what the contract can do. If a marketplace receives an address, it cannot tell whether that address is an NFT contract, a multi-token contract or something else. It can call a function and hope, but a missing function might revert, or fall through to a fallback that does something unexpected.

ERC-165 gives contracts a standard way to say "I implement interface X", and gives callers a safe way to ask.

How it works

Interface IDs. An interface is identified by the XOR of the 4-byte function selectors of all its functions. For example, the ID of ERC-165 itself is the selector of supportsInterface(bytes4), which is 0x01ffc9a7.

Publishing. A compliant contract implements:

function supportsInterface(bytes4 interfaceID) external view returns (bool);

It must return:

  • true for 0x01ffc9a7 (ERC-165 itself)
  • false for 0xffffffff
  • true for every other interface it implements, and false otherwise

The function must use at most 30,000 gas.

Detecting. To check whether a contract supports ERC-165 at all, make a STATICCALL (EIP-214) with 30,000 gas. Ask supportsInterface(0x01ffc9a7), which must return true. Then ask supportsInterface(0xffffffff), which must return false. The second check catches older contracts whose fallback returns true for any call. If both checks pass, you can query any interface ID directly.

Sample code

Implementing ERC-165 alongside a custom interface:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

interface IERC165 {
    function supportsInterface(bytes4 interfaceId) external view returns (bool);
}

interface IGreeter {
    function greet() external view returns (string memory);
}

contract Greeter is IERC165, IGreeter {
    function supportsInterface(bytes4 id) external pure returns (bool) {
        return id == type(IERC165).interfaceId   // 0x01ffc9a7
            || id == type(IGreeter).interfaceId;
    }

    function greet() external pure returns (string memory) {
        return "hello";
    }
}

Checking whether an address is an ERC-721 contract (ethers v6):

import { Contract, JsonRpcProvider } from "ethers";

const provider = new JsonRpcProvider(process.env.RPC_URL);
const c = new Contract(addr, ["function supportsInterface(bytes4) view returns (bool)"], provider);

const isErc165 =
  (await c.supportsInterface("0x01ffc9a7")) && !(await c.supportsInterface("0xffffffff"));
const isErc721 = isErc165 && (await c.supportsInterface("0x80ac58cd"));

Things to know

  • ERC-721 and ERC-1155 require ERC-165, which is why NFT marketplaces can detect token types automatically. ERC-20 came earlier and does not require it.
  • The interface ID covers function selectors only. Events, return types and optional functions are not part of it.
  • A contract that returns true is only making a claim. ERC-165 does not prove the functions behave correctly.
  • In Solidity, type(I).interfaceId computes the ID for you. OpenZeppelin's ERC165 base contract is the usual starting point.
  • ERC-1820 is a registry-based alternative that also lets regular accounts (EOAs) register interfaces.

#interface #introspection #standard #erc

Details

Authors
Christian Reitwießner <[email protected]>, Nick Johnson <[email protected]>, Fabian Vogelsteller <[email protected]>, Jordi Baylina <[email protected]>, Konrad Feldmeier <[email protected]>, William Entriken <[email protected]>
Status
Final
Type
Standards Track / ERC
Created
2018-01-23
Requires
Required by

Machine-readable: Markdown · JSON