BeaconProxy.sol 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.0 (proxy/beacon/BeaconProxy.sol)
  3. pragma solidity ^0.8.0;
  4. import "./IBeacon.sol";
  5. import "../Proxy.sol";
  6. import "../ERC1967/ERC1967Upgrade.sol";
  7. /**
  8. * @dev This contract implements a proxy that gets the implementation address for each call from a {UpgradeableBeacon}.
  9. *
  10. * The beacon address is stored in storage slot `uint256(keccak256('eip1967.proxy.beacon')) - 1`, so that it doesn't
  11. * conflict with the storage layout of the implementation behind the proxy.
  12. *
  13. * _Available since v3.4._
  14. */
  15. contract BeaconProxy is Proxy, ERC1967Upgrade {
  16. /**
  17. * @dev Initializes the proxy with `beacon`.
  18. *
  19. * If `data` is nonempty, it's used as data in a delegate call to the implementation returned by the beacon. This
  20. * will typically be an encoded function call, and allows initializating the storage of the proxy like a Solidity
  21. * constructor.
  22. *
  23. * Requirements:
  24. *
  25. * - `beacon` must be a contract with the interface {IBeacon}.
  26. */
  27. constructor(address beacon, bytes memory data) payable {
  28. assert(_BEACON_SLOT == bytes32(uint256(keccak256("eip1967.proxy.beacon")) - 1));
  29. _upgradeBeaconToAndCall(beacon, data, false);
  30. }
  31. /**
  32. * @dev Returns the current beacon address.
  33. */
  34. function _beacon() internal view virtual returns (address) {
  35. return _getBeacon();
  36. }
  37. /**
  38. * @dev Returns the current implementation address of the associated beacon.
  39. */
  40. function _implementation() internal view virtual override returns (address) {
  41. return IBeacon(_getBeacon()).implementation();
  42. }
  43. /**
  44. * @dev Changes the proxy to use a new beacon. Deprecated: see {_upgradeBeaconToAndCall}.
  45. *
  46. * If `data` is nonempty, it's used as data in a delegate call to the implementation returned by the beacon.
  47. *
  48. * Requirements:
  49. *
  50. * - `beacon` must be a contract.
  51. * - The implementation returned by `beacon` must be a contract.
  52. */
  53. function _setBeacon(address beacon, bytes memory data) internal virtual {
  54. _upgradeBeaconToAndCall(beacon, data, false);
  55. }
  56. }