UUPSUpgradeable.sol 4.5 KB

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