Skip to content
Merged
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
19 changes: 15 additions & 4 deletions packages/swap/src/providers/jupiter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ async function getJupiterTokens(abortable?: {
const url = JUPITER_TOKENS_URL;
let failed = false;
let tokens: JupiterTokenInfo[];
const backoff = [0, 100, 500, 1000, 2_500, 5_000];
const backoff = [0, 100, 500, 1000, 2_000, 4_000];
let backoffi = 0;
let errRef: undefined | { err: Error };

Expand Down Expand Up @@ -795,7 +795,7 @@ async function getJupiterQuote(

let failed = false;
let quote: JupiterQuoteResponse;
const backoff = [0, 100, 500, 1000, 2_500, 5_000];
const backoff = [0, 100, 500, 1000, 2_000, 4_000];
let backoffi = 0;
let errRef: undefined | { err: Error };

Expand Down Expand Up @@ -936,12 +936,23 @@ async function getJupiterSwap(
feeAccount: referrerATAPubkey?.toBase58(),
quoteResponse: quote,
destinationTokenAccount: dstATAPubkey?.toBase58(),
/** @see https://station.jup.ag/api-v6/post-swap */
prioritizationFeeLamports: {
/**
* The automatic fee seems low and frequently causes transactions
* to be dropped when traffic is high
*
* This number has been arbitrary selected from manual testing @ 2024-11-21
* where there's been a bunch of network activity causing dropped transactions
*/
autoMultiplier: 6,
},
};

const url = `${JUPITER_API_URL}swap`;
let failed = false;
let swap: JupiterSwapResponse;
const backoff = [0, 100, 500, 1000, 2_500, 5_000];
const backoff = [0, 100, 500, 1000, 2_000, 4_000];
let backoffi = 0;
let errRef: undefined | { err: Error };

Expand Down Expand Up @@ -1197,6 +1208,6 @@ function sleep(
clearTimeout(timeout);
}
abortable?.signal?.addEventListener("abort", onAbortDuringSleep);
const timeout = setTimeout(onTimeout);
const timeout = setTimeout(onTimeout, duration);
});
}
9 changes: 8 additions & 1 deletion packages/swap/src/providers/jupiter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,14 @@ export type JupiterSwapParams = {
trackingAccount?: string;
/** Integer */
computeUnitPriceMicroLamports?: number;
prioritizationFeeLamports?: number;
/** Integer */
prioritizationFeeLamports?:
| number
| "auto"
| {
/** Integer */
autoMultiplier: number;
};
/** Default: false */
asLegacyTransaction?: boolean;
/** Default: false */
Expand Down
52 changes: 52 additions & 0 deletions packages/swap/src/utils/solana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,58 @@ export function extractComputeBudget(
);
}

/**
* @see https://solana.com/docs/core/fees#prioritization-fees
*/
export function extractComputeUnitPriceMicroLamports(
tx: VersionedTransaction,
): undefined | number | bigint {
/** Compute unit price */
let computeUnitPriceMicroLamports: undefined | number | bigint;

// eslint-disable-next-line no-restricted-syntax, no-labels
instructionLoop: for (
let i = 0, len = tx.message.compiledInstructions.length;
i < len;
i++
) {
const instr = tx.message.compiledInstructions[i];
const program = tx.message.staticAccountKeys[instr.programIdIndex];
if (!ComputeBudgetProgram.programId.equals(program)) continue;

const keys = instr.accountKeyIndexes.map(
(accountKeyIndex): AccountMeta => ({
pubkey: tx.message.staticAccountKeys[accountKeyIndex],
isSigner: tx.message.isAccountSigner(accountKeyIndex),
isWritable: tx.message.isAccountWritable(accountKeyIndex),
}),
);

// Decompile the instruction
const instruction = new TransactionInstruction({
keys,
programId: program,
data: Buffer.from(instr.data),
});

const type = ComputeBudgetInstruction.decodeInstructionType(instruction);
switch (type) {
case "SetComputeUnitPrice": {
// Compute limit
const command =
ComputeBudgetInstruction.decodeSetComputeUnitPrice(instruction);
computeUnitPriceMicroLamports = command.microLamports;
// eslint-disable-next-line no-labels
break instructionLoop;
}
default: /** noop */
break;
}
}

return computeUnitPriceMicroLamports;
}

/**
* Insert new instructions at the start of a transaction, after compute budget and compute limit instructions
*/
Expand Down