BeaconProxy.sol 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.7.0) (proxy/beacon/BeaconProxy.sol)
  3. pragma solidity ^0.8.19;
  4. import {IBeacon} from "./IBeacon.sol";
  5. import {Proxy} from "../Proxy.sol";
  6. import {ERC1967Utils} from "../ERC1967/ERC1967Utils.sol";
  7. /**
  8. * @dev This contract implements a proxy that gets the implementation address for each call from an {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. * CAUTION: The beacon address can only be set once during construction, and cannot be changed afterwards.
  14. * You must ensure that you either control the beacon, or trust the beacon to not upgrade the implementation maliciously.
  15. */
  16. contract BeaconProxy is Proxy {
  17. // An immutable address for the beacon to avoid unnecessary SLOADs before each delegate call.
  18. address private immutable _beacon;
  19. /**
  20. * @dev Initializes the proxy with `beacon`.
  21. *
  22. * If `data` is nonempty, it's used as data in a delegate call to the implementation returned by the beacon. This
  23. * will typically be an encoded function call, and allows initializing the storage of the proxy like a Solidity
  24. * constructor.
  25. *
  26. * Requirements:
  27. *
  28. * - `beacon` must be a contract with the interface {IBeacon}.
  29. */
  30. constructor(address beacon, bytes memory data) payable {
  31. ERC1967Utils.upgradeBeaconToAndCall(beacon, data, false);
  32. _beacon = beacon;
  33. }
  34. /**
  35. * @dev Returns the current implementation address of the associated beacon.
  36. */
  37. function _implementation() internal view virtual override returns (address) {
  38. return IBeacon(_getBeacon()).implementation();
  39. }
  40. /**
  41. * @dev Returns the beacon.
  42. */
  43. function _getBeacon() internal view virtual returns (address) {
  44. return _beacon;
  45. }
  46. }