-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOwnableWithOperator.sol
92 lines (77 loc) · 2.49 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
// SPDX-FileCopyrightText: 2022 P2P Validator <info@p2p.org>
// SPDX-License-Identifier: MIT
pragma solidity 0.8.10;
import "./Ownable.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 not the operator
*/
error Access__CallerNeitherOperatorNorOwner(address _caller, address _operator, address _owner);
/**
* @dev Ownable with an additional role of operator
*/
abstract contract OwnableWithOperator is Ownable {
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);
}
_;
}
/**
* @dev Returns the current operator.
*/
function operator() external 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));
}
}