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.
89 lines
3.0 KiB
89 lines
3.0 KiB
4 years ago
|
// SPDX-License-Identifier: MIT
|
||
|
|
||
4 years ago
|
pragma solidity ^0.8.0;
|
||
4 years ago
|
|
||
|
import "./IBeacon.sol";
|
||
4 years ago
|
import "../Proxy.sol";
|
||
|
import "../../utils/Address.sol";
|
||
4 years ago
|
|
||
|
/**
|
||
|
* @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.
|
||
4 years ago
|
*
|
||
|
* _Available since v3.4._
|
||
4 years ago
|
*/
|
||
|
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}.
|
||
|
*/
|
||
4 years ago
|
constructor(address beacon, bytes memory data) payable {
|
||
4 years ago
|
assert(_BEACON_SLOT == bytes32(uint256(keccak256("eip1967.proxy.beacon")) - 1));
|
||
|
_setBeacon(beacon, data);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @dev Returns the current beacon address.
|
||
|
*/
|
||
4 years ago
|
function _beacon() internal view virtual returns (address beacon) {
|
||
4 years ago
|
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.
|
||
|
*/
|
||
4 years ago
|
function _implementation() internal view virtual override returns (address) {
|
||
4 years ago
|
return IBeacon(_beacon()).implementation();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @dev Changes the proxy to use a new beacon.
|
||
|
*
|
||
4 years ago
|
* If `data` is nonempty, it's used as data in a delegate call to the implementation returned by the beacon.
|
||
4 years ago
|
*
|
||
|
* Requirements:
|
||
|
*
|
||
|
* - `beacon` must be a contract.
|
||
|
* - The implementation returned by `beacon` must be a contract.
|
||
|
*/
|
||
4 years ago
|
function _setBeacon(address beacon, bytes memory data) internal virtual {
|
||
4 years ago
|
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");
|
||
|
}
|
||
|
}
|
||
|
}
|