|
| 1 | +import { resolve } from "node:path"; |
| 2 | + |
| 3 | +import { indexFlowPulseLogs, type IndexerState } from "./indexer.ts"; |
| 4 | +import { |
| 5 | + baseCanaryIndexerCheckpoint, |
| 6 | + type BaseCanaryIndexerCheckpoint, |
| 7 | + writeBaseCanaryIndexerCheckpoint, |
| 8 | + writeIndexerState, |
| 9 | +} from "./persistence.ts"; |
| 10 | +import { |
| 11 | + blockArgumentToDecimalString, |
| 12 | + blockArgumentToRpcQuantity, |
| 13 | + normalizeEvmAddresses, |
| 14 | + readArgValue, |
| 15 | +} from "./reader-utils.ts"; |
| 16 | +import { BASE_MAINNET_CHAIN_ID, readBaseMainnetCanaryFlowPulseLogs } from "./rpc.ts"; |
| 17 | + |
| 18 | +export const BASE_CANARY_MAX_BLOCK_SPAN = 5_000n; |
| 19 | + |
| 20 | +export interface BaseCanaryReaderOptions { |
| 21 | + rpcUrl: string; |
| 22 | + addresses: string[]; |
| 23 | + fromBlock: string; |
| 24 | + toBlock: string; |
| 25 | + outPath?: string; |
| 26 | + checkpointPath?: string; |
| 27 | + finalizedBlockNumber?: string; |
| 28 | + generatedAt?: string; |
| 29 | + acknowledgeMainnetCanary?: boolean; |
| 30 | + fetchImpl?: typeof fetch; |
| 31 | +} |
| 32 | + |
| 33 | +export interface BaseCanaryReaderResult { |
| 34 | + state: IndexerState; |
| 35 | + checkpoint: BaseCanaryIndexerCheckpoint; |
| 36 | + statePath: string; |
| 37 | + checkpointPath: string; |
| 38 | +} |
| 39 | + |
| 40 | +interface CliOptions extends BaseCanaryReaderOptions { |
| 41 | + outPath: string; |
| 42 | + checkpointPath: string; |
| 43 | + acknowledgeMainnetCanary: true; |
| 44 | +} |
| 45 | + |
| 46 | +function assertCanaryAcknowledged(acknowledgeMainnetCanary?: boolean): void { |
| 47 | + if (acknowledgeMainnetCanary !== true) { |
| 48 | + throw new Error("--acknowledge-mainnet-canary is required for the Base mainnet canary reader"); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +function assertCanaryBlockRange(fromBlock: string, toBlock: string): void { |
| 53 | + if (BigInt(toBlock) < BigInt(fromBlock)) { |
| 54 | + throw new Error("--to-block must be greater than or equal to --from-block"); |
| 55 | + } |
| 56 | + |
| 57 | + const span = BigInt(toBlock) - BigInt(fromBlock); |
| 58 | + if (span > BASE_CANARY_MAX_BLOCK_SPAN) { |
| 59 | + throw new Error( |
| 60 | + `Base canary reader refuses broad scans; block span ${span.toString()} exceeds ${BASE_CANARY_MAX_BLOCK_SPAN.toString()}`, |
| 61 | + ); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +export function parseBaseCanaryReaderArgs(args: string[]): CliOptions { |
| 66 | + let rpcUrl = ""; |
| 67 | + let fromBlock = ""; |
| 68 | + let toBlock = ""; |
| 69 | + let finalizedBlockNumber: string | undefined; |
| 70 | + let acknowledgeMainnetCanary = false; |
| 71 | + const addresses: string[] = []; |
| 72 | + let outPath = "out/base-canary-indexer-state.json"; |
| 73 | + let checkpointPath = "out/base-canary-indexer-checkpoint.json"; |
| 74 | + |
| 75 | + for (let index = 0; index < args.length; index += 1) { |
| 76 | + const arg = args[index]; |
| 77 | + if (arg === "--acknowledge-mainnet-canary") { |
| 78 | + acknowledgeMainnetCanary = true; |
| 79 | + } else if (arg === "--rpc-url") { |
| 80 | + rpcUrl = readArgValue(args, index, arg); |
| 81 | + index += 1; |
| 82 | + } else if (arg === "--address" || arg === "--addresses") { |
| 83 | + addresses.push(readArgValue(args, index, arg)); |
| 84 | + index += 1; |
| 85 | + } else if (arg === "--from-block") { |
| 86 | + fromBlock = readArgValue(args, index, arg); |
| 87 | + index += 1; |
| 88 | + } else if (arg === "--to-block") { |
| 89 | + toBlock = readArgValue(args, index, arg); |
| 90 | + index += 1; |
| 91 | + } else if (arg === "--finalized-block") { |
| 92 | + finalizedBlockNumber = blockArgumentToDecimalString(readArgValue(args, index, arg)); |
| 93 | + index += 1; |
| 94 | + } else if (arg === "--out") { |
| 95 | + outPath = readArgValue(args, index, arg); |
| 96 | + index += 1; |
| 97 | + } else if (arg === "--checkpoint-out") { |
| 98 | + checkpointPath = readArgValue(args, index, arg); |
| 99 | + index += 1; |
| 100 | + } else { |
| 101 | + throw new Error(`unknown argument: ${arg}`); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + if (rpcUrl.trim() === "") { |
| 106 | + throw new Error("--rpc-url is required; FlowMemory does not ship a default RPC endpoint"); |
| 107 | + } |
| 108 | + if (fromBlock.trim() === "") { |
| 109 | + throw new Error("--from-block is required"); |
| 110 | + } |
| 111 | + if (toBlock.trim() === "") { |
| 112 | + throw new Error("--to-block is required"); |
| 113 | + } |
| 114 | + |
| 115 | + assertCanaryAcknowledged(acknowledgeMainnetCanary); |
| 116 | + |
| 117 | + const normalizedFromBlock = blockArgumentToDecimalString(fromBlock); |
| 118 | + const normalizedToBlock = blockArgumentToDecimalString(toBlock); |
| 119 | + assertCanaryBlockRange(normalizedFromBlock, normalizedToBlock); |
| 120 | + |
| 121 | + return { |
| 122 | + rpcUrl, |
| 123 | + addresses: normalizeEvmAddresses(addresses), |
| 124 | + fromBlock: normalizedFromBlock, |
| 125 | + toBlock: normalizedToBlock, |
| 126 | + finalizedBlockNumber, |
| 127 | + outPath, |
| 128 | + checkpointPath, |
| 129 | + acknowledgeMainnetCanary: true, |
| 130 | + }; |
| 131 | +} |
| 132 | + |
| 133 | +export async function runBaseCanaryReader(options: BaseCanaryReaderOptions): Promise<BaseCanaryReaderResult> { |
| 134 | + assertCanaryAcknowledged(options.acknowledgeMainnetCanary); |
| 135 | + |
| 136 | + const addresses = normalizeEvmAddresses(options.addresses); |
| 137 | + const fromBlock = blockArgumentToDecimalString(options.fromBlock); |
| 138 | + const toBlock = blockArgumentToDecimalString(options.toBlock); |
| 139 | + const outPath = resolve(options.outPath ?? "out/base-canary-indexer-state.json"); |
| 140 | + const checkpointPath = resolve(options.checkpointPath ?? "out/base-canary-indexer-checkpoint.json"); |
| 141 | + |
| 142 | + assertCanaryBlockRange(fromBlock, toBlock); |
| 143 | + |
| 144 | + const readResult = await readBaseMainnetCanaryFlowPulseLogs({ |
| 145 | + rpcUrl: options.rpcUrl, |
| 146 | + addresses, |
| 147 | + fromBlock: blockArgumentToRpcQuantity(fromBlock), |
| 148 | + toBlock: blockArgumentToRpcQuantity(toBlock), |
| 149 | + fetchImpl: options.fetchImpl, |
| 150 | + }); |
| 151 | + |
| 152 | + const finalizedBlockNumber = options.finalizedBlockNumber === undefined |
| 153 | + ? undefined |
| 154 | + : blockArgumentToDecimalString(options.finalizedBlockNumber); |
| 155 | + |
| 156 | + const state = indexFlowPulseLogs(readResult.logs, { |
| 157 | + chainId: BASE_MAINNET_CHAIN_ID, |
| 158 | + finalizedBlockNumber, |
| 159 | + source: "base-mainnet-canary-rpc", |
| 160 | + sourceAddresses: addresses, |
| 161 | + }); |
| 162 | + const checkpoint = baseCanaryIndexerCheckpoint({ |
| 163 | + addresses, |
| 164 | + fromBlock, |
| 165 | + toBlock, |
| 166 | + finalizedBlockNumber, |
| 167 | + statePath: outPath, |
| 168 | + state, |
| 169 | + generatedAt: options.generatedAt, |
| 170 | + }); |
| 171 | + |
| 172 | + writeIndexerState(outPath, state); |
| 173 | + writeBaseCanaryIndexerCheckpoint(checkpointPath, checkpoint); |
| 174 | + |
| 175 | + return { |
| 176 | + state, |
| 177 | + checkpoint, |
| 178 | + statePath: outPath, |
| 179 | + checkpointPath, |
| 180 | + }; |
| 181 | +} |
| 182 | + |
| 183 | +function usage(): string { |
| 184 | + return [ |
| 185 | + "Usage:", |
| 186 | + " node src/base-canary.ts --acknowledge-mainnet-canary --rpc-url <url> --address <0x...> --from-block <n> --to-block <n> [--finalized-block <n>] [--out <path>] [--checkpoint-out <path>]", |
| 187 | + "", |
| 188 | + "Boundary:", |
| 189 | + ` This reader only accepts Base mainnet chainId ${BASE_MAINNET_CHAIN_ID} and is canary-only.`, |
| 190 | + ` It refuses scans wider than ${BASE_CANARY_MAX_BLOCK_SPAN.toString()} blocks and stores no RPC URLs or keys.`, |
| 191 | + ].join("\n"); |
| 192 | +} |
| 193 | + |
| 194 | +if (process.argv[1]?.replaceAll("\\", "/").endsWith("/base-canary.ts")) { |
| 195 | + runBaseCanaryReader(parseBaseCanaryReaderArgs(process.argv.slice(2))) |
| 196 | + .then((result) => { |
| 197 | + console.log(JSON.stringify({ |
| 198 | + schema: "flowmemory.indexer.base_canary_reader_summary.v0", |
| 199 | + network: result.checkpoint.network, |
| 200 | + chainId: result.checkpoint.chainId, |
| 201 | + statePath: result.statePath, |
| 202 | + checkpointPath: result.checkpointPath, |
| 203 | + observationCount: result.checkpoint.observationCount, |
| 204 | + rejectedLogCount: result.checkpoint.rejectedLogCount, |
| 205 | + duplicateCount: result.checkpoint.duplicateCount, |
| 206 | + lastIndexedBlock: result.checkpoint.lastIndexedBlock, |
| 207 | + productionReady: result.checkpoint.safety.productionReady, |
| 208 | + }, null, 2)); |
| 209 | + }) |
| 210 | + .catch((error) => { |
| 211 | + console.error(error instanceof Error ? error.message : error); |
| 212 | + console.error(usage()); |
| 213 | + process.exitCode = 1; |
| 214 | + }); |
| 215 | +} |
0 commit comments