StorageSlot.sol 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 ERC-1967 implementation slot:
  14. * ```solidity
  15. * contract ERC1967 {
  16. * // Define the slot. Alternatively, use the SlotDerivation library to derive the slot.
  17. * bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
  18. *
  19. * function _getImplementation() internal view returns (address) {
  20. * return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
  21. * }
  22. *
  23. * function _setImplementation(address newImplementation) internal {
  24. * require(newImplementation.code.length > 0);
  25. * StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
  26. * }
  27. * }
  28. * ```
  29. *
  30. * TIP: Consider using this library along with {SlotDerivation}.
  31. */
  32. library StorageSlot {
  33. struct AddressSlot {
  34. address value;
  35. }
  36. struct BooleanSlot {
  37. bool value;
  38. }
  39. struct Bytes32Slot {
  40. bytes32 value;
  41. }
  42. struct Uint256Slot {
  43. uint256 value;
  44. }
  45. struct Int256Slot {
  46. int256 value;
  47. }
  48. struct StringSlot {
  49. string value;
  50. }
  51. struct BytesSlot {
  52. bytes value;
  53. }
  54. /**
  55. * @dev Returns an `AddressSlot` with member `value` located at `slot`.
  56. */
  57. function getAddressSlot(bytes32 slot) internal pure returns (AddressSlot storage r) {
  58. /// @solidity memory-safe-assembly
  59. assembly {
  60. r.slot := slot
  61. }
  62. }
  63. /**
  64. * @dev Returns an `BooleanSlot` with member `value` located at `slot`.
  65. */
  66. function getBooleanSlot(bytes32 slot) internal pure returns (BooleanSlot storage r) {
  67. /// @solidity memory-safe-assembly
  68. assembly {
  69. r.slot := slot
  70. }
  71. }
  72. /**
  73. * @dev Returns an `Bytes32Slot` with member `value` located at `slot`.
  74. */
  75. function getBytes32Slot(bytes32 slot) internal pure returns (Bytes32Slot storage r) {
  76. /// @solidity memory-safe-assembly
  77. assembly {
  78. r.slot := slot
  79. }
  80. }
  81. /**
  82. * @dev Returns an `Uint256Slot` with member `value` located at `slot`.
  83. */
  84. function getUint256Slot(bytes32 slot) internal pure returns (Uint256Slot storage r) {
  85. /// @solidity memory-safe-assembly
  86. assembly {
  87. r.slot := slot
  88. }
  89. }
  90. /**
  91. * @dev Returns an `Int256Slot` with member `value` located at `slot`.
  92. */
  93. function getInt256Slot(bytes32 slot) internal pure returns (Int256Slot storage r) {
  94. /// @solidity memory-safe-assembly
  95. assembly {
  96. r.slot := slot
  97. }
  98. }
  99. /**
  100. * @dev Returns an `StringSlot` with member `value` located at `slot`.
  101. */
  102. function getStringSlot(bytes32 slot) internal pure returns (StringSlot storage r) {
  103. /// @solidity memory-safe-assembly
  104. assembly {
  105. r.slot := slot
  106. }
  107. }
  108. /**
  109. * @dev Returns an `StringSlot` representation of the string storage pointer `store`.
  110. */
  111. function getStringSlot(string storage store) internal pure returns (StringSlot storage r) {
  112. /// @solidity memory-safe-assembly
  113. assembly {
  114. r.slot := store.slot
  115. }
  116. }
  117. /**
  118. * @dev Returns an `BytesSlot` with member `value` located at `slot`.
  119. */
  120. function getBytesSlot(bytes32 slot) internal pure returns (BytesSlot storage r) {
  121. /// @solidity memory-safe-assembly
  122. assembly {
  123. r.slot := slot
  124. }
  125. }
  126. /**
  127. * @dev Returns an `BytesSlot` representation of the bytes storage pointer `store`.
  128. */
  129. function getBytesSlot(bytes storage store) internal pure returns (BytesSlot storage r) {
  130. /// @solidity memory-safe-assembly
  131. assembly {
  132. r.slot := store.slot
  133. }
  134. }
  135. }