11// SPDX-License-Identifier: MIT
2- // OpenZeppelin Contracts (last updated v4.9.0) (proxy/ERC1967/ERC1967Upgrade .sol)
2+ // OpenZeppelin Contracts (last updated v4.9.0) (proxy/ERC1967/ERC1967Utils .sol)
33
4- pragma solidity ^ 0.8.19 ;
4+ pragma solidity ^ 0.8.20 ;
55
66import "../beacon/IBeacon.sol " ;
77import "../../interfaces/IERC1967.sol " ;
@@ -15,7 +15,24 @@ import "../../utils/StorageSlot.sol";
1515 *
1616 * _Available since v4.1._
1717 */
18- abstract contract ERC1967Upgrade is IERC1967 {
18+ library ERC1967Utils {
19+ // We re-declare ERC-1967 events here because they can't be used directly from IERC1967.
20+ // This will be fixed in Solidity 0.8.21. At that point we should remove these events.
21+ /**
22+ * @dev Emitted when the implementation is upgraded.
23+ */
24+ event Upgraded (address indexed implementation );
25+
26+ /**
27+ * @dev Emitted when the admin account has changed.
28+ */
29+ event AdminChanged (address previousAdmin , address newAdmin );
30+
31+ /**
32+ * @dev Emitted when the beacon is changed.
33+ */
34+ event BeaconUpgraded (address indexed beacon );
35+
1936 // This is the keccak-256 hash of "eip1967.proxy.rollback" subtracted by 1
2037 bytes32 private constant _ROLLBACK_SLOT = 0x4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd9143 ;
2138
@@ -24,7 +41,8 @@ abstract contract ERC1967Upgrade is IERC1967 {
2441 * This is the keccak-256 hash of "eip1967.proxy.implementation" subtracted by 1, and is
2542 * validated in the constructor.
2643 */
27- bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc ;
44+ // solhint-disable-next-line private-vars-leading-underscore
45+ bytes32 internal constant IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc ;
2846
2947 /**
3048 * @dev The `implementation` of the proxy is invalid.
@@ -49,8 +67,8 @@ abstract contract ERC1967Upgrade is IERC1967 {
4967 /**
5068 * @dev Returns the current implementation address.
5169 */
52- function _getImplementation () internal view returns (address ) {
53- return StorageSlot.getAddressSlot (_IMPLEMENTATION_SLOT ).value;
70+ function getImplementation () internal view returns (address ) {
71+ return StorageSlot.getAddressSlot (IMPLEMENTATION_SLOT ).value;
5472 }
5573
5674 /**
@@ -60,26 +78,26 @@ abstract contract ERC1967Upgrade is IERC1967 {
6078 if (newImplementation.code.length == 0 ) {
6179 revert ERC1967InvalidImplementation (newImplementation);
6280 }
63- StorageSlot.getAddressSlot (_IMPLEMENTATION_SLOT ).value = newImplementation;
81+ StorageSlot.getAddressSlot (IMPLEMENTATION_SLOT ).value = newImplementation;
6482 }
6583
6684 /**
6785 * @dev Perform implementation upgrade
6886 *
69- * Emits an {Upgraded} event.
87+ * Emits an {IERC1967- Upgraded} event.
7088 */
71- function _upgradeTo (address newImplementation ) internal {
89+ function upgradeTo (address newImplementation ) internal {
7290 _setImplementation (newImplementation);
7391 emit Upgraded (newImplementation);
7492 }
7593
7694 /**
7795 * @dev Perform implementation upgrade with additional setup call.
7896 *
79- * Emits an {Upgraded} event.
97+ * Emits an {IERC1967- Upgraded} event.
8098 */
81- function _upgradeToAndCall (address newImplementation , bytes memory data , bool forceCall ) internal {
82- _upgradeTo (newImplementation);
99+ function upgradeToAndCall (address newImplementation , bytes memory data , bool forceCall ) internal {
100+ upgradeTo (newImplementation);
83101 if (data.length > 0 || forceCall) {
84102 Address.functionDelegateCall (newImplementation, data);
85103 }
@@ -88,24 +106,24 @@ abstract contract ERC1967Upgrade is IERC1967 {
88106 /**
89107 * @dev Perform implementation upgrade with security checks for UUPS proxies, and additional setup call.
90108 *
91- * Emits an {Upgraded} event.
109+ * Emits an {IERC1967- Upgraded} event.
92110 */
93- function _upgradeToAndCallUUPS (address newImplementation , bytes memory data , bool forceCall ) internal {
111+ function upgradeToAndCallUUPS (address newImplementation , bytes memory data , bool forceCall ) internal {
94112 // Upgrades from old implementations will perform a rollback test. This test requires the new
95113 // implementation to upgrade back to the old, non-ERC1822 compliant, implementation. Removing
96114 // this special case will break upgrade paths from old UUPS implementation to new ones.
97115 if (StorageSlot.getBooleanSlot (_ROLLBACK_SLOT).value) {
98116 _setImplementation (newImplementation);
99117 } else {
100118 try IERC1822Proxiable (newImplementation).proxiableUUID () returns (bytes32 slot ) {
101- if (slot != _IMPLEMENTATION_SLOT ) {
119+ if (slot != IMPLEMENTATION_SLOT ) {
102120 revert ERC1967UnsupportedProxiableUUID (slot);
103121 }
104122 } catch {
105123 // The implementation is not UUPS
106124 revert ERC1967InvalidImplementation (newImplementation);
107125 }
108- _upgradeToAndCall (newImplementation, data, forceCall);
126+ upgradeToAndCall (newImplementation, data, forceCall);
109127 }
110128 }
111129
@@ -114,7 +132,8 @@ abstract contract ERC1967Upgrade is IERC1967 {
114132 * This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1, and is
115133 * validated in the constructor.
116134 */
117- bytes32 internal constant _ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103 ;
135+ // solhint-disable-next-line private-vars-leading-underscore
136+ bytes32 internal constant ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103 ;
118137
119138 /**
120139 * @dev Returns the current admin.
@@ -123,8 +142,8 @@ abstract contract ERC1967Upgrade is IERC1967 {
123142 * https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
124143 * `0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103`
125144 */
126- function _getAdmin () internal view returns (address ) {
127- return StorageSlot.getAddressSlot (_ADMIN_SLOT ).value;
145+ function getAdmin () internal view returns (address ) {
146+ return StorageSlot.getAddressSlot (ADMIN_SLOT ).value;
128147 }
129148
130149 /**
@@ -134,30 +153,31 @@ abstract contract ERC1967Upgrade is IERC1967 {
134153 if (newAdmin == address (0 )) {
135154 revert ERC1967InvalidAdmin (address (0 ));
136155 }
137- StorageSlot.getAddressSlot (_ADMIN_SLOT ).value = newAdmin;
156+ StorageSlot.getAddressSlot (ADMIN_SLOT ).value = newAdmin;
138157 }
139158
140159 /**
141160 * @dev Changes the admin of the proxy.
142161 *
143- * Emits an {AdminChanged} event.
162+ * Emits an {IERC1967- AdminChanged} event.
144163 */
145- function _changeAdmin (address newAdmin ) internal {
146- emit AdminChanged (_getAdmin (), newAdmin);
164+ function changeAdmin (address newAdmin ) internal {
165+ emit AdminChanged (getAdmin (), newAdmin);
147166 _setAdmin (newAdmin);
148167 }
149168
150169 /**
151170 * @dev The storage slot of the UpgradeableBeacon contract which defines the implementation for this proxy.
152- * This is bytes32(uint256(keccak256('eip1967.proxy.beacon')) - 1)) and is validated in the constructor.
171+ * This is bytes32(uint256(keccak256('eip1967.proxy.beacon')) - 1) and is validated in the constructor.
153172 */
154- bytes32 internal constant _BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50 ;
173+ // solhint-disable-next-line private-vars-leading-underscore
174+ bytes32 internal constant BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50 ;
155175
156176 /**
157177 * @dev Returns the current beacon.
158178 */
159- function _getBeacon () internal view returns (address ) {
160- return StorageSlot.getAddressSlot (_BEACON_SLOT ).value;
179+ function getBeacon () internal view returns (address ) {
180+ return StorageSlot.getAddressSlot (BEACON_SLOT ).value;
161181 }
162182
163183 /**
@@ -173,16 +193,16 @@ abstract contract ERC1967Upgrade is IERC1967 {
173193 revert ERC1967InvalidImplementation (beaconImplementation);
174194 }
175195
176- StorageSlot.getAddressSlot (_BEACON_SLOT ).value = newBeacon;
196+ StorageSlot.getAddressSlot (BEACON_SLOT ).value = newBeacon;
177197 }
178198
179199 /**
180200 * @dev Perform beacon upgrade with additional setup call. Note: This upgrades the address of the beacon, it does
181201 * not upgrade the implementation contained in the beacon (see {UpgradeableBeacon-_setImplementation} for that).
182202 *
183- * Emits a { BeaconUpgraded} event.
203+ * Emits an {IERC1967- BeaconUpgraded} event.
184204 */
185- function _upgradeBeaconToAndCall (address newBeacon , bytes memory data , bool forceCall ) internal {
205+ function upgradeBeaconToAndCall (address newBeacon , bytes memory data , bool forceCall ) internal {
186206 _setBeacon (newBeacon);
187207 emit BeaconUpgraded (newBeacon);
188208 if (data.length > 0 || forceCall) {
0 commit comments