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

getQuote

Fetch quotes, simulate them, and return a single winning quote based on a selection strategy.

Example

getQuote
import { getQuote } from "@spandex/core";
import { config } from "./config.js";
 
const quote = await getQuote({
  config,
  swap: {
    chainId: 8453,
    inputToken: "0x4200000000000000000000000000000000000006", // WETH
    outputToken: "0xd9AAEC86B65D86f6A7B5B1b0c42FFA531710b6CA", // USDbC
    mode: "exactIn",
    inputAmount: 1_000_000_000_000_000_000n, // 1 WETH
    slippageBps: 50,
    swapperAccount: "0x1234567890abcdef1234567890abcdef12345678",
  },
  strategy: "bestPrice",
});
 
if (!quote) {
  throw new Error("No providers returned a successful quote");
}
 
console.log(quote.provider, quote.simulation.outputAmount);

Params

config

Config

Aggregator configuration object created via createConfig.

swap

SwapParams

Parameters defining the swap operation. See SwapParams reference for details.

strategy

QuoteSelectionStrategy

Selection strategy used to pick a winning quote. Built-in strategy names:

StrategyWaits for AllSelection CriteriaBest For
bestPriceHighest simulated outputPrice optimization
estimatedGasLowest simulated gas usedGas efficiency
fastestFirst successLowest latency
priorityProvider orderFailover chains
CustomDependsYour logicSpecial requirements

Custom function signature:

type QuoteSelectionFn = (
  quotes: Array<Promise<SimulatedQuote>>
) => Promise<SuccessfulSimulatedQuote | null>

client

PublicClient | undefined

Optional custom PublicClient to use for onchain simulations. If not provided, the configured clients in the config will be used.

simulate

typeof simulateQuote | undefined

Optional override for the simulation function used on each quote.

Returns

Promise<SuccessfulSimulatedQuote | null>

Winning simulated quote, or null if no provider succeeds.