Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,21 @@ const UninstallConfirmationCard = ({ xnft }: { xnft: any }) => {
//
// Confirm tx.
//
logger.debug("starting transaction confirmation", { txSig });
try {
await confirmTransaction(
// Add a UI-level timeout to prevent indefinite hanging
const confirmationPromise = confirmTransaction(
ctx.connection,
txSig,
ctx.commitment === "finalized" ? "finalized" : "confirmed"
);

const timeoutPromise = new Promise((_, reject) =>
setTimeout(() => reject(new Error("UI timeout: confirmation took too long")), 45000)
);

await Promise.race([confirmationPromise, timeoutPromise]);
logger.debug("transaction confirmed successfully", { txSig });
setCardType("complete");
} catch (err: any) {
logger.error("unable to confirm", err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ import type {
GetVersionedTransactionConfig,
} from "@solana/web3.js";

import { getLogger } from "@coral-xyz/common";

const logger = getLogger("confirm-transaction");

export async function confirmTransaction(
c: Connection,
txSig: string,
commitmentOrConfig?: GetVersionedTransactionConfig | Finality
): Promise<ReturnType<(typeof c)["getParsedTransaction"]>> {
return new Promise(async (resolve, reject) => {
logger.debug("starting confirmation with timeout", { txSig });
setTimeout(
() =>
reject(new Error(`30 second timeout: unable to confirm transaction`)),
30000
);
await new Promise((resolve) => setTimeout(resolve, 5000));
logger.debug("initial 5s delay complete, starting polling", { txSig });

const config = {
// Support confirming Versioned Transactions
Expand All @@ -27,11 +33,20 @@ export async function confirmTransaction(
: commitmentOrConfig),
};

let attempts = 0;
let tx = await c.getParsedTransaction(txSig, config);
logger.debug("first getParsedTransaction attempt", { txSig, found: tx !== null });
while (tx === null) {
attempts++;
logger.debug(`polling attempt ${attempts}`, { txSig });
tx = await c.getParsedTransaction(txSig, config);
await new Promise((resolve) => setTimeout(resolve, 1000));
if (attempts > 25) { // Prevent infinite loop, though timeout should catch it
logger.warn("exceeded max polling attempts", { txSig, attempts });
break;
}
}
logger.debug("confirmation complete", { txSig, found: tx !== null, attempts });
resolve(tx);
});
}