SlotDerivation.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. const format = require('../format-lines');
  2. const { TYPES } = require('./Slot.opts');
  3. const header = `\
  4. pragma solidity ^0.8.20;
  5. /**
  6. * @dev Library for computing storage (and transient storage) locations from namespaces and deriving slots
  7. * corresponding to standard patterns. The derivation method for array and mapping matches the storage layout used by
  8. * the solidity language / compiler.
  9. *
  10. * 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.].
  11. *
  12. * Example usage:
  13. * \`\`\`solidity
  14. * contract Example {
  15. * // Add the library methods
  16. * using StorageSlot for bytes32;
  17. * using SlotDerivation for bytes32;
  18. *
  19. * // Declare a namespace
  20. * string private constant _NAMESPACE = "<namespace>" // eg. OpenZeppelin.Slot
  21. *
  22. * function setValueInNamespace(uint256 key, address newValue) internal {
  23. * _NAMESPACE.erc7201Slot().deriveMapping(key).getAddressSlot().value = newValue;
  24. * }
  25. *
  26. * function getValueInNamespace(uint256 key) internal view returns (address) {
  27. * return _NAMESPACE.erc7201Slot().deriveMapping(key).getAddressSlot().value;
  28. * }
  29. * }
  30. * \`\`\`
  31. *
  32. * TIP: Consider using this library along with {StorageSlot}.
  33. *
  34. * NOTE: This library provides a way to manipulate storage locations in a non-standard way. Tooling for checking
  35. * upgrade safety will ignore the slots accessed through this library.
  36. */
  37. `;
  38. const namespace = `\
  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. /// @solidity memory-safe-assembly
  44. assembly {
  45. mstore(0x00, sub(keccak256(add(namespace, 0x20), mload(namespace)), 1))
  46. slot := and(keccak256(0x00, 0x20), not(0xff))
  47. }
  48. }
  49. `;
  50. const array = `\
  51. /**
  52. * @dev Add an offset to a slot to get the n-th element of a structure or an array.
  53. */
  54. function offset(bytes32 slot, uint256 pos) internal pure returns (bytes32 result) {
  55. unchecked {
  56. return bytes32(uint256(slot) + pos);
  57. }
  58. }
  59. /**
  60. * @dev Derive the location of the first element in an array from the slot where the length is stored.
  61. */
  62. function deriveArray(bytes32 slot) internal pure returns (bytes32 result) {
  63. /// @solidity memory-safe-assembly
  64. assembly {
  65. mstore(0x00, slot)
  66. result := keccak256(0x00, 0x20)
  67. }
  68. }
  69. `;
  70. const mapping = ({ type }) => `\
  71. /**
  72. * @dev Derive the location of a mapping element from the key.
  73. */
  74. function deriveMapping(bytes32 slot, ${type} key) internal pure returns (bytes32 result) {
  75. /// @solidity memory-safe-assembly
  76. assembly {
  77. mstore(0x00, key)
  78. mstore(0x20, slot)
  79. result := keccak256(0x00, 0x40)
  80. }
  81. }
  82. `;
  83. const mapping2 = ({ type }) => `\
  84. /**
  85. * @dev Derive the location of a mapping element from the key.
  86. */
  87. function deriveMapping(bytes32 slot, ${type} memory key) internal pure returns (bytes32 result) {
  88. /// @solidity memory-safe-assembly
  89. assembly {
  90. let length := mload(key)
  91. let begin := add(key, 0x20)
  92. let end := add(begin, length)
  93. let cache := mload(end)
  94. mstore(end, slot)
  95. result := keccak256(begin, add(length, 0x20))
  96. mstore(end, cache)
  97. }
  98. }
  99. `;
  100. // GENERATE
  101. module.exports = format(
  102. header.trimEnd(),
  103. 'library SlotDerivation {',
  104. namespace,
  105. array,
  106. TYPES.map(type => (type.isValueType ? mapping(type) : mapping2(type))),
  107. '}',
  108. );