Creates a standard method to publish and detect what interfaces a smart contract implements.
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:
true
wheninterfaceID
is0x01ffc9a7
(EIP165 interface)false
wheninterfaceID
is0xffffffff
true
for any otherinterfaceID
thiscontract implements
false
for any otherinterfaceID
*not*
implemented by the contract- This function
must
return abool
and use at most30,000
gas.
Example:
In order to check if the contract called implements ERC165
- first send a call to the contract using the selector 0x01ffc9a7
- If the fails or comes back as false, the contract does not implement ERC165
- If it returned true, the send a second call to check if selector 0xffffffff is valid
- 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);
}
}