ERC1967Utils.sol 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 Returns the current implementation address.
  47. */
  48. function getImplementation() internal view returns (address) {
  49. return StorageSlot.getAddressSlot(IMPLEMENTATION_SLOT).value;
  50. }
  51. /**
  52. * @dev Stores a new address in the EIP1967 implementation slot.
  53. */
  54. function _setImplementation(address newImplementation) private {
  55. if (newImplementation.code.length == 0) {
  56. revert ERC1967InvalidImplementation(newImplementation);
  57. }
  58. StorageSlot.getAddressSlot(IMPLEMENTATION_SLOT).value = newImplementation;
  59. }
  60. /**
  61. * @dev Perform implementation upgrade
  62. *
  63. * Emits an {IERC1967-Upgraded} event.
  64. */
  65. function upgradeTo(address newImplementation) internal {
  66. _setImplementation(newImplementation);
  67. emit Upgraded(newImplementation);
  68. }
  69. /**
  70. * @dev Perform implementation upgrade with additional setup call.
  71. *
  72. * Emits an {IERC1967-Upgraded} event.
  73. */
  74. function upgradeToAndCall(address newImplementation, bytes memory data, bool forceCall) internal {
  75. upgradeTo(newImplementation);
  76. if (data.length > 0 || forceCall) {
  77. Address.functionDelegateCall(newImplementation, data);
  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 Perform beacon upgrade with additional setup call. Note: This upgrades the address of the beacon, it does
  142. * not upgrade the implementation contained in the beacon (see {UpgradeableBeacon-_setImplementation} for that).
  143. *
  144. * Emits an {IERC1967-BeaconUpgraded} event.
  145. */
  146. function upgradeBeaconToAndCall(address newBeacon, bytes memory data, bool forceCall) internal {
  147. _setBeacon(newBeacon);
  148. emit BeaconUpgraded(newBeacon);
  149. if (data.length > 0 || forceCall) {
  150. Address.functionDelegateCall(IBeacon(newBeacon).implementation(), data);
  151. }
  152. }
  153. }