ERC165.sol 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. pragma solidity ^0.5.0;
  2. import "./IERC165.sol";
  3. /**
  4. * @dev Implementation of the {IERC165} interface.
  5. *
  6. * Contracts may inherit from this and call {_registerInterface} to declare
  7. * their support of an interface.
  8. */
  9. contract ERC165 is IERC165 {
  10. /*
  11. * bytes4(keccak256('supportsInterface(bytes4)')) == 0x01ffc9a7
  12. */
  13. bytes4 private constant _INTERFACE_ID_ERC165 = 0x01ffc9a7;
  14. /**
  15. * @dev Mapping of interface ids to whether or not it's supported.
  16. */
  17. mapping(bytes4 => bool) private _supportedInterfaces;
  18. constructor () internal {
  19. // Derived contracts need only register support for their own interfaces,
  20. // we register support for ERC165 itself here
  21. _registerInterface(_INTERFACE_ID_ERC165);
  22. }
  23. /**
  24. * @dev See {IERC165-supportsInterface}.
  25. *
  26. * Time complexity O(1), guaranteed to always use less than 30 000 gas.
  27. */
  28. function supportsInterface(bytes4 interfaceId) external view returns (bool) {
  29. return _supportedInterfaces[interfaceId];
  30. }
  31. /**
  32. * @dev Registers the contract as an implementer of the interface defined by
  33. * `interfaceId`. Support of the actual ERC165 interface is automatic and
  34. * registering its interface id is not required.
  35. *
  36. * See {IERC165-supportsInterface}.
  37. *
  38. * Requirements:
  39. *
  40. * - `interfaceId` cannot be the ERC165 invalid interface (`0xffffffff`).
  41. */
  42. function _registerInterface(bytes4 interfaceId) internal {
  43. require(interfaceId != 0xffffffff, "ERC165: invalid interface id");
  44. _supportedInterfaces[interfaceId] = true;
  45. }
  46. }