ERC165.sol 1.7 KB

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