StorageSlot.sol 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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.24;
  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. * Since version 5.1, this library also support writing and reading value types to and from transient storage.
  31. *
  32. * * Example using transient storage:
  33. * ```solidity
  34. * contract Lock {
  35. * // Define the slot. Alternatively, use the SlotDerivation library to derive the slot.
  36. * bytes32 internal constant _LOCK_SLOT = 0xf4678858b2b588224636b8522b729e7722d32fc491da849ed75b3fdf3c84f542;
  37. *
  38. * modifier locked() {
  39. * require(!_LOCK_SLOT.asBoolean().tload());
  40. *
  41. * _LOCK_SLOT.asBoolean().tstore(true);
  42. * _;
  43. * _LOCK_SLOT.asBoolean().tstore(false);
  44. * }
  45. * }
  46. * ```
  47. *
  48. * TIP: Consider using this library along with {SlotDerivation}.
  49. */
  50. library StorageSlot {
  51. struct AddressSlot {
  52. address value;
  53. }
  54. struct BooleanSlot {
  55. bool value;
  56. }
  57. struct Bytes32Slot {
  58. bytes32 value;
  59. }
  60. struct Uint256Slot {
  61. uint256 value;
  62. }
  63. struct Int256Slot {
  64. int256 value;
  65. }
  66. struct StringSlot {
  67. string value;
  68. }
  69. struct BytesSlot {
  70. bytes value;
  71. }
  72. /**
  73. * @dev Returns an `AddressSlot` with member `value` located at `slot`.
  74. */
  75. function getAddressSlot(bytes32 slot) internal pure returns (AddressSlot storage r) {
  76. /// @solidity memory-safe-assembly
  77. assembly {
  78. r.slot := slot
  79. }
  80. }
  81. /**
  82. * @dev Returns an `BooleanSlot` with member `value` located at `slot`.
  83. */
  84. function getBooleanSlot(bytes32 slot) internal pure returns (BooleanSlot storage r) {
  85. /// @solidity memory-safe-assembly
  86. assembly {
  87. r.slot := slot
  88. }
  89. }
  90. /**
  91. * @dev Returns an `Bytes32Slot` with member `value` located at `slot`.
  92. */
  93. function getBytes32Slot(bytes32 slot) internal pure returns (Bytes32Slot storage r) {
  94. /// @solidity memory-safe-assembly
  95. assembly {
  96. r.slot := slot
  97. }
  98. }
  99. /**
  100. * @dev Returns an `Uint256Slot` with member `value` located at `slot`.
  101. */
  102. function getUint256Slot(bytes32 slot) internal pure returns (Uint256Slot storage r) {
  103. /// @solidity memory-safe-assembly
  104. assembly {
  105. r.slot := slot
  106. }
  107. }
  108. /**
  109. * @dev Returns an `Int256Slot` with member `value` located at `slot`.
  110. */
  111. function getInt256Slot(bytes32 slot) internal pure returns (Int256Slot storage r) {
  112. /// @solidity memory-safe-assembly
  113. assembly {
  114. r.slot := slot
  115. }
  116. }
  117. /**
  118. * @dev Returns an `StringSlot` with member `value` located at `slot`.
  119. */
  120. function getStringSlot(bytes32 slot) internal pure returns (StringSlot storage r) {
  121. /// @solidity memory-safe-assembly
  122. assembly {
  123. r.slot := slot
  124. }
  125. }
  126. /**
  127. * @dev Returns an `StringSlot` representation of the string storage pointer `store`.
  128. */
  129. function getStringSlot(string storage store) internal pure returns (StringSlot storage r) {
  130. /// @solidity memory-safe-assembly
  131. assembly {
  132. r.slot := store.slot
  133. }
  134. }
  135. /**
  136. * @dev Returns an `BytesSlot` with member `value` located at `slot`.
  137. */
  138. function getBytesSlot(bytes32 slot) internal pure returns (BytesSlot storage r) {
  139. /// @solidity memory-safe-assembly
  140. assembly {
  141. r.slot := slot
  142. }
  143. }
  144. /**
  145. * @dev Returns an `BytesSlot` representation of the bytes storage pointer `store`.
  146. */
  147. function getBytesSlot(bytes storage store) internal pure returns (BytesSlot storage r) {
  148. /// @solidity memory-safe-assembly
  149. assembly {
  150. r.slot := store.slot
  151. }
  152. }
  153. /**
  154. * @dev UDVT that represent a slot holding a address.
  155. */
  156. type AddressSlotType is bytes32;
  157. /**
  158. * @dev Cast an arbitrary slot to a AddressSlotType.
  159. */
  160. function asAddress(bytes32 slot) internal pure returns (AddressSlotType) {
  161. return AddressSlotType.wrap(slot);
  162. }
  163. /**
  164. * @dev UDVT that represent a slot holding a bool.
  165. */
  166. type BooleanSlotType is bytes32;
  167. /**
  168. * @dev Cast an arbitrary slot to a BooleanSlotType.
  169. */
  170. function asBoolean(bytes32 slot) internal pure returns (BooleanSlotType) {
  171. return BooleanSlotType.wrap(slot);
  172. }
  173. /**
  174. * @dev UDVT that represent a slot holding a bytes32.
  175. */
  176. type Bytes32SlotType is bytes32;
  177. /**
  178. * @dev Cast an arbitrary slot to a Bytes32SlotType.
  179. */
  180. function asBytes32(bytes32 slot) internal pure returns (Bytes32SlotType) {
  181. return Bytes32SlotType.wrap(slot);
  182. }
  183. /**
  184. * @dev UDVT that represent a slot holding a uint256.
  185. */
  186. type Uint256SlotType is bytes32;
  187. /**
  188. * @dev Cast an arbitrary slot to a Uint256SlotType.
  189. */
  190. function asUint256(bytes32 slot) internal pure returns (Uint256SlotType) {
  191. return Uint256SlotType.wrap(slot);
  192. }
  193. /**
  194. * @dev UDVT that represent a slot holding a int256.
  195. */
  196. type Int256SlotType is bytes32;
  197. /**
  198. * @dev Cast an arbitrary slot to a Int256SlotType.
  199. */
  200. function asInt256(bytes32 slot) internal pure returns (Int256SlotType) {
  201. return Int256SlotType.wrap(slot);
  202. }
  203. /**
  204. * @dev Load the value held at location `slot` in transient storage.
  205. */
  206. function tload(AddressSlotType slot) internal view returns (address value) {
  207. /// @solidity memory-safe-assembly
  208. assembly {
  209. value := tload(slot)
  210. }
  211. }
  212. /**
  213. * @dev Store `value` at location `slot` in transient storage.
  214. */
  215. function tstore(AddressSlotType slot, address value) internal {
  216. /// @solidity memory-safe-assembly
  217. assembly {
  218. tstore(slot, value)
  219. }
  220. }
  221. /**
  222. * @dev Load the value held at location `slot` in transient storage.
  223. */
  224. function tload(BooleanSlotType slot) internal view returns (bool value) {
  225. /// @solidity memory-safe-assembly
  226. assembly {
  227. value := tload(slot)
  228. }
  229. }
  230. /**
  231. * @dev Store `value` at location `slot` in transient storage.
  232. */
  233. function tstore(BooleanSlotType slot, bool value) internal {
  234. /// @solidity memory-safe-assembly
  235. assembly {
  236. tstore(slot, value)
  237. }
  238. }
  239. /**
  240. * @dev Load the value held at location `slot` in transient storage.
  241. */
  242. function tload(Bytes32SlotType slot) internal view returns (bytes32 value) {
  243. /// @solidity memory-safe-assembly
  244. assembly {
  245. value := tload(slot)
  246. }
  247. }
  248. /**
  249. * @dev Store `value` at location `slot` in transient storage.
  250. */
  251. function tstore(Bytes32SlotType slot, bytes32 value) internal {
  252. /// @solidity memory-safe-assembly
  253. assembly {
  254. tstore(slot, value)
  255. }
  256. }
  257. /**
  258. * @dev Load the value held at location `slot` in transient storage.
  259. */
  260. function tload(Uint256SlotType slot) internal view returns (uint256 value) {
  261. /// @solidity memory-safe-assembly
  262. assembly {
  263. value := tload(slot)
  264. }
  265. }
  266. /**
  267. * @dev Store `value` at location `slot` in transient storage.
  268. */
  269. function tstore(Uint256SlotType slot, uint256 value) internal {
  270. /// @solidity memory-safe-assembly
  271. assembly {
  272. tstore(slot, value)
  273. }
  274. }
  275. /**
  276. * @dev Load the value held at location `slot` in transient storage.
  277. */
  278. function tload(Int256SlotType slot) internal view returns (int256 value) {
  279. /// @solidity memory-safe-assembly
  280. assembly {
  281. value := tload(slot)
  282. }
  283. }
  284. /**
  285. * @dev Store `value` at location `slot` in transient storage.
  286. */
  287. function tstore(Int256SlotType slot, int256 value) internal {
  288. /// @solidity memory-safe-assembly
  289. assembly {
  290. tstore(slot, value)
  291. }
  292. }
  293. }