Skip to content

Latest commit

 

History

History
71 lines (63 loc) · 2.93 KB

ERC165.md

File metadata and controls

71 lines (63 loc) · 2.93 KB

ERC165

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

RULES

An interface that implements ERC165: SHALL:
Implement this interface below

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

The four byte selector for the function is 0x01ffc9a7.
You can calculate this by running bytes4(keccak256('supportsInterface(bytes4)'));
Therefore the implementing contract will have a supportsInterface function that returns:

  1. true when interfaceID is 0x01ffc9a7 (EIP165 interface)
  2. false when interfaceID is 0xffffffff
  3. true for any other interfaceID this contract implements
  4. false for any other interfaceID *not* implemented by the contract
  5. This function must return a bool and use at most 30,000 gas.

Example:
In order to check if the contract called implements ERC165

  1. first send a call to the contract using the selector 0x01ffc9a7
  2. If the fails or comes back as false, the contract does not implement ERC165
  3. If it returned true, the send a second call to check if selector 0xffffffff is valid
  4. If that returns true or fails the contract does not implement ERC165.
pragma solidity 0.8.19;
import "./ERC165.sol";

interface theContractYouNeedToCallsInterface {
    function thisFunctionExists(uint256) external returns (int256);
    function thisFunctionAlsoExists(address someWhereOutThere) external returns (uint256);
}

contract explainERC165 is ERC165{
    //This function must return true for selectors for the implemented functions
    //This function must return false for all other function selectors.
    function supportsInterface(bytes4 interfaceID) external view returns (bool) {
        return
          interfaceID == this.supportsInterface.selector || // ERC165
          interfaceID == this.thisFunctionExists.selector ^ this.thisFunctionAlsoExists.selector; 
    }
    
    //This function is declared in the interface
    function thisFunctionExists(uint256) external returns (int256)
    {
    	int256 someIntvalue = ComplicatedInternalfunction();
    	return someIntvalue;
    }
    
    //This function is declared in the interface
    function thisFunctionAlsoExists(address _someWhereOutThere) external returns (uint256)
    {
    	//https://en.wikipedia.org/wiki/Somewhere_Out_There_(An_American_Tail_song)
    	//https://en.wikipedia.org/wiki/An_American_Tail 
    	//if you want to understand my names used for the variables :)
    	address Fievel = IAmGivingAwayMyAge(_someWhereOutThere);
    }
}