ERC1967Upgrade.sol 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.5.0) (proxy/ERC1967/ERC1967Upgrade.sol)
  3. pragma solidity ^0.8.2;
  4. import "../beacon/IBeacon.sol";
  5. import "../../interfaces/IERC1967.sol";
  6. import "../../interfaces/draft-IERC1822.sol";
  7. import "../../utils/Address.sol";
  8. import "../../utils/StorageSlot.sol";
  9. /**
  10. * @dev This abstract contract provides getters and event emitting update functions for
  11. * https://eips.ethereum.org/EIPS/eip-1967[EIP1967] slots.
  12. *
  13. * _Available since v4.1._
  14. *
  15. * @custom:oz-upgrades-unsafe-allow delegatecall
  16. */
  17. abstract contract ERC1967Upgrade is IERC1967 {
  18. // This is the keccak-256 hash of "eip1967.proxy.rollback" subtracted by 1
  19. bytes32 private constant _ROLLBACK_SLOT = 0x4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd9143;
  20. /**
  21. * @dev Storage slot with the address of the current implementation.
  22. * This is the keccak-256 hash of "eip1967.proxy.implementation" subtracted by 1, and is
  23. * validated in the constructor.
  24. */
  25. bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
  26. /**
  27. * @dev Returns the current implementation address.
  28. */
  29. function _getImplementation() internal view returns (address) {
  30. return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
  31. }
  32. /**
  33. * @dev Stores a new address in the EIP1967 implementation slot.
  34. */
  35. function _setImplementation(address newImplementation) private {
  36. require(Address.isContract(newImplementation), "ERC1967: new implementation is not a contract");
  37. StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
  38. }
  39. /**
  40. * @dev Perform implementation upgrade
  41. *
  42. * Emits an {Upgraded} event.
  43. */
  44. function _upgradeTo(address newImplementation) internal {
  45. _setImplementation(newImplementation);
  46. emit Upgraded(newImplementation);
  47. }
  48. /**
  49. * @dev Perform implementation upgrade with additional setup call.
  50. *
  51. * Emits an {Upgraded} event.
  52. */
  53. function _upgradeToAndCall(
  54. address newImplementation,
  55. bytes memory data,
  56. bool forceCall
  57. ) internal {
  58. _upgradeTo(newImplementation);
  59. if (data.length > 0 || forceCall) {
  60. Address.functionDelegateCall(newImplementation, data);
  61. }
  62. }
  63. /**
  64. * @dev Perform implementation upgrade with security checks for UUPS proxies, and additional setup call.
  65. *
  66. * Emits an {Upgraded} event.
  67. */
  68. function _upgradeToAndCallUUPS(
  69. address newImplementation,
  70. bytes memory data,
  71. bool forceCall
  72. ) internal {
  73. // Upgrades from old implementations will perform a rollback test. This test requires the new
  74. // implementation to upgrade back to the old, non-ERC1822 compliant, implementation. Removing
  75. // this special case will break upgrade paths from old UUPS implementation to new ones.
  76. if (StorageSlot.getBooleanSlot(_ROLLBACK_SLOT).value) {
  77. _setImplementation(newImplementation);
  78. } else {
  79. try IERC1822Proxiable(newImplementation).proxiableUUID() returns (bytes32 slot) {
  80. require(slot == _IMPLEMENTATION_SLOT, "ERC1967Upgrade: unsupported proxiableUUID");
  81. } catch {
  82. revert("ERC1967Upgrade: new implementation is not UUPS");
  83. }
  84. _upgradeToAndCall(newImplementation, data, forceCall);
  85. }
  86. }
  87. /**
  88. * @dev Storage slot with the admin of the contract.
  89. * This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1, and is
  90. * validated in the constructor.
  91. */
  92. bytes32 internal constant _ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
  93. /**
  94. * @dev Returns the current admin.
  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. require(newAdmin != address(0), "ERC1967: new admin is the zero address");
  104. StorageSlot.getAddressSlot(_ADMIN_SLOT).value = newAdmin;
  105. }
  106. /**
  107. * @dev Changes the admin of the proxy.
  108. *
  109. * Emits an {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. bytes32 internal constant _BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50;
  120. /**
  121. * @dev Returns the current beacon.
  122. */
  123. function _getBeacon() internal view returns (address) {
  124. return StorageSlot.getAddressSlot(_BEACON_SLOT).value;
  125. }
  126. /**
  127. * @dev Stores a new beacon in the EIP1967 beacon slot.
  128. */
  129. function _setBeacon(address newBeacon) private {
  130. require(Address.isContract(newBeacon), "ERC1967: new beacon is not a contract");
  131. require(
  132. Address.isContract(IBeacon(newBeacon).implementation()),
  133. "ERC1967: beacon implementation is not a contract"
  134. );
  135. StorageSlot.getAddressSlot(_BEACON_SLOT).value = newBeacon;
  136. }
  137. /**
  138. * @dev Perform beacon upgrade with additional setup call. Note: This upgrades the address of the beacon, it does
  139. * not upgrade the implementation contained in the beacon (see {UpgradeableBeacon-_setImplementation} for that).
  140. *
  141. * Emits a {BeaconUpgraded} event.
  142. */
  143. function _upgradeBeaconToAndCall(
  144. address newBeacon,
  145. bytes memory data,
  146. bool forceCall
  147. ) internal {
  148. _setBeacon(newBeacon);
  149. emit BeaconUpgraded(newBeacon);
  150. if (data.length > 0 || forceCall) {
  151. Address.functionDelegateCall(IBeacon(newBeacon).implementation(), data);
  152. }
  153. }
  154. }