Merge branch 'master'
This commit is contained in:
86
contracts/proxy/BeaconProxy.sol
Normal file
86
contracts/proxy/BeaconProxy.sol
Normal file
@ -0,0 +1,86 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
pragma solidity >=0.6.0 <0.8.0;
|
||||
|
||||
import "./Proxy.sol";
|
||||
import "../utils/Address.sol";
|
||||
import "./IBeacon.sol";
|
||||
|
||||
/**
|
||||
* @dev This contract implements a proxy that gets the implementation address for each call from a {UpgradeableBeacon}.
|
||||
*
|
||||
* The beacon address is stored in storage slot `uint256(keccak256('eip1967.proxy.beacon')) - 1`, so that it doesn't
|
||||
* conflict with the storage layout of the implementation behind the proxy.
|
||||
*/
|
||||
contract BeaconProxy is Proxy {
|
||||
/**
|
||||
* @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 private constant _BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50;
|
||||
|
||||
/**
|
||||
* @dev Initializes the proxy with `beacon`.
|
||||
*
|
||||
* If `data` is nonempty, it's used as data in a delegate call to the implementation returned by the beacon. This
|
||||
* will typically be an encoded function call, and allows initializating the storage of the proxy like a Solidity
|
||||
* constructor.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - `beacon` must be a contract with the interface {IBeacon}.
|
||||
*/
|
||||
constructor(address beacon, bytes memory data) payable {
|
||||
assert(_BEACON_SLOT == bytes32(uint256(keccak256("eip1967.proxy.beacon")) - 1));
|
||||
_setBeacon(beacon, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the current beacon address.
|
||||
*/
|
||||
function _beacon() internal view virtual returns (address beacon) {
|
||||
bytes32 slot = _BEACON_SLOT;
|
||||
// solhint-disable-next-line no-inline-assembly
|
||||
assembly {
|
||||
beacon := sload(slot)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the current implementation address of the associated beacon.
|
||||
*/
|
||||
function _implementation() internal view virtual override returns (address) {
|
||||
return IBeacon(_beacon()).implementation();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Changes the proxy to use a new beacon.
|
||||
*
|
||||
* If `data` is nonempty, it's used as data in a delegate call to the implementation returned by the beacon.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - `beacon` must be a contract.
|
||||
* - The implementation returned by `beacon` must be a contract.
|
||||
*/
|
||||
function _setBeacon(address beacon, bytes memory data) internal virtual {
|
||||
require(
|
||||
Address.isContract(beacon),
|
||||
"BeaconProxy: beacon is not a contract"
|
||||
);
|
||||
require(
|
||||
Address.isContract(IBeacon(beacon).implementation()),
|
||||
"BeaconProxy: beacon implementation is not a contract"
|
||||
);
|
||||
bytes32 slot = _BEACON_SLOT;
|
||||
|
||||
// solhint-disable-next-line no-inline-assembly
|
||||
assembly {
|
||||
sstore(slot, beacon)
|
||||
}
|
||||
|
||||
if (data.length > 0) {
|
||||
Address.functionDelegateCall(_implementation(), data, "BeaconProxy: function call failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
76
contracts/proxy/Clones.sol
Normal file
76
contracts/proxy/Clones.sol
Normal file
@ -0,0 +1,76 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
pragma solidity >=0.6.0 <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.
|
||||
*/
|
||||
library Clones {
|
||||
/**
|
||||
* @dev Deploys and returns the address of a clone that mimics the behaviour of `master`.
|
||||
*
|
||||
* This function uses the create opcode, which should never revert.
|
||||
*/
|
||||
function clone(address master) internal returns (address instance) {
|
||||
// solhint-disable-next-line no-inline-assembly
|
||||
assembly {
|
||||
let ptr := mload(0x40)
|
||||
mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
|
||||
mstore(add(ptr, 0x14), shl(0x60, master))
|
||||
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 `master`.
|
||||
*
|
||||
* This function uses the create2 opcode and a `salt` to deterministically deploy
|
||||
* the clone. Using the same `master` and `salt` multiple time will revert, since
|
||||
* the clones cannot be deployed twice at the same address.
|
||||
*/
|
||||
function cloneDeterministic(address master, bytes32 salt) internal returns (address instance) {
|
||||
// solhint-disable-next-line no-inline-assembly
|
||||
assembly {
|
||||
let ptr := mload(0x40)
|
||||
mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
|
||||
mstore(add(ptr, 0x14), shl(0x60, master))
|
||||
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 master, bytes32 salt, address deployer) internal pure returns (address predicted) {
|
||||
// solhint-disable-next-line no-inline-assembly
|
||||
assembly {
|
||||
let ptr := mload(0x40)
|
||||
mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
|
||||
mstore(add(ptr, 0x14), shl(0x60, master))
|
||||
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 master, bytes32 salt) internal view returns (address predicted) {
|
||||
return predictDeterministicAddress(master, salt, address(this));
|
||||
}
|
||||
}
|
||||
15
contracts/proxy/IBeacon.sol
Normal file
15
contracts/proxy/IBeacon.sol
Normal file
@ -0,0 +1,15 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
pragma solidity >=0.6.0 <0.8.0;
|
||||
|
||||
/**
|
||||
* @dev This is the interface that {BeaconProxy} expects of its beacon.
|
||||
*/
|
||||
interface IBeacon {
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
@ -3,16 +3,17 @@
|
||||
// solhint-disable-next-line compiler-version
|
||||
pragma solidity >=0.4.24 <0.8.0;
|
||||
|
||||
import "../utils/Address.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 a proxied contract can't have 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 {UpgradeableProxy-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.
|
||||
*/
|
||||
@ -49,15 +50,6 @@ abstract contract Initializable {
|
||||
|
||||
/// @dev Returns true if and only if the function is running in the constructor
|
||||
function _isConstructor() private view returns (bool) {
|
||||
// extcodesize checks the size of the code stored in an address, and
|
||||
// address returns the current address. Since the code is still not
|
||||
// deployed when running a constructor, any checks on its code size will
|
||||
// yield zero, making it an effective way to detect if a contract is
|
||||
// under construction or not.
|
||||
address self = address(this);
|
||||
uint256 cs;
|
||||
// solhint-disable-next-line no-inline-assembly
|
||||
assembly { cs := extcodesize(self) }
|
||||
return cs == 0;
|
||||
return !Address.isContract(address(this));
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,19 +6,19 @@ pragma solidity ^0.7.0;
|
||||
* @dev This abstract contract provides a fallback function that delegates all calls to another contract using the EVM
|
||||
* instruction `delegatecall`. We refer to the second contract as the _implementation_ behind the proxy, and it has to
|
||||
* be specified by overriding the virtual {_implementation} function.
|
||||
*
|
||||
*
|
||||
* Additionally, delegation to the implementation can be triggered manually through the {_fallback} function, or to a
|
||||
* different contract through the {_delegate} function.
|
||||
*
|
||||
*
|
||||
* The success and return data of the delegated call will be returned back to the caller of the proxy.
|
||||
*/
|
||||
abstract contract Proxy {
|
||||
/**
|
||||
* @dev Delegates the current call to `implementation`.
|
||||
*
|
||||
*
|
||||
* This function does not return to its internall call site, it will return directly to the external caller.
|
||||
*/
|
||||
function _delegate(address implementation) internal {
|
||||
function _delegate(address implementation) internal virtual {
|
||||
// solhint-disable-next-line no-inline-assembly
|
||||
assembly {
|
||||
// Copy msg.data. We take full control of memory in this inline assembly
|
||||
@ -44,14 +44,14 @@ abstract contract Proxy {
|
||||
* @dev This is a virtual function that should be overriden so it returns the address to which the fallback function
|
||||
* and {_fallback} should delegate.
|
||||
*/
|
||||
function _implementation() internal virtual view returns (address);
|
||||
function _implementation() internal view virtual returns (address);
|
||||
|
||||
/**
|
||||
* @dev Delegates the current call to the address returned by `_implementation()`.
|
||||
*
|
||||
*
|
||||
* This function does not return to its internall call site, it will return directly to the external caller.
|
||||
*/
|
||||
function _fallback() internal {
|
||||
function _fallback() internal virtual {
|
||||
_beforeFallback();
|
||||
_delegate(_implementation());
|
||||
}
|
||||
@ -60,7 +60,7 @@ abstract contract Proxy {
|
||||
* @dev Fallback function that delegates calls to the address returned by `_implementation()`. Will run if no other
|
||||
* function in the contract matches the call data.
|
||||
*/
|
||||
fallback () external payable {
|
||||
fallback () external payable virtual {
|
||||
_fallback();
|
||||
}
|
||||
|
||||
@ -68,14 +68,14 @@ abstract contract Proxy {
|
||||
* @dev Fallback function that delegates calls to the address returned by `_implementation()`. Will run if call data
|
||||
* is empty.
|
||||
*/
|
||||
receive () external payable {
|
||||
receive () external payable virtual {
|
||||
_fallback();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Hook that is called before falling back to the implementation. Can happen as part of a manual `_fallback`
|
||||
* call, or as part of the Solidity `fallback` or `receive` functions.
|
||||
*
|
||||
*
|
||||
* If overriden should call `super._beforeFallback()`.
|
||||
*/
|
||||
function _beforeFallback() internal virtual {
|
||||
|
||||
@ -13,12 +13,12 @@ contract ProxyAdmin is Ownable {
|
||||
|
||||
/**
|
||||
* @dev Returns the current implementation of `proxy`.
|
||||
*
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
*
|
||||
* - This contract must be the admin of `proxy`.
|
||||
*/
|
||||
function getProxyImplementation(TransparentUpgradeableProxy proxy) public view returns (address) {
|
||||
function getProxyImplementation(TransparentUpgradeableProxy proxy) public view virtual returns (address) {
|
||||
// We need to manually run the static call since the getter cannot be flagged as view
|
||||
// bytes4(keccak256("implementation()")) == 0x5c60da1b
|
||||
(bool success, bytes memory returndata) = address(proxy).staticcall(hex"5c60da1b");
|
||||
@ -28,12 +28,12 @@ contract ProxyAdmin is Ownable {
|
||||
|
||||
/**
|
||||
* @dev Returns the current admin of `proxy`.
|
||||
*
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
*
|
||||
* - This contract must be the admin of `proxy`.
|
||||
*/
|
||||
function getProxyAdmin(TransparentUpgradeableProxy proxy) public view returns (address) {
|
||||
function getProxyAdmin(TransparentUpgradeableProxy proxy) public view virtual returns (address) {
|
||||
// We need to manually run the static call since the getter cannot be flagged as view
|
||||
// bytes4(keccak256("admin()")) == 0xf851a440
|
||||
(bool success, bytes memory returndata) = address(proxy).staticcall(hex"f851a440");
|
||||
@ -43,35 +43,35 @@ contract ProxyAdmin is Ownable {
|
||||
|
||||
/**
|
||||
* @dev Changes the admin of `proxy` to `newAdmin`.
|
||||
*
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
*
|
||||
* - This contract must be the current admin of `proxy`.
|
||||
*/
|
||||
function changeProxyAdmin(TransparentUpgradeableProxy proxy, address newAdmin) public onlyOwner {
|
||||
function changeProxyAdmin(TransparentUpgradeableProxy proxy, address newAdmin) public virtual onlyOwner {
|
||||
proxy.changeAdmin(newAdmin);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Upgrades `proxy` to `implementation`. See {TransparentUpgradeableProxy-upgradeTo}.
|
||||
*
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
*
|
||||
* - This contract must be the admin of `proxy`.
|
||||
*/
|
||||
function upgrade(TransparentUpgradeableProxy proxy, address implementation) public onlyOwner {
|
||||
function upgrade(TransparentUpgradeableProxy proxy, address implementation) public virtual onlyOwner {
|
||||
proxy.upgradeTo(implementation);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Upgrades `proxy` to `implementation` and calls a function on the new implementation. See
|
||||
* {TransparentUpgradeableProxy-upgradeToAndCall}.
|
||||
*
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
*
|
||||
* - This contract must be the admin of `proxy`.
|
||||
*/
|
||||
function upgradeAndCall(TransparentUpgradeableProxy proxy, address implementation, bytes memory data) public payable onlyOwner {
|
||||
function upgradeAndCall(TransparentUpgradeableProxy proxy, address implementation, bytes memory data) public payable virtual onlyOwner {
|
||||
proxy.upgradeToAndCall{value: msg.value}(implementation, data);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,12 +3,16 @@
|
||||
[.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 the proxy pattern for upgradeability. For an in-depth overview of this pattern check out the xref:upgrades-plugins::proxies.adoc[Proxy Upgrade Pattern] page.
|
||||
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.
|
||||
|
||||
The abstract {Proxy} contract implements the core delegation functionality. If the concrete proxies that we provide below are not suitable, we encourage building on top of this base contract since it contains an assembly block that may be hard to get right.
|
||||
|
||||
Upgradeability is implemented in the {UpgradeableProxy} contract, although it provides only an internal upgrade interface. For an upgrade interface exposed externally to an admin, we provide {TransparentUpgradeableProxy}. Both of these contracts use the storage slots specified in https://eips.ethereum.org/EIPS/eip-1967[EIP1967] to avoid clashes with the storage of the implementation contract behind the proxy.
|
||||
|
||||
An alternative upgradeability mechanism is provided in <<Beacon>>. This pattern, popularized by Dharma, allows multiple proxies to be upgraded to a different implementation in a single transaction. In this pattern, the proxy contract doesn't hold the implementation address in storage like {UpgradeableProxy}, but the address of a {UpgradeableBeacon} contract, which is where the implementation address is actually stored and retrieved from. The `upgrade` operations that change the implementation contract address are then sent to the beacon instead of to the proxy contract, and all proxies that follow that beacon are automatically upgraded.
|
||||
|
||||
The {Clones} library provides a way to deploy minimal non-upgradeable proxies for cheap. This can be useful for applications that require deploying many instances of the same contract (for example one per user, or one per task). These instances are designed to be both cheap to deploy, and cheap to call. The drawback being that they are not upgradeable.
|
||||
|
||||
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 Buidler.
|
||||
|
||||
== Core
|
||||
@ -19,6 +23,18 @@ CAUTION: Using upgradeable proxies correctly and securely is a difficult task th
|
||||
|
||||
{{TransparentUpgradeableProxy}}
|
||||
|
||||
== Beacon
|
||||
|
||||
{{BeaconProxy}}
|
||||
|
||||
{{IBeacon}}
|
||||
|
||||
{{UpgradeableBeacon}}
|
||||
|
||||
== Minimal Clones
|
||||
|
||||
{{Clones}}
|
||||
|
||||
== Utilities
|
||||
|
||||
{{Initializable}}
|
||||
|
||||
@ -6,22 +6,22 @@ import "./UpgradeableProxy.sol";
|
||||
|
||||
/**
|
||||
* @dev This contract implements a proxy that is upgradeable by an admin.
|
||||
*
|
||||
*
|
||||
* To avoid https://medium.com/nomic-labs-blog/malicious-backdoors-in-ethereum-proxies-62629adf3357[proxy selector
|
||||
* clashing], which can potentially be used in an attack, this contract uses the
|
||||
* https://blog.openzeppelin.com/the-transparent-proxy-pattern/[transparent proxy pattern]. This pattern implies two
|
||||
* things that go hand in hand:
|
||||
*
|
||||
*
|
||||
* 1. If any account other than the admin calls the proxy, the call will be forwarded to the implementation, even if
|
||||
* that call matches one of the admin functions exposed by the proxy itself.
|
||||
* 2. If the admin calls the proxy, it can access the admin functions, but its calls will never be forwarded to the
|
||||
* implementation. If the admin tries to call a function on the implementation it will fail with an error that says
|
||||
* "admin cannot fallback to proxy target".
|
||||
*
|
||||
*
|
||||
* These properties mean that the admin account can only be used for admin actions like upgrading the proxy or changing
|
||||
* the admin, so it's best if it's a dedicated account that is not used for anything else. This will avoid headaches due
|
||||
* to sudden errors when trying to call a function from the proxy implementation.
|
||||
*
|
||||
*
|
||||
* Our recommendation is for the dedicated account to be an instance of the {ProxyAdmin} contract. If set up this way,
|
||||
* you should think of the `ProxyAdmin` instance as the real administrative interface of your proxy.
|
||||
*/
|
||||
@ -60,9 +60,9 @@ contract TransparentUpgradeableProxy is UpgradeableProxy {
|
||||
|
||||
/**
|
||||
* @dev Returns the current admin.
|
||||
*
|
||||
*
|
||||
* NOTE: Only the admin can call this function. See {ProxyAdmin-getProxyAdmin}.
|
||||
*
|
||||
*
|
||||
* 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`
|
||||
@ -73,9 +73,9 @@ contract TransparentUpgradeableProxy is UpgradeableProxy {
|
||||
|
||||
/**
|
||||
* @dev Returns the current implementation.
|
||||
*
|
||||
*
|
||||
* NOTE: Only the admin can call this function. See {ProxyAdmin-getProxyImplementation}.
|
||||
*
|
||||
*
|
||||
* 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.
|
||||
* `0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc`
|
||||
@ -86,12 +86,12 @@ contract TransparentUpgradeableProxy is UpgradeableProxy {
|
||||
|
||||
/**
|
||||
* @dev Changes the admin of the proxy.
|
||||
*
|
||||
*
|
||||
* Emits an {AdminChanged} event.
|
||||
*
|
||||
*
|
||||
* NOTE: Only the admin can call this function. See {ProxyAdmin-changeProxyAdmin}.
|
||||
*/
|
||||
function changeAdmin(address newAdmin) external ifAdmin {
|
||||
function changeAdmin(address newAdmin) external virtual ifAdmin {
|
||||
require(newAdmin != address(0), "TransparentUpgradeableProxy: new admin is the zero address");
|
||||
emit AdminChanged(_admin(), newAdmin);
|
||||
_setAdmin(newAdmin);
|
||||
@ -99,10 +99,10 @@ contract TransparentUpgradeableProxy is UpgradeableProxy {
|
||||
|
||||
/**
|
||||
* @dev Upgrade the implementation of the proxy.
|
||||
*
|
||||
*
|
||||
* NOTE: Only the admin can call this function. See {ProxyAdmin-upgrade}.
|
||||
*/
|
||||
function upgradeTo(address newImplementation) external ifAdmin {
|
||||
function upgradeTo(address newImplementation) external virtual ifAdmin {
|
||||
_upgradeTo(newImplementation);
|
||||
}
|
||||
|
||||
@ -110,20 +110,18 @@ contract TransparentUpgradeableProxy is UpgradeableProxy {
|
||||
* @dev Upgrade the implementation of the proxy, and then call a function from the new implementation as specified
|
||||
* by `data`, which should be an encoded function call. This is useful to initialize new storage variables in the
|
||||
* proxied contract.
|
||||
*
|
||||
*
|
||||
* NOTE: Only the admin can call this function. See {ProxyAdmin-upgradeAndCall}.
|
||||
*/
|
||||
function upgradeToAndCall(address newImplementation, bytes calldata data) external payable ifAdmin {
|
||||
function upgradeToAndCall(address newImplementation, bytes calldata data) external payable virtual ifAdmin {
|
||||
_upgradeTo(newImplementation);
|
||||
// solhint-disable-next-line avoid-low-level-calls
|
||||
(bool success,) = newImplementation.delegatecall(data);
|
||||
require(success);
|
||||
Address.functionDelegateCall(newImplementation, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the current admin.
|
||||
*/
|
||||
function _admin() internal view returns (address adm) {
|
||||
function _admin() internal view virtual returns (address adm) {
|
||||
bytes32 slot = _ADMIN_SLOT;
|
||||
// solhint-disable-next-line no-inline-assembly
|
||||
assembly {
|
||||
@ -146,7 +144,7 @@ contract TransparentUpgradeableProxy is UpgradeableProxy {
|
||||
/**
|
||||
* @dev Makes sure the admin cannot access the fallback function. See {Proxy-_beforeFallback}.
|
||||
*/
|
||||
function _beforeFallback() internal override virtual {
|
||||
function _beforeFallback() internal virtual override {
|
||||
require(msg.sender != _admin(), "TransparentUpgradeableProxy: admin cannot fallback to proxy target");
|
||||
super._beforeFallback();
|
||||
}
|
||||
|
||||
64
contracts/proxy/UpgradeableBeacon.sol
Normal file
64
contracts/proxy/UpgradeableBeacon.sol
Normal file
@ -0,0 +1,64 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
pragma solidity >=0.6.0 <0.8.0;
|
||||
|
||||
import "./IBeacon.sol";
|
||||
import "../access/Ownable.sol";
|
||||
import "../utils/Address.sol";
|
||||
|
||||
/**
|
||||
* @dev This contract is used in conjunction with one or more instances of {BeaconProxy} to determine their
|
||||
* implementation contract, which is where they will delegate all function calls.
|
||||
*
|
||||
* An owner is able to change the implementation the beacon points to, thus upgrading the proxies that use this beacon.
|
||||
*/
|
||||
contract UpgradeableBeacon is IBeacon, Ownable {
|
||||
address private _implementation;
|
||||
|
||||
/**
|
||||
* @dev Emitted when the implementation returned by the beacon is changed.
|
||||
*/
|
||||
event Upgraded(address indexed implementation);
|
||||
|
||||
/**
|
||||
* @dev Sets the address of the initial implementation, and the deployer account as the owner who can upgrade the
|
||||
* beacon.
|
||||
*/
|
||||
constructor(address implementation_) {
|
||||
_setImplementation(implementation_);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the current implementation address.
|
||||
*/
|
||||
function implementation() public view virtual override returns (address) {
|
||||
return _implementation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Upgrades the beacon to a new implementation.
|
||||
*
|
||||
* Emits an {Upgraded} event.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - msg.sender must be the owner of the contract.
|
||||
* - `newImplementation` must be a contract.
|
||||
*/
|
||||
function upgradeTo(address newImplementation) public virtual onlyOwner {
|
||||
_setImplementation(newImplementation);
|
||||
emit Upgraded(newImplementation);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Sets the implementation contract address for this beacon
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - `newImplementation` must be a contract.
|
||||
*/
|
||||
function _setImplementation(address newImplementation) private {
|
||||
require(Address.isContract(newImplementation), "UpgradeableBeacon: implementation is not a contract");
|
||||
_implementation = newImplementation;
|
||||
}
|
||||
}
|
||||
@ -10,14 +10,14 @@ import "../utils/Address.sol";
|
||||
* implementation address that can be changed. This address is stored in storage in the location specified by
|
||||
* https://eips.ethereum.org/EIPS/eip-1967[EIP1967], so that it doesn't conflict with the storage layout of the
|
||||
* implementation behind the proxy.
|
||||
*
|
||||
*
|
||||
* Upgradeability is only provided internally through {_upgradeTo}. For an externally upgradeable proxy see
|
||||
* {TransparentUpgradeableProxy}.
|
||||
*/
|
||||
contract UpgradeableProxy is Proxy {
|
||||
/**
|
||||
* @dev Initializes the upgradeable proxy with an initial implementation specified by `_logic`.
|
||||
*
|
||||
*
|
||||
* If `_data` is nonempty, it's used as data in a delegate call to `_logic`. This will typically be an encoded
|
||||
* function call, and allows initializating the storage of the proxy like a Solidity constructor.
|
||||
*/
|
||||
@ -25,9 +25,7 @@ contract UpgradeableProxy is Proxy {
|
||||
assert(_IMPLEMENTATION_SLOT == bytes32(uint256(keccak256("eip1967.proxy.implementation")) - 1));
|
||||
_setImplementation(_logic);
|
||||
if(_data.length > 0) {
|
||||
// solhint-disable-next-line avoid-low-level-calls
|
||||
(bool success,) = _logic.delegatecall(_data);
|
||||
require(success);
|
||||
Address.functionDelegateCall(_logic, _data);
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,7 +44,7 @@ contract UpgradeableProxy is Proxy {
|
||||
/**
|
||||
* @dev Returns the current implementation address.
|
||||
*/
|
||||
function _implementation() internal override view returns (address impl) {
|
||||
function _implementation() internal view virtual override returns (address impl) {
|
||||
bytes32 slot = _IMPLEMENTATION_SLOT;
|
||||
// solhint-disable-next-line no-inline-assembly
|
||||
assembly {
|
||||
@ -56,10 +54,10 @@ contract UpgradeableProxy is Proxy {
|
||||
|
||||
/**
|
||||
* @dev Upgrades the proxy to a new implementation.
|
||||
*
|
||||
*
|
||||
* Emits an {Upgraded} event.
|
||||
*/
|
||||
function _upgradeTo(address newImplementation) internal {
|
||||
function _upgradeTo(address newImplementation) internal virtual {
|
||||
_setImplementation(newImplementation);
|
||||
emit Upgraded(newImplementation);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user