|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.13; |
| 3 | + |
| 4 | +import {IOperatorFilterRegistry} from "./IOperatorFilterRegistry.sol"; |
| 5 | + |
| 6 | +/** |
| 7 | + * @title UpdateableOperatorFilterer |
| 8 | + * @notice Abstract contract whose constructor automatically registers and optionally subscribes to or copies another |
| 9 | + * registrant's entries in the OperatorFilterRegistry. This contract allows the Owner to update the |
| 10 | + * OperatorFilterRegistry address via updateOperatorFilterRegistryAddress, including to the zero address, |
| 11 | + * which will bypass registry checks. |
| 12 | + * @dev This smart contract is meant to be inherited by token contracts so they can use the following: |
| 13 | + * - `onlyAllowedOperator` modifier for `transferFrom` and `safeTransferFrom` methods. |
| 14 | + * - `onlyAllowedOperatorApproval` modifier for `approve` and `setApprovalForAll` methods. |
| 15 | + */ |
| 16 | +abstract contract UpdateableOperatorFilterer { |
| 17 | + error OperatorNotAllowed(address operator); |
| 18 | + error OnlyOwner(); |
| 19 | + |
| 20 | + event OperatorFilterRegistryUpdated(address previousRegistryAddress, address newRegistryAddress); |
| 21 | + |
| 22 | + IOperatorFilterRegistry public operatorFilterRegistry; |
| 23 | + |
| 24 | + constructor(address _registry, address subscriptionOrRegistrantToCopy, bool subscribe) { |
| 25 | + IOperatorFilterRegistry registry = IOperatorFilterRegistry(_registry); |
| 26 | + operatorFilterRegistry = registry; |
| 27 | + // If an inheriting token contract is deployed to a network without the registry deployed, the modifier |
| 28 | + // will not revert, but the contract will need to be registered with the registry once it is deployed in |
| 29 | + // order for the modifier to filter addresses. |
| 30 | + if (address(registry).code.length > 0) { |
| 31 | + if (subscribe) { |
| 32 | + registry.registerAndSubscribe(address(this), subscriptionOrRegistrantToCopy); |
| 33 | + } else { |
| 34 | + if (subscriptionOrRegistrantToCopy != address(0)) { |
| 35 | + registry.registerAndCopyEntries(address(this), subscriptionOrRegistrantToCopy); |
| 36 | + } else { |
| 37 | + registry.register(address(this)); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + modifier onlyAllowedOperator(address from) virtual { |
| 44 | + // Allow spending tokens from addresses with balance |
| 45 | + // Note that this still allows listings and marketplaces with escrow to transfer tokens if transferred |
| 46 | + // from an EOA. |
| 47 | + if (from != msg.sender) { |
| 48 | + _checkFilterOperator(msg.sender); |
| 49 | + } |
| 50 | + _; |
| 51 | + } |
| 52 | + |
| 53 | + modifier onlyAllowedOperatorApproval(address operator) virtual { |
| 54 | + _checkFilterOperator(operator); |
| 55 | + _; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @notice Update the address that the contract will make OperatorFilter checks against. When set to the zero |
| 60 | + * address, checks will be bypassed. OnlyOwner. |
| 61 | + */ |
| 62 | + function updateOperatorFilterRegistryAddress(address newRegistry) public virtual { |
| 63 | + if (msg.sender != owner()) { |
| 64 | + revert OnlyOwner(); |
| 65 | + } |
| 66 | + _updateRegistryAddress(newRegistry); |
| 67 | + } |
| 68 | + |
| 69 | + function _updateRegistryAddress(address newRegistry) internal { |
| 70 | + address oldRegistry = address(operatorFilterRegistry); |
| 71 | + operatorFilterRegistry = IOperatorFilterRegistry(newRegistry); |
| 72 | + emit OperatorFilterRegistryUpdated(oldRegistry, newRegistry); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @dev assume the contract has an owner, but leave specific Ownable implementation up to inheriting contract |
| 77 | + */ |
| 78 | + function owner() public view virtual returns (address); |
| 79 | + |
| 80 | + function _checkFilterOperator(address operator) internal view virtual { |
| 81 | + IOperatorFilterRegistry registry = operatorFilterRegistry; |
| 82 | + // Check registry code length to facilitate testing in environments without a deployed registry. |
| 83 | + if (address(registry) != address(0) && address(registry).code.length > 0) { |
| 84 | + if (!registry.isOperatorAllowed(address(this), operator)) { |
| 85 | + revert OperatorNotAllowed(operator); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments