Transpile 7bce2b72
This commit is contained in:
84
contracts/proxy/ClonesUpgradeable.sol
Normal file
84
contracts/proxy/ClonesUpgradeable.sol
Normal file
@ -0,0 +1,84 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts v4.4.1 (proxy/Clones.sol)
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
/**
|
||||
* @dev https://eips.ethereum.org/EIPS/eip-1167[EIP 1167] is a standard for
|
||||
* deploying minimal proxy contracts, also known as "clones".
|
||||
*
|
||||
* > To simply and cheaply clone contract functionality in an immutable way, this standard specifies
|
||||
* > a minimal bytecode implementation that delegates all calls to a known, fixed address.
|
||||
*
|
||||
* The library includes functions to deploy a proxy using either `create` (traditional deployment) or `create2`
|
||||
* (salted deterministic deployment). It also includes functions to predict the addresses of clones deployed using the
|
||||
* deterministic method.
|
||||
*
|
||||
* _Available since v3.4._
|
||||
*/
|
||||
library ClonesUpgradeable {
|
||||
/**
|
||||
* @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
|
||||
*
|
||||
* This function uses the create opcode, which should never revert.
|
||||
*/
|
||||
function clone(address implementation) internal returns (address instance) {
|
||||
assembly {
|
||||
let ptr := mload(0x40)
|
||||
mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
|
||||
mstore(add(ptr, 0x14), shl(0x60, implementation))
|
||||
mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
|
||||
instance := create(0, ptr, 0x37)
|
||||
}
|
||||
require(instance != address(0), "ERC1167: create failed");
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
|
||||
*
|
||||
* This function uses the create2 opcode and a `salt` to deterministically deploy
|
||||
* the clone. Using the same `implementation` and `salt` multiple time will revert, since
|
||||
* the clones cannot be deployed twice at the same address.
|
||||
*/
|
||||
function cloneDeterministic(address implementation, bytes32 salt) internal returns (address instance) {
|
||||
assembly {
|
||||
let ptr := mload(0x40)
|
||||
mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
|
||||
mstore(add(ptr, 0x14), shl(0x60, implementation))
|
||||
mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
|
||||
instance := create2(0, ptr, 0x37, salt)
|
||||
}
|
||||
require(instance != address(0), "ERC1167: create2 failed");
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
|
||||
*/
|
||||
function predictDeterministicAddress(
|
||||
address implementation,
|
||||
bytes32 salt,
|
||||
address deployer
|
||||
) internal pure returns (address predicted) {
|
||||
assembly {
|
||||
let ptr := mload(0x40)
|
||||
mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
|
||||
mstore(add(ptr, 0x14), shl(0x60, implementation))
|
||||
mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf3ff00000000000000000000000000000000)
|
||||
mstore(add(ptr, 0x38), shl(0x60, deployer))
|
||||
mstore(add(ptr, 0x4c), salt)
|
||||
mstore(add(ptr, 0x6c), keccak256(ptr, 0x37))
|
||||
predicted := keccak256(add(ptr, 0x37), 0x55)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
|
||||
*/
|
||||
function predictDeterministicAddress(address implementation, bytes32 salt)
|
||||
internal
|
||||
view
|
||||
returns (address predicted)
|
||||
{
|
||||
return predictDeterministicAddress(implementation, salt, address(this));
|
||||
}
|
||||
}
|
||||
207
contracts/proxy/ERC1967/ERC1967UpgradeUpgradeable.sol
Normal file
207
contracts/proxy/ERC1967/ERC1967UpgradeUpgradeable.sol
Normal file
@ -0,0 +1,207 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v4.5.0-rc.0) (proxy/ERC1967/ERC1967Upgrade.sol)
|
||||
|
||||
pragma solidity ^0.8.2;
|
||||
|
||||
import "../beacon/IBeaconUpgradeable.sol";
|
||||
import "../../interfaces/draft-IERC1822Upgradeable.sol";
|
||||
import "../../utils/AddressUpgradeable.sol";
|
||||
import "../../utils/StorageSlotUpgradeable.sol";
|
||||
import "../utils/Initializable.sol";
|
||||
|
||||
/**
|
||||
* @dev This abstract contract provides getters and event emitting update functions for
|
||||
* https://eips.ethereum.org/EIPS/eip-1967[EIP1967] slots.
|
||||
*
|
||||
* _Available since v4.1._
|
||||
*
|
||||
* @custom:oz-upgrades-unsafe-allow delegatecall
|
||||
*/
|
||||
abstract contract ERC1967UpgradeUpgradeable is Initializable {
|
||||
function __ERC1967Upgrade_init() internal onlyInitializing {
|
||||
__ERC1967Upgrade_init_unchained();
|
||||
}
|
||||
|
||||
function __ERC1967Upgrade_init_unchained() internal onlyInitializing {
|
||||
}
|
||||
// This is the keccak-256 hash of "eip1967.proxy.rollback" subtracted by 1
|
||||
bytes32 private constant _ROLLBACK_SLOT = 0x4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd9143;
|
||||
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
|
||||
|
||||
/**
|
||||
* @dev Emitted when the implementation is upgraded.
|
||||
*/
|
||||
event Upgraded(address indexed implementation);
|
||||
|
||||
/**
|
||||
* @dev Returns the current implementation address.
|
||||
*/
|
||||
function _getImplementation() internal view returns (address) {
|
||||
return StorageSlotUpgradeable.getAddressSlot(_IMPLEMENTATION_SLOT).value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Stores a new address in the EIP1967 implementation slot.
|
||||
*/
|
||||
function _setImplementation(address newImplementation) private {
|
||||
require(AddressUpgradeable.isContract(newImplementation), "ERC1967: new implementation is not a contract");
|
||||
StorageSlotUpgradeable.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Perform implementation upgrade
|
||||
*
|
||||
* Emits an {Upgraded} event.
|
||||
*/
|
||||
function _upgradeTo(address newImplementation) internal {
|
||||
_setImplementation(newImplementation);
|
||||
emit Upgraded(newImplementation);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Perform implementation upgrade with additional setup call.
|
||||
*
|
||||
* Emits an {Upgraded} event.
|
||||
*/
|
||||
function _upgradeToAndCall(
|
||||
address newImplementation,
|
||||
bytes memory data,
|
||||
bool forceCall
|
||||
) internal {
|
||||
_upgradeTo(newImplementation);
|
||||
if (data.length > 0 || forceCall) {
|
||||
_functionDelegateCall(newImplementation, data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Perform implementation upgrade with security checks for UUPS proxies, and additional setup call.
|
||||
*
|
||||
* Emits an {Upgraded} event.
|
||||
*/
|
||||
function _upgradeToAndCallUUPS(
|
||||
address newImplementation,
|
||||
bytes memory data,
|
||||
bool forceCall
|
||||
) internal {
|
||||
// Upgrades from old implementations will perform a rollback test. This test requires the new
|
||||
// implementation to upgrade back to the old, non-ERC1822 compliant, implementation. Removing
|
||||
// this special case will break upgrade paths from old UUPS implementation to new ones.
|
||||
if (StorageSlotUpgradeable.getBooleanSlot(_ROLLBACK_SLOT).value) {
|
||||
_setImplementation(newImplementation);
|
||||
} else {
|
||||
try IERC1822ProxiableUpgradeable(newImplementation).proxiableUUID() returns (bytes32 slot) {
|
||||
require(slot == _IMPLEMENTATION_SLOT, "ERC1967Upgrade: unsupported proxiableUUID");
|
||||
} catch {
|
||||
revert("ERC1967Upgrade: new implementation is not UUPS");
|
||||
}
|
||||
_upgradeToAndCall(newImplementation, data, forceCall);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
bytes32 internal constant _ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
|
||||
|
||||
/**
|
||||
* @dev Emitted when the admin account has changed.
|
||||
*/
|
||||
event AdminChanged(address previousAdmin, address newAdmin);
|
||||
|
||||
/**
|
||||
* @dev Returns the current admin.
|
||||
*/
|
||||
function _getAdmin() internal view returns (address) {
|
||||
return StorageSlotUpgradeable.getAddressSlot(_ADMIN_SLOT).value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Stores a new address in the EIP1967 admin slot.
|
||||
*/
|
||||
function _setAdmin(address newAdmin) private {
|
||||
require(newAdmin != address(0), "ERC1967: new admin is the zero address");
|
||||
StorageSlotUpgradeable.getAddressSlot(_ADMIN_SLOT).value = newAdmin;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Changes the admin of the proxy.
|
||||
*
|
||||
* Emits an {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.
|
||||
*/
|
||||
bytes32 internal constant _BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50;
|
||||
|
||||
/**
|
||||
* @dev Emitted when the beacon is upgraded.
|
||||
*/
|
||||
event BeaconUpgraded(address indexed beacon);
|
||||
|
||||
/**
|
||||
* @dev Returns the current beacon.
|
||||
*/
|
||||
function _getBeacon() internal view returns (address) {
|
||||
return StorageSlotUpgradeable.getAddressSlot(_BEACON_SLOT).value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Stores a new beacon in the EIP1967 beacon slot.
|
||||
*/
|
||||
function _setBeacon(address newBeacon) private {
|
||||
require(AddressUpgradeable.isContract(newBeacon), "ERC1967: new beacon is not a contract");
|
||||
require(
|
||||
AddressUpgradeable.isContract(IBeaconUpgradeable(newBeacon).implementation()),
|
||||
"ERC1967: beacon implementation is not a contract"
|
||||
);
|
||||
StorageSlotUpgradeable.getAddressSlot(_BEACON_SLOT).value = newBeacon;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Perform beacon upgrade with additional setup call. Note: This upgrades the address of the beacon, it does
|
||||
* not upgrade the implementation contained in the beacon (see {UpgradeableBeacon-_setImplementation} for that).
|
||||
*
|
||||
* Emits a {BeaconUpgraded} event.
|
||||
*/
|
||||
function _upgradeBeaconToAndCall(
|
||||
address newBeacon,
|
||||
bytes memory data,
|
||||
bool forceCall
|
||||
) internal {
|
||||
_setBeacon(newBeacon);
|
||||
emit BeaconUpgraded(newBeacon);
|
||||
if (data.length > 0 || forceCall) {
|
||||
_functionDelegateCall(IBeaconUpgradeable(newBeacon).implementation(), data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
|
||||
* but performing a delegate call.
|
||||
*
|
||||
* _Available since v3.4._
|
||||
*/
|
||||
function _functionDelegateCall(address target, bytes memory data) private returns (bytes memory) {
|
||||
require(AddressUpgradeable.isContract(target), "Address: delegate call to non-contract");
|
||||
|
||||
// solhint-disable-next-line avoid-low-level-calls
|
||||
(bool success, bytes memory returndata) = target.delegatecall(data);
|
||||
return AddressUpgradeable.verifyCallResult(success, returndata, "Address: low-level delegate call failed");
|
||||
}
|
||||
uint256[50] private __gap;
|
||||
}
|
||||
85
contracts/proxy/README.adoc
Normal file
85
contracts/proxy/README.adoc
Normal file
@ -0,0 +1,85 @@
|
||||
= Proxies
|
||||
|
||||
[.readme-notice]
|
||||
NOTE: This document is better viewed at https://docs.openzeppelin.com/contracts/api/proxy
|
||||
|
||||
This is a low-level set of contracts implementing different proxy patterns with and without upgradeability. For an in-depth overview of this pattern check out the xref:upgrades-plugins::proxies.adoc[Proxy Upgrade Pattern] page.
|
||||
|
||||
Most of the proxies below are built on an abstract base contract.
|
||||
|
||||
- {Proxy}: Abstract contract implementing the core delegation functionality.
|
||||
|
||||
In order to avoid clashes with the storage variables of the implementation contract behind a proxy, we use https://eips.ethereum.org/EIPS/eip-1967[EIP1967] storage slots.
|
||||
|
||||
- {ERC1967Upgrade}: Internal functions to get and set the storage slots defined in EIP1967.
|
||||
- {ERC1967Proxy}: A proxy using EIP1967 storage slots. Not upgradeable by default.
|
||||
|
||||
There are two alternative ways to add upgradeability to an ERC1967 proxy. Their differences are explained below in <<transparent-vs-uups>>.
|
||||
|
||||
- {TransparentUpgradeableProxy}: A proxy with a built in admin and upgrade interface.
|
||||
- {UUPSUpgradeable}: An upgradeability mechanism to be included in the implementation contract.
|
||||
|
||||
CAUTION: Using upgradeable proxies correctly and securely is a difficult task that requires deep knowledge of the proxy pattern, Solidity, and the EVM. Unless you want a lot of low level control, we recommend using the xref:upgrades-plugins::index.adoc[OpenZeppelin Upgrades Plugins] for Truffle and Hardhat.
|
||||
|
||||
A different family of proxies are beacon proxies. This pattern, popularized by Dharma, allows multiple proxies to be upgraded to a different implementation in a single transaction.
|
||||
|
||||
- {BeaconProxy}: A proxy that retreives its implementation from a beacon contract.
|
||||
- {UpgradeableBeacon}: A beacon contract with a built in admin that can upgrade the {BeaconProxy} pointing to it.
|
||||
|
||||
In this pattern, the proxy contract doesn't hold the implementation address in storage like an ERC1967 proxy, instead the address is stored in a separate beacon contract. The `upgrade` operations that are sent to the beacon instead of to the proxy contract, and all proxies that follow that beacon are automatically upgraded.
|
||||
|
||||
Outside the realm of upgradeability, proxies can also be useful to make cheap contract clones, such as those created by an on-chain factory contract that creates many instances of the same contract. These instances are designed to be both cheap to deploy, and cheap to call.
|
||||
|
||||
- {Clones}: A library that can deploy cheap minimal non-upgradeable proxies.
|
||||
|
||||
[[transparent-vs-uups]]
|
||||
== Transparent vs UUPS Proxies
|
||||
|
||||
The original proxies included in OpenZeppelin followed the https://blog.openzeppelin.com/the-transparent-proxy-pattern/[Transparent Proxy Pattern]. While this pattern is still provided, our recommendation is now shifting towards UUPS proxies, which are both lightweight and versatile. The name UUPS comes from https://eips.ethereum.org/EIPS/eip-1822[EIP1822], which first documented the pattern.
|
||||
|
||||
While both of these share the same interface for upgrades, in UUPS proxies the upgrade is handled by the implementation, and can eventually be removed. Transparent proxies, on the other hand, include the upgrade and admin logic in the proxy itself. This means {TransparentUpgradeableProxy} is more expensive to deploy than what is possible with UUPS proxies.
|
||||
|
||||
UUPS proxies are implemented using an {ERC1967Proxy}. Note that this proxy is not by itself upgradeable. It is the role of the implementation to include, alongside the contract's logic, all the code necessary to update the implementation's address that is stored at a specific slot in the proxy's storage space. This is where the {UUPSUpgradeable} contract comes in. Inheriting from it (and overriding the {xref-UUPSUpgradeable-_authorizeUpgrade-address-}[`_authorizeUpgrade`] function with the relevant access control mechanism) will turn your contract into a UUPS compliant implementation.
|
||||
|
||||
Note that since both proxies use the same storage slot for the implementation address, using a UUPS compliant implementation with a {TransparentUpgradeableProxy} might allow non-admins to perform upgrade operations.
|
||||
|
||||
By default, the upgrade functionality included in {UUPSUpgradeable} contains a security mechanism that will prevent any upgrades to a non UUPS compliant implementation. This prevents upgrades to an implementation contract that wouldn't contain the necessary upgrade mechanism, as it would lock the upgradeability of the proxy forever. This security mechanism can be bypassed by either of:
|
||||
|
||||
- Adding a flag mechanism in the implementation that will disable the upgrade function when triggered.
|
||||
- Upgrading to an implementation that features an upgrade mechanism without the additional security check, and then upgrading again to another implementation without the upgrade mechanism.
|
||||
|
||||
The current implementation of this security mechanism uses https://eips.ethereum.org/EIPS/eip-1822[EIP1822] to detect the storage slot used by the implementation. A previous implementation, now deprecated, relied on a rollback check. It is possible to upgrade from a contract using the old mechanism to a new one. The inverse is however not possible, as old implementations (before version 4.5) did not include the `ERC1822` interface.
|
||||
|
||||
== Core
|
||||
|
||||
{{Proxy}}
|
||||
|
||||
== ERC1967
|
||||
|
||||
{{ERC1967Proxy}}
|
||||
|
||||
{{ERC1967Upgrade}}
|
||||
|
||||
== Transparent Proxy
|
||||
|
||||
{{TransparentUpgradeableProxy}}
|
||||
|
||||
{{ProxyAdmin}}
|
||||
|
||||
== Beacon
|
||||
|
||||
{{BeaconProxy}}
|
||||
|
||||
{{IBeacon}}
|
||||
|
||||
{{UpgradeableBeacon}}
|
||||
|
||||
== Minimal Clones
|
||||
|
||||
{{Clones}}
|
||||
|
||||
== Utils
|
||||
|
||||
{{Initializable}}
|
||||
|
||||
{{UUPSUpgradeable}}
|
||||
16
contracts/proxy/beacon/IBeaconUpgradeable.sol
Normal file
16
contracts/proxy/beacon/IBeaconUpgradeable.sol
Normal file
@ -0,0 +1,16 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts v4.4.1 (proxy/beacon/IBeacon.sol)
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
/**
|
||||
* @dev This is the interface that {BeaconProxy} expects of its beacon.
|
||||
*/
|
||||
interface IBeaconUpgradeable {
|
||||
/**
|
||||
* @dev Must return an address that can be used as a delegate call target.
|
||||
*
|
||||
* {BeaconProxy} will check that this address is a contract.
|
||||
*/
|
||||
function implementation() external view returns (address);
|
||||
}
|
||||
80
contracts/proxy/utils/Initializable.sol
Normal file
80
contracts/proxy/utils/Initializable.sol
Normal file
@ -0,0 +1,80 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v4.5.0-rc.0) (proxy/utils/Initializable.sol)
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "../../utils/AddressUpgradeable.sol";
|
||||
|
||||
/**
|
||||
* @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed
|
||||
* behind a proxy. Since proxied contracts do not make use of a constructor, it's common to move constructor logic to an
|
||||
* external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
|
||||
* function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
|
||||
*
|
||||
* TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
|
||||
* possible by providing the encoded function call as the `_data` argument to {ERC1967Proxy-constructor}.
|
||||
*
|
||||
* CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
|
||||
* that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
|
||||
*
|
||||
* [CAUTION]
|
||||
* ====
|
||||
* Avoid leaving a contract uninitialized.
|
||||
*
|
||||
* An uninitialized contract can be taken over by an attacker. This applies to both a proxy and its implementation
|
||||
* contract, which may impact the proxy. To initialize the implementation contract, you can either invoke the
|
||||
* initializer manually, or you can include a constructor to automatically mark it as initialized when it is deployed:
|
||||
*
|
||||
* [.hljs-theme-light.nopadding]
|
||||
* ```
|
||||
* /// @custom:oz-upgrades-unsafe-allow constructor
|
||||
* constructor() initializer {}
|
||||
* ```
|
||||
* ====
|
||||
*/
|
||||
abstract contract Initializable {
|
||||
/**
|
||||
* @dev Indicates that the contract has been initialized.
|
||||
*/
|
||||
bool private _initialized;
|
||||
|
||||
/**
|
||||
* @dev Indicates that the contract is in the process of being initialized.
|
||||
*/
|
||||
bool private _initializing;
|
||||
|
||||
/**
|
||||
* @dev Modifier to protect an initializer function from being invoked twice.
|
||||
*/
|
||||
modifier initializer() {
|
||||
// If the contract is initializing we ignore whether _initialized is set in order to support multiple
|
||||
// inheritance patterns, but we only do this in the context of a constructor, because in other contexts the
|
||||
// contract may have been reentered.
|
||||
require(_initializing ? _isConstructor() : !_initialized, "Initializable: contract is already initialized");
|
||||
|
||||
bool isTopLevelCall = !_initializing;
|
||||
if (isTopLevelCall) {
|
||||
_initializing = true;
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
_;
|
||||
|
||||
if (isTopLevelCall) {
|
||||
_initializing = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Modifier to protect an initialization function so that it can only be invoked by functions with the
|
||||
* {initializer} modifier, directly or indirectly.
|
||||
*/
|
||||
modifier onlyInitializing() {
|
||||
require(_initializing, "Initializable: contract is not initializing");
|
||||
_;
|
||||
}
|
||||
|
||||
function _isConstructor() private view returns (bool) {
|
||||
return !AddressUpgradeable.isContract(address(this));
|
||||
}
|
||||
}
|
||||
104
contracts/proxy/utils/UUPSUpgradeable.sol
Normal file
104
contracts/proxy/utils/UUPSUpgradeable.sol
Normal file
@ -0,0 +1,104 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v4.5.0-rc.0) (proxy/utils/UUPSUpgradeable.sol)
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "../../interfaces/draft-IERC1822Upgradeable.sol";
|
||||
import "../ERC1967/ERC1967UpgradeUpgradeable.sol";
|
||||
import "./Initializable.sol";
|
||||
|
||||
/**
|
||||
* @dev An upgradeability mechanism designed for UUPS proxies. The functions included here can perform an upgrade of an
|
||||
* {ERC1967Proxy}, when this contract is set as the implementation behind such a proxy.
|
||||
*
|
||||
* A security mechanism ensures that an upgrade does not turn off upgradeability accidentally, although this risk is
|
||||
* reinstated if the upgrade retains upgradeability but removes the security mechanism, e.g. by replacing
|
||||
* `UUPSUpgradeable` with a custom implementation of upgrades.
|
||||
*
|
||||
* The {_authorizeUpgrade} function must be overridden to include access restriction to the upgrade mechanism.
|
||||
*
|
||||
* _Available since v4.1._
|
||||
*/
|
||||
abstract contract UUPSUpgradeable is Initializable, IERC1822ProxiableUpgradeable, ERC1967UpgradeUpgradeable {
|
||||
function __UUPSUpgradeable_init() internal onlyInitializing {
|
||||
__ERC1967Upgrade_init_unchained();
|
||||
__UUPSUpgradeable_init_unchained();
|
||||
}
|
||||
|
||||
function __UUPSUpgradeable_init_unchained() internal onlyInitializing {
|
||||
}
|
||||
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable state-variable-assignment
|
||||
address private immutable __self = address(this);
|
||||
|
||||
/**
|
||||
* @dev Check that the execution is being performed through a delegatecall call and that the execution context is
|
||||
* a proxy contract with an implementation (as defined in ERC1967) pointing to self. This should only be the case
|
||||
* for UUPS and transparent proxies that are using the current contract as their implementation. Execution of a
|
||||
* function through ERC1167 minimal proxies (clones) would not normally pass this test, but is not guaranteed to
|
||||
* fail.
|
||||
*/
|
||||
modifier onlyProxy() {
|
||||
require(address(this) != __self, "Function must be called through delegatecall");
|
||||
require(_getImplementation() == __self, "Function must be called through active proxy");
|
||||
_;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Check that the execution is not being performed through a delegate call. This allows a function to be
|
||||
* callable on the implementing contract but not through proxies.
|
||||
*/
|
||||
modifier notDelegated() {
|
||||
require(address(this) == __self, "UUPSUpgradeable: must not be called through delegatecall");
|
||||
_;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Implementation of the ERC1822 {proxiableUUID} function. This returns the storage slot used by the
|
||||
* implementation. It is used to validate that the this implementation remains valid after an upgrade.
|
||||
*
|
||||
* IMPORTANT: A proxy pointing at a proxiable contract should not be considered proxiable itself, because this risks
|
||||
* bricking a proxy that upgrades to it, by delegating to itself until out of gas. Thus it is critical that this
|
||||
* function revert if invoked through a proxy. This is guaranteed by the `notDelegated` modifier.
|
||||
*/
|
||||
function proxiableUUID() external view virtual override notDelegated returns (bytes32) {
|
||||
return _IMPLEMENTATION_SLOT;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Upgrade the implementation of the proxy to `newImplementation`.
|
||||
*
|
||||
* Calls {_authorizeUpgrade}.
|
||||
*
|
||||
* Emits an {Upgraded} event.
|
||||
*/
|
||||
function upgradeTo(address newImplementation) external virtual onlyProxy {
|
||||
_authorizeUpgrade(newImplementation);
|
||||
_upgradeToAndCallUUPS(newImplementation, new bytes(0), false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Upgrade the implementation of the proxy to `newImplementation`, and subsequently execute the function call
|
||||
* encoded in `data`.
|
||||
*
|
||||
* Calls {_authorizeUpgrade}.
|
||||
*
|
||||
* Emits an {Upgraded} event.
|
||||
*/
|
||||
function upgradeToAndCall(address newImplementation, bytes memory data) external payable virtual onlyProxy {
|
||||
_authorizeUpgrade(newImplementation);
|
||||
_upgradeToAndCallUUPS(newImplementation, data, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Function that should revert when `msg.sender` is not authorized to upgrade the contract. Called by
|
||||
* {upgradeTo} and {upgradeToAndCall}.
|
||||
*
|
||||
* Normally, this function will use an xref:access.adoc[access control] modifier such as {Ownable-onlyOwner}.
|
||||
*
|
||||
* ```solidity
|
||||
* function _authorizeUpgrade(address) internal override onlyOwner {}
|
||||
* ```
|
||||
*/
|
||||
function _authorizeUpgrade(address newImplementation) internal virtual;
|
||||
uint256[50] private __gap;
|
||||
}
|
||||
Reference in New Issue
Block a user