ERC165
해당 컨트랙트가 지원하는 인터페이스에 대한 정보를 제공한다. 보통 컨트랙트가 특정 표준을 지원하는지 확인할 때 사용된다.
인터페이스 식별자는 해당 인터페이스가 지원하는 모든 함수 선택자를 XOR 한 값이다. 예를 들어 Solidity101 인터페이스에 대한 인터페이스 식별자는 다음과 같이 계산한다.
pragma solidity ^0.4.20;
interface Solidity101 {
function hello() external pure;
function world(int) external pure;
}
contract Selector {
function calculateSelector() public pure returns (bytes4) {
Solidity101 i;
return i.hello.selector ^ i.world.selector;
}
}ERC165를 따르려면 supportsInterface 함수를 작성해야 한다. supportsInterface 함수에 조회하고자 하는 인터페이스 식별자를 파라미터로 전달한다.
pragma solidity ^0.4.20;
interface ERC165 {
/// @notice Query if a contract implements an interface
/// @param interfaceID The interface identifier, as specified in ERC-165
/// @dev Interface identification is specified in ERC-165. This function
/// uses less than 30,000 gas.
/// @return `true` if the contract implements `interfaceID` and
/// `interfaceID` is not 0xffffffff, `false` otherwise
function supportsInterface(bytes4 interfaceID) external view returns (bool);
}- 만약 해당 컨트랙트가 해당 인터페이스를 지원한다면
true, 지원하지 않는다면false를 리턴해야 한다. - 또한 이 컨트랙트는 ERC165 인터페이스를 따르므로(
supportsInterface로 조회 기능을 제공하므로) ERC165의 식별자인0x01ffc9a7로 조회했을 때는true를 리턴해야 한다. 0xffffffff로 조회하면 항상false를 리턴해야 한다.
ERC165를 지원하는 컨트랙트를 상속하고, 다른 인터페이스를 추가로 지원하거나 ERC165를 지원하는 컨트랙트를 다수 상속한다면 supportsInterface 함수를 오버라이드해야 한다. 지원하는 모든 인터페이스 식별자에 대하여 true를 리턴해야 하므로, 부모의 supportsInterface 의 결과를 포함하도록 주의한다.
MUST
supportsInterface함수는 bool을 리턴- 지원하는 인터페이스 식별자로 조회 시
true, 지원하지 않는다면false를 리턴 - ERC165의 식별자인
0x01ffc9a7(=bytes4(keccak256('supportsInterface(bytes4)'))) 조회했을 때true를 리턴 0xffffffff로 조회하면 항상false를 리턴- 30,000 gas 이하를 사용해야 한다.
Memo
다른 컨트랙트와 통합할 때 supportsInterface 요구사항이 있는지 확인하자.