Repository navigation
Expand file tree
/
Copy pathOwnableInit.sol
More file actions
60 lines (51 loc) · 2.21 KB
/
Copy pathOwnableInit.sol
File metadata and controls
60 lines (51 loc) · 2.21 KB
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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.36;
/// Ownership for a contract deployed behind a proxy.
///
/// OpenZeppelin's `Ownable` assigns the owner in its constructor, which does not
/// run for a proxy: it would write the implementation's storage and leave the
/// proxy unowned. The owner is assigned from an initializer instead.
///
/// `renounceOwnership` is not declared, so ownership cannot be dropped — the
/// one bound on the owner that a governance proposal cannot lift.
/// `transferOwnership` is declared because the deploy uses it to hand each
/// contract to the Timelock; a later proposal can move it again, and nothing
/// refuses a destination that cannot administer it.
///
/// `_owner` occupies a sequential slot, matching the position OZ's `Ownable`
/// takes in the inheritance chain.
abstract contract OwnableInit {
address private _owner;
/// Mirrors OZ's `Ownable` errors for tooling compatibility.
error OwnableUnauthorizedAccount(address account);
error OwnableInvalidOwner(address owner);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
modifier onlyOwner() {
_requireOwner();
_;
}
/// Factored out of the modifier so its body is not inlined at every
/// `onlyOwner` function across the inheriting contracts.
function _requireOwner() private view {
if (msg.sender != _owner) revert OwnableUnauthorizedAccount(msg.sender);
}
function owner() public view virtual returns (address) {
return _owner;
}
/// Assigned once, from the initializer. Rejects zero.
function _initOwner(address owner_) internal {
if (owner_ == address(0)) revert OwnableInvalidOwner(address(0));
_owner = owner_;
emit OwnershipTransferred(address(0), owner_);
}
/// Rejects zero, so it cannot serve as a renounce.
function transferOwnership(address newOwner) external onlyOwner {
_transferOwnership(newOwner);
}
function _transferOwnership(address newOwner) internal {
if (newOwner == address(0)) revert OwnableInvalidOwner(address(0));
address old = _owner;
_owner = newOwner;
emit OwnershipTransferred(old, newOwner);
}
}