From 048eb6c08a92091c8a5e69cb529eef1d2f499c45 Mon Sep 17 00:00:00 2001 From: Evan <96965321+0xIchigo@users.noreply.github.com> Date: Thu, 15 Aug 2024 14:16:17 -0400 Subject: [PATCH] fix(txts): Better `pollTransactionConfirmation` (#126) * Update pollTransactionConfirmation * Update README.md --- README.md | 2 +- src/RpcClient.ts | 21 ++++++++++++--------- src/types/types.ts | 9 +++++++++ 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 72f92d7..1da98c9 100644 --- a/README.md +++ b/README.md @@ -724,7 +724,7 @@ const units = helius.rpc.getComputeUnits(instructions, payerKey, []); ``` ### pollTransactionConfirmation() -This method polls a transaction to check whether it has been confirmed. It takes in a `TransactionSignature` and checks whether it has been confirmed within the timeout period. Currently, this method has a 15 second timeout and a 5 second retry interval, so it polls 3 times over 15 seconds. It returns the confirmed transaction signature or an error if the confirmation times out: +This method polls a transaction to check whether it has been confirmed. It takes in a `TransactionSignature` and checks whether it has been confirmed within the timeout period. Currently, this method defaults to a 15 second timeout and a 5 second retry interval, so it polls 3 times over 15 seconds. However, with `PollTransactionOptions`, these values can be changed in addition to the confirmation status. It returns the confirmed transaction signature or an error if the confirmation times out: ```ts let txSig = await helius.connection().sendRawTransaction(transaction.serialize(), { diff --git a/src/RpcClient.ts b/src/RpcClient.ts index 00b9c72..f21ca44 100644 --- a/src/RpcClient.ts +++ b/src/RpcClient.ts @@ -32,6 +32,7 @@ import { JITO_API_URLS, JITO_TIP_ACCOUNTS, JitoRegion, + PollTransactionOptions, SmartTransactionContext, } from './types'; @@ -496,33 +497,35 @@ export class RpcClient { /** * Poll a transaction to check whether it has been confirmed * @param {TransactionSignature} txtSig - The transaction signature + * @param {PollTransactionOptions} pollOptions - Optional parameters for polling * @returns {Promise} - The confirmed transaction signature or an error if the confirmation times out */ async pollTransactionConfirmation( - txtSig: TransactionSignature + txtSig: TransactionSignature, + pollOptions: PollTransactionOptions = { + confirmationStatuses: ["confirmed", "finalized"], + timeout: 15000, + interval: 5000 + } ): Promise { - // 15 second timeout - const timeout = 15000; - // 5 second retry interval - const interval = 5000; let elapsed = 0; return new Promise((resolve, reject) => { const intervalId = setInterval(async () => { - elapsed += interval; + elapsed += pollOptions.interval!; - if (elapsed >= timeout) { + if (elapsed >= pollOptions.timeout!) { clearInterval(intervalId); reject(new Error(`Transaction ${txtSig}'s confirmation timed out`)); } const status = await this.connection.getSignatureStatus(txtSig); - if (status?.value?.confirmationStatus === 'confirmed' || status?.value?.confirmationStatus === 'finalized') { + if (status?.value?.confirmationStatus && pollOptions.confirmationStatuses?.includes(status?.value?.confirmationStatus)) { clearInterval(intervalId); resolve(txtSig); } - }, interval); + }, pollOptions.interval); }); } diff --git a/src/types/types.ts b/src/types/types.ts index cf0061b..37eddaa 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -3,6 +3,7 @@ import type { Cluster, Keypair, Transaction, + TransactionConfirmationStatus, TransactionError, VersionedTransaction, } from '@solana/web3.js'; @@ -362,3 +363,11 @@ export interface GetPriorityFeeEstimateResponse { } export type JitoRegion = 'Default' | 'NY' | 'Amsterdam' | 'Frankfurt' | 'Tokyo'; + +export type PollTransactionOptions = { + confirmationStatuses?: TransactionConfirmationStatus[]; + // In milliseconds + timeout?: number; + // In milliseconds + interval?: number; +} \ No newline at end of file