-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSafeOwnableUpgradeable.sol
executable file
·150 lines (130 loc) · 4.51 KB
/
SafeOwnableUpgradeable.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// SPDX-License-Identifier: MIT
// Thanks Yos Riady
// Refer to https://github.com/boringcrypto/BoringSolidity/blob/master/contracts/BoringOwnable.sol
// https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable/blob/master/contracts/access/OwnableUpgradeable.sol
pragma solidity ^0.8.0;
import "./Initializable.sol";
import "./ContextUpgradeable.sol";
contract SafeOwnableUpgradeable is Initializable, ContextUpgradeable {
error CallerNotOwner();
error ZeroAddressOwnerSet();
error CallerNotPendingOwner();
/**
* @dev Storage slot with the admin of the contract.
* This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1, and is
* validated in the constructor.
*/
bytes32 private constant _ADMIN_SLOT =
0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
address private _owner;
address private _pendingOwner;
event OwnershipTransferred(
address indexed previousOwner,
address indexed newOwner
);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
function __Ownable_init(address owner_) internal onlyInitializing {
__Ownable_init_unchained(owner_);
}
function __Ownable_init_unchained(
address owner_
) internal onlyInitializing {
_transferOwnership(owner_);
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Return the address of the pending owner
*/
function pendingOwner() public view virtual returns (address) {
return _pendingOwner;
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
if (owner() != _msgSender()) {
revert CallerNotOwner();
}
_;
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions anymore. Can only be called by the current owner.
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby removing any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
* Note If direct is false, it will set an pending owner and the OwnerShipTransferring
* only happens when the pending owner claim the ownership
*/
function transferOwnership(
address newOwner,
bool direct
) public virtual onlyOwner {
if (newOwner == address(0)) {
revert ZeroAddressOwnerSet();
}
if (direct) {
_transferOwnership(newOwner);
} else {
_transferPendingOwnership(newOwner);
}
}
/**
* @dev pending owner call this function to claim ownership
*/
function claimOwnership() public {
if (msg.sender != _pendingOwner) {
revert CallerNotPendingOwner();
}
_claimOwnership();
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
// compatible with hardhat-deploy, maybe removed later
assembly {
sstore(_ADMIN_SLOT, newOwner)
}
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
/**
* @dev set the pending owner address
* Internal function without access restriction.
*/
function _transferPendingOwnership(address newOwner) internal virtual {
_pendingOwner = newOwner;
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _claimOwnership() internal virtual {
address oldOwner = _owner;
emit OwnershipTransferred(oldOwner, _pendingOwner);
_owner = _pendingOwner;
_pendingOwner = address(0);
}
/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[48] private __gap;
}