You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
179 lines
6.1 KiB
179 lines
6.1 KiB
// SPDX-License-Identifier: MIT
|
|
// OpenZeppelin Contracts (last updated v4.9.0) (proxy/ERC1967/ERC1967Utils.sol)
|
|
|
|
pragma solidity ^0.8.20;
|
|
|
|
import {IBeacon} from "../beacon/IBeacon.sol";
|
|
import {Address} from "../../utils/Address.sol";
|
|
import {StorageSlot} from "../../utils/StorageSlot.sol";
|
|
|
|
/**
|
|
* @dev This abstract contract provides getters and event emitting update functions for
|
|
* https://eips.ethereum.org/EIPS/eip-1967[EIP1967] slots.
|
|
*/
|
|
library ERC1967Utils {
|
|
// We re-declare ERC-1967 events here because they can't be used directly from IERC1967.
|
|
// This will be fixed in Solidity 0.8.21. At that point we should remove these events.
|
|
/**
|
|
* @dev Emitted when the implementation is upgraded.
|
|
*/
|
|
event Upgraded(address indexed implementation);
|
|
|
|
/**
|
|
* @dev Emitted when the admin account has changed.
|
|
*/
|
|
event AdminChanged(address previousAdmin, address newAdmin);
|
|
|
|
/**
|
|
* @dev Emitted when the beacon is changed.
|
|
*/
|
|
event BeaconUpgraded(address indexed beacon);
|
|
|
|
/**
|
|
* @dev Storage slot with the address of the current implementation.
|
|
* This is the keccak-256 hash of "eip1967.proxy.implementation" subtracted by 1, and is
|
|
* validated in the constructor.
|
|
*/
|
|
// 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 Returns the current implementation address.
|
|
*/
|
|
function getImplementation() internal view returns (address) {
|
|
return StorageSlot.getAddressSlot(IMPLEMENTATION_SLOT).value;
|
|
}
|
|
|
|
/**
|
|
* @dev Stores a new address in the EIP1967 implementation slot.
|
|
*/
|
|
function _setImplementation(address newImplementation) private {
|
|
if (newImplementation.code.length == 0) {
|
|
revert ERC1967InvalidImplementation(newImplementation);
|
|
}
|
|
StorageSlot.getAddressSlot(IMPLEMENTATION_SLOT).value = newImplementation;
|
|
}
|
|
|
|
/**
|
|
* @dev Perform implementation upgrade
|
|
*
|
|
* Emits an {IERC1967-Upgraded} event.
|
|
*/
|
|
function upgradeTo(address newImplementation) internal {
|
|
_setImplementation(newImplementation);
|
|
emit Upgraded(newImplementation);
|
|
}
|
|
|
|
/**
|
|
* @dev Perform implementation upgrade with additional setup call.
|
|
*
|
|
* Emits an {IERC1967-Upgraded} event.
|
|
*/
|
|
function upgradeToAndCall(address newImplementation, bytes memory data, bool forceCall) internal {
|
|
upgradeTo(newImplementation);
|
|
if (data.length > 0 || forceCall) {
|
|
Address.functionDelegateCall(newImplementation, data);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @dev Storage slot with the admin of the contract.
|
|
* This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1, and is
|
|
* validated in the constructor.
|
|
*/
|
|
// 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 EIP1967) 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 EIP1967 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 AdminChanged(getAdmin(), newAdmin);
|
|
_setAdmin(newAdmin);
|
|
}
|
|
|
|
/**
|
|
* @dev The storage slot of the UpgradeableBeacon contract which defines the implementation for this proxy.
|
|
* This is bytes32(uint256(keccak256('eip1967.proxy.beacon')) - 1) and is validated in the constructor.
|
|
*/
|
|
// 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 EIP1967 beacon slot.
|
|
*/
|
|
function _setBeacon(address newBeacon) private {
|
|
if (newBeacon.code.length == 0) {
|
|
revert ERC1967InvalidBeacon(newBeacon);
|
|
}
|
|
|
|
address beaconImplementation = IBeacon(newBeacon).implementation();
|
|
if (beaconImplementation.code.length == 0) {
|
|
revert ERC1967InvalidImplementation(beaconImplementation);
|
|
}
|
|
|
|
StorageSlot.getAddressSlot(BEACON_SLOT).value = newBeacon;
|
|
}
|
|
|
|
/**
|
|
* @dev Change the beacon and trigger a setup call.
|
|
*
|
|
* 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, bool forceCall) internal {
|
|
_setBeacon(newBeacon);
|
|
emit BeaconUpgraded(newBeacon);
|
|
if (data.length > 0 || forceCall) {
|
|
Address.functionDelegateCall(IBeacon(newBeacon).implementation(), data);
|
|
}
|
|
}
|
|
}
|
|
|