Skip to content

Commit

Permalink
Merge pull request #510 from enkryptcom/fix/jupiter-swap-to-address
Browse files Browse the repository at this point in the history
Fix support for different token addresses in Jupiter swap
  • Loading branch information
kvhnuke authored Sep 10, 2024
2 parents 5b22281 + f5c1465 commit 3531a96
Show file tree
Hide file tree
Showing 5 changed files with 524 additions and 102 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

2 comments on commit 3531a96

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.