-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOwnableWithOperator.sol
107 lines (89 loc) · 3.09 KB
/
OwnableWithOperator.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// SPDX-FileCopyrightText: 2023 P2P Validator <info@p2p.org>
// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;
import "./Ownable2Step.sol";
import "./IOwnableWithOperator.sol";
/**
* @notice newOperator is the zero address
*/
error Access__ZeroNewOperator();
/**
* @notice newOperator is the same as the old one
*/
error Access__SameOperator(address _operator);
/**
* @notice caller is neither the operator nor owner
*/
error Access__CallerNeitherOperatorNorOwner(address _caller, address _operator, address _owner);
/**
* @notice address is neither the operator nor owner
*/
error Access__AddressNeitherOperatorNorOwner(address _address, address _operator, address _owner);
/**
* @dev Ownable with an additional role of operator
*/
abstract contract OwnableWithOperator is Ownable2Step, IOwnableWithOperator {
address private s_operator;
/**
* @dev Emits when the operator has been changed
* @param _previousOperator address of the previous operator
* @param _newOperator address of the new operator
*/
event OperatorChanged(
address indexed _previousOperator,
address indexed _newOperator
);
/**
* @dev Throws if called by any account other than the operator or the owner.
*/
modifier onlyOperatorOrOwner() {
address currentOwner = owner();
address currentOperator = s_operator;
if (currentOperator != _msgSender() && currentOwner != _msgSender()) {
revert Access__CallerNeitherOperatorNorOwner(_msgSender(), currentOperator, currentOwner);
}
_;
}
function checkOperatorOrOwner(address _address) public view virtual {
address currentOwner = owner();
address currentOperator = s_operator;
if (_address == address(0) || (currentOperator != _address && currentOwner != _address)) {
revert Access__AddressNeitherOperatorNorOwner(_address, currentOperator, currentOwner);
}
}
/**
* @dev Returns the current operator.
*/
function operator() public view virtual returns (address) {
return s_operator;
}
/**
* @dev Transfers operator to a new account (`newOperator`).
* Can only be called by the current owner.
*/
function changeOperator(address _newOperator) external virtual onlyOwner {
if (_newOperator == address(0)) {
revert Access__ZeroNewOperator();
}
if (_newOperator == s_operator) {
revert Access__SameOperator(_newOperator);
}
_changeOperator(_newOperator);
}
/**
* @dev Transfers operator to a new account (`newOperator`).
* Internal function without access restriction.
*/
function _changeOperator(address _newOperator) internal virtual {
address oldOperator = s_operator;
s_operator = _newOperator;
emit OperatorChanged(oldOperator, _newOperator);
}
/**
* @dev Dismisses the old operator without setting a new one.
* Can only be called by the current owner.
*/
function dismissOperator() external virtual onlyOwner {
_changeOperator(address(0));
}
}