ERC1967Utils.sol 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. * _Available since v4.1._
  12. */
  13. library ERC1967Utils {
  14. // We re-declare ERC-1967 events here because they can't be used directly from IERC1967.
  15. // This will be fixed in Solidity 0.8.21. At that point we should remove these events.
  16. /**
  17. * @dev Emitted when the implementation is upgraded.
  18. */
  19. event Upgraded(address indexed implementation);
  20. /**
  21. * @dev Emitted when the admin account has changed.
  22. */
  23. event AdminChanged(address previousAdmin, address newAdmin);
  24. /**
  25. * @dev Emitted when the beacon is changed.
  26. */
  27. event BeaconUpgraded(address indexed beacon);
  28. /**
  29. * @dev Storage slot with the address of the current implementation.
  30. * This is the keccak-256 hash of "eip1967.proxy.implementation" subtracted by 1, and is
  31. * validated in the constructor.
  32. */
  33. // solhint-disable-next-line private-vars-leading-underscore
  34. bytes32 internal constant IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
  35. /**
  36. * @dev The `implementation` of the proxy is invalid.
  37. */
  38. error ERC1967InvalidImplementation(address implementation);
  39. /**
  40. * @dev The `admin` of the proxy is invalid.
  41. */
  42. error ERC1967InvalidAdmin(address admin);
  43. /**
  44. * @dev The `beacon` of the proxy is invalid.
  45. */
  46. error ERC1967InvalidBeacon(address beacon);
  47. /**
  48. * @dev Returns the current implementation address.
  49. */
  50. function getImplementation() internal view returns (address) {
  51. return StorageSlot.getAddressSlot(IMPLEMENTATION_SLOT).value;
  52. }
  53. /**
  54. * @dev Stores a new address in the EIP1967 implementation slot.
  55. */
  56. function _setImplementation(address newImplementation) private {
  57. if (newImplementation.code.length == 0) {
  58. revert ERC1967InvalidImplementation(newImplementation);
  59. }
  60. StorageSlot.getAddressSlot(IMPLEMENTATION_SLOT).value = newImplementation;
  61. }
  62. /**
  63. * @dev Perform implementation upgrade
  64. *
  65. * Emits an {IERC1967-Upgraded} event.
  66. */
  67. function upgradeTo(address newImplementation) internal {
  68. _setImplementation(newImplementation);
  69. emit Upgraded(newImplementation);
  70. }
  71. /**
  72. * @dev Perform implementation upgrade with additional setup call.
  73. *
  74. * Emits an {IERC1967-Upgraded} event.
  75. */
  76. function upgradeToAndCall(address newImplementation, bytes memory data, bool forceCall) internal {
  77. upgradeTo(newImplementation);
  78. if (data.length > 0 || forceCall) {
  79. Address.functionDelegateCall(newImplementation, data);
  80. }
  81. }
  82. /**
  83. * @dev Storage slot with the admin of the contract.
  84. * This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1, and is
  85. * validated in the constructor.
  86. */
  87. // solhint-disable-next-line private-vars-leading-underscore
  88. bytes32 internal constant ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
  89. /**
  90. * @dev Returns the current admin.
  91. *
  92. * TIP: To get this value clients can read directly from the storage slot shown below (specified by EIP1967) using the
  93. * https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
  94. * `0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103`
  95. */
  96. function getAdmin() internal view returns (address) {
  97. return StorageSlot.getAddressSlot(ADMIN_SLOT).value;
  98. }
  99. /**
  100. * @dev Stores a new address in the EIP1967 admin slot.
  101. */
  102. function _setAdmin(address newAdmin) private {
  103. if (newAdmin == address(0)) {
  104. revert ERC1967InvalidAdmin(address(0));
  105. }
  106. StorageSlot.getAddressSlot(ADMIN_SLOT).value = newAdmin;
  107. }
  108. /**
  109. * @dev Changes the admin of the proxy.
  110. *
  111. * Emits an {IERC1967-AdminChanged} event.
  112. */
  113. function changeAdmin(address newAdmin) internal {
  114. emit AdminChanged(getAdmin(), newAdmin);
  115. _setAdmin(newAdmin);
  116. }
  117. /**
  118. * @dev The storage slot of the UpgradeableBeacon contract which defines the implementation for this proxy.
  119. * This is bytes32(uint256(keccak256('eip1967.proxy.beacon')) - 1) and is validated in the constructor.
  120. */
  121. // solhint-disable-next-line private-vars-leading-underscore
  122. bytes32 internal constant BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50;
  123. /**
  124. * @dev Returns the current beacon.
  125. */
  126. function getBeacon() internal view returns (address) {
  127. return StorageSlot.getAddressSlot(BEACON_SLOT).value;
  128. }
  129. /**
  130. * @dev Stores a new beacon in the EIP1967 beacon slot.
  131. */
  132. function _setBeacon(address newBeacon) private {
  133. if (newBeacon.code.length == 0) {
  134. revert ERC1967InvalidBeacon(newBeacon);
  135. }
  136. address beaconImplementation = IBeacon(newBeacon).implementation();
  137. if (beaconImplementation.code.length == 0) {
  138. revert ERC1967InvalidImplementation(beaconImplementation);
  139. }
  140. StorageSlot.getAddressSlot(BEACON_SLOT).value = newBeacon;
  141. }
  142. /**
  143. * @dev Perform beacon upgrade with additional setup call. Note: This upgrades the address of the beacon, it does
  144. * not upgrade the implementation contained in the beacon (see {UpgradeableBeacon-_setImplementation} for that).
  145. *
  146. * Emits an {IERC1967-BeaconUpgraded} event.
  147. */
  148. function upgradeBeaconToAndCall(address newBeacon, bytes memory data, bool forceCall) internal {
  149. _setBeacon(newBeacon);
  150. emit BeaconUpgraded(newBeacon);
  151. if (data.length > 0 || forceCall) {
  152. Address.functionDelegateCall(IBeacon(newBeacon).implementation(), data);
  153. }
  154. }
  155. }