Tide Ayoade
3 min readNov 23, 2020

ERC-165 or Solidity Interface identifier standard

ERC 165, the Standard Interface identifier is used to publish and detect what interfaces a smart contract implements e.g ERC 721 standards.

It is very useful listing tokens in exchanges as it helps the operator(_address) identify the standard is implemented in the contract.

The implementation looks like below

pragma solidity ^0.4.20;
interface ERC165 {
function supportsInterface(bytes4 interfaceID) external view returns (bool);
}

This ERC165 interface above implements a getter for the bytes of our Interface

How to calculate bytes Interface, The XOR of all functions in an interface E.g
— — — — — — — — — — — — — — — — — — — — — — — —
1)

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;
}

— — — — — — — — — — — — — — — — — — — -

2) The bytes4 of the keccak256 encoding of input data types for an Interface function.

The ERC-165 identifier for the interface below is 0x150b7a0 from

bytes4(keccak256(“onERC721Received(address,address,uint256, bytes)”)`

interface ERC721TokenReceiver {
function onERC721Received(address _operator, address _from, uint256 _tokenId, bytes _data) external returns(bytes4);
}

or

2b) IERC721Receiver(0).onERC721Received.selector i.e

pragma solidity 0.5.8;
import "./IERC21Received.sol";
contract Selector {
calculateERC721ReceivedInterface(){
IERC721Receiver i;
returns i.onERC721Received.selector
}
}

// @ dev This interface is must be implemented by operator or contract address trading ERC721 tokens or approved to hold ERC721 tokens

3) XOR all interface function in IERC721, except optional functions

pragma solidity 0.4.20;
import "./IERC721.sol"
contract selector{
function calculateInterface() public pure returns (bytes4){
IERC721 i;
returns (i.balanceOf.selector ^ i.ownerOf.selector ^ i.safetransferFrom.selector ^ i.approve.selector ^ i.setApproval.selector ^ i.(all function in the IERC721).selector)
// returns 0x80ac58cd.
}
}

— — — — — — — — — — — — — — — — — — — — —

pragma solidity ^0.4.20;
interface Solidity101 {
function hello() external pure;
function world(int) external pure;
}
pragma solidity ^0.4.20;/// @title ERC-721 Non-Fungible Token Standard
///
@dev See https://eips.ethereum.org/EIPS/eip-721
/// Note: the ERC-165 identifier for this interface is 0x80ac58cd.
interface ERC721 /* is ERC165 */ {
event Approval(address indexed _owner, address indexed _approved, uint256 indexed _tokenId);
event ApprovalForAll(address indexed _owner, address indexed _operator, bool _approved);function balanceOf(address _owner) external view returns (uint256); function ownerOf(uint256 _tokenId) external view returns (address);function safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes data) external payable;
function safeTransferFrom(address _from, address _to, uint256 _tokenId) external payable;
function transferFrom(address _from, address _to, uint256 _tokenId) external payable;
function approve(address _approved, uint256 _tokenId) external payable;
function setApprovalForAll(address _operator, bool _approved) external;
function getApproved(uint256 _tokenId) external view returns (address);
function isApprovedForAll(address _owner, address _operator) external view returns (bool);
}
interface ERC165 {function supportsInterface(bytes4 interfaceID) external view returns (bool);
}

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

To dig deeper look at https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified

Not sure this is the best way to write an article, more of thought-saving for future references

Comments are welcomed!

Tide Ayoade
Tide Ayoade

Written by Tide Ayoade

Everything Blockchain | Ethereum

No responses yet