UUPSUpgradeable.sol 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (proxy/utils/UUPSUpgradeable.sol)
  3. pragma solidity ^0.8.19;
  4. import "../../interfaces/draft-IERC1822.sol";
  5. import "../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. * _Available since v4.1._
  17. */
  18. abstract contract UUPSUpgradeable is IERC1822Proxiable {
  19. /// @custom:oz-upgrades-unsafe-allow state-variable-immutable state-variable-assignment
  20. address private immutable __self = address(this);
  21. /**
  22. * @dev The call is from an unauthorized context.
  23. */
  24. error UUPSUnauthorizedCallContext();
  25. /**
  26. * @dev The storage `slot` is unsupported as a UUID.
  27. */
  28. error UUPSUnsupportedProxiableUUID(bytes32 slot);
  29. /**
  30. * @dev Check that the execution is being performed through a delegatecall call and that the execution context is
  31. * a proxy contract with an implementation (as defined in ERC1967) pointing to self. This should only be the case
  32. * for UUPS and transparent proxies that are using the current contract as their implementation. Execution of a
  33. * function through ERC1167 minimal proxies (clones) would not normally pass this test, but is not guaranteed to
  34. * fail.
  35. */
  36. modifier onlyProxy() {
  37. if (
  38. address(this) == __self || // Must be called through delegatecall
  39. ERC1967Utils.getImplementation() != __self // Must be called through an active proxy
  40. ) {
  41. revert UUPSUnauthorizedCallContext();
  42. }
  43. _;
  44. }
  45. /**
  46. * @dev Check that the execution is not being performed through a delegate call. This allows a function to be
  47. * callable on the implementing contract but not through proxies.
  48. */
  49. modifier notDelegated() {
  50. if (address(this) != __self) {
  51. // Must not be called through delegatecall
  52. revert UUPSUnauthorizedCallContext();
  53. }
  54. _;
  55. }
  56. /**
  57. * @dev Implementation of the ERC1822 {proxiableUUID} function. This returns the storage slot used by the
  58. * implementation. It is used to validate the implementation's compatibility when performing an upgrade.
  59. *
  60. * IMPORTANT: A proxy pointing at a proxiable contract should not be considered proxiable itself, because this risks
  61. * bricking a proxy that upgrades to it, by delegating to itself until out of gas. Thus it is critical that this
  62. * function revert if invoked through a proxy. This is guaranteed by the `notDelegated` modifier.
  63. */
  64. function proxiableUUID() external view virtual notDelegated returns (bytes32) {
  65. return ERC1967Utils.IMPLEMENTATION_SLOT;
  66. }
  67. /**
  68. * @dev Upgrade the implementation of the proxy to `newImplementation`.
  69. *
  70. * Calls {_authorizeUpgrade}.
  71. *
  72. * Emits an {Upgraded} event.
  73. *
  74. * @custom:oz-upgrades-unsafe-allow-reachable delegatecall
  75. */
  76. function upgradeTo(address newImplementation) public virtual onlyProxy {
  77. _authorizeUpgrade(newImplementation);
  78. _upgradeToAndCallUUPS(newImplementation, new bytes(0), false);
  79. }
  80. /**
  81. * @dev Upgrade the implementation of the proxy to `newImplementation`, and subsequently execute the function call
  82. * encoded in `data`.
  83. *
  84. * Calls {_authorizeUpgrade}.
  85. *
  86. * Emits an {Upgraded} event.
  87. *
  88. * @custom:oz-upgrades-unsafe-allow-reachable delegatecall
  89. */
  90. function upgradeToAndCall(address newImplementation, bytes memory data) public payable virtual onlyProxy {
  91. _authorizeUpgrade(newImplementation);
  92. _upgradeToAndCallUUPS(newImplementation, data, true);
  93. }
  94. /**
  95. * @dev Function that should revert when `msg.sender` is not authorized to upgrade the contract. Called by
  96. * {upgradeTo} and {upgradeToAndCall}.
  97. *
  98. * Normally, this function will use an xref:access.adoc[access control] modifier such as {Ownable-onlyOwner}.
  99. *
  100. * ```solidity
  101. * function _authorizeUpgrade(address) internal onlyOwner {}
  102. * ```
  103. */
  104. function _authorizeUpgrade(address newImplementation) internal virtual;
  105. /**
  106. * @dev Perform implementation upgrade with security checks for UUPS proxies, and additional setup call.
  107. *
  108. * Emits an {IERC1967-Upgraded} event.
  109. */
  110. function _upgradeToAndCallUUPS(address newImplementation, bytes memory data, bool forceCall) private {
  111. try IERC1822Proxiable(newImplementation).proxiableUUID() returns (bytes32 slot) {
  112. if (slot != ERC1967Utils.IMPLEMENTATION_SLOT) {
  113. revert UUPSUnsupportedProxiableUUID(slot);
  114. }
  115. ERC1967Utils.upgradeToAndCall(newImplementation, data, forceCall);
  116. } catch {
  117. // The implementation is not UUPS
  118. revert ERC1967Utils.ERC1967InvalidImplementation(newImplementation);
  119. }
  120. }
  121. }