/** * CCIP CLI Lane Latency Command * * Queries real-time lane latency statistics between source and destination chains * using the CCIP API. Shows average, median, and percentile latencies. * * @example * ```bash * # Get latency between Ethereum and Arbitrum * ccip-cli lane-latency ethereum-mainnet arbitrum-mainnet * * # Use custom API URL * ccip-cli lane-latency sepolia fuji --api https://custom-api.example.com * * # Latency for one token rather than the lane as a whole * ccip-cli lane-latency 1 42161 --source-token 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48 * ``` * * @packageDocumentation */ import { CCIPAPIClient, CCIPApiClientNotAvailableError, jsonStringify, networkInfo, } from '@chainlink/ccip-sdk/src/index.ts' import type { Argv } from 'yargs' import type { GlobalOpts } from '../index.ts' import { type Ctx, Format } from './types.ts' import { formatDisplayAddress, formatDuration, getCtx, logParsedError, prettyTable, } from './utils.ts' export const command = ['laneLatency ', 'lane-latency '] export const describe = 'Query real-time lane latency between source and destination chains' export const aliases = ['latency'] /** * Yargs builder for the lane-latency command. * @param yargs - Yargs instance. * @returns Configured yargs instance with command options. */ export const builder = (yargs: Argv) => yargs .positional('source', { type: 'string', demandOption: true, describe: 'Source network (chainId, selector, or name). Example: ethereum-mainnet', }) .positional('dest', { type: 'string', demandOption: true, describe: 'Destination network (chainId, selector, or name). Example: arbitrum-mainnet', }) .option('block-confirmations', { alias: 'c', type: 'number', describe: 'Number of block confirmations to use for latency calculation', }) .option('source-token', { type: 'string', describe: "Source token address, to estimate from that token's history alone", }) /** * Handler for the lane-latency command. * @param argv - Command line arguments. */ export async function handler(argv: Awaited['argv']> & GlobalOpts) { const [ctx, destroy] = getCtx(argv) return getLaneLatencyCmd(ctx, argv) .catch((err) => { process.exitCode = 1 if (!logParsedError.call(ctx, err)) ctx.logger.error(err) }) .finally(destroy) } /** Exported for testing */ export async function getLaneLatencyCmd(ctx: Ctx, argv: Parameters[0]) { const { output } = ctx // Respect --no-api flag - this command requires API access if (argv.api === false) { throw new CCIPApiClientNotAvailableError({ context: { reason: 'The lane-latency command requires API access. Remove --no-api flag to use this command.', }, }) } const sourceNetwork = networkInfo(argv.source) const destNetwork = networkInfo(argv.dest) const apiClient = CCIPAPIClient.fromUrl(argv.api === true ? undefined : argv.api, ctx) const result = await apiClient.getLaneLatency( sourceNetwork.chainSelector, destNetwork.chainSelector, argv.blockConfirmations, argv.sourceToken ? { sourceTokenAddress: argv.sourceToken } : undefined, ) switch (argv.format) { case Format.json: output.write(jsonStringify(result, 2)) break case Format.log: output.write('Lane Latency:', result) break default: { prettyTable.call(ctx, { Source: `${sourceNetwork.name} [${sourceNetwork.chainSelector}]`, Destination: `${destNetwork.name} [${destNetwork.chainSelector}]`, ...(argv.sourceToken ? { 'Source Token': formatDisplayAddress(argv.sourceToken, sourceNetwork.family) } : {}), 'Estimated Delivery': `~${formatDuration(result.totalMs / 1000)}`, 'Latency (ms)': result.totalMs.toLocaleString(), }) } } }