SlotDerivation.sol 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. library SlotDerivation {
  37. /**
  38. * @dev Derive an ERC-7201 slot from a string (namespace).
  39. */
  40. function erc7201Slot(string memory namespace) internal pure returns (bytes32 slot) {
  41. assembly ("memory-safe") {
  42. mstore(0x00, sub(keccak256(add(namespace, 0x20), mload(namespace)), 1))
  43. slot := and(keccak256(0x00, 0x20), not(0xff))
  44. }
  45. }
  46. /**
  47. * @dev Add an offset to a slot to get the n-th element of a structure or an array.
  48. */
  49. function offset(bytes32 slot, uint256 pos) internal pure returns (bytes32 result) {
  50. unchecked {
  51. return bytes32(uint256(slot) + pos);
  52. }
  53. }
  54. /**
  55. * @dev Derive the location of the first element in an array from the slot where the length is stored.
  56. */
  57. function deriveArray(bytes32 slot) internal pure returns (bytes32 result) {
  58. assembly ("memory-safe") {
  59. mstore(0x00, slot)
  60. result := keccak256(0x00, 0x20)
  61. }
  62. }
  63. /**
  64. * @dev Derive the location of a mapping element from the key.
  65. */
  66. function deriveMapping(bytes32 slot, address key) internal pure returns (bytes32 result) {
  67. assembly ("memory-safe") {
  68. mstore(0x00, and(key, shr(96, not(0))))
  69. mstore(0x20, slot)
  70. result := keccak256(0x00, 0x40)
  71. }
  72. }
  73. /**
  74. * @dev Derive the location of a mapping element from the key.
  75. */
  76. function deriveMapping(bytes32 slot, bool key) internal pure returns (bytes32 result) {
  77. assembly ("memory-safe") {
  78. mstore(0x00, iszero(iszero(key)))
  79. mstore(0x20, slot)
  80. result := keccak256(0x00, 0x40)
  81. }
  82. }
  83. /**
  84. * @dev Derive the location of a mapping element from the key.
  85. */
  86. function deriveMapping(bytes32 slot, bytes32 key) internal pure returns (bytes32 result) {
  87. assembly ("memory-safe") {
  88. mstore(0x00, key)
  89. mstore(0x20, slot)
  90. result := keccak256(0x00, 0x40)
  91. }
  92. }
  93. /**
  94. * @dev Derive the location of a mapping element from the key.
  95. */
  96. function deriveMapping(bytes32 slot, uint256 key) internal pure returns (bytes32 result) {
  97. assembly ("memory-safe") {
  98. mstore(0x00, key)
  99. mstore(0x20, slot)
  100. result := keccak256(0x00, 0x40)
  101. }
  102. }
  103. /**
  104. * @dev Derive the location of a mapping element from the key.
  105. */
  106. function deriveMapping(bytes32 slot, int256 key) internal pure returns (bytes32 result) {
  107. assembly ("memory-safe") {
  108. mstore(0x00, key)
  109. mstore(0x20, slot)
  110. result := keccak256(0x00, 0x40)
  111. }
  112. }
  113. /**
  114. * @dev Derive the location of a mapping element from the key.
  115. */
  116. function deriveMapping(bytes32 slot, string memory key) internal pure returns (bytes32 result) {
  117. assembly ("memory-safe") {
  118. let length := mload(key)
  119. let begin := add(key, 0x20)
  120. let end := add(begin, length)
  121. let cache := mload(end)
  122. mstore(end, slot)
  123. result := keccak256(begin, add(length, 0x20))
  124. mstore(end, cache)
  125. }
  126. }
  127. /**
  128. * @dev Derive the location of a mapping element from the key.
  129. */
  130. function deriveMapping(bytes32 slot, bytes memory key) internal pure returns (bytes32 result) {
  131. assembly ("memory-safe") {
  132. let length := mload(key)
  133. let begin := add(key, 0x20)
  134. let end := add(begin, length)
  135. let cache := mload(end)
  136. mstore(end, slot)
  137. result := keccak256(begin, add(length, 0x20))
  138. mstore(end, cache)
  139. }
  140. }
  141. }