StorageSlotMock.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const format = require('../format-lines');
  2. const { TYPES } = require('./Slot.opts');
  3. const header = `\
  4. pragma solidity ^0.8.20;
  5. import {Multicall} from "../utils/Multicall.sol";
  6. import {StorageSlot} from "../utils/StorageSlot.sol";
  7. `;
  8. const storageSetValueType = ({ type, name }) => `\
  9. function set${name}Slot(bytes32 slot, ${type} value) public {
  10. slot.get${name}Slot().value = value;
  11. }
  12. `;
  13. const storageGetValueType = ({ type, name }) => `\
  14. function get${name}Slot(bytes32 slot) public view returns (${type}) {
  15. return slot.get${name}Slot().value;
  16. }
  17. `;
  18. const storageSetNonValueType = ({ type, name }) => `\
  19. mapping(uint256 key => ${type}) public ${type}Map;
  20. function set${name}Slot(bytes32 slot, ${type} calldata value) public {
  21. slot.get${name}Slot().value = value;
  22. }
  23. function set${name}Storage(uint256 key, ${type} calldata value) public {
  24. ${type}Map[key].get${name}Slot().value = value;
  25. }
  26. function get${name}Slot(bytes32 slot) public view returns (${type} memory) {
  27. return slot.get${name}Slot().value;
  28. }
  29. function get${name}Storage(uint256 key) public view returns (${type} memory) {
  30. return ${type}Map[key].get${name}Slot().value;
  31. }
  32. `;
  33. // GENERATE
  34. module.exports = format(
  35. header,
  36. 'contract StorageSlotMock is Multicall {',
  37. format(
  38. [].concat(
  39. 'using StorageSlot for *;',
  40. '',
  41. TYPES.filter(type => type.isValueType).map(type => storageSetValueType(type)),
  42. TYPES.filter(type => type.isValueType).map(type => storageGetValueType(type)),
  43. TYPES.filter(type => !type.isValueType).map(type => storageSetNonValueType(type)),
  44. ),
  45. ).trimEnd(),
  46. '}',
  47. );