ERC1967Utils.sol 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (proxy/ERC1967/ERC1967Utils.sol)
  3. pragma solidity ^0.8.20;
  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. library ERC1967Utils {
  16. // We re-declare ERC-1967 events here because they can't be used directly from IERC1967.
  17. // This will be fixed in Solidity 0.8.21. At that point we should remove these events.
  18. /**
  19. * @dev Emitted when the implementation is upgraded.
  20. */
  21. event Upgraded(address indexed implementation);
  22. /**
  23. * @dev Emitted when the admin account has changed.
  24. */
  25. event AdminChanged(address previousAdmin, address newAdmin);
  26. /**
  27. * @dev Emitted when the beacon is changed.
  28. */
  29. event BeaconUpgraded(address indexed beacon);
  30. // This is the keccak-256 hash of "eip1967.proxy.rollback" subtracted by 1
  31. bytes32 private constant _ROLLBACK_SLOT = 0x4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd9143;
  32. /**
  33. * @dev Storage slot with the address of the current implementation.
  34. * This is the keccak-256 hash of "eip1967.proxy.implementation" subtracted by 1, and is
  35. * validated in the constructor.
  36. */
  37. // solhint-disable-next-line private-vars-leading-underscore
  38. bytes32 internal constant IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
  39. /**
  40. * @dev The `implementation` of the proxy is invalid.
  41. */
  42. error ERC1967InvalidImplementation(address implementation);
  43. /**
  44. * @dev The `admin` of the proxy is invalid.
  45. */
  46. error ERC1967InvalidAdmin(address admin);
  47. /**
  48. * @dev The `beacon` of the proxy is invalid.
  49. */
  50. error ERC1967InvalidBeacon(address beacon);
  51. /**
  52. * @dev The storage `slot` is unsupported as a UUID.
  53. */
  54. error ERC1967UnsupportedProxiableUUID(bytes32 slot);
  55. /**
  56. * @dev Returns the current implementation address.
  57. */
  58. function getImplementation() internal view returns (address) {
  59. return StorageSlot.getAddressSlot(IMPLEMENTATION_SLOT).value;
  60. }
  61. /**
  62. * @dev Stores a new address in the EIP1967 implementation slot.
  63. */
  64. function _setImplementation(address newImplementation) private {
  65. if (newImplementation.code.length == 0) {
  66. revert ERC1967InvalidImplementation(newImplementation);
  67. }
  68. StorageSlot.getAddressSlot(IMPLEMENTATION_SLOT).value = newImplementation;
  69. }
  70. /**
  71. * @dev Perform implementation upgrade
  72. *
  73. * Emits an {IERC1967-Upgraded} event.
  74. */
  75. function upgradeTo(address newImplementation) internal {
  76. _setImplementation(newImplementation);
  77. emit Upgraded(newImplementation);
  78. }
  79. /**
  80. * @dev Perform implementation upgrade with additional setup call.
  81. *
  82. * Emits an {IERC1967-Upgraded} event.
  83. */
  84. function upgradeToAndCall(address newImplementation, bytes memory data, bool forceCall) internal {
  85. upgradeTo(newImplementation);
  86. if (data.length > 0 || forceCall) {
  87. Address.functionDelegateCall(newImplementation, data);
  88. }
  89. }
  90. /**
  91. * @dev Perform implementation upgrade with security checks for UUPS proxies, and additional setup call.
  92. *
  93. * Emits an {IERC1967-Upgraded} event.
  94. */
  95. function upgradeToAndCallUUPS(address newImplementation, bytes memory data, bool forceCall) internal {
  96. // Upgrades from old implementations will perform a rollback test. This test requires the new
  97. // implementation to upgrade back to the old, non-ERC1822 compliant, implementation. Removing
  98. // this special case will break upgrade paths from old UUPS implementation to new ones.
  99. if (StorageSlot.getBooleanSlot(_ROLLBACK_SLOT).value) {
  100. _setImplementation(newImplementation);
  101. } else {
  102. try IERC1822Proxiable(newImplementation).proxiableUUID() returns (bytes32 slot) {
  103. if (slot != IMPLEMENTATION_SLOT) {
  104. revert ERC1967UnsupportedProxiableUUID(slot);
  105. }
  106. } catch {
  107. // The implementation is not UUPS
  108. revert ERC1967InvalidImplementation(newImplementation);
  109. }
  110. upgradeToAndCall(newImplementation, data, forceCall);
  111. }
  112. }
  113. /**
  114. * @dev Storage slot with the admin of the contract.
  115. * This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1, and is
  116. * validated in the constructor.
  117. */
  118. // solhint-disable-next-line private-vars-leading-underscore
  119. bytes32 internal constant ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
  120. /**
  121. * @dev Returns the current admin.
  122. *
  123. * TIP: To get this value clients can read directly from the storage slot shown below (specified by EIP1967) using the
  124. * https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
  125. * `0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103`
  126. */
  127. function getAdmin() internal view returns (address) {
  128. return StorageSlot.getAddressSlot(ADMIN_SLOT).value;
  129. }
  130. /**
  131. * @dev Stores a new address in the EIP1967 admin slot.
  132. */
  133. function _setAdmin(address newAdmin) private {
  134. if (newAdmin == address(0)) {
  135. revert ERC1967InvalidAdmin(address(0));
  136. }
  137. StorageSlot.getAddressSlot(ADMIN_SLOT).value = newAdmin;
  138. }
  139. /**
  140. * @dev Changes the admin of the proxy.
  141. *
  142. * Emits an {IERC1967-AdminChanged} event.
  143. */
  144. function changeAdmin(address newAdmin) internal {
  145. emit AdminChanged(getAdmin(), newAdmin);
  146. _setAdmin(newAdmin);
  147. }
  148. /**
  149. * @dev The storage slot of the UpgradeableBeacon contract which defines the implementation for this proxy.
  150. * This is bytes32(uint256(keccak256('eip1967.proxy.beacon')) - 1) and is validated in the constructor.
  151. */
  152. // solhint-disable-next-line private-vars-leading-underscore
  153. bytes32 internal constant BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50;
  154. /**
  155. * @dev Returns the current beacon.
  156. */
  157. function getBeacon() internal view returns (address) {
  158. return StorageSlot.getAddressSlot(BEACON_SLOT).value;
  159. }
  160. /**
  161. * @dev Stores a new beacon in the EIP1967 beacon slot.
  162. */
  163. function _setBeacon(address newBeacon) private {
  164. if (newBeacon.code.length == 0) {
  165. revert ERC1967InvalidBeacon(newBeacon);
  166. }
  167. address beaconImplementation = IBeacon(newBeacon).implementation();
  168. if (beaconImplementation.code.length == 0) {
  169. revert ERC1967InvalidImplementation(beaconImplementation);
  170. }
  171. StorageSlot.getAddressSlot(BEACON_SLOT).value = newBeacon;
  172. }
  173. /**
  174. * @dev Perform beacon upgrade with additional setup call. Note: This upgrades the address of the beacon, it does
  175. * not upgrade the implementation contained in the beacon (see {UpgradeableBeacon-_setImplementation} for that).
  176. *
  177. * Emits an {IERC1967-BeaconUpgraded} event.
  178. */
  179. function upgradeBeaconToAndCall(address newBeacon, bytes memory data, bool forceCall) internal {
  180. _setBeacon(newBeacon);
  181. emit BeaconUpgraded(newBeacon);
  182. if (data.length > 0 || forceCall) {
  183. Address.functionDelegateCall(IBeacon(newBeacon).implementation(), data);
  184. }
  185. }
  186. }