---
eip: 165
label: ERC-165
title: Standard Interface Detection
status: Final
type: Standards Track
category: ERC
created: 2018-01-23
requires: 214
official: https://eips.ethereum.org/EIPS/eip-165
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-165.md
canonical: https://eips.huatuo.xyz/ko/eip-165/
lang: ko
tldr: supportsInterface(bytes4) 함수 하나로 컨트랙트가 어떤 인터페이스를 구현하는지 선언할 수 있게 해서, 다른 컨트랙트와 앱이 호출하기 전에 확인할 수 있도록 한다.
tags: [interface, introspection, standard, erc]
---

# ERC-165: Standard Interface Detection

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

**TL;DR:** supportsInterface(bytes4) 함수 하나로 컨트랙트가 어떤 인터페이스를 구현하는지 선언할 수 있게 해서, 다른 컨트랙트와 앱이 호출하기 전에 확인할 수 있도록 한다.

## 어떤 문제를 해결하나요?

컨트랙트 주소만으로는 그 컨트랙트가 무엇을 할 수 있는지 알 수 없습니다. 마켓플레이스가 주소를 받아도 그것이 NFT 컨트랙트인지, 멀티 토큰 컨트랙트인지, 다른 무엇인지 알 수 없습니다. 함수를 호출해 보고 기대할 수는 있지만, 함수가 없으면 revert될 수도 있고 예상치 못한 동작을 하는 fallback으로 빠질 수도 있습니다.

ERC-165는 컨트랙트가 "나는 인터페이스 X를 구현한다"고 말할 수 있는 표준 방법과, 호출자가 안전하게 물어볼 수 있는 방법을 제공합니다.

## 어떻게 동작하나요?

**인터페이스 ID.** 인터페이스는 포함된 모든 함수의 4바이트 함수 셀렉터를 XOR한 값으로 식별됩니다. 예를 들어 ERC-165 자체의 ID는 `supportsInterface(bytes4)`의 셀렉터인 `0x01ffc9a7`입니다.

**공개.** 표준을 따르는 컨트랙트는 다음을 구현합니다.

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

반환값은 다음과 같아야 합니다.

- `0x01ffc9a7`(ERC-165 자체)에는 `true`
- `0xffffffff`에는 `false`
- 구현한 다른 모든 인터페이스에는 `true`, 그렇지 않으면 `false`

이 함수는 최대 30,000 가스만 사용해야 합니다.

**감지.** 컨트랙트가 ERC-165를 지원하는지 확인하려면 30,000 가스로 `STATICCALL`([EIP-214](/eip-214/))을 보냅니다. 먼저 `supportsInterface(0x01ffc9a7)`을 물어보면 true가 반환되어야 합니다. 그다음 `supportsInterface(0xffffffff)`를 물어보면 false가 반환되어야 합니다. 두 번째 확인은 어떤 호출에도 true를 반환하는 fallback을 가진 오래된 컨트랙트를 걸러냅니다. 두 확인을 모두 통과하면 어떤 인터페이스 ID든 직접 조회할 수 있습니다.

## 샘플 코드

커스텀 인터페이스와 함께 ERC-165를 구현하는 예입니다.

```solidity
// 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";
    }
}
```

주소가 ERC-721 컨트랙트인지 확인하는 예(ethers v6):

```ts
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"));
```

## 알아둘 점

- [ERC-721](/eip-721/)과 [ERC-1155](/eip-1155/)는 ERC-165를 요구하기 때문에 NFT 마켓플레이스가 토큰 유형을 자동으로 감지할 수 있습니다. [ERC-20](/eip-20/)은 그보다 먼저 나왔기 때문에 요구하지 않습니다.
- 인터페이스 ID는 함수 셀렉터만 포함합니다. 이벤트, 반환 타입, 선택적 함수는 포함되지 않습니다.
- `true`를 반환하는 컨트랙트는 주장을 하는 것일 뿐입니다. ERC-165는 함수가 올바르게 동작함을 증명하지 않습니다.
- Solidity에서는 `type(I).interfaceId`가 ID를 계산해 줍니다. OpenZeppelin의 `ERC165` 베이스 컨트랙트가 보통의 출발점입니다.
- [ERC-1820](/eip-1820/)은 레지스트리 기반의 대안으로, 일반 계정(EOA)도 인터페이스를 등록할 수 있습니다.


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-165
