SlotDerivation.sol 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: MIT
  2. // This file was procedurally generated from scripts/generate/templates/SlotDerivation.js.
  3. pragma solidity ^0.8.20;
  4. /**
  5. * @dev Library for computing storage (and transient storage) locations from namespaces and deriving slots
  6. * corresponding to standard patterns. The derivation method for array and mapping matches the storage layout used by
  7. * the solidity language / compiler.
  8. *
  9. * See https://docs.soliditylang.org/en/v0.8.20/internals/layout_in_storage.html#mappings-and-dynamic-arrays[Solidity docs for mappings and dynamic arrays.].
  10. *
  11. * Example usage:
  12. * ```solidity
  13. * contract Example {
  14. * // Add the library methods
  15. * using StorageSlot for bytes32;
  16. * using SlotDerivation for bytes32;
  17. *
  18. * // Declare a namespace
  19. * string private constant _NAMESPACE = "<namespace>" // eg. OpenZeppelin.Slot
  20. *
  21. * function setValueInNamespace(uint256 key, address newValue) internal {
  22. * _NAMESPACE.erc7201Slot().deriveMapping(key).getAddressSlot().value = newValue;
  23. * }
  24. *
  25. * function getValueInNamespace(uint256 key) internal view returns (address) {
  26. * return _NAMESPACE.erc7201Slot().deriveMapping(key).getAddressSlot().value;
  27. * }
  28. * }
  29. * ```
  30. *
  31. * TIP: Consider using this library along with {StorageSlot}.
  32. *
  33. * NOTE: This library provides a way to manipulate storage locations in a non-standard way. Tooling for checking
  34. * upgrade safety will ignore the slots accessed through this library.
  35. *
  36. * _Available since v5.1._
  37. */
  38. library SlotDerivation {
  39. /**
  40. * @dev Derive an ERC-7201 slot from a string (namespace).
  41. */
  42. function erc7201Slot(string memory namespace) internal pure returns (bytes32 slot) {
  43. assembly ("memory-safe") {
  44. mstore(0x00, sub(keccak256(add(namespace, 0x20), mload(namespace)), 1))
  45. slot := and(keccak256(0x00, 0x20), not(0xff))
  46. }
  47. }
  48. /**
  49. * @dev Add an offset to a slot to get the n-th element of a structure or an array.
  50. */
  51. function offset(bytes32 slot, uint256 pos) internal pure returns (bytes32 result) {
  52. unchecked {
  53. return bytes32(uint256(slot) + pos);
  54. }
  55. }
  56. /**
  57. * @dev Derive the location of the first element in an array from the slot where the length is stored.
  58. */
  59. function deriveArray(bytes32 slot) internal pure returns (bytes32 result) {
  60. assembly ("memory-safe") {
  61. mstore(0x00, slot)
  62. result := keccak256(0x00, 0x20)
  63. }
  64. }
  65. /**
  66. * @dev Derive the location of a mapping element from the key.
  67. */
  68. function deriveMapping(bytes32 slot, address key) internal pure returns (bytes32 result) {
  69. assembly ("memory-safe") {
  70. mstore(0x00, and(key, shr(96, not(0))))
  71. mstore(0x20, slot)
  72. result := keccak256(0x00, 0x40)
  73. }
  74. }
  75. /**
  76. * @dev Derive the location of a mapping element from the key.
  77. */
  78. function deriveMapping(bytes32 slot, bool key) internal pure returns (bytes32 result) {
  79. assembly ("memory-safe") {
  80. mstore(0x00, iszero(iszero(key)))
  81. mstore(0x20, slot)
  82. result := keccak256(0x00, 0x40)
  83. }
  84. }
  85. /**
  86. * @dev Derive the location of a mapping element from the key.
  87. */
  88. function deriveMapping(bytes32 slot, bytes32 key) internal pure returns (bytes32 result) {
  89. assembly ("memory-safe") {
  90. mstore(0x00, key)
  91. mstore(0x20, slot)
  92. result := keccak256(0x00, 0x40)
  93. }
  94. }
  95. /**
  96. * @dev Derive the location of a mapping element from the key.
  97. */
  98. function deriveMapping(bytes32 slot, uint256 key) internal pure returns (bytes32 result) {
  99. assembly ("memory-safe") {
  100. mstore(0x00, key)
  101. mstore(0x20, slot)
  102. result := keccak256(0x00, 0x40)
  103. }
  104. }
  105. /**
  106. * @dev Derive the location of a mapping element from the key.
  107. */
  108. function deriveMapping(bytes32 slot, int256 key) internal pure returns (bytes32 result) {
  109. assembly ("memory-safe") {
  110. mstore(0x00, key)
  111. mstore(0x20, slot)
  112. result := keccak256(0x00, 0x40)
  113. }
  114. }
  115. /**
  116. * @dev Derive the location of a mapping element from the key.
  117. */
  118. function deriveMapping(bytes32 slot, string memory key) internal pure returns (bytes32 result) {
  119. assembly ("memory-safe") {
  120. let length := mload(key)
  121. let begin := add(key, 0x20)
  122. let end := add(begin, length)
  123. let cache := mload(end)
  124. mstore(end, slot)
  125. result := keccak256(begin, add(length, 0x20))
  126. mstore(end, cache)
  127. }
  128. }
  129. /**
  130. * @dev Derive the location of a mapping element from the key.
  131. */
  132. function deriveMapping(bytes32 slot, bytes memory key) internal pure returns (bytes32 result) {
  133. assembly ("memory-safe") {
  134. let length := mload(key)
  135. let begin := add(key, 0x20)
  136. let end := add(begin, length)
  137. let cache := mload(end)
  138. mstore(end, slot)
  139. result := keccak256(begin, add(length, 0x20))
  140. mstore(end, cache)
  141. }
  142. }
  143. }