MCPcopy
hub / github.com/subquery/subql / generateAdapter

Function generateAdapter

packages/cli/src/commands/codegen/import-abi.ts:84–230  ·  view source on GitHub ↗
(
  workingDir: string,
  args: GenerateInputs,
  logger: Logger,
  prompt: Prompt | null
)

Source from the content-addressed store, hash-verified

82
83// eslint-disable-next-line complexity
84async function generateAdapter(
85 workingDir: string,
86 args: GenerateInputs,
87 logger: Logger,
88 prompt: Prompt | null
89): Promise<z.infer<typeof generateOutputs>> {
90 const location = path.resolve(workingDir, args.location);
91 let isTs = false;
92
93 /*
94 ts manifest can be either single chain ts manifest
95 or multichain ts manifest
96 or multichain yaml manifest containing single chain ts project paths
97 */
98 const tsManifest = getTsManifest(location);
99
100 // Ensure the manifest is built so we can extract data
101 if (tsManifest) {
102 isTs = true;
103 await buildManifestFromLocation(tsManifest, logger.info.bind(logger));
104 }
105
106 const {manifests, root} = getProjectRootAndManifest(location);
107 if (manifests.length > 1) {
108 //
109 throw new Error('For multichain projects a specific manifest must be provided');
110 }
111 const yamlManifest = getManifestPath(root, manifests[0]);
112 const manifest = tsManifest ?? yamlManifest;
113
114 const project = loadFromJsonOrYaml(yamlManifest) as ProjectManifestV1_0_0;
115
116 if (getProjectNetwork(project) !== NETWORK_FAMILY.ethereum) {
117 throw new Error('ABI generation is only supported for Ethereum projects');
118 }
119
120 if (!prompt && !args.events && !args.functions) {
121 throw new Error('Please provide either events and/or functions from the ABI that you wish to import.');
122 }
123
124 if (!args.abiPath) {
125 if (!args.address) {
126 throw new Error('Please provide the Address the attempt to fetch from Etherscan or an ABI file path');
127 }
128 // const spinner = ora({text: 'Finding ABI from Etherscan', isSilent: flags.mcp}).start();
129 logger.info('Finding ABI on Etherscan');
130 try {
131 const abi = await tryFetchAbiFromExplorer(args.address, project.network.chainId);
132 if (!abi) {
133 // spinner.stop();
134 throw new Error('Unable to fetch ABI from Etherscan, please provide an ABI via the abiPath');
135 }
136
137 args.abiPath = await saveAbiToFile(abi, args.abiName ?? args.address, root);
138 logger.info('Found ABI from Etherscan');
139 } catch (e) {
140 // spinner.stop();
141 throw new Error('Failed to fetch ABI from Etherscan', {cause: e});

Callers 2

runMethod · 0.85
registerImportAbiMCPToolFunction · 0.85

Calls 15

getTsManifestFunction · 0.90
getManifestPathFunction · 0.90
loadFromJsonOrYamlFunction · 0.90
getProjectNetworkFunction · 0.90
tryFetchAbiFromExplorerFunction · 0.90
saveAbiToFileFunction · 0.90
loadDependencyFunction · 0.90
prepareAbiDirectoryFunction · 0.90

Tested by

no test coverage detected