ERC165Checker.sol 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity >=0.6.2 <0.8.0;
  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 a boolean array where each value corresponds to the
  39. * interfaces passed in and whether they're supported or not. This allows
  40. * you to batch check interfaces for a contract where your expectation
  41. * is that some interfaces may not be supported.
  42. *
  43. * See {IERC165-supportsInterface}.
  44. */
  45. function getSupportedInterfaces(address account, bytes4[] memory interfaceIds) internal view returns (bool[] memory) {
  46. // an array of booleans corresponding to interfaceIds and whether they're supported or not
  47. bool[] memory interfaceIdsSupported = new bool[](interfaceIds.length);
  48. // query support of ERC165 itself
  49. if (supportsERC165(account)) {
  50. // query support of each interface in interfaceIds
  51. for (uint256 i = 0; i < interfaceIds.length; i++) {
  52. interfaceIdsSupported[i] = _supportsERC165Interface(account, interfaceIds[i]);
  53. }
  54. }
  55. return interfaceIdsSupported;
  56. }
  57. /**
  58. * @dev Returns true if `account` supports all the interfaces defined in
  59. * `interfaceIds`. Support for {IERC165} itself is queried automatically.
  60. *
  61. * Batch-querying can lead to gas savings by skipping repeated checks for
  62. * {IERC165} support.
  63. *
  64. * See {IERC165-supportsInterface}.
  65. */
  66. function supportsAllInterfaces(address account, bytes4[] memory interfaceIds) internal view returns (bool) {
  67. // query support of ERC165 itself
  68. if (!supportsERC165(account)) {
  69. return false;
  70. }
  71. // query support of each interface in _interfaceIds
  72. for (uint256 i = 0; i < interfaceIds.length; i++) {
  73. if (!_supportsERC165Interface(account, interfaceIds[i])) {
  74. return false;
  75. }
  76. }
  77. // all interfaces supported
  78. return true;
  79. }
  80. /**
  81. * @notice Query if a contract implements an interface, does not check ERC165 support
  82. * @param account The address of the contract to query for support of an interface
  83. * @param interfaceId The interface identifier, as specified in ERC-165
  84. * @return true if the contract at account indicates support of the interface with
  85. * identifier interfaceId, false otherwise
  86. * @dev Assumes that account contains a contract that supports ERC165, otherwise
  87. * the behavior of this method is undefined. This precondition can be checked
  88. * with {supportsERC165}.
  89. * Interface identification is specified in ERC-165.
  90. */
  91. function _supportsERC165Interface(address account, bytes4 interfaceId) private view returns (bool) {
  92. // success determines whether the staticcall succeeded and result determines
  93. // whether the contract at account indicates support of _interfaceId
  94. (bool success, bool result) = _callERC165SupportsInterface(account, interfaceId);
  95. return (success && result);
  96. }
  97. /**
  98. * @notice Calls the function with selector 0x01ffc9a7 (ERC165) and suppresses throw
  99. * @param account The address of the contract to query for support of an interface
  100. * @param interfaceId The interface identifier, as specified in ERC-165
  101. * @return success true if the STATICCALL succeeded, false otherwise
  102. * @return result true if the STATICCALL succeeded and the contract at account
  103. * indicates support of the interface with identifier interfaceId, false otherwise
  104. */
  105. function _callERC165SupportsInterface(address account, bytes4 interfaceId)
  106. private
  107. view
  108. returns (bool, bool)
  109. {
  110. bytes memory encodedParams = abi.encodeWithSelector(_INTERFACE_ID_ERC165, interfaceId);
  111. (bool success, bytes memory result) = account.staticcall{ gas: 30000 }(encodedParams);
  112. if (result.length < 32) return (false, false);
  113. return (success, abi.decode(result, (bool)));
  114. }
  115. }