Created
October 25, 2024 19:48
-
-
Save crypt0miester/41ebbae1fdf94d749495be7847880999 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
Commitment, | |
Connection, | |
Context, | |
RpcResponseAndContext, | |
SignatureResult, | |
SignatureStatus, | |
TransactionSignature, | |
} from "@solana/web3.js"; | |
import { sleep } from "./index"; | |
export const enum TransactionStatus { | |
BLOCKHEIGHT_EXCEEDED = 0, | |
PROCESSED = 1, | |
TIMED_OUT = 2, | |
} | |
export async function awaitTransactionSignatureConfirmationBlockhashV2( | |
txid: TransactionSignature, | |
connection: Connection, | |
commitment: Commitment = "confirmed", | |
lastValidBlockHeight: number, | |
timeout = 60000, // 60 second timeout | |
): Promise<{ | |
status: SignatureStatus | null; | |
transactionStatus: TransactionStatus; | |
}> { | |
let done = false; | |
let status: SignatureStatus | null = null; | |
const checkBlockHeight = async () => { | |
try { | |
return await connection.getBlockHeight(commitment); | |
} catch (_e) { | |
return -1; | |
} | |
}; | |
let subId: number | undefined; | |
const confirmationPromise = new Promise<TransactionStatus>((resolve) => { | |
try { | |
subId = connection.onSignature( | |
txid, | |
(result: SignatureResult, context: Context) => { | |
done = true; | |
resolve(TransactionStatus.PROCESSED); | |
}, | |
commitment, | |
); | |
} catch (err) { | |
console.error("Error setting up onSignature listener:", err); | |
} | |
}); | |
const expiryPromise = (async () => { | |
const startTime = Date.now(); | |
while (Date.now() - startTime < timeout && !done) { | |
const currentBlockHeight = await checkBlockHeight(); | |
if (currentBlockHeight > lastValidBlockHeight) { | |
return TransactionStatus.BLOCKHEIGHT_EXCEEDED; | |
} | |
await sleep(1000); // Check every second | |
} | |
return TransactionStatus.TIMED_OUT; | |
})(); | |
const transactionStatus = await Promise.race([ | |
confirmationPromise, | |
expiryPromise, | |
]); | |
if (subId !== undefined) { | |
connection.removeSignatureListener(subId); | |
} | |
// Fetch the latest status regardless of the outcome | |
const signatureStatuses = await connection.getSignatureStatuses([txid]); | |
status = signatureStatuses && signatureStatuses.value[0]; | |
return { status, transactionStatus }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment