SlotDerivation.js 3.5 KB

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