-
Notifications
You must be signed in to change notification settings - Fork 518
/
BaseImmutableAdminUpgradeabilityProxy.sol
86 lines (77 loc) · 2.71 KB
/
BaseImmutableAdminUpgradeabilityProxy.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
// SPDX-License-Identifier: AGPL-3.0
pragma solidity 0.8.10;
import {BaseUpgradeabilityProxy} from '../../../dependencies/openzeppelin/upgradeability/BaseUpgradeabilityProxy.sol';
/**
* @title BaseImmutableAdminUpgradeabilityProxy
* @author Aave, inspired by the OpenZeppelin upgradeability proxy pattern
* @notice This contract combines an upgradeability proxy with an authorization
* mechanism for administrative tasks.
* @dev The admin role is stored in an immutable, which helps saving transactions costs
* All external functions in this contract must be guarded by the
* `ifAdmin` modifier. See ethereum/solidity#3864 for a Solidity
* feature proposal that would enable this to be done automatically.
*/
contract BaseImmutableAdminUpgradeabilityProxy is BaseUpgradeabilityProxy {
address internal immutable _admin;
/**
* @dev Constructor.
* @param admin The address of the admin
*/
constructor(address admin) {
_admin = admin;
}
modifier ifAdmin() {
if (msg.sender == _admin) {
_;
} else {
_fallback();
}
}
/**
* @notice Return the admin address
* @return The address of the proxy admin.
*/
function admin() external ifAdmin returns (address) {
return _admin;
}
/**
* @notice Return the implementation address
* @return The address of the implementation.
*/
function implementation() external ifAdmin returns (address) {
return _implementation();
}
/**
* @notice Upgrade the backing implementation of the proxy.
* @dev Only the admin can call this function.
* @param newImplementation The address of the new implementation.
*/
function upgradeTo(address newImplementation) external ifAdmin {
_upgradeTo(newImplementation);
}
/**
* @notice Upgrade the backing implementation of the proxy and call a function
* on the new implementation.
* @dev This is useful to initialize the proxied contract.
* @param newImplementation The address of the new implementation.
* @param data Data to send as msg.data in the low level call.
* It should include the signature and the parameters of the function to be called, as described in
* https://solidity.readthedocs.io/en/v0.4.24/abi-spec.html#function-selector-and-argument-encoding.
*/
function upgradeToAndCall(address newImplementation, bytes calldata data)
external
payable
ifAdmin
{
_upgradeTo(newImplementation);
(bool success, ) = newImplementation.delegatecall(data);
require(success);
}
/**
* @notice Only fall back when the sender is not the admin.
*/
function _willFallback() internal virtual override {
require(msg.sender != _admin, 'Cannot call fallback function from the proxy admin');
super._willFallback();
}
}