Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Skip to content

Cross-Chain Swaps

Set outputChainId on SwapParams to request a cross-chain quote. spanDEX will only select providers that support cross-chain swaps.

Example

quote.ts
import { buildCalls, getQuote, type QuoteCheck } from "@spandex/core";
import { config, submitCalls } from "./config.js";
 
const swap = {
  chainId: 8453,
  outputChainId: 10,
  inputToken: "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
  outputToken: "0x4200000000000000000000000000000000000006",
  mode: "exactIn",
  inputAmount: 1_000_000_000_000_000_000n, // 1 WETH
  slippageBps: 50,
  swapperAccount: "0x1234567890abcdef1234567890abcdef12345678",
};
 
const quote = await getQuote({
  config,
  swap,
  strategy: "fastest",
});
 
if (!quote) {
  throw new Error("No cross-chain quote was returned");
}
 
const calls = await buildCalls({
  config,
  swap,
  quote,
});
 
await submitCalls(calls);
 
if (quote.execution === "async") {
  await pollCrossChainCheck(quote.check);
}
 
async function pollCrossChainCheck(check: QuoteCheck) {
  while (true) {
    const response = await fetch(check.endpoint, {
      method: check.method,
    });
 
    if (!response.ok) {
      throw new Error(`Status check failed with ${response.status}`);
    }
 
    const payload = (await response.json()) as { status?: string };
 
    if (payload.status === "success") {
      return payload;
    }
 
    if (payload.status === "failure") {
      throw new Error("Cross-chain swap failed");
    }
 
    await new Promise((resolve) => setTimeout(resolve, 3_000));
  }
}

Notes

  • outputChainId is the switch that enables cross-chain routing.
  • buildCalls and transaction submission happen on the origin chain.
  • For async cross-chain quotes, spanDEX currently simulates only the origin-side approval and swap calls, then trusts the quoted output amount for price comparison.
  • Continue polling quote.check until your provider reports a terminal status.