Skip to content

Commit

Permalink
Merge pull request #562 from enkryptcom/fix/dropped-jupiter-sol-swap-…
Browse files Browse the repository at this point in the history
…transactions

fix: dropped jupiter sol swap transactions
  • Loading branch information
kvhnuke authored Dec 4, 2024
2 parents 969ea9c + 10dfeb0 commit 775e897
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 5 deletions.
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 @@ -578,7 +578,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 @@ -755,7 +755,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 @@ -894,12 +894,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 @@ -1152,6 +1163,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

0 comments on commit 775e897

Please sign in to comment.