ERC1967Storage.sol 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "../beacon/IBeacon.sol";
  4. import "../../utils/Address.sol";
  5. import "../../utils/StorageSlot.sol";
  6. /**
  7. * @dev This abstract contract provides setters and getters for the different
  8. * https://eips.ethereum.org/EIPS/eip-1967[EIP1967] storage slots.
  9. */
  10. abstract contract ERC1967Storage {
  11. /**
  12. * @dev Storage slot with the address of the current implementation.
  13. * This is the keccak-256 hash of "eip1967.proxy.implementation" subtracted by 1, and is
  14. * validated in the constructor.
  15. */
  16. bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
  17. /**
  18. * @dev Returns the current implementation address.
  19. */
  20. function _getImplementation() internal view returns (address) {
  21. return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
  22. }
  23. /**
  24. * @dev Stores a new address in the EIP1967 implementation slot.
  25. */
  26. function _setImplementation(address newImplementation) internal {
  27. require(Address.isContract(newImplementation), "ERC1967: new implementation is not a contract");
  28. StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
  29. }
  30. /**
  31. * @dev The storage slot of the UpgradeableBeacon contract which defines the implementation for this proxy.
  32. * This is bytes32(uint256(keccak256('eip1967.proxy.beacon')) - 1)) and is validated in the constructor.
  33. */
  34. bytes32 internal constant _BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50;
  35. /**
  36. * @dev Returns the current beacon.
  37. */
  38. function _getBeacon() internal view returns (address) {
  39. return StorageSlot.getAddressSlot(_BEACON_SLOT).value;
  40. }
  41. /**
  42. * @dev Stores a new beacon in the EIP1967 beacon slot.
  43. */
  44. function _setBeacon(address newBeacon) internal {
  45. require(
  46. Address.isContract(newBeacon),
  47. "ERC1967: new beacon is not a contract"
  48. );
  49. require(
  50. Address.isContract(IBeacon(newBeacon).implementation()),
  51. "ERC1967: beacon implementation is not a contract"
  52. );
  53. StorageSlot.getAddressSlot(_BEACON_SLOT).value = newBeacon;
  54. }
  55. /**
  56. * @dev Storage slot with the admin of the contract.
  57. * This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1, and is
  58. * validated in the constructor.
  59. */
  60. bytes32 internal constant _ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
  61. /**
  62. * @dev Returns the current admin.
  63. */
  64. function _getAdmin() internal view returns (address) {
  65. return StorageSlot.getAddressSlot(_ADMIN_SLOT).value;
  66. }
  67. /**
  68. * @dev Stores a new address in the EIP1967 admin slot.
  69. */
  70. function _setAdmin(address newAdmin) internal {
  71. require(newAdmin != address(0), "ERC1967: new admin is the zero address");
  72. StorageSlot.getAddressSlot(_ADMIN_SLOT).value = newAdmin;
  73. }
  74. }