UUPSUpgradeable.sol 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "../ERC1967/ERC1967Upgrade.sol";
  4. /**
  5. * @dev An upgradeability mechanism designed for UUPS proxies. The functions included here can perform an upgrade of an
  6. * {ERC1967Proxy}, when this contract is set as the implementation behind such a proxy.
  7. *
  8. * A security mechanism ensures that an upgrade does not turn off upgradeability accidentally, although this risk is
  9. * reinstated if the upgrade retains upgradeability but removes the security mechanism, e.g. by replacing
  10. * `UUPSUpgradeable` with a custom implementation of upgrades.
  11. *
  12. * The {_authorizeUpgrade} function must be overridden to include access restriction to the upgrade mechanism.
  13. *
  14. * _Available since v4.1._
  15. */
  16. abstract contract UUPSUpgradeable is ERC1967Upgrade {
  17. /**
  18. * @dev Upgrade the implementation of the proxy to `newImplementation`.
  19. *
  20. * Calls {_authorizeUpgrade}.
  21. *
  22. * Emits an {Upgraded} event.
  23. */
  24. function upgradeTo(address newImplementation) external virtual {
  25. _authorizeUpgrade(newImplementation);
  26. _upgradeToAndCallSecure(newImplementation, bytes(""), false);
  27. }
  28. /**
  29. * @dev Upgrade the implementation of the proxy to `newImplementation`, and subsequently execute the function call
  30. * encoded in `data`.
  31. *
  32. * Calls {_authorizeUpgrade}.
  33. *
  34. * Emits an {Upgraded} event.
  35. */
  36. function upgradeToAndCall(address newImplementation, bytes memory data) external payable virtual {
  37. _authorizeUpgrade(newImplementation);
  38. _upgradeToAndCallSecure(newImplementation, data, true);
  39. }
  40. /**
  41. * @dev Function that should revert when `msg.sender` is not authorized to upgrade the contract. Called by
  42. * {upgradeTo} and {upgradeToAndCall}.
  43. *
  44. * Normally, this function will use an xref:access.adoc[access control] modifier such as {Ownable-onlyOwner}.
  45. *
  46. * ```solidity
  47. * function _authorizeUpgrade(address) internal override onlyOwner {}
  48. * ```
  49. */
  50. function _authorizeUpgrade(address newImplementation) internal virtual;
  51. }