|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.24; |
| 3 | + |
| 4 | +/// @title ERC1967Utils |
| 5 | +/// @notice Library for reading and writing ERC-1967 storage slots and emitting corresponding events for upgradeable proxies. |
| 6 | +/// @author fomoweth |
| 7 | +library ERC1967Utils { |
| 8 | + /// @notice Thrown when the provided implementation address is invalid. |
| 9 | + error InvalidImplementation(); |
| 10 | + |
| 11 | + /// @notice Thrown when the provided admin address is invalid. |
| 12 | + error InvalidAdmin(); |
| 13 | + |
| 14 | + /// @notice Thrown when the provided beacon address is invalid. |
| 15 | + error InvalidBeacon(); |
| 16 | + |
| 17 | + /// @notice Thrown when Ether is sent to an upgrade with no initialization call. |
| 18 | + error NonPayable(); |
| 19 | + |
| 20 | + /// @notice Thrown when the returned UUID does not match expected ERC-1967 slot. |
| 21 | + error UnsupportedProxiableUUID(bytes32 slot); |
| 22 | + |
| 23 | + /// @notice Emitted when the ERC-1967 implementation slot is updated. |
| 24 | + event Upgraded(address indexed implementation); |
| 25 | + |
| 26 | + /// @notice Emitted when the ERC-1967 admin slot is updated. |
| 27 | + event AdminChanged(address previousAdmin, address newAdmin); |
| 28 | + |
| 29 | + /// @notice Emitted when the ERC-1967 beacon slot is updated. |
| 30 | + event BeaconUpgraded(address indexed beacon); |
| 31 | + |
| 32 | + /// @notice Precomputed event topic for {Upgraded}. |
| 33 | + /// @dev keccak256(bytes("Upgraded(address)")) |
| 34 | + bytes32 internal constant UPGRADED_EVENT_SIGNATURE = |
| 35 | + 0xbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b; |
| 36 | + |
| 37 | + /// @notice Precomputed event topic for {AdminChanged}. |
| 38 | + /// @dev keccak256(bytes("AdminChanged(address,address)")) |
| 39 | + bytes32 internal constant ADMIN_CHANGED_EVENT_SIGNATURE = |
| 40 | + 0x7e644d79422f17c01e4894b5f4f588d331ebfa28653d42ae832dc59e38c9798f; |
| 41 | + |
| 42 | + /// @notice Precomputed event topic for {BeaconUpgraded}. |
| 43 | + /// @dev keccak256(bytes("BeaconUpgraded(address)")) |
| 44 | + bytes32 internal constant BEACON_UPGRADED_EVENT_SIGNATURE = |
| 45 | + 0x1cf3b03a6cf19fa2baba4df148e9dcabedea7f8a5c07840e207e5c089be95d3e; |
| 46 | + |
| 47 | + /// @notice ERC-1967 storage slot for the implementation address. |
| 48 | + /// @dev bytes32(uint256(keccak256(bytes("eip1967.proxy.implementation"))) - 1) |
| 49 | + bytes32 internal constant IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc; |
| 50 | + |
| 51 | + /// @notice ERC-1967 storage slot for the admin address. |
| 52 | + /// @dev bytes32(uint256(keccak256(bytes("eip1967.proxy.admin"))) - 1) |
| 53 | + bytes32 internal constant ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103; |
| 54 | + |
| 55 | + /// @notice ERC-1967 storage slot for the beacon address. |
| 56 | + /// @dev bytes32(uint256(keccak256(bytes("eip1967.proxy.beacon"))) - 1) |
| 57 | + bytes32 internal constant BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50; |
| 58 | + |
| 59 | + /// @notice Returns the current implementation stored in the ERC-1967 implementation slot. |
| 60 | + /// @return implementation The address of the implementation contract. |
| 61 | + function getImplementation() internal view returns (address implementation) { |
| 62 | + assembly ("memory-safe") { |
| 63 | + implementation := sload(IMPLEMENTATION_SLOT) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /// @notice Upgrades the proxy implementation and optionally executes an initialization call. |
| 68 | + /// @dev Reverts with {InvalidImplementation} if `implementation` has no deployed code. |
| 69 | + /// Emits {Upgraded} with `implementation`. |
| 70 | + /// @param implementation The address of the new implementation contract. |
| 71 | + /// @param data ABI-encoded initializer calldata, or empty to skip the execution. |
| 72 | + function upgradeToAndCall(address implementation, bytes memory data) internal { |
| 73 | + assembly ("memory-safe") { |
| 74 | + implementation := shr(0x60, shl(0x60, implementation)) |
| 75 | + |
| 76 | + if iszero(extcodesize(implementation)) { |
| 77 | + mstore(0x00, 0x68155f9a) // InvalidImplementation() |
| 78 | + revert(0x1c, 0x04) |
| 79 | + } |
| 80 | + |
| 81 | + sstore(IMPLEMENTATION_SLOT, implementation) |
| 82 | + log2(codesize(), 0x00, UPGRADED_EVENT_SIGNATURE, implementation) |
| 83 | + } |
| 84 | + |
| 85 | + _executeInitialization(implementation, data); |
| 86 | + } |
| 87 | + |
| 88 | + /// @notice Upgrades the proxy implementation via the UUPS pattern with proxiable UUID validation. |
| 89 | + /// @dev Reverts with {InvalidImplementation} if `proxiableUUID()` call fails or does not return 32 bytes. |
| 90 | + /// Reverts with {UnsupportedProxiableUUID} if returned UUID is not {IMPLEMENTATION_SLOT}. |
| 91 | + /// Emits {Upgraded} with `implementation`. |
| 92 | + /// @param implementation The address of the new UUPS-compliant implementation contract. |
| 93 | + /// @param data ABI-encoded initializer calldata, or empty to skip the execution. |
| 94 | + function upgradeToAndCallUUPS(address implementation, bytes memory data) internal { |
| 95 | + assembly ("memory-safe") { |
| 96 | + implementation := shr(0x60, shl(0x60, implementation)) |
| 97 | + |
| 98 | + mstore(0x00, 0x52d1902d) // proxiableUUID() |
| 99 | + |
| 100 | + if iszero(and(eq(returndatasize(), 0x20), staticcall(gas(), implementation, 0x1c, 0x04, 0x20, 0x20))) { |
| 101 | + mstore(0x00, 0x68155f9a) // InvalidImplementation() |
| 102 | + revert(0x1c, 0x04) |
| 103 | + } |
| 104 | + |
| 105 | + if iszero(eq(mload(0x20), IMPLEMENTATION_SLOT)) { |
| 106 | + mstore(0x00, 0x3878d626) // UnsupportedProxiableUUID(bytes32) |
| 107 | + revert(0x1c, 0x24) |
| 108 | + } |
| 109 | + |
| 110 | + sstore(IMPLEMENTATION_SLOT, implementation) |
| 111 | + log2(codesize(), 0x00, UPGRADED_EVENT_SIGNATURE, implementation) |
| 112 | + } |
| 113 | + |
| 114 | + _executeInitialization(implementation, data); |
| 115 | + } |
| 116 | + |
| 117 | + /// @notice Returns the current admin stored in the ERC-1967 admin slot. |
| 118 | + /// @return admin The address of the proxy admin. |
| 119 | + function getAdmin() internal view returns (address admin) { |
| 120 | + assembly ("memory-safe") { |
| 121 | + admin := sload(ADMIN_SLOT) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /// @notice Updates the proxy admin to a new address. |
| 126 | + /// @dev Reverts with {InvalidAdmin} if `admin` is the zero address. |
| 127 | + /// Emits {AdminChanged} with previous admin and new admin. |
| 128 | + /// @param admin The address of the new proxy admin. |
| 129 | + function changeAdmin(address admin) internal { |
| 130 | + assembly ("memory-safe") { |
| 131 | + if iszero(shl(0x60, admin)) { |
| 132 | + mstore(0x00, 0xb5eba9f0) // InvalidAdmin() |
| 133 | + revert(0x1c, 0x04) |
| 134 | + } |
| 135 | + |
| 136 | + admin := shr(0x60, shl(0x60, admin)) |
| 137 | + |
| 138 | + mstore(0x00, sload(ADMIN_SLOT)) |
| 139 | + mstore(0x20, admin) |
| 140 | + sstore(ADMIN_SLOT, admin) |
| 141 | + log1(0x00, 0x40, ADMIN_CHANGED_EVENT_SIGNATURE) |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + /// @notice Returns the current beacon stored in the ERC-1967 beacon slot. |
| 146 | + /// @return beacon The address of the beacon contract. |
| 147 | + function getBeacon() internal view returns (address beacon) { |
| 148 | + assembly ("memory-safe") { |
| 149 | + beacon := sload(BEACON_SLOT) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /// @notice Returns the current implementation resolved by beacon via `implementation()`. |
| 154 | + /// @dev Reverts with {InvalidBeacon} if the call fails or does not return 32 bytes. |
| 155 | + /// @param beacon The address of the beacon contract to query. |
| 156 | + /// @return implementation The address of the implementation returned by the `beacon.implementation()`. |
| 157 | + function getBeaconImplementation(address beacon) internal view returns (address implementation) { |
| 158 | + assembly ("memory-safe") { |
| 159 | + mstore(0x00, 0x5c60da1b) // implementation() |
| 160 | + |
| 161 | + if iszero(and(eq(returndatasize(), 0x20), staticcall(gas(), beacon, 0x1c, 0x04, 0x00, 0x20))) { |
| 162 | + mstore(0x00, 0x30740e75) // InvalidBeacon() |
| 163 | + revert(0x1c, 0x04) |
| 164 | + } |
| 165 | + |
| 166 | + implementation := mload(0x00) |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + /// @notice Upgrades the beacon and optionally executes an initialization call on its implementation. |
| 171 | + /// @dev Reverts with {InvalidBeacon} if `implementation()` call fails or returned implementation |
| 172 | + /// has no deployed code. Emits {BeaconUpgraded} with `beacon`. |
| 173 | + /// @param beacon The address of the new beacon contract. |
| 174 | + /// @param data ABI-encoded initializer calldata, or empty to skip the execution. |
| 175 | + function upgradeBeaconToAndCall(address beacon, bytes memory data) internal { |
| 176 | + assembly ("memory-safe") { |
| 177 | + beacon := shr(0x60, shl(0x60, beacon)) |
| 178 | + |
| 179 | + mstore(0x00, returndatasize()) |
| 180 | + mstore(0x01, 0x5c60da1b) // implementation() |
| 181 | + |
| 182 | + if iszero(extcodesize(mload(staticcall(gas(), beacon, 0x1d, 0x04, 0x01, 0x20)))) { |
| 183 | + mstore(0x01, 0x30740e75) // InvalidBeacon() |
| 184 | + revert(0x1d, 0x04) |
| 185 | + } |
| 186 | + |
| 187 | + sstore(BEACON_SLOT, beacon) |
| 188 | + log2(codesize(), 0x00, BEACON_UPGRADED_EVENT_SIGNATURE, beacon) |
| 189 | + } |
| 190 | + |
| 191 | + _executeInitialization(getBeaconImplementation(beacon), data); |
| 192 | + } |
| 193 | + |
| 194 | + /// @notice Executes initialization on `implementation` with `data` if provided; otherwise validates that no Ether was sent. |
| 195 | + /// @dev Reverts with {NonPayable} if `data` is empty and `msg.value` is nonzero. |
| 196 | + /// @param implementation The address of the target for the initialization delegatecall. |
| 197 | + /// @param data ABI-encoded initializer calldata, or empty to skip the execution. |
| 198 | + function _executeInitialization(address implementation, bytes memory data) private { |
| 199 | + assembly ("memory-safe") { |
| 200 | + switch mload(data) |
| 201 | + case 0x00 { |
| 202 | + if callvalue() { |
| 203 | + mstore(0x00, 0x6fb1b0e9) // NonPayable() |
| 204 | + revert(0x1c, 0x04) |
| 205 | + } |
| 206 | + } |
| 207 | + default { |
| 208 | + if iszero(delegatecall(gas(), implementation, add(data, 0x20), mload(data), codesize(), 0x00)) { |
| 209 | + let ptr := mload(0x40) |
| 210 | + returndatacopy(ptr, 0x00, returndatasize()) |
| 211 | + revert(ptr, returndatasize()) |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | +} |
0 commit comments