TransientSlot.sol 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // SPDX-License-Identifier: MIT
  2. // This file was procedurally generated from scripts/generate/templates/TransientSlot.js.
  3. pragma solidity ^0.8.24;
  4. /**
  5. * @dev Library for reading and writing value-types to specific transient storage slots.
  6. *
  7. * Transient slots are often used to store temporary values that are removed after the current transaction.
  8. * This library helps with reading and writing to such slots without the need for inline assembly.
  9. *
  10. * * Example reading and writing values using transient storage:
  11. * ```solidity
  12. * contract Lock {
  13. * using TransientSlot for *;
  14. *
  15. * // Define the slot. Alternatively, use the SlotDerivation library to derive the slot.
  16. * bytes32 internal constant _LOCK_SLOT = 0xf4678858b2b588224636b8522b729e7722d32fc491da849ed75b3fdf3c84f542;
  17. *
  18. * modifier locked() {
  19. * require(!_LOCK_SLOT.asBoolean().tload());
  20. *
  21. * _LOCK_SLOT.asBoolean().tstore(true);
  22. * _;
  23. * _LOCK_SLOT.asBoolean().tstore(false);
  24. * }
  25. * }
  26. * ```
  27. *
  28. * TIP: Consider using this library along with {SlotDerivation}.
  29. */
  30. library TransientSlot {
  31. /**
  32. * @dev UDVT that represent a slot holding a address.
  33. */
  34. type AddressSlot is bytes32;
  35. /**
  36. * @dev Cast an arbitrary slot to a AddressSlot.
  37. */
  38. function asAddress(bytes32 slot) internal pure returns (AddressSlot) {
  39. return AddressSlot.wrap(slot);
  40. }
  41. /**
  42. * @dev UDVT that represent a slot holding a bool.
  43. */
  44. type BooleanSlot is bytes32;
  45. /**
  46. * @dev Cast an arbitrary slot to a BooleanSlot.
  47. */
  48. function asBoolean(bytes32 slot) internal pure returns (BooleanSlot) {
  49. return BooleanSlot.wrap(slot);
  50. }
  51. /**
  52. * @dev UDVT that represent a slot holding a bytes32.
  53. */
  54. type Bytes32Slot is bytes32;
  55. /**
  56. * @dev Cast an arbitrary slot to a Bytes32Slot.
  57. */
  58. function asBytes32(bytes32 slot) internal pure returns (Bytes32Slot) {
  59. return Bytes32Slot.wrap(slot);
  60. }
  61. /**
  62. * @dev UDVT that represent a slot holding a uint256.
  63. */
  64. type Uint256Slot is bytes32;
  65. /**
  66. * @dev Cast an arbitrary slot to a Uint256Slot.
  67. */
  68. function asUint256(bytes32 slot) internal pure returns (Uint256Slot) {
  69. return Uint256Slot.wrap(slot);
  70. }
  71. /**
  72. * @dev UDVT that represent a slot holding a int256.
  73. */
  74. type Int256Slot is bytes32;
  75. /**
  76. * @dev Cast an arbitrary slot to a Int256Slot.
  77. */
  78. function asInt256(bytes32 slot) internal pure returns (Int256Slot) {
  79. return Int256Slot.wrap(slot);
  80. }
  81. /**
  82. * @dev Load the value held at location `slot` in transient storage.
  83. */
  84. function tload(AddressSlot slot) internal view returns (address value) {
  85. assembly ("memory-safe") {
  86. value := tload(slot)
  87. }
  88. }
  89. /**
  90. * @dev Store `value` at location `slot` in transient storage.
  91. */
  92. function tstore(AddressSlot slot, address value) internal {
  93. assembly ("memory-safe") {
  94. tstore(slot, value)
  95. }
  96. }
  97. /**
  98. * @dev Load the value held at location `slot` in transient storage.
  99. */
  100. function tload(BooleanSlot slot) internal view returns (bool value) {
  101. assembly ("memory-safe") {
  102. value := tload(slot)
  103. }
  104. }
  105. /**
  106. * @dev Store `value` at location `slot` in transient storage.
  107. */
  108. function tstore(BooleanSlot slot, bool value) internal {
  109. assembly ("memory-safe") {
  110. tstore(slot, value)
  111. }
  112. }
  113. /**
  114. * @dev Load the value held at location `slot` in transient storage.
  115. */
  116. function tload(Bytes32Slot slot) internal view returns (bytes32 value) {
  117. assembly ("memory-safe") {
  118. value := tload(slot)
  119. }
  120. }
  121. /**
  122. * @dev Store `value` at location `slot` in transient storage.
  123. */
  124. function tstore(Bytes32Slot slot, bytes32 value) internal {
  125. assembly ("memory-safe") {
  126. tstore(slot, value)
  127. }
  128. }
  129. /**
  130. * @dev Load the value held at location `slot` in transient storage.
  131. */
  132. function tload(Uint256Slot slot) internal view returns (uint256 value) {
  133. assembly ("memory-safe") {
  134. value := tload(slot)
  135. }
  136. }
  137. /**
  138. * @dev Store `value` at location `slot` in transient storage.
  139. */
  140. function tstore(Uint256Slot slot, uint256 value) internal {
  141. assembly ("memory-safe") {
  142. tstore(slot, value)
  143. }
  144. }
  145. /**
  146. * @dev Load the value held at location `slot` in transient storage.
  147. */
  148. function tload(Int256Slot slot) internal view returns (int256 value) {
  149. assembly ("memory-safe") {
  150. value := tload(slot)
  151. }
  152. }
  153. /**
  154. * @dev Store `value` at location `slot` in transient storage.
  155. */
  156. function tstore(Int256Slot slot, int256 value) internal {
  157. assembly ("memory-safe") {
  158. tstore(slot, value)
  159. }
  160. }
  161. }