UUPSUpgradeable.sol 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 {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, being 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 ERC1967) 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 ERC1167 minimal proxies (clones) would not normally pass this test, but is not guaranteed to
  41. * fail.
  42. */
  43. modifier onlyProxy() {
  44. if (
  45. address(this) == __self || // Must be called through delegatecall
  46. ERC1967Utils.getImplementation() != __self // Must be called through an active proxy
  47. ) {
  48. revert UUPSUnauthorizedCallContext();
  49. }
  50. _;
  51. }
  52. /**
  53. * @dev Check that the execution is not being performed through a delegate call. This allows a function to be
  54. * callable on the implementing contract but not through proxies.
  55. */
  56. modifier notDelegated() {
  57. if (address(this) != __self) {
  58. // Must not be called through delegatecall
  59. revert UUPSUnauthorizedCallContext();
  60. }
  61. _;
  62. }
  63. /**
  64. * @dev Implementation of the ERC1822 {proxiableUUID} function. This returns the storage slot used by the
  65. * implementation. It is used to validate the implementation's compatibility when performing an upgrade.
  66. *
  67. * IMPORTANT: A proxy pointing at a proxiable contract should not be considered proxiable itself, because this risks
  68. * bricking a proxy that upgrades to it, by delegating to itself until out of gas. Thus it is critical that this
  69. * function revert if invoked through a proxy. This is guaranteed by the `notDelegated` modifier.
  70. */
  71. function proxiableUUID() external view virtual notDelegated returns (bytes32) {
  72. return ERC1967Utils.IMPLEMENTATION_SLOT;
  73. }
  74. /**
  75. * @dev Upgrade the implementation of the proxy to `newImplementation`, and subsequently execute the function call
  76. * encoded in `data`.
  77. *
  78. * Calls {_authorizeUpgrade}.
  79. *
  80. * Emits an {Upgraded} event.
  81. *
  82. * @custom:oz-upgrades-unsafe-allow-reachable delegatecall
  83. */
  84. function upgradeToAndCall(address newImplementation, bytes memory data) public payable virtual onlyProxy {
  85. _authorizeUpgrade(newImplementation);
  86. _upgradeToAndCallUUPS(newImplementation, data);
  87. }
  88. /**
  89. * @dev Function that should revert when `msg.sender` is not authorized to upgrade the contract. Called by
  90. * {upgradeToAndCall}.
  91. *
  92. * Normally, this function will use an xref:access.adoc[access control] modifier such as {Ownable-onlyOwner}.
  93. *
  94. * ```solidity
  95. * function _authorizeUpgrade(address) internal onlyOwner {}
  96. * ```
  97. */
  98. function _authorizeUpgrade(address newImplementation) internal virtual;
  99. /**
  100. * @dev Perform implementation upgrade with security checks for UUPS proxies, and additional setup call.
  101. *
  102. * Emits an {IERC1967-Upgraded} event.
  103. */
  104. function _upgradeToAndCallUUPS(address newImplementation, bytes memory data) private {
  105. try IERC1822Proxiable(newImplementation).proxiableUUID() returns (bytes32 slot) {
  106. if (slot != ERC1967Utils.IMPLEMENTATION_SLOT) {
  107. revert UUPSUnsupportedProxiableUUID(slot);
  108. }
  109. ERC1967Utils.upgradeToAndCall(newImplementation, data);
  110. } catch {
  111. // The implementation is not UUPS
  112. revert ERC1967Utils.ERC1967InvalidImplementation(newImplementation);
  113. }
  114. }
  115. }