UUPSUpgradeable.sol 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.1 (proxy/utils/UUPSUpgradeable.sol)
  3. pragma solidity ^0.8.0;
  4. import "../ERC1967/ERC1967Upgrade.sol";
  5. /**
  6. * @dev An upgradeability mechanism designed for UUPS proxies. The functions included here can perform an upgrade of an
  7. * {ERC1967Proxy}, when this contract is set as the implementation behind such a proxy.
  8. *
  9. * A security mechanism ensures that an upgrade does not turn off upgradeability accidentally, although this risk is
  10. * reinstated if the upgrade retains upgradeability but removes the security mechanism, e.g. by replacing
  11. * `UUPSUpgradeable` with a custom implementation of upgrades.
  12. *
  13. * The {_authorizeUpgrade} function must be overridden to include access restriction to the upgrade mechanism.
  14. *
  15. * _Available since v4.1._
  16. */
  17. abstract contract UUPSUpgradeable is ERC1967Upgrade {
  18. /// @custom:oz-upgrades-unsafe-allow state-variable-immutable state-variable-assignment
  19. address private immutable __self = address(this);
  20. /**
  21. * @dev Check that the execution is being performed through a delegatecall call and that the execution context is
  22. * a proxy contract with an implementation (as defined in ERC1967) pointing to self. This should only be the case
  23. * for UUPS and transparent proxies that are using the current contract as their implementation. Execution of a
  24. * function through ERC1167 minimal proxies (clones) would not normally pass this test, but is not guaranteed to
  25. * fail.
  26. */
  27. modifier onlyProxy() {
  28. require(address(this) != __self, "Function must be called through delegatecall");
  29. require(_getImplementation() == __self, "Function must be called through active proxy");
  30. _;
  31. }
  32. /**
  33. * @dev Upgrade the implementation of the proxy to `newImplementation`.
  34. *
  35. * Calls {_authorizeUpgrade}.
  36. *
  37. * Emits an {Upgraded} event.
  38. */
  39. function upgradeTo(address newImplementation) external virtual onlyProxy {
  40. _authorizeUpgrade(newImplementation);
  41. _upgradeToAndCallSecure(newImplementation, new bytes(0), false);
  42. }
  43. /**
  44. * @dev Upgrade the implementation of the proxy to `newImplementation`, and subsequently execute the function call
  45. * encoded in `data`.
  46. *
  47. * Calls {_authorizeUpgrade}.
  48. *
  49. * Emits an {Upgraded} event.
  50. */
  51. function upgradeToAndCall(address newImplementation, bytes memory data) external payable virtual onlyProxy {
  52. _authorizeUpgrade(newImplementation);
  53. _upgradeToAndCallSecure(newImplementation, data, true);
  54. }
  55. /**
  56. * @dev Function that should revert when `msg.sender` is not authorized to upgrade the contract. Called by
  57. * {upgradeTo} and {upgradeToAndCall}.
  58. *
  59. * Normally, this function will use an xref:access.adoc[access control] modifier such as {Ownable-onlyOwner}.
  60. *
  61. * ```solidity
  62. * function _authorizeUpgrade(address) internal override onlyOwner {}
  63. * ```
  64. */
  65. function _authorizeUpgrade(address newImplementation) internal virtual;
  66. }