|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +pragma solidity ^0.8.27; |
| 3 | + |
| 4 | +import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; |
| 5 | +import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol"; |
| 6 | + |
| 7 | +/// @title Pausable |
| 8 | +/// @notice Owner-controlled pause state with additional operators that may only pause. |
| 9 | +abstract contract Pausable is Ownable { |
| 10 | + using EnumerableSet for EnumerableSet.AddressSet; |
| 11 | + |
| 12 | + /// @notice The zero address was provided where an operator address is required. |
| 13 | + error ZeroAddress(); |
| 14 | + /// @notice The caller is neither the owner nor a pause operator. |
| 15 | + error UnauthorizedPauser(address account); |
| 16 | + /// @notice The contract is paused. |
| 17 | + error EnforcedPause(); |
| 18 | + /// @notice The contract is not paused. |
| 19 | + error ExpectedPause(); |
| 20 | + /// @notice Ownership renunciation is disabled to keep pause recovery available. |
| 21 | + error OwnershipRenunciationDisabled(); |
| 22 | + |
| 23 | + EnumerableSet.AddressSet private _operators; |
| 24 | + /// @notice Returns whether the contract is currently paused. |
| 25 | + bool public paused; |
| 26 | + |
| 27 | + /// @notice Emitted when `account` pauses the contract. |
| 28 | + event Paused(address indexed account); |
| 29 | + /// @notice Emitted when `account` unpauses the contract. |
| 30 | + event Unpaused(address indexed account); |
| 31 | + /// @notice Emitted when an operator permission is updated. |
| 32 | + event OperatorSet(address indexed operator, bool allowed); |
| 33 | + |
| 34 | + /// @notice Initializes the owner and optional initial pause operators. |
| 35 | + /// @param owner_ The owner allowed to manage operators and unpause. |
| 36 | + /// @param initialOperators The initial set of addresses allowed to pause. |
| 37 | + constructor(address owner_, address[] memory initialOperators) Ownable(owner_) { |
| 38 | + for (uint256 i; i < initialOperators.length; i++) { |
| 39 | + _setOperator(initialOperators[i], true); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /// @notice Reverts when the contract is paused. |
| 44 | + modifier whenNotPaused() { |
| 45 | + if (paused) { |
| 46 | + revert EnforcedPause(); |
| 47 | + } |
| 48 | + _; |
| 49 | + } |
| 50 | + |
| 51 | + /// @notice Reverts when the contract is not paused. |
| 52 | + modifier whenPaused() { |
| 53 | + if (!paused) { |
| 54 | + revert ExpectedPause(); |
| 55 | + } |
| 56 | + _; |
| 57 | + } |
| 58 | + |
| 59 | + /// @notice Pauses the contract. |
| 60 | + /// @dev Callable by the owner or an approved operator. |
| 61 | + function pause() external whenNotPaused { |
| 62 | + if (msg.sender != owner() && !_operators.contains(msg.sender)) { |
| 63 | + revert UnauthorizedPauser(msg.sender); |
| 64 | + } |
| 65 | + |
| 66 | + paused = true; |
| 67 | + emit Paused(msg.sender); |
| 68 | + } |
| 69 | + |
| 70 | + /// @notice Unpauses the contract. |
| 71 | + /// @dev Callable only by the owner. |
| 72 | + function unpause() external onlyOwner whenPaused { |
| 73 | + paused = false; |
| 74 | + emit Unpaused(msg.sender); |
| 75 | + } |
| 76 | + |
| 77 | + /// @notice Updates whether `operator` is allowed to pause. |
| 78 | + /// @param operator The address to update. |
| 79 | + /// @param allowed Whether the address may pause. |
| 80 | + function setOperator(address operator, bool allowed) external onlyOwner { |
| 81 | + _setOperator(operator, allowed); |
| 82 | + } |
| 83 | + |
| 84 | + /// @notice Returns whether `account` is allowed to pause. |
| 85 | + function isOperator(address account) external view returns (bool) { |
| 86 | + return _operators.contains(account); |
| 87 | + } |
| 88 | + |
| 89 | + /// @notice Returns the current pause operators in storage order. |
| 90 | + /// @dev Ordering is not guaranteed and may change when operators are removed. |
| 91 | + function getOperators() external view returns (address[] memory) { |
| 92 | + return _operators.values(); |
| 93 | + } |
| 94 | + |
| 95 | + /// @notice Disables ownership renunciation to avoid permanently locked pause state. |
| 96 | + function renounceOwnership() public view override onlyOwner { |
| 97 | + revert OwnershipRenunciationDisabled(); |
| 98 | + } |
| 99 | + |
| 100 | + function _setOperator(address operator, bool allowed) private { |
| 101 | + if (operator == address(0)) { |
| 102 | + revert ZeroAddress(); |
| 103 | + } |
| 104 | + |
| 105 | + if (allowed) { |
| 106 | + _operators.add(operator); |
| 107 | + } else { |
| 108 | + _operators.remove(operator); |
| 109 | + } |
| 110 | + |
| 111 | + emit OperatorSet(operator, allowed); |
| 112 | + } |
| 113 | +} |
0 commit comments