{"version":3,"file":"reachability.cjs","names":[],"sources":["../src/reachability.ts"],"sourcesContent":["/**\n * Client-side reachability over the zap-pack catalog.\n *\n * `GET /compose/zap-packs` lists single routing edges. `POST /compose/route`\n * chains up to the server's hop budget of them, so the set of routable pairs\n * is the closure of the catalog, not the catalog itself. This module computes\n * that closure locally with the planner's admissibility rule, so an integrator\n * can offer every pair `route` connects without a request per pair.\n *\n * The rule, mirrored from the server's route search:\n *\n * - every edge except the last must leave its output in the proxy for the\n *   next edge to consume: no `availability`, no `recipient: 'required'`\n * - the last edge must be synchronous, or `availability: 'future'` when the\n *   caller opts into `allowAsyncRoutes`\n * - a route never visits the same token twice\n *\n * Amount bounds (`inAmountMin` / `inAmountMax`) on an intermediate edge\n * cannot be checked without an amount, so a pair reported here is routable\n * structurally, exactly as a listed edge is. Routes that open with a\n * `lifi.swap` are not enumerable and are not reported. The server also\n * drops blacklisted tokens as intermediates, which is server-side config\n * this module cannot see, so a reported pair may still answer\n * `no_route_error`.\n */\n\nimport type { ZapPackEdge, ZapPackOverview } from './discovery.js';\n\n/** A token identified by address and chain. */\nexport interface TokenRef {\n  readonly address: string;\n  readonly chainId: number;\n}\n\n/** A from/to pair `POST /compose/route` can connect through the catalog. */\nexport interface RoutablePair {\n  readonly in: TokenRef;\n  readonly out: TokenRef;\n  /** Catalog edges on the shortest admissible route; `1` is a listed edge. */\n  readonly hops: number;\n}\n\nexport interface ReachabilityOptions {\n  /**\n   * Hop budget. The server's `no_route_error` reports its effective value in\n   * `details.maxHops`; the deployment default is 2.\n   */\n  readonly maxHops?: number;\n  /**\n   * Admit a route whose last edge has `availability: 'future'`. Mirrors the\n   * `allowAsyncRoutes` request option; such a pair is a `no_route_error`\n   * without it.\n   */\n  readonly allowAsyncRoutes?: boolean;\n}\n\nexport interface Reachability {\n  readonly maxHops: number;\n  /** Whether `route` can connect `from` to `to` through the catalog. */\n  readonly canRoute: (from: TokenRef, to: TokenRef) => boolean;\n  /** Every pair starting at `from`, shortest routes first. */\n  readonly routableFrom: (from: TokenRef) => readonly RoutablePair[];\n  /** Every routable pair in the catalog. */\n  readonly pairs: () => readonly RoutablePair[];\n}\n\nconst DEFAULT_MAX_HOPS = 2;\n\nconst tokenKey = (token: TokenRef): string =>\n  `${token.chainId}:${token.address.toLowerCase()}`;\n\nconst isSync = (edge: ZapPackEdge): boolean => edge.availability === undefined;\n\n// The output stays in the proxy, so a following edge can consume it.\nconst isProxyHeld = (edge: ZapPackEdge): boolean =>\n  isSync(edge) && edge.recipient !== 'required';\n\n// One edge out of a frontier token. Records the pair it completes, and returns\n// its output when a further hop may consume it (proxy-held) and no shorter\n// path already expanded that token.\nconst visitEdge = ({\n  edge,\n  from,\n  depth,\n  sourceKey,\n  isTerminal,\n  found,\n  expanded,\n}: {\n  readonly edge: ZapPackEdge;\n  readonly from: TokenRef;\n  readonly depth: number;\n  readonly sourceKey: string;\n  readonly isTerminal: (edge: ZapPackEdge) => boolean;\n  readonly found: Map<string, RoutablePair>;\n  readonly expanded: Set<string>;\n}): TokenRef | undefined => {\n  const key = tokenKey(edge.out);\n  if (key === sourceKey) return undefined;\n  if (isTerminal(edge) && !found.has(key)) {\n    found.set(key, { in: from, out: edge.out, hops: depth });\n  }\n  if (!isProxyHeld(edge) || expanded.has(key)) return undefined;\n  expanded.add(key);\n  return edge.out;\n};\n\n// Every pair reachable from one token within `maxHops`, breadth-first so the\n// recorded hop count is the shortest one.\nconst closureFrom = ({\n  byIn,\n  from,\n  maxHops,\n  isTerminal,\n}: {\n  readonly byIn: ReadonlyMap<string, readonly ZapPackEdge[]>;\n  readonly from: TokenRef;\n  readonly maxHops: number;\n  readonly isTerminal: (edge: ZapPackEdge) => boolean;\n}): Map<string, RoutablePair> => {\n  const sourceKey = tokenKey(from);\n  const found = new Map<string, RoutablePair>();\n  const expanded = new Set<string>([sourceKey]);\n  let frontier: readonly TokenRef[] = [from];\n  for (let depth = 1; depth <= maxHops && frontier.length > 0; depth++) {\n    const next: TokenRef[] = [];\n    for (const node of frontier) {\n      for (const edge of byIn.get(tokenKey(node)) ?? []) {\n        const out = visitEdge({\n          edge,\n          from,\n          depth,\n          sourceKey,\n          isTerminal,\n          found,\n          expanded,\n        });\n        if (out) next.push(out);\n      }\n    }\n    frontier = next;\n  }\n  return found;\n};\n\nconst indexByIn = (\n  packs: readonly ZapPackOverview[],\n): Map<string, ZapPackEdge[]> => {\n  const byIn = new Map<string, ZapPackEdge[]>();\n  for (const pack of packs) {\n    for (const edge of pack.edges) {\n      const key = tokenKey(edge.in);\n      const list = byIn.get(key);\n      if (list) list.push(edge);\n      else byIn.set(key, [edge]);\n    }\n  }\n  return byIn;\n};\n\n/**\n * Builds a reachability index over the packs returned by\n * {@link ComposeClient.getZapPacks}. Per-source closures are computed on\n * first use and memoised, so `canRoute` on a handful of pairs stays cheap\n * while `pairs()` enumerates everything.\n */\nexport const buildReachability = (\n  packs: readonly ZapPackOverview[],\n  options: ReachabilityOptions = {},\n): Reachability => {\n  const maxHops = options.maxHops ?? DEFAULT_MAX_HOPS;\n  const allowAsync = options.allowAsyncRoutes ?? false;\n  const isTerminal = (edge: ZapPackEdge): boolean =>\n    isSync(edge) || (allowAsync && edge.availability === 'future');\n\n  const byIn = indexByIn(packs);\n  const memo = new Map<string, Map<string, RoutablePair>>();\n  const closure = (from: TokenRef): Map<string, RoutablePair> => {\n    const key = tokenKey(from);\n    const cached = memo.get(key);\n    if (cached) return cached;\n    const computed = closureFrom({ byIn, from, maxHops, isTerminal });\n    memo.set(key, computed);\n    return computed;\n  };\n\n  return {\n    maxHops,\n    canRoute: (from, to) => closure(from).has(tokenKey(to)),\n    routableFrom: (from) => [...closure(from).values()],\n    pairs: () => {\n      const sources = new Map<string, TokenRef>();\n      for (const pack of packs) {\n        for (const edge of pack.edges) sources.set(tokenKey(edge.in), edge.in);\n      }\n      const all: RoutablePair[] = [];\n      for (const source of sources.values()) {\n        all.push(...closure(source).values());\n      }\n      return all;\n    },\n  };\n};\n"],"mappings":";;AAkEA,MAAM,mBA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