StorageSlot.sol 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.1.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. assembly ("memory-safe") {
  59. r.slot := slot
  60. }
  61. }
  62. /**
  63. * @dev Returns a `BooleanSlot` with member `value` located at `slot`.
  64. */
  65. function getBooleanSlot(bytes32 slot) internal pure returns (BooleanSlot storage r) {
  66. assembly ("memory-safe") {
  67. r.slot := slot
  68. }
  69. }
  70. /**
  71. * @dev Returns a `Bytes32Slot` with member `value` located at `slot`.
  72. */
  73. function getBytes32Slot(bytes32 slot) internal pure returns (Bytes32Slot storage r) {
  74. assembly ("memory-safe") {
  75. r.slot := slot
  76. }
  77. }
  78. /**
  79. * @dev Returns a `Uint256Slot` with member `value` located at `slot`.
  80. */
  81. function getUint256Slot(bytes32 slot) internal pure returns (Uint256Slot storage r) {
  82. assembly ("memory-safe") {
  83. r.slot := slot
  84. }
  85. }
  86. /**
  87. * @dev Returns a `Int256Slot` with member `value` located at `slot`.
  88. */
  89. function getInt256Slot(bytes32 slot) internal pure returns (Int256Slot storage r) {
  90. assembly ("memory-safe") {
  91. r.slot := slot
  92. }
  93. }
  94. /**
  95. * @dev Returns a `StringSlot` with member `value` located at `slot`.
  96. */
  97. function getStringSlot(bytes32 slot) internal pure returns (StringSlot storage r) {
  98. assembly ("memory-safe") {
  99. r.slot := slot
  100. }
  101. }
  102. /**
  103. * @dev Returns an `StringSlot` representation of the string storage pointer `store`.
  104. */
  105. function getStringSlot(string storage store) internal pure returns (StringSlot storage r) {
  106. assembly ("memory-safe") {
  107. r.slot := store.slot
  108. }
  109. }
  110. /**
  111. * @dev Returns a `BytesSlot` with member `value` located at `slot`.
  112. */
  113. function getBytesSlot(bytes32 slot) internal pure returns (BytesSlot storage r) {
  114. assembly ("memory-safe") {
  115. r.slot := slot
  116. }
  117. }
  118. /**
  119. * @dev Returns an `BytesSlot` representation of the bytes storage pointer `store`.
  120. */
  121. function getBytesSlot(bytes storage store) internal pure returns (BytesSlot storage r) {
  122. assembly ("memory-safe") {
  123. r.slot := store.slot
  124. }
  125. }
  126. }