|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity ^0.8.20; |
| 4 | + |
| 5 | +import { CallContext } from "../../mixins/CallContext.sol"; |
| 6 | +import { ERC1967Logic } from "../ERC1967/ERC1967Logic.sol"; |
| 7 | + |
| 8 | +/// @title UUPSImplementation |
| 9 | +/// |
| 10 | +/// @author TSxo |
| 11 | +/// |
| 12 | +/// @dev An ERC-1822 and ERC-1967 compliant UUPS implementation contract. Designed |
| 13 | +/// to be used with an ERC-1967 proxy. |
| 14 | +/// |
| 15 | +/// # Acknowledgements |
| 16 | +/// |
| 17 | +/// Heavy inspiration is taken from: |
| 18 | +/// - OpenZeppelin; |
| 19 | +/// - Solmate; and |
| 20 | +/// - Solady. |
| 21 | +/// |
| 22 | +/// Thank you. |
| 23 | +abstract contract UUPSImplementation is CallContext, ERC1967Logic { |
| 24 | + // ------------------------------------------------------------------------- |
| 25 | + // Functions - External |
| 26 | + |
| 27 | + /// @notice Returns the slot at which the implementation address is stored. |
| 28 | + /// |
| 29 | + /// @dev Requirements: |
| 30 | + /// - Not callable through a proxy. This prevents upgrades to a proxy contract. |
| 31 | + /// |
| 32 | + /// See https://eips.ethereum.org/EIPS/eip-1822 |
| 33 | + function proxiableUUID() external view virtual notDelegated returns (bytes32) { |
| 34 | + return IMPLEMENTATION_SLOT; |
| 35 | + } |
| 36 | + |
| 37 | + // ------------------------------------------------------------------------- |
| 38 | + // Functions - Public |
| 39 | + |
| 40 | + /// @notice Upgrades the implementation of the proxy to `newImplementation` |
| 41 | + /// and executes the function call, if any, encoded in `data`. |
| 42 | + /// |
| 43 | + /// @dev Requirements: |
| 44 | + /// - Only callable through a proxy. |
| 45 | + /// - The caller must be authorized to perform the upgrade. |
| 46 | + /// - The `newImplementation` must be ERC-1822 compliant. |
| 47 | + /// |
| 48 | + /// Emits an `Upgraded` event. |
| 49 | + function upgradeToAndCall(address newImplementation, bytes calldata data) public payable virtual onlyProxy { |
| 50 | + _authorizeUpgrade(newImplementation); |
| 51 | + |
| 52 | + assembly ("memory-safe") { |
| 53 | + mstore(0x00, 0x52d1902d) // `proxiableUUID()` |
| 54 | + |
| 55 | + let success := staticcall(gas(), newImplementation, 0x1c, 0x04, 0x00, 0x20) |
| 56 | + |
| 57 | + if iszero(success) { revert(0x00, 0x00) } |
| 58 | + if iszero(eq(returndatasize(), 0x20)) { revert(0x00, 0x00) } |
| 59 | + |
| 60 | + if iszero(eq(mload(0x00), IMPLEMENTATION_SLOT)) { |
| 61 | + mstore(0x00, 0x5f73959e) // `ERC1967Logic__UpgradeFailed()` |
| 62 | + revert(0x1c, 0x04) |
| 63 | + } |
| 64 | + |
| 65 | + log2(0x00, 0x00, UPGRADED, newImplementation) |
| 66 | + sstore(IMPLEMENTATION_SLOT, newImplementation) |
| 67 | + |
| 68 | + if data.length { |
| 69 | + let ptr := mload(0x40) |
| 70 | + calldatacopy(ptr, data.offset, data.length) |
| 71 | + |
| 72 | + success := delegatecall(gas(), newImplementation, ptr, data.length, 0x00, 0x00) |
| 73 | + if iszero(success) { |
| 74 | + returndatacopy(ptr, 0x00, returndatasize()) |
| 75 | + revert(ptr, returndatasize()) |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // ------------------------------------------------------------------------- |
| 82 | + // Functions - Internal |
| 83 | + |
| 84 | + /// @notice Called by `upgradeToAndCall` to check whether the `msg.sender` |
| 85 | + /// is authorized to perform the upgrade. |
| 86 | + /// |
| 87 | + /// @dev Override this function with your preferred access check. Example: |
| 88 | + /// |
| 89 | + /// ```solidity |
| 90 | + /// function _authorizeUpgrade(address) internal override onlyOwner {} |
| 91 | + /// ``` |
| 92 | + function _authorizeUpgrade(address newImplementation) internal virtual; |
| 93 | +} |
0 commit comments