ERC1967Storage.sol 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. * _Available since v4.1._
  11. */
  12. abstract contract ERC1967Storage {
  13. /**
  14. * @dev Storage slot with the address of the current implementation.
  15. * This is the keccak-256 hash of "eip1967.proxy.implementation" subtracted by 1, and is
  16. * validated in the constructor.
  17. */
  18. bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
  19. /**
  20. * @dev Returns the current implementation address.
  21. */
  22. function _getImplementation() internal view returns (address) {
  23. return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
  24. }
  25. /**
  26. * @dev Stores a new address in the EIP1967 implementation slot.
  27. */
  28. function _setImplementation(address newImplementation) internal {
  29. require(Address.isContract(newImplementation), "ERC1967: new implementation is not a contract");
  30. StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
  31. }
  32. /**
  33. * @dev The storage slot of the UpgradeableBeacon contract which defines the implementation for this proxy.
  34. * This is bytes32(uint256(keccak256('eip1967.proxy.beacon')) - 1)) and is validated in the constructor.
  35. */
  36. bytes32 internal constant _BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50;
  37. /**
  38. * @dev Returns the current beacon.
  39. */
  40. function _getBeacon() internal view returns (address) {
  41. return StorageSlot.getAddressSlot(_BEACON_SLOT).value;
  42. }
  43. /**
  44. * @dev Stores a new beacon in the EIP1967 beacon slot.
  45. */
  46. function _setBeacon(address newBeacon) internal {
  47. require(
  48. Address.isContract(newBeacon),
  49. "ERC1967: new beacon is not a contract"
  50. );
  51. require(
  52. Address.isContract(IBeacon(newBeacon).implementation()),
  53. "ERC1967: beacon implementation is not a contract"
  54. );
  55. StorageSlot.getAddressSlot(_BEACON_SLOT).value = newBeacon;
  56. }
  57. /**
  58. * @dev Storage slot with the admin of the contract.
  59. * This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1, and is
  60. * validated in the constructor.
  61. */
  62. bytes32 internal constant _ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
  63. /**
  64. * @dev Returns the current admin.
  65. */
  66. function _getAdmin() internal view returns (address) {
  67. return StorageSlot.getAddressSlot(_ADMIN_SLOT).value;
  68. }
  69. /**
  70. * @dev Stores a new address in the EIP1967 admin slot.
  71. */
  72. function _setAdmin(address newAdmin) internal {
  73. require(newAdmin != address(0), "ERC1967: new admin is the zero address");
  74. StorageSlot.getAddressSlot(_ADMIN_SLOT).value = newAdmin;
  75. }
  76. }