ERC1967Upgrade.sol 6.9 KB

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