StorageSlot.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. const format = require('../format-lines');
  2. const { TYPES } = require('./Slot.opts');
  3. const header = `\
  4. pragma solidity ^0.8.24;
  5. /**
  6. * @dev Library for reading and writing primitive types to specific storage slots.
  7. *
  8. * Storage slots are often used to avoid storage conflict when dealing with upgradeable contracts.
  9. * This library helps with reading and writing to such slots without the need for inline assembly.
  10. *
  11. * The functions in this library return Slot structs that contain a \`value\` member that can be used to read or write.
  12. *
  13. * Example usage to set ERC-1967 implementation slot:
  14. * \`\`\`solidity
  15. * contract ERC1967 {
  16. * // Define the slot. Alternatively, use the SlotDerivation library to derive the slot.
  17. * bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
  18. *
  19. * function _getImplementation() internal view returns (address) {
  20. * return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
  21. * }
  22. *
  23. * function _setImplementation(address newImplementation) internal {
  24. * require(newImplementation.code.length > 0);
  25. * StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
  26. * }
  27. * }
  28. * \`\`\`
  29. *
  30. * Since version 5.1, this library also support writing and reading value types to and from transient storage.
  31. *
  32. * * Example using transient storage:
  33. * \`\`\`solidity
  34. * contract Lock {
  35. * // Define the slot. Alternatively, use the SlotDerivation library to derive the slot.
  36. * bytes32 internal constant _LOCK_SLOT = 0xf4678858b2b588224636b8522b729e7722d32fc491da849ed75b3fdf3c84f542;
  37. *
  38. * modifier locked() {
  39. * require(!_LOCK_SLOT.asBoolean().tload());
  40. *
  41. * _LOCK_SLOT.asBoolean().tstore(true);
  42. * _;
  43. * _LOCK_SLOT.asBoolean().tstore(false);
  44. * }
  45. * }
  46. * \`\`\`
  47. *
  48. * TIP: Consider using this library along with {SlotDerivation}.
  49. */
  50. `;
  51. const struct = ({ type, name }) => `\
  52. struct ${name}Slot {
  53. ${type} value;
  54. }
  55. `;
  56. const get = ({ name }) => `\
  57. /**
  58. * @dev Returns ${
  59. name.toLowerCase().startsWith('a') ? 'an' : 'a'
  60. } \`${name}Slot\` with member \`value\` located at \`slot\`.
  61. */
  62. function get${name}Slot(bytes32 slot) internal pure returns (${name}Slot storage r) {
  63. /// @solidity memory-safe-assembly
  64. assembly {
  65. r.slot := slot
  66. }
  67. }
  68. `;
  69. const getStorage = ({ type, name }) => `\
  70. /**
  71. * @dev Returns an \`${name}Slot\` representation of the ${type} storage pointer \`store\`.
  72. */
  73. function get${name}Slot(${type} storage store) internal pure returns (${name}Slot storage r) {
  74. /// @solidity memory-safe-assembly
  75. assembly {
  76. r.slot := store.slot
  77. }
  78. }
  79. `;
  80. const udvt = ({ type, name }) => `\
  81. /**
  82. * @dev UDVT that represent a slot holding a ${type}.
  83. */
  84. type ${name}SlotType is bytes32;
  85. /**
  86. * @dev Cast an arbitrary slot to a ${name}SlotType.
  87. */
  88. function as${name}(bytes32 slot) internal pure returns (${name}SlotType) {
  89. return ${name}SlotType.wrap(slot);
  90. }
  91. `;
  92. const transient = ({ type, name }) => `\
  93. /**
  94. * @dev Load the value held at location \`slot\` in transient storage.
  95. */
  96. function tload(${name}SlotType slot) internal view returns (${type} value) {
  97. /// @solidity memory-safe-assembly
  98. assembly {
  99. value := tload(slot)
  100. }
  101. }
  102. /**
  103. * @dev Store \`value\` at location \`slot\` in transient storage.
  104. */
  105. function tstore(${name}SlotType slot, ${type} value) internal {
  106. /// @solidity memory-safe-assembly
  107. assembly {
  108. tstore(slot, value)
  109. }
  110. }
  111. `;
  112. // GENERATE
  113. module.exports = format(
  114. header.trimEnd(),
  115. 'library StorageSlot {',
  116. format(
  117. [].concat(
  118. TYPES.map(type => struct(type)),
  119. TYPES.flatMap(type => [get(type), !type.isValueType && getStorage(type)].filter(Boolean)),
  120. TYPES.filter(type => type.isValueType).map(type => udvt(type)),
  121. TYPES.filter(type => type.isValueType).map(type => transient(type)),
  122. ),
  123. ).trimEnd(),
  124. '}',
  125. );