|
@@ -7,43 +7,22 @@ import "./IERC165.sol";
|
|
/**
|
|
/**
|
|
* @dev Implementation of the {IERC165} interface.
|
|
* @dev Implementation of the {IERC165} interface.
|
|
*
|
|
*
|
|
- * Contracts may inherit from this and call {_registerInterface} to declare
|
|
|
|
- * their support of an interface.
|
|
|
|
|
|
+ * Contracts that want to implement ERC165 should inherit from this contract and override {supportsInterface} to check
|
|
|
|
+ * for the additional interface id that will be supported. For example:
|
|
|
|
+ *
|
|
|
|
+ * ```solidity
|
|
|
|
+ * function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
|
|
|
|
+ * return interfaceId == type(MyInterface).interfaceId || super.supportsInterface(interfaceId);
|
|
|
|
+ * }
|
|
|
|
+ * ```
|
|
|
|
+ *
|
|
|
|
+ * Alternatively, {ERC165Storage} provides an easier to use but more expensive implementation.
|
|
*/
|
|
*/
|
|
abstract contract ERC165 is IERC165 {
|
|
abstract contract ERC165 is IERC165 {
|
|
- /**
|
|
|
|
- * @dev Mapping of interface ids to whether or not it's supported.
|
|
|
|
- */
|
|
|
|
- mapping(bytes4 => bool) private _supportedInterfaces;
|
|
|
|
-
|
|
|
|
- constructor () {
|
|
|
|
- // Derived contracts need only register support for their own interfaces,
|
|
|
|
- // we register support for ERC165 itself here
|
|
|
|
- _registerInterface(type(IERC165).interfaceId);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* @dev See {IERC165-supportsInterface}.
|
|
* @dev See {IERC165-supportsInterface}.
|
|
- *
|
|
|
|
- * Time complexity O(1), guaranteed to always use less than 30 000 gas.
|
|
|
|
*/
|
|
*/
|
|
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
|
|
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
|
|
- return _supportedInterfaces[interfaceId];
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * @dev Registers the contract as an implementer of the interface defined by
|
|
|
|
- * `interfaceId`. Support of the actual ERC165 interface is automatic and
|
|
|
|
- * registering its interface id is not required.
|
|
|
|
- *
|
|
|
|
- * See {IERC165-supportsInterface}.
|
|
|
|
- *
|
|
|
|
- * Requirements:
|
|
|
|
- *
|
|
|
|
- * - `interfaceId` cannot be the ERC165 invalid interface (`0xffffffff`).
|
|
|
|
- */
|
|
|
|
- function _registerInterface(bytes4 interfaceId) internal virtual {
|
|
|
|
- require(interfaceId != 0xffffffff, "ERC165: invalid interface id");
|
|
|
|
- _supportedInterfaces[interfaceId] = true;
|
|
|
|
|
|
+ return interfaceId == type(IERC165).interfaceId;
|
|
}
|
|
}
|
|
}
|
|
}
|