ERC165Checker.sol 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.6.2;
  3. /**
  4. * @dev Library used to query support of an interface declared via {IERC165}.
  5. *
  6. * Note that these functions return the actual result of the query: they do not
  7. * `revert` if an interface is not supported. It is up to the caller to decide
  8. * what to do in these cases.
  9. */
  10. library ERC165Checker {
  11. // As per the EIP-165 spec, no interface should ever match 0xffffffff
  12. bytes4 private constant _INTERFACE_ID_INVALID = 0xffffffff;
  13. /*
  14. * bytes4(keccak256('supportsInterface(bytes4)')) == 0x01ffc9a7
  15. */
  16. bytes4 private constant _INTERFACE_ID_ERC165 = 0x01ffc9a7;
  17. /**
  18. * @dev Returns true if `account` supports the {IERC165} interface,
  19. */
  20. function supportsERC165(address account) internal view returns (bool) {
  21. // Any contract that implements ERC165 must explicitly indicate support of
  22. // InterfaceId_ERC165 and explicitly indicate non-support of InterfaceId_Invalid
  23. return _supportsERC165Interface(account, _INTERFACE_ID_ERC165) &&
  24. !_supportsERC165Interface(account, _INTERFACE_ID_INVALID);
  25. }
  26. /**
  27. * @dev Returns true if `account` supports the interface defined by
  28. * `interfaceId`. Support for {IERC165} itself is queried automatically.
  29. *
  30. * See {IERC165-supportsInterface}.
  31. */
  32. function supportsInterface(address account, bytes4 interfaceId) internal view returns (bool) {
  33. // query support of both ERC165 as per the spec and support of _interfaceId
  34. return supportsERC165(account) &&
  35. _supportsERC165Interface(account, interfaceId);
  36. }
  37. /**
  38. * @dev Returns true if `account` supports all the interfaces defined in
  39. * `interfaceIds`. Support for {IERC165} itself is queried automatically.
  40. *
  41. * Batch-querying can lead to gas savings by skipping repeated checks for
  42. * {IERC165} support.
  43. *
  44. * See {IERC165-supportsInterface}.
  45. */
  46. function supportsAllInterfaces(address account, bytes4[] memory interfaceIds) internal view returns (bool) {
  47. // query support of ERC165 itself
  48. if (!supportsERC165(account)) {
  49. return false;
  50. }
  51. // query support of each interface in _interfaceIds
  52. for (uint256 i = 0; i < interfaceIds.length; i++) {
  53. if (!_supportsERC165Interface(account, interfaceIds[i])) {
  54. return false;
  55. }
  56. }
  57. // all interfaces supported
  58. return true;
  59. }
  60. /**
  61. * @notice Query if a contract implements an interface, does not check ERC165 support
  62. * @param account The address of the contract to query for support of an interface
  63. * @param interfaceId The interface identifier, as specified in ERC-165
  64. * @return true if the contract at account indicates support of the interface with
  65. * identifier interfaceId, false otherwise
  66. * @dev Assumes that account contains a contract that supports ERC165, otherwise
  67. * the behavior of this method is undefined. This precondition can be checked
  68. * with {supportsERC165}.
  69. * Interface identification is specified in ERC-165.
  70. */
  71. function _supportsERC165Interface(address account, bytes4 interfaceId) private view returns (bool) {
  72. // success determines whether the staticcall succeeded and result determines
  73. // whether the contract at account indicates support of _interfaceId
  74. (bool success, bool result) = _callERC165SupportsInterface(account, interfaceId);
  75. return (success && result);
  76. }
  77. /**
  78. * @notice Calls the function with selector 0x01ffc9a7 (ERC165) and suppresses throw
  79. * @param account The address of the contract to query for support of an interface
  80. * @param interfaceId The interface identifier, as specified in ERC-165
  81. * @return success true if the STATICCALL succeeded, false otherwise
  82. * @return result true if the STATICCALL succeeded and the contract at account
  83. * indicates support of the interface with identifier interfaceId, false otherwise
  84. */
  85. function _callERC165SupportsInterface(address account, bytes4 interfaceId)
  86. private
  87. view
  88. returns (bool, bool)
  89. {
  90. bytes memory encodedParams = abi.encodeWithSelector(_INTERFACE_ID_ERC165, interfaceId);
  91. (bool success, bytes memory result) = account.staticcall{ gas: 30000 }(encodedParams);
  92. if (result.length < 32) return (false, false);
  93. return (success, abi.decode(result, (bool)));
  94. }
  95. }