Skip to content

Commit

Permalink
fix(txts): Better pollTransactionConfirmation (#126)
Browse files Browse the repository at this point in the history
* Update pollTransactionConfirmation

* Update README.md
  • Loading branch information
0xIchigo authored Aug 15, 2024
1 parent 83c7e5c commit 048eb6c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(), {
Expand Down
21 changes: 12 additions & 9 deletions src/RpcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
JITO_API_URLS,
JITO_TIP_ACCOUNTS,
JitoRegion,
PollTransactionOptions,
SmartTransactionContext,
} from './types';

Expand Down Expand Up @@ -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<TransactionSignature>} - 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<TransactionSignature> {
// 15 second timeout
const timeout = 15000;
// 5 second retry interval
const interval = 5000;
let elapsed = 0;

return new Promise<TransactionSignature>((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);
});
}

Expand Down
9 changes: 9 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
Cluster,
Keypair,
Transaction,
TransactionConfirmationStatus,
TransactionError,
VersionedTransaction,
} from '@solana/web3.js';
Expand Down Expand Up @@ -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;
}

0 comments on commit 048eb6c

Please sign in to comment.