ERC1967Upgrade.sol 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.5.0) (proxy/ERC1967/ERC1967Upgrade.sol)
  3. pragma solidity ^0.8.2;
  4. import "../beacon/IBeacon.sol";
  5. import "../../interfaces/draft-IERC1822.sol";
  6. import "../../utils/Address.sol";
  7. import "../../utils/StorageSlot.sol";
  8. /**
  9. * @dev This abstract contract provides getters and event emitting update functions for
  10. * https://eips.ethereum.org/EIPS/eip-1967[EIP1967] slots.
  11. *
  12. * _Available since v4.1._
  13. *
  14. * @custom:oz-upgrades-unsafe-allow delegatecall
  15. */
  16. abstract contract ERC1967Upgrade {
  17. // This is the keccak-256 hash of "eip1967.proxy.rollback" subtracted by 1
  18. bytes32 private constant _ROLLBACK_SLOT = 0x4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd9143;
  19. /**
  20. * @dev Storage slot with the address of the current implementation.
  21. * This is the keccak-256 hash of "eip1967.proxy.implementation" subtracted by 1, and is
  22. * validated in the constructor.
  23. */
  24. bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
  25. /**
  26. * @dev Emitted when the implementation is upgraded.
  27. */
  28. event Upgraded(address indexed implementation);
  29. /**
  30. * @dev Returns the current implementation address.
  31. */
  32. function _getImplementation() internal view returns (address) {
  33. return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
  34. }
  35. /**
  36. * @dev Stores a new address in the EIP1967 implementation slot.
  37. */
  38. function _setImplementation(address newImplementation) private {
  39. require(Address.isContract(newImplementation), "ERC1967: new implementation is not a contract");
  40. StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
  41. }
  42. /**
  43. * @dev Perform implementation upgrade
  44. *
  45. * Emits an {Upgraded} event.
  46. */
  47. function _upgradeTo(address newImplementation) internal {
  48. _setImplementation(newImplementation);
  49. emit Upgraded(newImplementation);
  50. }
  51. /**
  52. * @dev Perform implementation upgrade with additional setup call.
  53. *
  54. * Emits an {Upgraded} event.
  55. */
  56. function _upgradeToAndCall(
  57. address newImplementation,
  58. bytes memory data,
  59. bool forceCall
  60. ) internal {
  61. _upgradeTo(newImplementation);
  62. if (data.length > 0 || forceCall) {
  63. Address.functionDelegateCall(newImplementation, data);
  64. }
  65. }
  66. /**
  67. * @dev Perform implementation upgrade with security checks for UUPS proxies, and additional setup call.
  68. *
  69. * Emits an {Upgraded} event.
  70. */
  71. function _upgradeToAndCallUUPS(
  72. address newImplementation,
  73. bytes memory data,
  74. bool forceCall
  75. ) internal {
  76. // Upgrades from old implementations will perform a rollback test. This test requires the new
  77. // implementation to upgrade back to the old, non-ERC1822 compliant, implementation. Removing
  78. // this special case will break upgrade paths from old UUPS implementation to new ones.
  79. if (StorageSlot.getBooleanSlot(_ROLLBACK_SLOT).value) {
  80. _setImplementation(newImplementation);
  81. } else {
  82. try IERC1822Proxiable(newImplementation).proxiableUUID() returns (bytes32 slot) {
  83. require(slot == _IMPLEMENTATION_SLOT, "ERC1967Upgrade: unsupported proxiableUUID");
  84. } catch {
  85. revert("ERC1967Upgrade: new implementation is not UUPS");
  86. }
  87. _upgradeToAndCall(newImplementation, data, forceCall);
  88. }
  89. }
  90. /**
  91. * @dev Storage slot with the admin of the contract.
  92. * This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1, and is
  93. * validated in the constructor.
  94. */
  95. bytes32 internal constant _ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
  96. /**
  97. * @dev Emitted when the admin account has changed.
  98. */
  99. event AdminChanged(address previousAdmin, address newAdmin);
  100. /**
  101. * @dev Returns the current admin.
  102. *
  103. * TIP: To get this value clients can read directly from the storage slot shown below (specified by EIP1967) using the
  104. * https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
  105. * `0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103`
  106. */
  107. function _getAdmin() internal view returns (address) {
  108. return StorageSlot.getAddressSlot(_ADMIN_SLOT).value;
  109. }
  110. /**
  111. * @dev Stores a new address in the EIP1967 admin slot.
  112. */
  113. function _setAdmin(address newAdmin) private {
  114. require(newAdmin != address(0), "ERC1967: new admin is the zero address");
  115. StorageSlot.getAddressSlot(_ADMIN_SLOT).value = newAdmin;
  116. }
  117. /**
  118. * @dev Changes the admin of the proxy.
  119. *
  120. * Emits an {AdminChanged} event.
  121. */
  122. function _changeAdmin(address newAdmin) internal {
  123. emit AdminChanged(_getAdmin(), newAdmin);
  124. _setAdmin(newAdmin);
  125. }
  126. /**
  127. * @dev The storage slot of the UpgradeableBeacon contract which defines the implementation for this proxy.
  128. * This is bytes32(uint256(keccak256('eip1967.proxy.beacon')) - 1)) and is validated in the constructor.
  129. */
  130. bytes32 internal constant _BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50;
  131. /**
  132. * @dev Emitted when the beacon is upgraded.
  133. */
  134. event BeaconUpgraded(address indexed beacon);
  135. /**
  136. * @dev Returns the current beacon.
  137. */
  138. function _getBeacon() internal view returns (address) {
  139. return StorageSlot.getAddressSlot(_BEACON_SLOT).value;
  140. }
  141. /**
  142. * @dev Stores a new beacon in the EIP1967 beacon slot.
  143. */
  144. function _setBeacon(address newBeacon) private {
  145. require(Address.isContract(newBeacon), "ERC1967: new beacon is not a contract");
  146. require(
  147. Address.isContract(IBeacon(newBeacon).implementation()),
  148. "ERC1967: beacon implementation is not a contract"
  149. );
  150. StorageSlot.getAddressSlot(_BEACON_SLOT).value = newBeacon;
  151. }
  152. /**
  153. * @dev Perform beacon upgrade with additional setup call. Note: This upgrades the address of the beacon, it does
  154. * not upgrade the implementation contained in the beacon (see {UpgradeableBeacon-_setImplementation} for that).
  155. *
  156. * Emits a {BeaconUpgraded} event.
  157. */
  158. function _upgradeBeaconToAndCall(
  159. address newBeacon,
  160. bytes memory data,
  161. bool forceCall
  162. ) internal {
  163. _setBeacon(newBeacon);
  164. emit BeaconUpgraded(newBeacon);
  165. if (data.length > 0 || forceCall) {
  166. Address.functionDelegateCall(IBeacon(newBeacon).implementation(), data);
  167. }
  168. }
  169. }