BeaconProxy.sol 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. contract BeaconProxy is Proxy {
  14. /**
  15. * @dev Initializes the proxy with `beacon`.
  16. *
  17. * If `data` is nonempty, it's used as data in a delegate call to the implementation returned by the beacon. This
  18. * will typically be an encoded function call, and allows initializing the storage of the proxy like a Solidity
  19. * constructor.
  20. *
  21. * Requirements:
  22. *
  23. * - `beacon` must be a contract with the interface {IBeacon}.
  24. */
  25. constructor(address beacon, bytes memory data) payable {
  26. ERC1967Utils.upgradeBeaconToAndCall(beacon, data, false);
  27. }
  28. /**
  29. * @dev Returns the current implementation address of the associated beacon.
  30. */
  31. function _implementation() internal view virtual override returns (address) {
  32. return IBeacon(ERC1967Utils.getBeacon()).implementation();
  33. }
  34. }