UUPSUpgradeable.sol 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.1.0) (proxy/utils/UUPSUpgradeable.sol)
  3. pragma solidity ^0.8.22;
  4. import {IERC1822Proxiable} from "../../interfaces/draft-IERC1822.sol";
  5. import {ERC1967Utils} from "../ERC1967/ERC1967Utils.sol";
  6. /**
  7. * @dev An upgradeability mechanism designed for UUPS proxies. The functions included here can perform an upgrade of an
  8. * {ERC1967Proxy}, when this contract is set as the implementation behind such a proxy.
  9. *
  10. * A security mechanism ensures that an upgrade does not turn off upgradeability accidentally, although this risk is
  11. * reinstated if the upgrade retains upgradeability but removes the security mechanism, e.g. by replacing
  12. * `UUPSUpgradeable` with a custom implementation of upgrades.
  13. *
  14. * The {_authorizeUpgrade} function must be overridden to include access restriction to the upgrade mechanism.
  15. */
  16. abstract contract UUPSUpgradeable is IERC1822Proxiable {
  17. /// @custom:oz-upgrades-unsafe-allow state-variable-immutable
  18. address private immutable __self = address(this);
  19. /**
  20. * @dev The version of the upgrade interface of the contract. If this getter is missing, both `upgradeTo(address)`
  21. * and `upgradeToAndCall(address,bytes)` are present, and `upgradeTo` must be used if no function should be called,
  22. * while `upgradeToAndCall` will invoke the `receive` function if the second argument is the empty byte string.
  23. * If the getter returns `"5.0.0"`, only `upgradeToAndCall(address,bytes)` is present, and the second argument must
  24. * be the empty byte string if no function should be called, making it impossible to invoke the `receive` function
  25. * during an upgrade.
  26. */
  27. string public constant UPGRADE_INTERFACE_VERSION = "5.0.0";
  28. /**
  29. * @dev The call is from an unauthorized context.
  30. */
  31. error UUPSUnauthorizedCallContext();
  32. /**
  33. * @dev The storage `slot` is unsupported as a UUID.
  34. */
  35. error UUPSUnsupportedProxiableUUID(bytes32 slot);
  36. /**
  37. * @dev Check that the execution is being performed through a delegatecall call and that the execution context is
  38. * a proxy contract with an implementation (as defined in ERC-1967) pointing to self. This should only be the case
  39. * for UUPS and transparent proxies that are using the current contract as their implementation. Execution of a
  40. * function through ERC-1167 minimal proxies (clones) would not normally pass this test, but is not guaranteed to
  41. * fail.
  42. */
  43. modifier onlyProxy() {
  44. _checkProxy();
  45. _;
  46. }
  47. /**
  48. * @dev Check that the execution is not being performed through a delegate call. This allows a function to be
  49. * callable on the implementing contract but not through proxies.
  50. */
  51. modifier notDelegated() {
  52. _checkNotDelegated();
  53. _;
  54. }
  55. /**
  56. * @dev Implementation of the ERC-1822 {proxiableUUID} function. This returns the storage slot used by the
  57. * implementation. It is used to validate the implementation's compatibility when performing an upgrade.
  58. *
  59. * IMPORTANT: A proxy pointing at a proxiable contract should not be considered proxiable itself, because this risks
  60. * bricking a proxy that upgrades to it, by delegating to itself until out of gas. Thus it is critical that this
  61. * function revert if invoked through a proxy. This is guaranteed by the `notDelegated` modifier.
  62. */
  63. function proxiableUUID() external view virtual notDelegated returns (bytes32) {
  64. return ERC1967Utils.IMPLEMENTATION_SLOT;
  65. }
  66. /**
  67. * @dev Upgrade the implementation of the proxy to `newImplementation`, and subsequently execute the function call
  68. * encoded in `data`.
  69. *
  70. * Calls {_authorizeUpgrade}.
  71. *
  72. * Emits an {Upgraded} event.
  73. *
  74. * @custom:oz-upgrades-unsafe-allow-reachable delegatecall
  75. */
  76. function upgradeToAndCall(address newImplementation, bytes memory data) public payable virtual onlyProxy {
  77. _authorizeUpgrade(newImplementation);
  78. _upgradeToAndCallUUPS(newImplementation, data);
  79. }
  80. /**
  81. * @dev Reverts if the execution is not performed via delegatecall or the execution
  82. * context is not of a proxy with an ERC-1967 compliant implementation pointing to self.
  83. * See {_onlyProxy}.
  84. */
  85. function _checkProxy() internal view virtual {
  86. if (
  87. address(this) == __self || // Must be called through delegatecall
  88. ERC1967Utils.getImplementation() != __self // Must be called through an active proxy
  89. ) {
  90. revert UUPSUnauthorizedCallContext();
  91. }
  92. }
  93. /**
  94. * @dev Reverts if the execution is performed via delegatecall.
  95. * See {notDelegated}.
  96. */
  97. function _checkNotDelegated() internal view virtual {
  98. if (address(this) != __self) {
  99. // Must not be called through delegatecall
  100. revert UUPSUnauthorizedCallContext();
  101. }
  102. }
  103. /**
  104. * @dev Function that should revert when `msg.sender` is not authorized to upgrade the contract. Called by
  105. * {upgradeToAndCall}.
  106. *
  107. * Normally, this function will use an xref:access.adoc[access control] modifier such as {Ownable-onlyOwner}.
  108. *
  109. * ```solidity
  110. * function _authorizeUpgrade(address) internal onlyOwner {}
  111. * ```
  112. */
  113. function _authorizeUpgrade(address newImplementation) internal virtual;
  114. /**
  115. * @dev Performs an implementation upgrade with a security check for UUPS proxies, and additional setup call.
  116. *
  117. * As a security check, {proxiableUUID} is invoked in the new implementation, and the return value
  118. * is expected to be the implementation slot in ERC-1967.
  119. *
  120. * Emits an {IERC1967-Upgraded} event.
  121. */
  122. function _upgradeToAndCallUUPS(address newImplementation, bytes memory data) private {
  123. try IERC1822Proxiable(newImplementation).proxiableUUID() returns (bytes32 slot) {
  124. if (slot != ERC1967Utils.IMPLEMENTATION_SLOT) {
  125. revert UUPSUnsupportedProxiableUUID(slot);
  126. }
  127. ERC1967Utils.upgradeToAndCall(newImplementation, data);
  128. } catch {
  129. // The implementation is not UUPS
  130. revert ERC1967Utils.ERC1967InvalidImplementation(newImplementation);
  131. }
  132. }
  133. }