StorageSlot.sol 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.0.0) (utils/StorageSlot.sol)
  3. // This file was procedurally generated from scripts/generate/templates/StorageSlot.js.
  4. pragma solidity ^0.8.20;
  5. /**
  6. * @dev Library for reading and writing primitive types to specific storage slots.
  7. *
  8. * Storage slots are often used to avoid storage conflict when dealing with upgradeable contracts.
  9. * This library helps with reading and writing to such slots without the need for inline assembly.
  10. *
  11. * The functions in this library return Slot structs that contain a `value` member that can be used to read or write.
  12. *
  13. * Example usage to set ERC1967 implementation slot:
  14. * ```solidity
  15. * contract ERC1967 {
  16. * bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
  17. *
  18. * function _getImplementation() internal view returns (address) {
  19. * return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
  20. * }
  21. *
  22. * function _setImplementation(address newImplementation) internal {
  23. * require(newImplementation.code.length > 0);
  24. * StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
  25. * }
  26. * }
  27. * ```
  28. */
  29. library StorageSlot {
  30. struct AddressSlot {
  31. address value;
  32. }
  33. struct BooleanSlot {
  34. bool value;
  35. }
  36. struct Bytes32Slot {
  37. bytes32 value;
  38. }
  39. struct Uint256Slot {
  40. uint256 value;
  41. }
  42. struct StringSlot {
  43. string value;
  44. }
  45. struct BytesSlot {
  46. bytes value;
  47. }
  48. /**
  49. * @dev Returns an `AddressSlot` with member `value` located at `slot`.
  50. */
  51. function getAddressSlot(bytes32 slot) internal pure returns (AddressSlot storage r) {
  52. /// @solidity memory-safe-assembly
  53. assembly {
  54. r.slot := slot
  55. }
  56. }
  57. /**
  58. * @dev Returns an `BooleanSlot` with member `value` located at `slot`.
  59. */
  60. function getBooleanSlot(bytes32 slot) internal pure returns (BooleanSlot storage r) {
  61. /// @solidity memory-safe-assembly
  62. assembly {
  63. r.slot := slot
  64. }
  65. }
  66. /**
  67. * @dev Returns an `Bytes32Slot` with member `value` located at `slot`.
  68. */
  69. function getBytes32Slot(bytes32 slot) internal pure returns (Bytes32Slot storage r) {
  70. /// @solidity memory-safe-assembly
  71. assembly {
  72. r.slot := slot
  73. }
  74. }
  75. /**
  76. * @dev Returns an `Uint256Slot` with member `value` located at `slot`.
  77. */
  78. function getUint256Slot(bytes32 slot) internal pure returns (Uint256Slot storage r) {
  79. /// @solidity memory-safe-assembly
  80. assembly {
  81. r.slot := slot
  82. }
  83. }
  84. /**
  85. * @dev Returns an `StringSlot` with member `value` located at `slot`.
  86. */
  87. function getStringSlot(bytes32 slot) internal pure returns (StringSlot storage r) {
  88. /// @solidity memory-safe-assembly
  89. assembly {
  90. r.slot := slot
  91. }
  92. }
  93. /**
  94. * @dev Returns an `StringSlot` representation of the string storage pointer `store`.
  95. */
  96. function getStringSlot(string storage store) internal pure returns (StringSlot storage r) {
  97. /// @solidity memory-safe-assembly
  98. assembly {
  99. r.slot := store.slot
  100. }
  101. }
  102. /**
  103. * @dev Returns an `BytesSlot` with member `value` located at `slot`.
  104. */
  105. function getBytesSlot(bytes32 slot) internal pure returns (BytesSlot storage r) {
  106. /// @solidity memory-safe-assembly
  107. assembly {
  108. r.slot := slot
  109. }
  110. }
  111. /**
  112. * @dev Returns an `BytesSlot` representation of the bytes storage pointer `store`.
  113. */
  114. function getBytesSlot(bytes storage store) internal pure returns (BytesSlot storage r) {
  115. /// @solidity memory-safe-assembly
  116. assembly {
  117. r.slot := store.slot
  118. }
  119. }
  120. }