ERC1967Utils.sol 6.1 KB

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