Skip to content

Commit 6d85f3c

Browse files
committed
Document @solana/transaction-confirmation with TypeDoc
1 parent 75aa166 commit 6d85f3c

7 files changed

+207
-1
lines changed

Diff for: packages/transaction-confirmation/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ try {
135135

136136
### `waitForRecentTransactionConfirmation()`
137137

138-
Supply your own confirmation implementations to this function to create a custom nonce transaction confirmation strategy.
138+
Supply your own confirmation implementations to this function to create a custom confirmation strategy for recently-landed transactions.
139139

140140
```ts
141141
import { waitForRecentTransactionConfirmation } from '@solana/transaction-confirmation';

Diff for: packages/transaction-confirmation/src/confirmation-strategy-blockheight.ts

+43
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@ import type { Commitment } from '@solana/rpc-types';
66

77
type GetBlockHeightExceedencePromiseFn = (config: {
88
abortSignal: AbortSignal;
9+
/**
10+
* Fetch the block height as of the highest slot that has reached this level of commitment.
11+
*
12+
* @defaultValue Whichever default is applied by the underlying {@link RpcApi} in use. For
13+
* example, when using an API created by a `createSolanaRpc*()` helper, the default commitment
14+
* is `"confirmed"` unless configured otherwise. Unmitigated by an API layer on the client, the
15+
* default commitment applied by the server is `"finalized"`.
16+
*/
917
commitment?: Commitment;
18+
/** The block height after which to reject the promise */
1019
lastValidBlockHeight: bigint;
1120
}) => Promise<void>;
1221

@@ -15,6 +24,40 @@ type CreateBlockHeightExceedencePromiseFactoryyConfig<TCluster> = {
1524
rpcSubscriptions: RpcSubscriptions<SlotNotificationsApi> & { '~cluster'?: TCluster };
1625
};
1726

27+
/**
28+
* Creates a promise that throws when the network progresses past the block height after which the
29+
* supplied blockhash is considered expired for use as a transaction lifetime specifier.
30+
*
31+
* When a transaction's lifetime is tied to a blockhash, that transaction can be landed on the
32+
* network until that blockhash expires. All blockhashes have a block height after which they are
33+
* considered to have expired.
34+
*
35+
* @param config
36+
*
37+
* @example
38+
* ```ts
39+
* import { isSolanaError, SolanaError } from '@solana/errors';
40+
* import { createBlockHeightExceedencePromiseFactory } from '@solana/transaction-confirmation';
41+
*
42+
* const getBlockHeightExceedencePromise = createBlockHeightExceedencePromiseFactory({
43+
* rpc,
44+
* rpcSubscriptions,
45+
* });
46+
* try {
47+
* await getBlockHeightExceedencePromise({ lastValidBlockHeight });
48+
* } catch (e) {
49+
* if (isSolanaError(e, SOLANA_ERROR__BLOCK_HEIGHT_EXCEEDED)) {
50+
* console.error(
51+
* `The block height of the network has exceeded ${e.context.lastValidBlockHeight}. ` +
52+
* `It is now ${e.context.currentBlockHeight}`,
53+
* );
54+
* // Re-sign and retry the transaction.
55+
* return;
56+
* }
57+
* throw e;
58+
* }
59+
* ```
60+
*/
1861
export function createBlockHeightExceedencePromiseFactory({
1962
rpc,
2063
rpcSubscriptions,

Diff for: packages/transaction-confirmation/src/confirmation-strategy-nonce.ts

+43
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,17 @@ import { Nonce } from '@solana/transaction-messages';
1010

1111
type GetNonceInvalidationPromiseFn = (config: {
1212
abortSignal: AbortSignal;
13+
/**
14+
* Fetch the nonce account details as of the highest slot that has reached this level of
15+
* commitment.
16+
*/
1317
commitment: Commitment;
18+
/**
19+
* The value of the nonce that we would expect to see in the nonce account in order for any
20+
* transaction with that nonce-based lifetime to be considered valid.
21+
*/
1422
currentNonceValue: Nonce;
23+
/** The address of the account in which the currently-valid nonce value is stored */
1524
nonceAccountAddress: Address;
1625
}) => Promise<void>;
1726

@@ -26,6 +35,40 @@ const NONCE_VALUE_OFFSET =
2635
32; // nonce authority(pubkey)
2736
// Then comes the nonce value.
2837

38+
/**
39+
* Creates a promise that throws when the value stored in a nonce account is not the expected one.
40+
*
41+
* When a transaction's lifetime is tied to the value stored in a nonce account, that transaction
42+
* can be landed on the network until the nonce is advanced to a new value.
43+
*
44+
* @param config
45+
*
46+
* @example
47+
* ```ts
48+
* import { isSolanaError, SolanaError } from '@solana/errors';
49+
* import { createNonceInvalidationPromiseFactory } from '@solana/transaction-confirmation';
50+
*
51+
* const getNonceInvalidationPromise = createNonceInvalidationPromiseFactory({
52+
* rpc,
53+
* rpcSubscriptions,
54+
* });
55+
* try {
56+
* await getNonceInvalidationPromise({
57+
* currentNonceValue,
58+
* nonceAccountAddress,
59+
* });
60+
* } catch (e) {
61+
* if (isSolanaError(e, SOLANA_ERROR__NONCE_INVALID)) {
62+
* console.error(`The nonce has advanced to ${e.context.actualNonceValue}`);
63+
* // Re-sign and retry the transaction.
64+
* return;
65+
* } else if (isSolanaError(e, SOLANA_ERROR__NONCE_ACCOUNT_NOT_FOUND)) {
66+
* console.error(`No nonce account was found at ${nonceAccountAddress}`);
67+
* }
68+
* throw e;
69+
* }
70+
* ```
71+
*/
2972
export function createNonceInvalidationPromiseFactory({
3073
rpc,
3174
rpcSubscriptions,

Diff for: packages/transaction-confirmation/src/confirmation-strategy-recent-signature.ts

+40
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ import { type Commitment, commitmentComparator } from '@solana/rpc-types';
88

99
type GetRecentSignatureConfirmationPromiseFn = (config: {
1010
abortSignal: AbortSignal;
11+
/**
12+
* The level of commitment the transaction must have achieved in order for the promise to
13+
* resolve.
14+
*/
1115
commitment: Commitment;
16+
/**
17+
* A 64 byte Ed25519 signature, encoded as a base-58 string, that uniquely identifies a
18+
* transaction by virtue of being the first or only signature in its list of signatures.
19+
*/
1220
signature: Signature;
1321
}) => Promise<void>;
1422

@@ -17,6 +25,38 @@ type CreateRecentSignatureConfirmationPromiseFactoryConfig<TCluster> = {
1725
rpcSubscriptions: RpcSubscriptions<SignatureNotificationsApi> & { '~cluster'?: TCluster };
1826
};
1927

28+
/**
29+
* Creates a promise that resolves when a recently-landed transaction achieves the target
30+
* confirmation commitment, and throws when the transaction fails with an error.
31+
*
32+
* The status of recently-landed transactions is available in the network's status cache. This
33+
* confirmation strategy will only yield a result if the signature is still in the status cache. To
34+
* fetch the status of transactions older than those available in the status cache, use the
35+
* {@link GetSignatureStatusesApi.getSignatureStatuses} method setting the
36+
* `searchTransactionHistory` configuration param to `true`.
37+
*
38+
* @param config
39+
*
40+
* @example
41+
* ```ts
42+
* import { createRecentSignatureConfirmationPromiseFactory } from '@solana/transaction-confirmation';
43+
*
44+
* const getRecentSignatureConfirmationPromise = createRecentSignatureConfirmationPromiseFactory({
45+
* rpc,
46+
* rpcSubscriptions,
47+
* });
48+
* try {
49+
* await getRecentSignatureConfirmationPromise({
50+
* commitment,
51+
* signature,
52+
* });
53+
* console.log(`The transaction with signature \`${signature}\` has achieved a commitment level of \`${commitment}\``);
54+
* } catch (e) {
55+
* console.error(`The transaction with signature \`${signature}\` failed`, e.cause);
56+
* throw e;
57+
* }
58+
* ```
59+
*/
2060
export function createRecentSignatureConfirmationPromiseFactory({
2161
rpc,
2262
rpcSubscriptions,

Diff for: packages/transaction-confirmation/src/confirmation-strategy-timeout.ts

+26
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,35 @@ import type { Commitment } from '@solana/rpc-types';
22

33
type Config = Readonly<{
44
abortSignal: AbortSignal;
5+
/**
6+
* The timeout promise will throw after 30 seconds when the commitment is `processed`, and 60
7+
* seconds otherwise.
8+
*/
59
commitment: Commitment;
610
}>;
711

12+
/**
13+
* When no other heuristic exists to infer that a transaction has expired, you can use this promise
14+
* factory with a commitment level. It throws after 30 seconds when the commitment is `processed`,
15+
* and 60 seconds otherwise. You would typically race this with another confirmation strategy.
16+
*
17+
* @param config
18+
*
19+
* @example
20+
* ```ts
21+
* import { safeRace } from '@solana/promises';
22+
* import { getTimeoutPromise } from '@solana/transaction-confirmation';
23+
*
24+
* try {
25+
* await safeRace([getCustomTransactionConfirmationPromise(/* ... *\/), getTimeoutPromise({ commitment })]);
26+
* } catch (e) {
27+
* if (e instanceof DOMException && e.name === 'TimeoutError') {
28+
* console.log('Could not confirm transaction after a timeout');
29+
* }
30+
* throw e;
31+
* }
32+
* ```
33+
*/
834
export async function getTimeoutPromise({ abortSignal: callerAbortSignal, commitment }: Config) {
935
return await new Promise((_, reject) => {
1036
const handleAbort = (e: AbortSignalEventMap['abort']) => {

Diff for: packages/transaction-confirmation/src/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* This package contains utilities for confirming transactions and for building your own transaction
3+
* confirmation strategies.
4+
*
5+
* @packageDocumentation
6+
*/
17
export * from './confirmation-strategy-blockheight';
28
export * from './confirmation-strategy-nonce';
39
export * from './confirmation-strategy-recent-signature';

Diff for: packages/transaction-confirmation/src/waiters.ts

+48
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,35 @@ interface WaitForRecentTransactionWithBlockhashLifetimeConfirmationConfig
2525
interface WaitForRecentTransactionWithTimeBasedLifetimeConfirmationConfig
2626
extends BaseTransactionConfirmationStrategyConfig {
2727
getTimeoutPromise: typeof getTimeoutPromise;
28+
/**
29+
* A 64 byte Ed25519 signature, encoded as a base-58 string, that uniquely identifies a
30+
* transaction by virtue of being the first or only signature in its list of signatures.
31+
*/
2832
signature: Signature;
2933
}
3034

35+
/**
36+
* Supply your own confirmation implementations to this function to create a custom nonce
37+
* transaction confirmation strategy.
38+
*
39+
* @example
40+
* ```ts
41+
* import { waitForDurableNonceTransactionConfirmation } from '@solana/transaction-confirmation';
42+
*
43+
* try {
44+
* await waitForDurableNonceTransactionConfirmation({
45+
* getNonceInvalidationPromise({ abortSignal, commitment, currentNonceValue, nonceAccountAddress }) {
46+
* // Return a promise that rejects when a nonce becomes invalid.
47+
* },
48+
* getRecentSignatureConfirmationPromise({ abortSignal, commitment, signature }) {
49+
* // Return a promise that resolves when a transaction achieves confirmation
50+
* },
51+
* });
52+
* } catch (e) {
53+
* // Handle errors.
54+
* }
55+
* ```
56+
*/
3157
export async function waitForDurableNonceTransactionConfirmation(
3258
config: WaitForDurableNonceTransactionConfirmationConfig,
3359
): Promise<void> {
@@ -47,6 +73,28 @@ export async function waitForDurableNonceTransactionConfirmation(
4773
);
4874
}
4975

76+
/**
77+
* Supply your own confirmation implementations to this function to create a custom confirmation
78+
* strategy for recently-landed transactions.
79+
*
80+
* @example
81+
* ```ts
82+
* import { waitForRecentTransactionConfirmation } from '@solana/transaction-confirmation';
83+
*
84+
* try {
85+
* await waitForRecentTransactionConfirmation({
86+
* getBlockHeightExceedencePromise({ abortSignal, commitment, lastValidBlockHeight }) {
87+
* // Return a promise that rejects when the blockhash's block height has been exceeded
88+
* },
89+
* getRecentSignatureConfirmationPromise({ abortSignal, commitment, signature }) {
90+
* // Return a promise that resolves when a transaction achieves confirmation
91+
* },
92+
* });
93+
* } catch (e) {
94+
* // Handle errors.
95+
* }
96+
* ```
97+
*/
5098
export async function waitForRecentTransactionConfirmation(
5199
config: WaitForRecentTransactionWithBlockhashLifetimeConfirmationConfig,
52100
): Promise<void> {

0 commit comments

Comments
 (0)