import path from "path"; import { JsonRpcProvider } from "ethers"; import { config as hardhatConfig } from "hardhat"; import { reconcileExternalExecution } from "./lib/externalExecutionScanner"; const parseArgs = (argv: string[]): Record => { const args: Record = {}; for (let index = 0; index < argv.length; index += 1) { const item = argv[index]; if (!item.startsWith("--")) continue; const key = item.slice(2); const value = argv[index + 1]; if (!value || value.startsWith("--")) throw new Error(`--${key} requires a value`); if (args[key] !== undefined) throw new Error(`--${key} may be provided only once`); args[key] = value; index += 1; } return args; }; const required = (args: Record, key: string): string => { const value = args[key]?.trim(); if (!value) throw new Error(`--${key} is required`); return value; }; const main = async (): Promise => { const args = parseArgs(process.argv.slice(2)); const network = required(args, "network"); const networkConfig = hardhatConfig.networks[network]; if (!networkConfig || !("url" in networkConfig) || typeof networkConfig.url !== "string") { throw new Error(`Hardhat network ${network} does not define an HTTP RPC URL`); } const provider = new JsonRpcProvider(networkConfig.url); try { const report = await reconcileExternalExecution({ root: path.resolve(args.root || process.cwd()), taskId: required(args, "task"), network, executionId: required(args, "executionId"), provider, batchSize: args.batchSize === undefined ? undefined : Number(args.batchSize), }); console.log(JSON.stringify({ status: report.status, reportPath: report.reportPath, nextBlock: report.segments?.at(-1)?.nextBlock, finalizedBlock: report.finalizedBlock, })); if (report.status === "failed") process.exitCode = 1; } finally { provider.destroy(); } }; void main().catch((error) => { console.error(error instanceof Error ? error.message : String(error)); process.exitCode = 1; });