StorageSlot.sol 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. assembly ("memory-safe") {
  77. r.slot := slot
  78. }
  79. }
  80. /**
  81. * @dev Returns a `BooleanSlot` with member `value` located at `slot`.
  82. */
  83. function getBooleanSlot(bytes32 slot) internal pure returns (BooleanSlot storage r) {
  84. assembly ("memory-safe") {
  85. r.slot := slot
  86. }
  87. }
  88. /**
  89. * @dev Returns a `Bytes32Slot` with member `value` located at `slot`.
  90. */
  91. function getBytes32Slot(bytes32 slot) internal pure returns (Bytes32Slot storage r) {
  92. assembly ("memory-safe") {
  93. r.slot := slot
  94. }
  95. }
  96. /**
  97. * @dev Returns a `Uint256Slot` with member `value` located at `slot`.
  98. */
  99. function getUint256Slot(bytes32 slot) internal pure returns (Uint256Slot storage r) {
  100. assembly ("memory-safe") {
  101. r.slot := slot
  102. }
  103. }
  104. /**
  105. * @dev Returns a `Int256Slot` with member `value` located at `slot`.
  106. */
  107. function getInt256Slot(bytes32 slot) internal pure returns (Int256Slot storage r) {
  108. assembly ("memory-safe") {
  109. r.slot := slot
  110. }
  111. }
  112. /**
  113. * @dev Returns a `StringSlot` with member `value` located at `slot`.
  114. */
  115. function getStringSlot(bytes32 slot) internal pure returns (StringSlot storage r) {
  116. assembly ("memory-safe") {
  117. r.slot := slot
  118. }
  119. }
  120. /**
  121. * @dev Returns an `StringSlot` representation of the string storage pointer `store`.
  122. */
  123. function getStringSlot(string storage store) internal pure returns (StringSlot storage r) {
  124. assembly ("memory-safe") {
  125. r.slot := store.slot
  126. }
  127. }
  128. /**
  129. * @dev Returns a `BytesSlot` with member `value` located at `slot`.
  130. */
  131. function getBytesSlot(bytes32 slot) internal pure returns (BytesSlot storage r) {
  132. assembly ("memory-safe") {
  133. r.slot := slot
  134. }
  135. }
  136. /**
  137. * @dev Returns an `BytesSlot` representation of the bytes storage pointer `store`.
  138. */
  139. function getBytesSlot(bytes storage store) internal pure returns (BytesSlot storage r) {
  140. assembly ("memory-safe") {
  141. r.slot := store.slot
  142. }
  143. }
  144. /**
  145. * @dev UDVT that represent a slot holding a address.
  146. */
  147. type AddressSlotType is bytes32;
  148. /**
  149. * @dev Cast an arbitrary slot to a AddressSlotType.
  150. */
  151. function asAddress(bytes32 slot) internal pure returns (AddressSlotType) {
  152. return AddressSlotType.wrap(slot);
  153. }
  154. /**
  155. * @dev UDVT that represent a slot holding a bool.
  156. */
  157. type BooleanSlotType is bytes32;
  158. /**
  159. * @dev Cast an arbitrary slot to a BooleanSlotType.
  160. */
  161. function asBoolean(bytes32 slot) internal pure returns (BooleanSlotType) {
  162. return BooleanSlotType.wrap(slot);
  163. }
  164. /**
  165. * @dev UDVT that represent a slot holding a bytes32.
  166. */
  167. type Bytes32SlotType is bytes32;
  168. /**
  169. * @dev Cast an arbitrary slot to a Bytes32SlotType.
  170. */
  171. function asBytes32(bytes32 slot) internal pure returns (Bytes32SlotType) {
  172. return Bytes32SlotType.wrap(slot);
  173. }
  174. /**
  175. * @dev UDVT that represent a slot holding a uint256.
  176. */
  177. type Uint256SlotType is bytes32;
  178. /**
  179. * @dev Cast an arbitrary slot to a Uint256SlotType.
  180. */
  181. function asUint256(bytes32 slot) internal pure returns (Uint256SlotType) {
  182. return Uint256SlotType.wrap(slot);
  183. }
  184. /**
  185. * @dev UDVT that represent a slot holding a int256.
  186. */
  187. type Int256SlotType is bytes32;
  188. /**
  189. * @dev Cast an arbitrary slot to a Int256SlotType.
  190. */
  191. function asInt256(bytes32 slot) internal pure returns (Int256SlotType) {
  192. return Int256SlotType.wrap(slot);
  193. }
  194. /**
  195. * @dev Load the value held at location `slot` in transient storage.
  196. */
  197. function tload(AddressSlotType slot) internal view returns (address value) {
  198. assembly ("memory-safe") {
  199. value := tload(slot)
  200. }
  201. }
  202. /**
  203. * @dev Store `value` at location `slot` in transient storage.
  204. */
  205. function tstore(AddressSlotType slot, address value) internal {
  206. assembly ("memory-safe") {
  207. tstore(slot, value)
  208. }
  209. }
  210. /**
  211. * @dev Load the value held at location `slot` in transient storage.
  212. */
  213. function tload(BooleanSlotType slot) internal view returns (bool value) {
  214. assembly ("memory-safe") {
  215. value := tload(slot)
  216. }
  217. }
  218. /**
  219. * @dev Store `value` at location `slot` in transient storage.
  220. */
  221. function tstore(BooleanSlotType slot, bool value) internal {
  222. assembly ("memory-safe") {
  223. tstore(slot, value)
  224. }
  225. }
  226. /**
  227. * @dev Load the value held at location `slot` in transient storage.
  228. */
  229. function tload(Bytes32SlotType slot) internal view returns (bytes32 value) {
  230. assembly ("memory-safe") {
  231. value := tload(slot)
  232. }
  233. }
  234. /**
  235. * @dev Store `value` at location `slot` in transient storage.
  236. */
  237. function tstore(Bytes32SlotType slot, bytes32 value) internal {
  238. assembly ("memory-safe") {
  239. tstore(slot, value)
  240. }
  241. }
  242. /**
  243. * @dev Load the value held at location `slot` in transient storage.
  244. */
  245. function tload(Uint256SlotType slot) internal view returns (uint256 value) {
  246. assembly ("memory-safe") {
  247. value := tload(slot)
  248. }
  249. }
  250. /**
  251. * @dev Store `value` at location `slot` in transient storage.
  252. */
  253. function tstore(Uint256SlotType slot, uint256 value) internal {
  254. assembly ("memory-safe") {
  255. tstore(slot, value)
  256. }
  257. }
  258. /**
  259. * @dev Load the value held at location `slot` in transient storage.
  260. */
  261. function tload(Int256SlotType slot) internal view returns (int256 value) {
  262. assembly ("memory-safe") {
  263. value := tload(slot)
  264. }
  265. }
  266. /**
  267. * @dev Store `value` at location `slot` in transient storage.
  268. */
  269. function tstore(Int256SlotType slot, int256 value) internal {
  270. assembly ("memory-safe") {
  271. tstore(slot, value)
  272. }
  273. }
  274. }