UUPSUpgradeable.sol 5.6 KB

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