TransientSlot.sol 5.0 KB

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