StorageSlot.sol 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.1 (utils/StorageSlot.sol)
  3. pragma solidity ^0.8.0;
  4. /**
  5. * @dev Library for reading and writing primitive types to specific storage slots.
  6. *
  7. * Storage slots are often used to avoid storage conflict when dealing with upgradeable contracts.
  8. * This library helps with reading and writing to such slots without the need for inline assembly.
  9. *
  10. * The functions in this library return Slot structs that contain a `value` member that can be used to read or write.
  11. *
  12. * Example usage to set ERC1967 implementation slot:
  13. * ```
  14. * contract ERC1967 {
  15. * bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
  16. *
  17. * function _getImplementation() internal view returns (address) {
  18. * return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
  19. * }
  20. *
  21. * function _setImplementation(address newImplementation) internal {
  22. * require(Address.isContract(newImplementation), "ERC1967: new implementation is not a contract");
  23. * StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
  24. * }
  25. * }
  26. * ```
  27. *
  28. * _Available since v4.1 for `address`, `bool`, `bytes32`, and `uint256`._
  29. */
  30. library StorageSlot {
  31. struct AddressSlot {
  32. address value;
  33. }
  34. struct BooleanSlot {
  35. bool value;
  36. }
  37. struct Bytes32Slot {
  38. bytes32 value;
  39. }
  40. struct Uint256Slot {
  41. uint256 value;
  42. }
  43. /**
  44. * @dev Returns an `AddressSlot` with member `value` located at `slot`.
  45. */
  46. function getAddressSlot(bytes32 slot) internal pure returns (AddressSlot storage r) {
  47. assembly {
  48. r.slot := slot
  49. }
  50. }
  51. /**
  52. * @dev Returns an `BooleanSlot` with member `value` located at `slot`.
  53. */
  54. function getBooleanSlot(bytes32 slot) internal pure returns (BooleanSlot storage r) {
  55. assembly {
  56. r.slot := slot
  57. }
  58. }
  59. /**
  60. * @dev Returns an `Bytes32Slot` with member `value` located at `slot`.
  61. */
  62. function getBytes32Slot(bytes32 slot) internal pure returns (Bytes32Slot storage r) {
  63. assembly {
  64. r.slot := slot
  65. }
  66. }
  67. /**
  68. * @dev Returns an `Uint256Slot` with member `value` located at `slot`.
  69. */
  70. function getUint256Slot(bytes32 slot) internal pure returns (Uint256Slot storage r) {
  71. assembly {
  72. r.slot := slot
  73. }
  74. }
  75. }