Skip to content

Commit

Permalink
fix: support for different token addresses in jupiter swap on solana
Browse files Browse the repository at this point in the history
  • Loading branch information
NickKelly1 committed Sep 10, 2024
1 parent a6f8b7b commit 08e84b3
Show file tree
Hide file tree
Showing 3 changed files with 281 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ export const executeSwap = async (
// Send the transaction
let txHash: string;
try {
// TODO: don't skip preflight
txHash = await conn.sendRawTransaction(tx.serialize());
} catch (err) {
// Log error info if possible
Expand Down Expand Up @@ -248,8 +249,8 @@ export const executeSwap = async (
// Trim the error so it doesn't break the UI by being too huge
let msg = (err as Error).message;
const len = msg.length;
if (len > 128 + 10 + len.toString().length) {
msg = `${msg.slice(0, 128)}... (128/${len.toString()})`;
if (len > 160 + 10 + len.toString().length) {
msg = `${msg.slice(0, 160)}... (160/${len.toString()})`;
}
throw new Error(msg);
}
Expand Down
54 changes: 46 additions & 8 deletions packages/extension/src/ui/action/views/swap/libs/solana-gasvals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,57 @@ export const getSolanaTransactionFees = async (
): Promise<Pick<GasFeeType, GasPriceTypes.REGULAR>> => {
let feesumlamp = additionalFee;
const conn = ((await network.api()) as SolanaAPI).web3;
const latestBlockHash = await conn.getLatestBlockhash();
let latestBlockHash = await conn.getLatestBlockhash();
for (let i = 0, len = txs.length; i < len; i++) {
const tx = txs[i];
// Use the latest block hash in-case it's fallen too far behind
tx.message.recentBlockhash = latestBlockHash.blockhash;
/** Base fee + priority fee + rent fees (but the rent fees seem slightly higher than expected @ 2024-09-04) */
const fee = await conn.getFeeForMessage(tx.message);
if (fee.value == null) {
throw new Error(
`Failed to get fee for Solana VersionedTransaction ${i}. Transaction block hash possibly expired.`
);

// Not sure why but getFeeForMessage sometimes returns null, so we will retry
// with small backoff in-case it helps
const backoff = [0, 500, 1_500];
/** 0 indexed attempt, used to index backoff ms from `backoff` */
let attempt = 0;
let fee: number;
// eslint-disable-next-line no-constant-condition
while (true) {
if (attempt >= backoff.length) {
throw new Error(
`Failed to get fee for Solana VersionedTransaction ${i + 1}` +
` after ${backoff.length} attempts.` +
` Transaction block hash ${tx.message.recentBlockhash} possibly expired.`
);
}
if (backoff[attempt] > 0) {
// wait before retrying
await new Promise((res) => {
return setTimeout(res, backoff[attempt]);
});
}
// Update the block hash in-case it caused 0 fees to be returned
if (attempt > 0) {
latestBlockHash = await conn.getLatestBlockhash();
tx.message.recentBlockhash = latestBlockHash.blockhash;
}

/** Base fee + priority fee (Don't know why this returns null sometimes) */
const feeResult = await conn.getFeeForMessage(tx.message);
if (feeResult.value == null) {
console.warn(
`Failed to get fee for Solana VersionedTransaction` +
` ${i + 1}. Transaction block hash ${tx.message.recentBlockhash}` +
` possibly expired. Attempt ${attempt + 1}/${backoff.length}.`
);
} else {
// Success
fee = feeResult.value;
break;
}

attempt += 1;
}
feesumlamp = feesumlamp.add(toBN(fee.value));

feesumlamp = feesumlamp.add(toBN(fee));
}

// Convert from lamports to SOL
Expand Down
Loading

0 comments on commit 08e84b3

Please sign in to comment.