ERC1967Utils.sol 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 Perform implementation upgrade with additional setup call.
  66. *
  67. * Emits an {IERC1967-Upgraded} event.
  68. */
  69. function upgradeToAndCall(address newImplementation, bytes memory data) internal {
  70. _setImplementation(newImplementation);
  71. emit Upgraded(newImplementation);
  72. if (data.length > 0) {
  73. Address.functionDelegateCall(newImplementation, data);
  74. } else {
  75. _checkNonPayable();
  76. }
  77. }
  78. /**
  79. * @dev Storage slot with the admin of the contract.
  80. * This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1, and is
  81. * validated in the constructor.
  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 EIP1967) using the
  89. * 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 EIP1967 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 bytes32(uint256(keccak256('eip1967.proxy.beacon')) - 1) and is validated in the constructor.
  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 EIP1967 beacon slot.
  127. */
  128. function _setBeacon(address newBeacon) private {
  129. if (newBeacon.code.length == 0) {
  130. revert ERC1967InvalidBeacon(newBeacon);
  131. }
  132. address beaconImplementation = IBeacon(newBeacon).implementation();
  133. if (beaconImplementation.code.length == 0) {
  134. revert ERC1967InvalidImplementation(beaconImplementation);
  135. }
  136. StorageSlot.getAddressSlot(BEACON_SLOT).value = newBeacon;
  137. }
  138. /**
  139. * @dev Change the beacon and trigger a setup call if data is nonempty.
  140. *
  141. * Emits an {IERC1967-BeaconUpgraded} event.
  142. *
  143. * CAUTION: Invoking this function has no effect on an instance of {BeaconProxy} since v5, since
  144. * it uses an immutable beacon without looking at the value of the ERC-1967 beacon slot for
  145. * efficiency.
  146. */
  147. function upgradeBeaconToAndCall(address newBeacon, bytes memory data) internal {
  148. _setBeacon(newBeacon);
  149. emit BeaconUpgraded(newBeacon);
  150. if (data.length > 0) {
  151. Address.functionDelegateCall(IBeacon(newBeacon).implementation(), data);
  152. } else {
  153. _checkNonPayable();
  154. }
  155. }
  156. function _checkNonPayable() private {
  157. if (msg.value > 0) {
  158. revert ERC1967NonPayable();
  159. }
  160. }
  161. }