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

Getting Started - Core

To get started we will create a shared meta aggregator instance with three providers, default settings, and support for base. Then, we will fetch the best quote for a swap and execute it.

1. Install

Install the core library:

npm
npm i viem @spandex/core

2. Configure

See the configuration reference for all options.

import { createConfig, fabric, kyberswap, odos } from "@spandex/core";
import { createPublicClient, http, type PublicClient } from "viem";
import { base } from "viem/chains";
 
// Create a base client for quote simulation
const baseClient = createPublicClient({
  chain: base,
  transport: http("https://base.drpc.org"),
});
 
// The aggregator instance can be shared across your application
export const config = createConfig({
  providers: [fabric({ appId: "test" }), odos({}), kyberswap({ clientId: "test" })],
  clients: [baseClient] as PublicClient[],
});

3. Fetch Quotes and Execute

import { config } from "./config.js";
import { createWalletClient, http } from "viem";
import { executeQuote, getQuote } from "@spandex/core";
import { base } from "viem/chains";
 
const swapParams = {
  chainId: 8453,
  inputToken: "0x4200000000000000000000000000000000000006", // WETH
  outputToken: "0xd9AAEC86B65D86f6A7B5B1b0c42FFA531710b6CA", // USDbC
  mode: "exactIn",
  inputAmount: 1_000_000_000_000_000_000n, // 1 WETH
  slippageBps: 50, // Tolerance
  swapperAccount: "0x1234567890abcdef1234567890abcdef12345678",
};
 
const quote = await getQuote({
  config,
  swap: swapParams,
  strategy: "bestPrice",
});
 
if (!quote) {
  throw new Error("No providers responded in time");
}
 
const walletClient = createWalletClient({
  account: "0xdead00000000000000000000000000000000beef",
  chain: base,
  transport: http(),
});
 
const { transactionHash } = await executeQuote({
  config,
  swap: swapParams,
  quote,
  walletClient,
});
 
console.log(`Swapped via ${quote.provider} @ ${transactionHash}`);