ERC1967UpgradeUpgradeable.sol 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.5.0-rc.0) (proxy/ERC1967/ERC1967Upgrade.sol)
  3. pragma solidity ^0.8.2;
  4. import "../beacon/IBeaconUpgradeable.sol";
  5. import "../../interfaces/draft-IERC1822Upgradeable.sol";
  6. import "../../utils/AddressUpgradeable.sol";
  7. import "../../utils/StorageSlotUpgradeable.sol";
  8. import "../utils/Initializable.sol";
  9. /**
  10. * @dev This abstract contract provides getters and event emitting update functions for
  11. * https://eips.ethereum.org/EIPS/eip-1967[EIP1967] slots.
  12. *
  13. * _Available since v4.1._
  14. *
  15. * @custom:oz-upgrades-unsafe-allow delegatecall
  16. */
  17. abstract contract ERC1967UpgradeUpgradeable is Initializable {
  18. function __ERC1967Upgrade_init() internal onlyInitializing {
  19. }
  20. function __ERC1967Upgrade_init_unchained() internal onlyInitializing {
  21. }
  22. // This is the keccak-256 hash of "eip1967.proxy.rollback" subtracted by 1
  23. bytes32 private constant _ROLLBACK_SLOT = 0x4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd9143;
  24. /**
  25. * @dev Storage slot with the address of the current implementation.
  26. * This is the keccak-256 hash of "eip1967.proxy.implementation" subtracted by 1, and is
  27. * validated in the constructor.
  28. */
  29. bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
  30. /**
  31. * @dev Emitted when the implementation is upgraded.
  32. */
  33. event Upgraded(address indexed implementation);
  34. /**
  35. * @dev Returns the current implementation address.
  36. */
  37. function _getImplementation() internal view returns (address) {
  38. return StorageSlotUpgradeable.getAddressSlot(_IMPLEMENTATION_SLOT).value;
  39. }
  40. /**
  41. * @dev Stores a new address in the EIP1967 implementation slot.
  42. */
  43. function _setImplementation(address newImplementation) private {
  44. require(AddressUpgradeable.isContract(newImplementation), "ERC1967: new implementation is not a contract");
  45. StorageSlotUpgradeable.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
  46. }
  47. /**
  48. * @dev Perform implementation upgrade
  49. *
  50. * Emits an {Upgraded} event.
  51. */
  52. function _upgradeTo(address newImplementation) internal {
  53. _setImplementation(newImplementation);
  54. emit Upgraded(newImplementation);
  55. }
  56. /**
  57. * @dev Perform implementation upgrade with additional setup call.
  58. *
  59. * Emits an {Upgraded} event.
  60. */
  61. function _upgradeToAndCall(
  62. address newImplementation,
  63. bytes memory data,
  64. bool forceCall
  65. ) internal {
  66. _upgradeTo(newImplementation);
  67. if (data.length > 0 || forceCall) {
  68. _functionDelegateCall(newImplementation, data);
  69. }
  70. }
  71. /**
  72. * @dev Perform implementation upgrade with security checks for UUPS proxies, and additional setup call.
  73. *
  74. * Emits an {Upgraded} event.
  75. */
  76. function _upgradeToAndCallUUPS(
  77. address newImplementation,
  78. bytes memory data,
  79. bool forceCall
  80. ) internal {
  81. // Upgrades from old implementations will perform a rollback test. This test requires the new
  82. // implementation to upgrade back to the old, non-ERC1822 compliant, implementation. Removing
  83. // this special case will break upgrade paths from old UUPS implementation to new ones.
  84. if (StorageSlotUpgradeable.getBooleanSlot(_ROLLBACK_SLOT).value) {
  85. _setImplementation(newImplementation);
  86. } else {
  87. try IERC1822ProxiableUpgradeable(newImplementation).proxiableUUID() returns (bytes32 slot) {
  88. require(slot == _IMPLEMENTATION_SLOT, "ERC1967Upgrade: unsupported proxiableUUID");
  89. } catch {
  90. revert("ERC1967Upgrade: new implementation is not UUPS");
  91. }
  92. _upgradeToAndCall(newImplementation, data, forceCall);
  93. }
  94. }
  95. /**
  96. * @dev Storage slot with the admin of the contract.
  97. * This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1, and is
  98. * validated in the constructor.
  99. */
  100. bytes32 internal constant _ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
  101. /**
  102. * @dev Emitted when the admin account has changed.
  103. */
  104. event AdminChanged(address previousAdmin, address newAdmin);
  105. /**
  106. * @dev Returns the current admin.
  107. */
  108. function _getAdmin() internal view returns (address) {
  109. return StorageSlotUpgradeable.getAddressSlot(_ADMIN_SLOT).value;
  110. }
  111. /**
  112. * @dev Stores a new address in the EIP1967 admin slot.
  113. */
  114. function _setAdmin(address newAdmin) private {
  115. require(newAdmin != address(0), "ERC1967: new admin is the zero address");
  116. StorageSlotUpgradeable.getAddressSlot(_ADMIN_SLOT).value = newAdmin;
  117. }
  118. /**
  119. * @dev Changes the admin of the proxy.
  120. *
  121. * Emits an {AdminChanged} event.
  122. */
  123. function _changeAdmin(address newAdmin) internal {
  124. emit AdminChanged(_getAdmin(), newAdmin);
  125. _setAdmin(newAdmin);
  126. }
  127. /**
  128. * @dev The storage slot of the UpgradeableBeacon contract which defines the implementation for this proxy.
  129. * This is bytes32(uint256(keccak256('eip1967.proxy.beacon')) - 1)) and is validated in the constructor.
  130. */
  131. bytes32 internal constant _BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50;
  132. /**
  133. * @dev Emitted when the beacon is upgraded.
  134. */
  135. event BeaconUpgraded(address indexed beacon);
  136. /**
  137. * @dev Returns the current beacon.
  138. */
  139. function _getBeacon() internal view returns (address) {
  140. return StorageSlotUpgradeable.getAddressSlot(_BEACON_SLOT).value;
  141. }
  142. /**
  143. * @dev Stores a new beacon in the EIP1967 beacon slot.
  144. */
  145. function _setBeacon(address newBeacon) private {
  146. require(AddressUpgradeable.isContract(newBeacon), "ERC1967: new beacon is not a contract");
  147. require(
  148. AddressUpgradeable.isContract(IBeaconUpgradeable(newBeacon).implementation()),
  149. "ERC1967: beacon implementation is not a contract"
  150. );
  151. StorageSlotUpgradeable.getAddressSlot(_BEACON_SLOT).value = newBeacon;
  152. }
  153. /**
  154. * @dev Perform beacon upgrade with additional setup call. Note: This upgrades the address of the beacon, it does
  155. * not upgrade the implementation contained in the beacon (see {UpgradeableBeacon-_setImplementation} for that).
  156. *
  157. * Emits a {BeaconUpgraded} event.
  158. */
  159. function _upgradeBeaconToAndCall(
  160. address newBeacon,
  161. bytes memory data,
  162. bool forceCall
  163. ) internal {
  164. _setBeacon(newBeacon);
  165. emit BeaconUpgraded(newBeacon);
  166. if (data.length > 0 || forceCall) {
  167. _functionDelegateCall(IBeaconUpgradeable(newBeacon).implementation(), data);
  168. }
  169. }
  170. /**
  171. * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
  172. * but performing a delegate call.
  173. *
  174. * _Available since v3.4._
  175. */
  176. function _functionDelegateCall(address target, bytes memory data) private returns (bytes memory) {
  177. require(AddressUpgradeable.isContract(target), "Address: delegate call to non-contract");
  178. // solhint-disable-next-line avoid-low-level-calls
  179. (bool success, bytes memory returndata) = target.delegatecall(data);
  180. return AddressUpgradeable.verifyCallResult(success, returndata, "Address: low-level delegate call failed");
  181. }
  182. /**
  183. * This empty reserved space is put in place to allow future versions to add new
  184. * variables without shifting down storage in the inheritance chain.
  185. * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
  186. */
  187. uint256[50] private __gap;
  188. }