StorageSlot.sol 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.7.0) (utils/StorageSlot.sol)
  3. // This file was procedurally generated from scripts/generate/templates/StorageSlot.js.
  4. pragma solidity ^0.8.0;
  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(Address.isContract(newImplementation), "ERC1967: new implementation is not a contract");
  24. * StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
  25. * }
  26. * }
  27. * ```
  28. *
  29. * _Available since v4.1 for `address`, `bool`, `bytes32`, `uint256`._
  30. * _Available since v4.9 for `string`, `bytes`._
  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 StringSlot {
  46. string value;
  47. }
  48. struct BytesSlot {
  49. bytes value;
  50. }
  51. /**
  52. * @dev Returns an `AddressSlot` with member `value` located at `slot`.
  53. */
  54. function getAddressSlot(bytes32 slot) internal pure returns (AddressSlot storage r) {
  55. /// @solidity memory-safe-assembly
  56. assembly {
  57. r.slot := slot
  58. }
  59. }
  60. /**
  61. * @dev Returns an `BooleanSlot` with member `value` located at `slot`.
  62. */
  63. function getBooleanSlot(bytes32 slot) internal pure returns (BooleanSlot storage r) {
  64. /// @solidity memory-safe-assembly
  65. assembly {
  66. r.slot := slot
  67. }
  68. }
  69. /**
  70. * @dev Returns an `Bytes32Slot` with member `value` located at `slot`.
  71. */
  72. function getBytes32Slot(bytes32 slot) internal pure returns (Bytes32Slot storage r) {
  73. /// @solidity memory-safe-assembly
  74. assembly {
  75. r.slot := slot
  76. }
  77. }
  78. /**
  79. * @dev Returns an `Uint256Slot` with member `value` located at `slot`.
  80. */
  81. function getUint256Slot(bytes32 slot) internal pure returns (Uint256Slot storage r) {
  82. /// @solidity memory-safe-assembly
  83. assembly {
  84. r.slot := slot
  85. }
  86. }
  87. /**
  88. * @dev Returns an `StringSlot` with member `value` located at `slot`.
  89. */
  90. function getStringSlot(bytes32 slot) internal pure returns (StringSlot storage r) {
  91. /// @solidity memory-safe-assembly
  92. assembly {
  93. r.slot := slot
  94. }
  95. }
  96. /**
  97. * @dev Returns an `StringSlot` representation of the string storage pointer `store`.
  98. */
  99. function getStringSlot(string storage store) internal pure returns (StringSlot storage r) {
  100. /// @solidity memory-safe-assembly
  101. assembly {
  102. r.slot := store.slot
  103. }
  104. }
  105. /**
  106. * @dev Returns an `BytesSlot` with member `value` located at `slot`.
  107. */
  108. function getBytesSlot(bytes32 slot) internal pure returns (BytesSlot storage r) {
  109. /// @solidity memory-safe-assembly
  110. assembly {
  111. r.slot := slot
  112. }
  113. }
  114. /**
  115. * @dev Returns an `BytesSlot` representation of the bytes storage pointer `store`.
  116. */
  117. function getBytesSlot(bytes storage store) internal pure returns (BytesSlot storage r) {
  118. /// @solidity memory-safe-assembly
  119. assembly {
  120. r.slot := store.slot
  121. }
  122. }
  123. }