Branch data Line data Source code
1 : : // SPDX-License-Identifier: MIT
2 : : pragma solidity ^0.8.10;
3 : :
4 : : import {BaseUpgradeabilityProxy} from '../../dependencies/openzeppelin/upgradeability/BaseUpgradeabilityProxy.sol';
5 : :
6 : : /**
7 : : * @title BaseImmutableAdminUpgradeabilityProxy
8 : : * @author Aave, inspired by the OpenZeppelin upgradeability proxy pattern
9 : : * @notice This contract combines an upgradeability proxy with an authorization
10 : : * mechanism for administrative tasks.
11 : : * @dev The admin role is stored in an immutable, which helps saving transactions costs
12 : : * All external functions in this contract must be guarded by the
13 : : * `ifAdmin` modifier. See ethereum/solidity#3864 for a Solidity
14 : : * feature proposal that would enable this to be done automatically.
15 : : */
16 : : contract BaseImmutableAdminUpgradeabilityProxy is BaseUpgradeabilityProxy {
17 : : address internal immutable _admin;
18 : :
19 : : /**
20 : : * @dev Constructor.
21 : : * @param admin_ The address of the admin
22 : : */
23 : : constructor(address admin_) {
24 : 46 : _admin = admin_;
25 : : }
26 : :
27 : : modifier ifAdmin() {
28 : 2 : if (msg.sender == _admin) {
29 : : _;
30 : : } else {
31 : 1 : _fallback();
32 : : }
33 : : }
34 : :
35 : : /**
36 : : * @notice Return the admin address
37 : : * @return The address of the proxy admin.
38 : : */
39 : : function admin() external ifAdmin returns (address) {
40 : 8 : return _admin;
41 : : }
42 : :
43 : : /**
44 : : * @notice Return the implementation address
45 : : * @return The address of the implementation.
46 : : */
47 : : function implementation() external ifAdmin returns (address) {
48 : 3 : return _implementation();
49 : : }
50 : :
51 : : /**
52 : : * @notice Upgrade the backing implementation of the proxy.
53 : : * @dev Only the admin can call this function.
54 : : * @param newImplementation The address of the new implementation.
55 : : */
56 : : function upgradeTo(address newImplementation) external ifAdmin {
57 : 1 : _upgradeTo(newImplementation);
58 : : }
59 : :
60 : : /**
61 : : * @notice Upgrade the backing implementation of the proxy and call a function
62 : : * on the new implementation.
63 : : * @dev This is useful to initialize the proxied contract.
64 : : * @param newImplementation The address of the new implementation.
65 : : * @param data Data to send as msg.data in the low level call.
66 : : * It should include the signature and the parameters of the function to be called, as described in
67 : : * https://solidity.readthedocs.io/en/v0.4.24/abi-spec.html#function-selector-and-argument-encoding.
68 : : */
69 : : function upgradeToAndCall(
70 : : address newImplementation,
71 : : bytes calldata data
72 : : ) external payable ifAdmin {
73 : 12 : _upgradeTo(newImplementation);
74 : 12 : (bool success, ) = newImplementation.delegatecall(data);
75 : 12 : require(success);
76 : : }
77 : :
78 : : /**
79 : : * @notice Only fall back when the sender is not the admin.
80 : : */
81 : : function _willFallback() internal virtual override {
82 : 2165226 : require(msg.sender != _admin, 'Cannot call fallback function from the proxy admin');
83 : 2165226 : super._willFallback();
84 : : }
85 : : }
|