Skip to content

refactor(sdk): update jupiter's api url #1575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 21 additions & 174 deletions sdk/src/driftClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ import { castNumberToSpotPrecision } from './math/spotMarket';
import {
JupiterClient,
QuoteResponse,
Route,
SwapMode,
} from './jupiter/jupiterClient';
import { getNonIdleUserFilter } from './memcmp';
Expand Down Expand Up @@ -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({
Expand All @@ -5067,10 +5068,10 @@ export class DriftClient {
amount,
slippageBps,
swapMode,
route,
reduceOnly,
txParams,
v6,
quote,
onlyDirectRoutes = false,
}: {
jupiterClient: JupiterClient;
Expand All @@ -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<TransactionSignature> {
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,
Expand All @@ -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,
Expand Down
94 changes: 10 additions & 84 deletions sdk/src/jupiter/jupiterClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,57 +224,17 @@ 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;
lookupTableCahce = new Map<string, AddressLookupTableAccount>();

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<Route[]> {
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;
}

/**
Expand Down Expand Up @@ -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();
Expand All @@ -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',
Expand Down Expand Up @@ -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<VersionedTransaction> {
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
Expand Down
Loading