UpgradeableBeacon.sol 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.1 (proxy/beacon/UpgradeableBeacon.sol)
  3. pragma solidity ^0.8.19;
  4. import "./IBeacon.sol";
  5. import "../../access/Ownable.sol";
  6. /**
  7. * @dev This contract is used in conjunction with one or more instances of {BeaconProxy} to determine their
  8. * implementation contract, which is where they will delegate all function calls.
  9. *
  10. * An owner is able to change the implementation the beacon points to, thus upgrading the proxies that use this beacon.
  11. */
  12. contract UpgradeableBeacon is IBeacon, Ownable {
  13. address private _implementation;
  14. /**
  15. * @dev Emitted when the implementation returned by the beacon is changed.
  16. */
  17. event Upgraded(address indexed implementation);
  18. /**
  19. * @dev Sets the address of the initial implementation, and the initial owner who can upgrade the beacon.
  20. */
  21. constructor(address implementation_, address initialOwner) Ownable(initialOwner) {
  22. _setImplementation(implementation_);
  23. }
  24. /**
  25. * @dev Returns the current implementation address.
  26. */
  27. function implementation() public view virtual returns (address) {
  28. return _implementation;
  29. }
  30. /**
  31. * @dev Upgrades the beacon to a new implementation.
  32. *
  33. * Emits an {Upgraded} event.
  34. *
  35. * Requirements:
  36. *
  37. * - msg.sender must be the owner of the contract.
  38. * - `newImplementation` must be a contract.
  39. */
  40. function upgradeTo(address newImplementation) public virtual onlyOwner {
  41. _setImplementation(newImplementation);
  42. emit Upgraded(newImplementation);
  43. }
  44. /**
  45. * @dev Sets the implementation contract address for this beacon
  46. *
  47. * Requirements:
  48. *
  49. * - `newImplementation` must be a contract.
  50. */
  51. function _setImplementation(address newImplementation) private {
  52. require(newImplementation.code.length > 0, "UpgradeableBeacon: implementation is not a contract");
  53. _implementation = newImplementation;
  54. }
  55. }