UUPSUpgradeable.sol 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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(address context);
  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(address(this));
  36. }
  37. address implementation = _getImplementation();
  38. if (implementation != __self) {
  39. // Must be called through an active proxy
  40. revert UUPSUnauthorizedCallContext(implementation);
  41. }
  42. _;
  43. }
  44. /**
  45. * @dev Check that the execution is not being performed through a delegate call. This allows a function to be
  46. * callable on the implementing contract but not through proxies.
  47. */
  48. modifier notDelegated() {
  49. if (address(this) != __self) {
  50. // Must not be called through delegatecall
  51. revert UUPSUnauthorizedCallContext(address(this));
  52. }
  53. _;
  54. }
  55. /**
  56. * @dev Implementation of the ERC1822 {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 override notDelegated returns (bytes32) {
  64. return _IMPLEMENTATION_SLOT;
  65. }
  66. /**
  67. * @dev Upgrade the implementation of the proxy to `newImplementation`.
  68. *
  69. * Calls {_authorizeUpgrade}.
  70. *
  71. * Emits an {Upgraded} event.
  72. *
  73. * @custom:oz-upgrades-unsafe-allow-reachable delegatecall
  74. */
  75. function upgradeTo(address newImplementation) public virtual onlyProxy {
  76. _authorizeUpgrade(newImplementation);
  77. _upgradeToAndCallUUPS(newImplementation, new bytes(0), false);
  78. }
  79. /**
  80. * @dev Upgrade the implementation of the proxy to `newImplementation`, and subsequently execute the function call
  81. * encoded in `data`.
  82. *
  83. * Calls {_authorizeUpgrade}.
  84. *
  85. * Emits an {Upgraded} event.
  86. *
  87. * @custom:oz-upgrades-unsafe-allow-reachable delegatecall
  88. */
  89. function upgradeToAndCall(address newImplementation, bytes memory data) public payable virtual onlyProxy {
  90. _authorizeUpgrade(newImplementation);
  91. _upgradeToAndCallUUPS(newImplementation, data, true);
  92. }
  93. /**
  94. * @dev Function that should revert when `msg.sender` is not authorized to upgrade the contract. Called by
  95. * {upgradeTo} and {upgradeToAndCall}.
  96. *
  97. * Normally, this function will use an xref:access.adoc[access control] modifier such as {Ownable-onlyOwner}.
  98. *
  99. * ```solidity
  100. * function _authorizeUpgrade(address) internal override onlyOwner {}
  101. * ```
  102. */
  103. function _authorizeUpgrade(address newImplementation) internal virtual;
  104. }