ERC1967Utils.sol 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 {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[EIP1967] 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, and is
  29. * validated in the constructor.
  30. */
  31. // solhint-disable-next-line private-vars-leading-underscore
  32. bytes32 internal constant IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
  33. /**
  34. * @dev The `implementation` of the proxy is invalid.
  35. */
  36. error ERC1967InvalidImplementation(address implementation);
  37. /**
  38. * @dev The `admin` of the proxy is invalid.
  39. */
  40. error ERC1967InvalidAdmin(address admin);
  41. /**
  42. * @dev The `beacon` of the proxy is invalid.
  43. */
  44. error ERC1967InvalidBeacon(address beacon);
  45. /**
  46. * @dev An upgrade function sees `msg.value > 0` that may be lost.
  47. */
  48. error ERC1967NonPayable();
  49. /**
  50. * @dev Returns the current implementation address.
  51. */
  52. function getImplementation() internal view returns (address) {
  53. return StorageSlot.getAddressSlot(IMPLEMENTATION_SLOT).value;
  54. }
  55. /**
  56. * @dev Stores a new address in the EIP1967 implementation slot.
  57. */
  58. function _setImplementation(address newImplementation) private {
  59. if (newImplementation.code.length == 0) {
  60. revert ERC1967InvalidImplementation(newImplementation);
  61. }
  62. StorageSlot.getAddressSlot(IMPLEMENTATION_SLOT).value = newImplementation;
  63. }
  64. /**
  65. * @dev Performs implementation upgrade with additional setup call if data is nonempty.
  66. * This function is payable only if the setup call is performed, otherwise `msg.value` is rejected
  67. * to avoid stuck value in the contract.
  68. *
  69. * Emits an {IERC1967-Upgraded} event.
  70. */
  71. function upgradeToAndCall(address newImplementation, bytes memory data) internal {
  72. _setImplementation(newImplementation);
  73. emit Upgraded(newImplementation);
  74. if (data.length > 0) {
  75. Address.functionDelegateCall(newImplementation, data);
  76. } else {
  77. _checkNonPayable();
  78. }
  79. }
  80. /**
  81. * @dev Storage slot with the admin of the contract.
  82. * This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1, and is
  83. * validated in the constructor.
  84. */
  85. // solhint-disable-next-line private-vars-leading-underscore
  86. bytes32 internal constant ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
  87. /**
  88. * @dev Returns the current admin.
  89. *
  90. * TIP: To get this value clients can read directly from the storage slot shown below (specified by EIP1967) using the
  91. * https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
  92. * `0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103`
  93. */
  94. function getAdmin() internal view returns (address) {
  95. return StorageSlot.getAddressSlot(ADMIN_SLOT).value;
  96. }
  97. /**
  98. * @dev Stores a new address in the EIP1967 admin slot.
  99. */
  100. function _setAdmin(address newAdmin) private {
  101. if (newAdmin == address(0)) {
  102. revert ERC1967InvalidAdmin(address(0));
  103. }
  104. StorageSlot.getAddressSlot(ADMIN_SLOT).value = newAdmin;
  105. }
  106. /**
  107. * @dev Changes the admin of the proxy.
  108. *
  109. * Emits an {IERC1967-AdminChanged} event.
  110. */
  111. function changeAdmin(address newAdmin) internal {
  112. emit AdminChanged(getAdmin(), newAdmin);
  113. _setAdmin(newAdmin);
  114. }
  115. /**
  116. * @dev The storage slot of the UpgradeableBeacon contract which defines the implementation for this proxy.
  117. * This is bytes32(uint256(keccak256('eip1967.proxy.beacon')) - 1) and is validated in the constructor.
  118. */
  119. // solhint-disable-next-line private-vars-leading-underscore
  120. bytes32 internal constant BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50;
  121. /**
  122. * @dev Returns the current beacon.
  123. */
  124. function getBeacon() internal view returns (address) {
  125. return StorageSlot.getAddressSlot(BEACON_SLOT).value;
  126. }
  127. /**
  128. * @dev Stores a new beacon in the EIP1967 beacon slot.
  129. */
  130. function _setBeacon(address newBeacon) private {
  131. if (newBeacon.code.length == 0) {
  132. revert ERC1967InvalidBeacon(newBeacon);
  133. }
  134. address beaconImplementation = IBeacon(newBeacon).implementation();
  135. if (beaconImplementation.code.length == 0) {
  136. revert ERC1967InvalidImplementation(beaconImplementation);
  137. }
  138. StorageSlot.getAddressSlot(BEACON_SLOT).value = newBeacon;
  139. }
  140. /**
  141. * @dev Change the beacon and trigger a setup call if data is nonempty.
  142. * This function is payable only if the setup call is performed, otherwise `msg.value` is rejected
  143. * to avoid stuck value in the contract.
  144. *
  145. * Emits an {IERC1967-BeaconUpgraded} event.
  146. *
  147. * CAUTION: Invoking this function has no effect on an instance of {BeaconProxy} since v5, since
  148. * it uses an immutable beacon without looking at the value of the ERC-1967 beacon slot for
  149. * efficiency.
  150. */
  151. function upgradeBeaconToAndCall(address newBeacon, bytes memory data) internal {
  152. _setBeacon(newBeacon);
  153. emit BeaconUpgraded(newBeacon);
  154. if (data.length > 0) {
  155. Address.functionDelegateCall(IBeacon(newBeacon).implementation(), data);
  156. } else {
  157. _checkNonPayable();
  158. }
  159. }
  160. /**
  161. * @dev Reverts if `msg.value` is not zero.
  162. */
  163. function _checkNonPayable() private {
  164. if (msg.value > 0) {
  165. revert ERC1967NonPayable();
  166. }
  167. }
  168. }