UpgradeableBeacon.sol 2.0 KB

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