123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- // SPDX-License-Identifier: MIT
- // OpenZeppelin Contracts (last updated v5.1.0) (proxy/ERC1967/ERC1967Utils.sol)
- pragma solidity ^0.8.22;
- import {IBeacon} from "../beacon/IBeacon.sol";
- import {IERC1967} from "../../interfaces/IERC1967.sol";
- import {Address} from "../../utils/Address.sol";
- import {StorageSlot} from "../../utils/StorageSlot.sol";
- /**
- * @dev This library provides getters and event emitting update functions for
- * https://eips.ethereum.org/EIPS/eip-1967[ERC-1967] slots.
- */
- library ERC1967Utils {
- /**
- * @dev Storage slot with the address of the current implementation.
- * This is the keccak-256 hash of "eip1967.proxy.implementation" subtracted by 1.
- */
- // solhint-disable-next-line private-vars-leading-underscore
- bytes32 internal constant IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
- /**
- * @dev The `implementation` of the proxy is invalid.
- */
- error ERC1967InvalidImplementation(address implementation);
- /**
- * @dev The `admin` of the proxy is invalid.
- */
- error ERC1967InvalidAdmin(address admin);
- /**
- * @dev The `beacon` of the proxy is invalid.
- */
- error ERC1967InvalidBeacon(address beacon);
- /**
- * @dev An upgrade function sees `msg.value > 0` that may be lost.
- */
- error ERC1967NonPayable();
- /**
- * @dev Returns the current implementation address.
- */
- function getImplementation() internal view returns (address) {
- return StorageSlot.getAddressSlot(IMPLEMENTATION_SLOT).value;
- }
- /**
- * @dev Stores a new address in the ERC-1967 implementation slot.
- */
- function _setImplementation(address newImplementation) private {
- if (newImplementation.code.length == 0) {
- revert ERC1967InvalidImplementation(newImplementation);
- }
- StorageSlot.getAddressSlot(IMPLEMENTATION_SLOT).value = newImplementation;
- }
- /**
- * @dev Performs implementation upgrade with additional setup call if data is nonempty.
- * This function is payable only if the setup call is performed, otherwise `msg.value` is rejected
- * to avoid stuck value in the contract.
- *
- * Emits an {IERC1967-Upgraded} event.
- */
- function upgradeToAndCall(address newImplementation, bytes memory data) internal {
- _setImplementation(newImplementation);
- emit IERC1967.Upgraded(newImplementation);
- if (data.length > 0) {
- Address.functionDelegateCall(newImplementation, data);
- } else {
- _checkNonPayable();
- }
- }
- /**
- * @dev Storage slot with the admin of the contract.
- * This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1.
- */
- // solhint-disable-next-line private-vars-leading-underscore
- bytes32 internal constant ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
- /**
- * @dev Returns the current admin.
- *
- * TIP: To get this value clients can read directly from the storage slot shown below (specified by ERC-1967) using
- * the https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
- * `0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103`
- */
- function getAdmin() internal view returns (address) {
- return StorageSlot.getAddressSlot(ADMIN_SLOT).value;
- }
- /**
- * @dev Stores a new address in the ERC-1967 admin slot.
- */
- function _setAdmin(address newAdmin) private {
- if (newAdmin == address(0)) {
- revert ERC1967InvalidAdmin(address(0));
- }
- StorageSlot.getAddressSlot(ADMIN_SLOT).value = newAdmin;
- }
- /**
- * @dev Changes the admin of the proxy.
- *
- * Emits an {IERC1967-AdminChanged} event.
- */
- function changeAdmin(address newAdmin) internal {
- emit IERC1967.AdminChanged(getAdmin(), newAdmin);
- _setAdmin(newAdmin);
- }
- /**
- * @dev The storage slot of the UpgradeableBeacon contract which defines the implementation for this proxy.
- * This is the keccak-256 hash of "eip1967.proxy.beacon" subtracted by 1.
- */
- // solhint-disable-next-line private-vars-leading-underscore
- bytes32 internal constant BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50;
- /**
- * @dev Returns the current beacon.
- */
- function getBeacon() internal view returns (address) {
- return StorageSlot.getAddressSlot(BEACON_SLOT).value;
- }
- /**
- * @dev Stores a new beacon in the ERC-1967 beacon slot.
- */
- function _setBeacon(address newBeacon) private {
- if (newBeacon.code.length == 0) {
- revert ERC1967InvalidBeacon(newBeacon);
- }
- StorageSlot.getAddressSlot(BEACON_SLOT).value = newBeacon;
- address beaconImplementation = IBeacon(newBeacon).implementation();
- if (beaconImplementation.code.length == 0) {
- revert ERC1967InvalidImplementation(beaconImplementation);
- }
- }
- /**
- * @dev Change the beacon and trigger a setup call if data is nonempty.
- * This function is payable only if the setup call is performed, otherwise `msg.value` is rejected
- * to avoid stuck value in the contract.
- *
- * Emits an {IERC1967-BeaconUpgraded} event.
- *
- * CAUTION: Invoking this function has no effect on an instance of {BeaconProxy} since v5, since
- * it uses an immutable beacon without looking at the value of the ERC-1967 beacon slot for
- * efficiency.
- */
- function upgradeBeaconToAndCall(address newBeacon, bytes memory data) internal {
- _setBeacon(newBeacon);
- emit IERC1967.BeaconUpgraded(newBeacon);
- if (data.length > 0) {
- Address.functionDelegateCall(IBeacon(newBeacon).implementation(), data);
- } else {
- _checkNonPayable();
- }
- }
- /**
- * @dev Reverts if `msg.value` is not zero. It can be used to avoid `msg.value` stuck in the contract
- * if an upgrade doesn't perform an initialization call.
- */
- function _checkNonPayable() private {
- if (msg.value > 0) {
- revert ERC1967NonPayable();
- }
- }
- }
|