Swaps (Jupiter)
You can route spot swaps using Jupiter directly from your Drift account. The SDK fetches a Jupiter quote, builds the swap transaction, and routes it so the input and output tokens flow through your Drift spot balances.
To perform spot swaps, initialize a JupiterClient with your RPC connection:
import { Connection } from "@solana/web3.js";
import { JupiterClient } from "@drift-labs/sdk";
const connection = new Connection("<RPC_URL>", "confirmed");
const jupiterClient = new JupiterClient({ connection });Class JupiterClientReference ↗
Class JupiterClientReference ↗| Property | Type | Required |
|---|---|---|
url | string | Yes |
connection | Connection | Yes |
lookupTableCahce | Map<string, AddressLookupTableAccount> | Yes |
apiKey | any | No |
getHeaders | anyGet the headers for API requests, including API key if configured | Yes |
getQuote | ({ inputMint, outputMint, amount, maxAccounts, slippageBps, swapMode, onlyDirectRoutes, excludeDexes, autoSlippage, maxAutoSlippageBps, usdEstimate, }: { inputMint: PublicKey; outputMint: PublicKey; amount: BN; maxAccounts?: number; slippageBps?: number; swapMode?: SwapMode; onlyDirectRoutes?: boolean; excludeDexes?...Get routes for a swap | Yes |
getSwap | ({ quote, userPublicKey, slippageBps, }: { quote: QuoteResponse; userPublicKey: PublicKey; slippageBps?: number; }) => Promise<VersionedTransaction>Get a swap transaction for quote | Yes |
getTransactionMessageAndLookupTables | ({ transaction, }: { transaction: VersionedTransaction; }) => Promise<{ transactionMessage: TransactionMessage; lookupTables: AddressLookupTableAccount[]; }>Get the transaction message and lookup tables for a transaction | Yes |
getLookupTable | (accountKey: PublicKey) => Promise<AddressLookupTableAccount> | Yes |
getJupiterInstructions | ({ transactionMessage, inputMint, outputMint, }: { transactionMessage: TransactionMessage; inputMint: PublicKey; outputMint: PublicKey; }) => TransactionInstruction[]Get the jupiter instructions from transaction by filtering out instructions to compute budget and associated token programs | Yes |
Before executing a swap, preview the expected output and route with getQuote(). This lets you check slippage and confirm the route before committing:
// Preview a quote before swapping (shape depends on Jupiter route selection).
const quote = await jupiterClient.getQuote({
inputMint: "<INPUT_MINT>",
outputMint: "<OUTPUT_MINT>",
amount: "<AMOUNT_IN_BASE_UNITS>",
});
console.log(quote);Method JupiterClient.getQuoteReference ↗
Method JupiterClient.getQuoteReference ↗| Parameter | Type | Required |
|---|---|---|
__0 | { inputMint: PublicKey; outputMint: PublicKey; amount: BN; maxAccounts?: number; slippageBps?: number; swapMode?: SwapMode; onlyDirectRoutes?: boolean; excludeDexes?: string[]; autoSlippage?: boolean; maxAutoSlippageBps?: number; usdEstimate?: number; }the mint of the input token | Yes |
| Returns |
|---|
Promise<QuoteResponse> |
Execute the swap through Drift. The SDK fetches the best Jupiter route and sends the transaction so the tokens move in and out of your Drift spot balances:
// Assumes `driftClient` is subscribed.
const txSig = await driftClient.swap({
jupiterClient,
inMarketIndex: 0, // e.g. USDC (spot market index)
outMarketIndex: 1, // e.g. SOL (spot market index)
amount: driftClient.convertToSpotPrecision(0, 10), // 10 USDC
slippageBps: 50, // 0.5% max slippage
onlyDirectRoutes: false, // allow multi-hop routes for better pricing
});
console.log(txSig);Method DriftClient.swapReference ↗
Method DriftClient.swapReference ↗| Parameter | Type | Required |
|---|---|---|
__0 | { swapClient?: UnifiedSwapClient | SwapClient; jupiterClient?: JupiterClient; outMarketIndex: number; inMarketIndex: number; outAssociatedTokenAccount?: PublicKey; ... 8 more ...; quote?: UnifiedQuoteResponse; }swap client to find routes and instructions (Titan or Jupiter) | Yes |
| Returns |
|---|
Promise<string> |
Parameters:
| Parameter | Description | Optional | Default |
|---|---|---|---|
jupiterClient | Instance of JupiterClient for fetching quotes | No | |
inMarketIndex | Drift spot market index for the input token | No | |
outMarketIndex | Drift spot market index for the output token | No | |
amount | Amount to swap as a BN in spot market precision | No | |
slippageBps | Maximum allowed slippage in basis points | Yes | 50 |
onlyDirectRoutes | If true, restricts to direct token pairs only (no multi-hop) | Yes | false |
computeUnits | Override for compute budget | Yes | |
prioritizationFeeMicroLamports | Priority fee in micro-lamports | Yes |
Last updated on