ERC165.sol 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^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. * @dev Mapping of interface ids to whether or not it's supported.
  13. */
  14. mapping(bytes4 => bool) private _supportedInterfaces;
  15. constructor () {
  16. // Derived contracts need only register support for their own interfaces,
  17. // we register support for ERC165 itself here
  18. _registerInterface(type(IERC165).interfaceId);
  19. }
  20. /**
  21. * @dev See {IERC165-supportsInterface}.
  22. *
  23. * Time complexity O(1), guaranteed to always use less than 30 000 gas.
  24. */
  25. function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
  26. return _supportedInterfaces[interfaceId];
  27. }
  28. /**
  29. * @dev Registers the contract as an implementer of the interface defined by
  30. * `interfaceId`. Support of the actual ERC165 interface is automatic and
  31. * registering its interface id is not required.
  32. *
  33. * See {IERC165-supportsInterface}.
  34. *
  35. * Requirements:
  36. *
  37. * - `interfaceId` cannot be the ERC165 invalid interface (`0xffffffff`).
  38. */
  39. function _registerInterface(bytes4 interfaceId) internal virtual {
  40. require(interfaceId != 0xffffffff, "ERC165: invalid interface id");
  41. _supportedInterfaces[interfaceId] = true;
  42. }
  43. }