UUPSUpgradeable.sol 4.1 KB

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