diff --git a/sdk/src/driftClient.ts b/sdk/src/driftClient.ts index 931493eba..b78c67e34 100644 --- a/sdk/src/driftClient.ts +++ b/sdk/src/driftClient.ts @@ -147,7 +147,6 @@ import { castNumberToSpotPrecision } from './math/spotMarket'; import { JupiterClient, QuoteResponse, - Route, SwapMode, } from './jupiter/jupiterClient'; import { getNonIdleUserFilter } from './memcmp'; @@ -5056,6 +5055,8 @@ export class DriftClient { * @param swapMode jupiter swapMode (ExactIn or ExactOut), default is ExactIn * @param route the jupiter route to use for the swap * @param reduceOnly specify if In or Out token on the drift account must reduceOnly, checked at end of swap + * @param v6 pass in the quote response from Jupiter quote's API (deprecated, use quote instead) + * @param quote pass in the quote response from Jupiter quote's API * @param txParams */ public async swap({ @@ -5067,10 +5068,10 @@ export class DriftClient { amount, slippageBps, swapMode, - route, reduceOnly, txParams, v6, + quote, onlyDirectRoutes = false, }: { jupiterClient: JupiterClient; @@ -5081,49 +5082,31 @@ export class DriftClient { amount: BN; slippageBps?: number; swapMode?: SwapMode; - route?: Route; reduceOnly?: SwapReduceOnly; txParams?: TxParams; onlyDirectRoutes?: boolean; v6?: { quote?: QuoteResponse; }; + quote?: QuoteResponse; }): Promise { - let ixs: anchor.web3.TransactionInstruction[]; - let lookupTables: anchor.web3.AddressLookupTableAccount[]; - - if (v6) { - const res = await this.getJupiterSwapIxV6({ - jupiterClient, - outMarketIndex, - inMarketIndex, - outAssociatedTokenAccount, - inAssociatedTokenAccount, - amount, - slippageBps, - swapMode, - quote: v6.quote, - reduceOnly, - onlyDirectRoutes, - }); - ixs = res.ixs; - lookupTables = res.lookupTables; - } else { - const res = await this.getJupiterSwapIx({ - jupiterClient, - outMarketIndex, - inMarketIndex, - outAssociatedTokenAccount, - inAssociatedTokenAccount, - amount, - slippageBps, - swapMode, - route, - reduceOnly, - }); - ixs = res.ixs; - lookupTables = res.lookupTables; - } + const quoteToUse = quote ?? v6?.quote; + + const res = await this.getJupiterSwapIxV6({ + jupiterClient, + outMarketIndex, + inMarketIndex, + outAssociatedTokenAccount, + inAssociatedTokenAccount, + amount, + slippageBps, + swapMode, + quote: quoteToUse, + reduceOnly, + onlyDirectRoutes, + }); + const ixs = res.ixs; + const lookupTables = res.lookupTables; const tx = (await this.buildTransaction( ixs, @@ -5139,142 +5122,6 @@ export class DriftClient { return txSig; } - public async getJupiterSwapIx({ - jupiterClient, - outMarketIndex, - inMarketIndex, - outAssociatedTokenAccount, - inAssociatedTokenAccount, - amount, - slippageBps, - swapMode, - onlyDirectRoutes, - route, - reduceOnly, - userAccountPublicKey, - }: { - jupiterClient: JupiterClient; - outMarketIndex: number; - inMarketIndex: number; - outAssociatedTokenAccount?: PublicKey; - inAssociatedTokenAccount?: PublicKey; - amount: BN; - slippageBps?: number; - swapMode?: SwapMode; - onlyDirectRoutes?: boolean; - route?: Route; - reduceOnly?: SwapReduceOnly; - userAccountPublicKey?: PublicKey; - }): Promise<{ - ixs: TransactionInstruction[]; - lookupTables: AddressLookupTableAccount[]; - }> { - const outMarket = this.getSpotMarketAccount(outMarketIndex); - const inMarket = this.getSpotMarketAccount(inMarketIndex); - - if (!route) { - const routes = await jupiterClient.getRoutes({ - inputMint: inMarket.mint, - outputMint: outMarket.mint, - amount, - slippageBps, - swapMode, - onlyDirectRoutes, - }); - - if (!routes || routes.length === 0) { - throw new Error('No jupiter routes found'); - } - - route = routes[0]; - } - - const transaction = await jupiterClient.getSwapTransaction({ - route, - userPublicKey: this.provider.wallet.publicKey, - slippageBps, - }); - - const { transactionMessage, lookupTables } = - await jupiterClient.getTransactionMessageAndLookupTables({ - transaction, - }); - - const jupiterInstructions = jupiterClient.getJupiterInstructions({ - transactionMessage, - inputMint: inMarket.mint, - outputMint: outMarket.mint, - }); - - const preInstructions = []; - if (!outAssociatedTokenAccount) { - const tokenProgram = this.getTokenProgramForSpotMarket(outMarket); - outAssociatedTokenAccount = await this.getAssociatedTokenAccount( - outMarket.marketIndex, - false, - tokenProgram - ); - - const accountInfo = await this.connection.getAccountInfo( - outAssociatedTokenAccount - ); - if (!accountInfo) { - preInstructions.push( - this.createAssociatedTokenAccountIdempotentInstruction( - outAssociatedTokenAccount, - this.provider.wallet.publicKey, - this.provider.wallet.publicKey, - outMarket.mint, - tokenProgram - ) - ); - } - } - - if (!inAssociatedTokenAccount) { - const tokenProgram = this.getTokenProgramForSpotMarket(outMarket); - inAssociatedTokenAccount = await this.getAssociatedTokenAccount( - inMarket.marketIndex, - false, - tokenProgram - ); - - const accountInfo = await this.connection.getAccountInfo( - inAssociatedTokenAccount - ); - if (!accountInfo) { - preInstructions.push( - this.createAssociatedTokenAccountIdempotentInstruction( - inAssociatedTokenAccount, - this.provider.wallet.publicKey, - this.provider.wallet.publicKey, - inMarket.mint, - tokenProgram - ) - ); - } - } - - const { beginSwapIx, endSwapIx } = await this.getSwapIx({ - outMarketIndex, - inMarketIndex, - amountIn: new BN(route.inAmount), - inTokenAccount: inAssociatedTokenAccount, - outTokenAccount: outAssociatedTokenAccount, - reduceOnly, - userAccountPublicKey, - }); - - const ixs = [ - ...preInstructions, - beginSwapIx, - ...jupiterInstructions, - endSwapIx, - ]; - - return { ixs, lookupTables }; - } - public async getJupiterSwapIxV6({ jupiterClient, outMarketIndex, diff --git a/sdk/src/jupiter/jupiterClient.ts b/sdk/src/jupiter/jupiterClient.ts index 1aa09e48b..e943db585 100644 --- a/sdk/src/jupiter/jupiterClient.ts +++ b/sdk/src/jupiter/jupiterClient.ts @@ -224,6 +224,9 @@ export interface QuoteResponse { errorCode?: string; } +export const RECOMMENDED_JUPITER_API_VERSION = '/v1'; +export const RECOMMENDED_JUPITER_API = 'https://lite-api.jup.ag/swap'; + export class JupiterClient { url: string; connection: Connection; @@ -231,50 +234,7 @@ export class JupiterClient { constructor({ connection, url }: { connection: Connection; url?: string }) { this.connection = connection; - this.url = url ?? 'https://quote-api.jup.ag'; - } - - /** - * ** @deprecated - use getQuote - * Get routes for a swap - * @param inputMint the mint of the input token - * @param outputMint the mint of the output token - * @param amount the amount of the input token - * @param slippageBps the slippage tolerance in basis points - * @param swapMode the swap mode (ExactIn or ExactOut) - * @param onlyDirectRoutes whether to only return direct routes - */ - public async getRoutes({ - inputMint, - outputMint, - amount, - slippageBps = 50, - swapMode = 'ExactIn', - onlyDirectRoutes = false, - }: { - inputMint: PublicKey; - outputMint: PublicKey; - amount: BN; - slippageBps?: number; - swapMode?: SwapMode; - onlyDirectRoutes?: boolean; - }): Promise { - const params = new URLSearchParams({ - inputMint: inputMint.toString(), - outputMint: outputMint.toString(), - amount: amount.toString(), - slippageBps: slippageBps.toString(), - swapMode, - onlyDirectRoutes: onlyDirectRoutes.toString(), - }).toString(); - - const apiVersionParam = - this.url === 'https://quote-api.jup.ag' ? '/v4' : ''; - const { data: routes } = await ( - await fetch(`${this.url}${apiVersionParam}/quote?${params}`) - ).json(); - - return routes; + this.url = url ?? RECOMMENDED_JUPITER_API; } /** @@ -330,7 +290,9 @@ export class JupiterClient { params.delete('maxAccounts'); } const apiVersionParam = - this.url === 'https://quote-api.jup.ag' ? '/v6' : ''; + this.url === RECOMMENDED_JUPITER_API + ? RECOMMENDED_JUPITER_API_VERSION + : ''; const quote = await ( await fetch(`${this.url}${apiVersionParam}/quote?${params.toString()}`) ).json(); @@ -357,7 +319,9 @@ export class JupiterClient { } const apiVersionParam = - this.url === 'https://quote-api.jup.ag' ? '/v6' : ''; + this.url === RECOMMENDED_JUPITER_API + ? RECOMMENDED_JUPITER_API_VERSION + : ''; const resp = await ( await fetch(`${this.url}${apiVersionParam}/swap`, { method: 'POST', @@ -390,44 +354,6 @@ export class JupiterClient { } } - /** - * ** @deprecated - use getSwap - * Get a swap transaction for a route - * @param route the route to perform swap - * @param userPublicKey the signer's wallet public key - * @param slippageBps the slippage tolerance in basis points - */ - public async getSwapTransaction({ - route, - userPublicKey, - slippageBps = 50, - }: { - route: Route; - userPublicKey: PublicKey; - slippageBps?: number; - }): Promise { - const apiVersionParam = - this.url === 'https://quote-api.jup.ag' ? '/v4' : ''; - const resp = await ( - await fetch(`${this.url}${apiVersionParam}/swap`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - route, - userPublicKey, - slippageBps, - }), - }) - ).json(); - - const { swapTransaction } = resp; - - const swapTransactionBuf = Buffer.from(swapTransaction, 'base64'); - return VersionedTransaction.deserialize(swapTransactionBuf); - } - /** * Get the transaction message and lookup tables for a transaction * @param transaction