StorageSlot.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. assembly ("memory-safe") {
  64. r.slot := slot
  65. }
  66. }
  67. `;
  68. const getStorage = ({ type, name }) => `\
  69. /**
  70. * @dev Returns an \`${name}Slot\` representation of the ${type} storage pointer \`store\`.
  71. */
  72. function get${name}Slot(${type} storage store) internal pure returns (${name}Slot storage r) {
  73. assembly ("memory-safe") {
  74. r.slot := store.slot
  75. }
  76. }
  77. `;
  78. const udvt = ({ type, name }) => `\
  79. /**
  80. * @dev UDVT that represent a slot holding a ${type}.
  81. */
  82. type ${name}SlotType is bytes32;
  83. /**
  84. * @dev Cast an arbitrary slot to a ${name}SlotType.
  85. */
  86. function as${name}(bytes32 slot) internal pure returns (${name}SlotType) {
  87. return ${name}SlotType.wrap(slot);
  88. }
  89. `;
  90. const transient = ({ type, name }) => `\
  91. /**
  92. * @dev Load the value held at location \`slot\` in transient storage.
  93. */
  94. function tload(${name}SlotType slot) internal view returns (${type} value) {
  95. assembly ("memory-safe") {
  96. value := tload(slot)
  97. }
  98. }
  99. /**
  100. * @dev Store \`value\` at location \`slot\` in transient storage.
  101. */
  102. function tstore(${name}SlotType slot, ${type} value) internal {
  103. assembly ("memory-safe") {
  104. tstore(slot, value)
  105. }
  106. }
  107. `;
  108. // GENERATE
  109. module.exports = format(
  110. header.trimEnd(),
  111. 'library StorageSlot {',
  112. format(
  113. [].concat(
  114. TYPES.map(type => struct(type)),
  115. TYPES.flatMap(type => [get(type), !type.isValueType && getStorage(type)].filter(Boolean)),
  116. TYPES.filter(type => type.isValueType).map(type => udvt(type)),
  117. TYPES.filter(type => type.isValueType).map(type => transient(type)),
  118. ),
  119. ).trimEnd(),
  120. '}',
  121. );