UpgradeableBeacon.sol 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.0.0) (proxy/beacon/UpgradeableBeacon.sol)
  3. pragma solidity ^0.8.20;
  4. import {IBeacon} from "./IBeacon.sol";
  5. import {Ownable} from "../../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 The `implementation` of the beacon is invalid.
  16. */
  17. error BeaconInvalidImplementation(address implementation);
  18. /**
  19. * @dev Emitted when the implementation returned by the beacon is changed.
  20. */
  21. event Upgraded(address indexed implementation);
  22. /**
  23. * @dev Sets the address of the initial implementation, and the initial owner who can upgrade the beacon.
  24. */
  25. constructor(address implementation_, address initialOwner) Ownable(initialOwner) {
  26. _setImplementation(implementation_);
  27. }
  28. /**
  29. * @dev Returns the current implementation address.
  30. */
  31. function implementation() public view virtual returns (address) {
  32. return _implementation;
  33. }
  34. /**
  35. * @dev Upgrades the beacon to a new implementation.
  36. *
  37. * Emits an {Upgraded} event.
  38. *
  39. * Requirements:
  40. *
  41. * - msg.sender must be the owner of the contract.
  42. * - `newImplementation` must be a contract.
  43. */
  44. function upgradeTo(address newImplementation) public virtual onlyOwner {
  45. _setImplementation(newImplementation);
  46. }
  47. /**
  48. * @dev Sets the implementation contract address for this beacon
  49. *
  50. * Requirements:
  51. *
  52. * - `newImplementation` must be a contract.
  53. */
  54. function _setImplementation(address newImplementation) private {
  55. if (newImplementation.code.length == 0) {
  56. revert BeaconInvalidImplementation(newImplementation);
  57. }
  58. _implementation = newImplementation;
  59. emit Upgraded(newImplementation);
  60. }
  61. }