ERC1967Upgrade.sol 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.2;
  3. import "./ERC1967Storage.sol";
  4. /**
  5. * @dev This abstract contract provides event emitting update functions for
  6. * https://eips.ethereum.org/EIPS/eip-1967[EIP1967] slots.
  7. *
  8. * _Available since v4.1._
  9. *
  10. * @custom:oz-upgrades-unsafe-allow delegatecall
  11. */
  12. abstract contract ERC1967Upgrade is ERC1967Storage {
  13. // This is the keccak-256 hash of "eip1967.proxy.rollback" subtracted by 1
  14. bytes32 private constant _ROLLBACK_SLOT = 0x4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd9143;
  15. /**
  16. * @dev Emitted when the implementation is upgraded.
  17. */
  18. event Upgraded(address indexed implementation);
  19. /**
  20. * @dev Emitted when the beacon is upgraded.
  21. */
  22. event BeaconUpgraded(address indexed beacon);
  23. /**
  24. * @dev Emitted when the admin account has changed.
  25. */
  26. event AdminChanged(address previousAdmin, address newAdmin);
  27. /**
  28. * @dev Perform implementation upgrade
  29. *
  30. * Emits an {Upgraded} event.
  31. */
  32. function _upgradeTo(address newImplementation) internal {
  33. _setImplementation(newImplementation);
  34. emit Upgraded(newImplementation);
  35. }
  36. /**
  37. * @dev Perform implementation upgrade with additional setup call.
  38. *
  39. * Emits an {Upgraded} event.
  40. */
  41. function _upgradeToAndCall(address newImplementation, bytes memory data, bool forceCall) internal {
  42. _setImplementation(newImplementation);
  43. emit Upgraded(newImplementation);
  44. if (data.length > 0 || forceCall) {
  45. Address.functionDelegateCall(newImplementation, data);
  46. }
  47. }
  48. /**
  49. * @dev Perform implementation upgrade with security checks for UUPS proxies, and additional setup call.
  50. *
  51. * Emits an {Upgraded} event.
  52. */
  53. function _upgradeToAndCallSecure(address newImplementation, bytes memory data, bool forceCall) internal {
  54. address oldImplementation = _getImplementation();
  55. // do inital upgrade
  56. _setImplementation(newImplementation);
  57. // do setup call
  58. if (data.length > 0 || forceCall) {
  59. Address.functionDelegateCall(newImplementation, data);
  60. }
  61. // check if nested in an upgrade check
  62. StorageSlot.BooleanSlot storage rollbackTesting = StorageSlot.getBooleanSlot(_ROLLBACK_SLOT);
  63. if (!rollbackTesting.value) {
  64. // trigger upgrade check with flag set to true
  65. rollbackTesting.value = true;
  66. Address.functionDelegateCall(
  67. newImplementation,
  68. abi.encodeWithSignature(
  69. "upgradeTo(address)",
  70. oldImplementation
  71. )
  72. );
  73. rollbackTesting.value = false;
  74. // check upgrade was effective
  75. require(oldImplementation == _getImplementation(), "ERC1967Upgrade: upgrade breaks further upgrades");
  76. // reset upgrade
  77. _setImplementation(newImplementation);
  78. // emit event
  79. emit Upgraded(newImplementation);
  80. }
  81. }
  82. /**
  83. * @dev Perform beacon upgrade with additional setup call. Note: This upgrades the address of the beacon, it does
  84. * not upgrade the implementation contained in the beacon (see {UpgradeableBeacon-_setImplementation} for that).
  85. *
  86. * Emits a {BeaconUpgraded} event.
  87. */
  88. function _upgradeBeaconToAndCall(address newBeacon, bytes memory data, bool forceCall) internal {
  89. _setBeacon(newBeacon);
  90. emit BeaconUpgraded(newBeacon);
  91. if (data.length > 0 || forceCall) {
  92. Address.functionDelegateCall(IBeacon(newBeacon).implementation(), data);
  93. }
  94. }
  95. /**
  96. * @dev Changes the admin of the proxy.
  97. *
  98. * Emits an {AdminChanged} event.
  99. */
  100. function _changeAdmin(address newAdmin) internal {
  101. emit AdminChanged(_getAdmin(), newAdmin);
  102. _setAdmin(newAdmin);
  103. }
  104. }