ERC1967Utils.sol 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.0.0) (proxy/ERC1967/ERC1967Utils.sol)
  3. pragma solidity ^0.8.20;
  4. import {IBeacon} from "../beacon/IBeacon.sol";
  5. import {Address} from "../../utils/Address.sol";
  6. import {StorageSlot} from "../../utils/StorageSlot.sol";
  7. /**
  8. * @dev This abstract contract provides getters and event emitting update functions for
  9. * https://eips.ethereum.org/EIPS/eip-1967[ERC-1967] slots.
  10. */
  11. library ERC1967Utils {
  12. // We re-declare ERC-1967 events here because they can't be used directly from IERC1967.
  13. // This will be fixed in Solidity 0.8.21. At that point we should remove these events.
  14. /**
  15. * @dev Emitted when the implementation is upgraded.
  16. */
  17. event Upgraded(address indexed implementation);
  18. /**
  19. * @dev Emitted when the admin account has changed.
  20. */
  21. event AdminChanged(address previousAdmin, address newAdmin);
  22. /**
  23. * @dev Emitted when the beacon is changed.
  24. */
  25. event BeaconUpgraded(address indexed beacon);
  26. /**
  27. * @dev Storage slot with the address of the current implementation.
  28. * This is the keccak-256 hash of "eip1967.proxy.implementation" subtracted by 1.
  29. */
  30. // solhint-disable-next-line private-vars-leading-underscore
  31. bytes32 internal constant IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
  32. /**
  33. * @dev The `implementation` of the proxy is invalid.
  34. */
  35. error ERC1967InvalidImplementation(address implementation);
  36. /**
  37. * @dev The `admin` of the proxy is invalid.
  38. */
  39. error ERC1967InvalidAdmin(address admin);
  40. /**
  41. * @dev The `beacon` of the proxy is invalid.
  42. */
  43. error ERC1967InvalidBeacon(address beacon);
  44. /**
  45. * @dev An upgrade function sees `msg.value > 0` that may be lost.
  46. */
  47. error ERC1967NonPayable();
  48. /**
  49. * @dev Returns the current implementation address.
  50. */
  51. function getImplementation() internal view returns (address) {
  52. return StorageSlot.getAddressSlot(IMPLEMENTATION_SLOT).value;
  53. }
  54. /**
  55. * @dev Stores a new address in the ERC-1967 implementation slot.
  56. */
  57. function _setImplementation(address newImplementation) private {
  58. if (newImplementation.code.length == 0) {
  59. revert ERC1967InvalidImplementation(newImplementation);
  60. }
  61. StorageSlot.getAddressSlot(IMPLEMENTATION_SLOT).value = newImplementation;
  62. }
  63. /**
  64. * @dev Performs implementation upgrade with additional setup call if data is nonempty.
  65. * This function is payable only if the setup call is performed, otherwise `msg.value` is rejected
  66. * to avoid stuck value in the contract.
  67. *
  68. * Emits an {IERC1967-Upgraded} event.
  69. */
  70. function upgradeToAndCall(address newImplementation, bytes memory data) internal {
  71. _setImplementation(newImplementation);
  72. emit Upgraded(newImplementation);
  73. if (data.length > 0) {
  74. Address.functionDelegateCall(newImplementation, data);
  75. } else {
  76. _checkNonPayable();
  77. }
  78. }
  79. /**
  80. * @dev Storage slot with the admin of the contract.
  81. * This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1.
  82. */
  83. // solhint-disable-next-line private-vars-leading-underscore
  84. bytes32 internal constant ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
  85. /**
  86. * @dev Returns the current admin.
  87. *
  88. * TIP: To get this value clients can read directly from the storage slot shown below (specified by ERC-1967) using
  89. * the https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
  90. * `0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103`
  91. */
  92. function getAdmin() internal view returns (address) {
  93. return StorageSlot.getAddressSlot(ADMIN_SLOT).value;
  94. }
  95. /**
  96. * @dev Stores a new address in the ERC-1967 admin slot.
  97. */
  98. function _setAdmin(address newAdmin) private {
  99. if (newAdmin == address(0)) {
  100. revert ERC1967InvalidAdmin(address(0));
  101. }
  102. StorageSlot.getAddressSlot(ADMIN_SLOT).value = newAdmin;
  103. }
  104. /**
  105. * @dev Changes the admin of the proxy.
  106. *
  107. * Emits an {IERC1967-AdminChanged} event.
  108. */
  109. function changeAdmin(address newAdmin) internal {
  110. emit AdminChanged(getAdmin(), newAdmin);
  111. _setAdmin(newAdmin);
  112. }
  113. /**
  114. * @dev The storage slot of the UpgradeableBeacon contract which defines the implementation for this proxy.
  115. * This is the keccak-256 hash of "eip1967.proxy.beacon" subtracted by 1.
  116. */
  117. // solhint-disable-next-line private-vars-leading-underscore
  118. bytes32 internal constant BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50;
  119. /**
  120. * @dev Returns the current beacon.
  121. */
  122. function getBeacon() internal view returns (address) {
  123. return StorageSlot.getAddressSlot(BEACON_SLOT).value;
  124. }
  125. /**
  126. * @dev Stores a new beacon in the ERC-1967 beacon slot.
  127. */
  128. function _setBeacon(address newBeacon) private {
  129. if (newBeacon.code.length == 0) {
  130. revert ERC1967InvalidBeacon(newBeacon);
  131. }
  132. StorageSlot.getAddressSlot(BEACON_SLOT).value = newBeacon;
  133. address beaconImplementation = IBeacon(newBeacon).implementation();
  134. if (beaconImplementation.code.length == 0) {
  135. revert ERC1967InvalidImplementation(beaconImplementation);
  136. }
  137. }
  138. /**
  139. * @dev Change the beacon and trigger a setup call if data is nonempty.
  140. * This function is payable only if the setup call is performed, otherwise `msg.value` is rejected
  141. * to avoid stuck value in the contract.
  142. *
  143. * Emits an {IERC1967-BeaconUpgraded} event.
  144. *
  145. * CAUTION: Invoking this function has no effect on an instance of {BeaconProxy} since v5, since
  146. * it uses an immutable beacon without looking at the value of the ERC-1967 beacon slot for
  147. * efficiency.
  148. */
  149. function upgradeBeaconToAndCall(address newBeacon, bytes memory data) internal {
  150. _setBeacon(newBeacon);
  151. emit BeaconUpgraded(newBeacon);
  152. if (data.length > 0) {
  153. Address.functionDelegateCall(IBeacon(newBeacon).implementation(), data);
  154. } else {
  155. _checkNonPayable();
  156. }
  157. }
  158. /**
  159. * @dev Reverts if `msg.value` is not zero. It can be used to avoid `msg.value` stuck in the contract
  160. * if an upgrade doesn't perform an initialization call.
  161. */
  162. function _checkNonPayable() private {
  163. if (msg.value > 0) {
  164. revert ERC1967NonPayable();
  165. }
  166. }
  167. }