/
PendingTransaction.ts
171 lines (159 loc) · 4.16 KB
/
PendingTransaction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import type {
BlockhashWithExpiryBlockHeight,
Cluster,
Connection,
Finality,
SignatureResult,
TransactionSignature,
} from "@solana/web3.js";
import promiseRetry from "promise-retry";
import type { WrapOptions } from "retry";
import { generateTXLink } from "../utils/txLink.js";
import { TransactionReceipt } from "./TransactionReceipt.js";
/**
* Options for awaiting a transaction confirmation.
*/
export interface TransactionWaitOptions
extends WrapOptions,
Partial<BlockhashWithExpiryBlockHeight> {
/**
* Commitment of the transaction. Defaults to `confirmed`.
*/
readonly commitment?: Finality;
/**
* Whether or not to use websockets for awaiting confirmation. Defaults to `false`.
*/
readonly useWebsocket?: boolean;
}
/**
* Transaction which may or may not be confirmed.
*/
export class PendingTransaction {
private _receipt: TransactionReceipt | null = null;
constructor(
readonly connection: Connection,
readonly signature: TransactionSignature
) {}
/**
* Gets the transaction receipt, if it has already been fetched.
*
* You probably want the async version of this function, `wait`.
*/
get receipt(): TransactionReceipt | null {
return this._receipt;
}
/**
* Waits for the confirmation of the transaction.
* @returns
*/
async wait({
commitment = "confirmed",
useWebsocket = true,
...retryOpts
}: TransactionWaitOptions = {}): Promise<TransactionReceipt> {
if (this._receipt) {
return this._receipt;
}
if (useWebsocket) {
await this.confirm({ commitment, ...retryOpts });
return await this.pollForReceipt({ commitment });
}
return await this.pollForReceipt({ commitment, ...retryOpts });
}
/**
* Fetches the TransactionReceipt via polling.
* @returns
*/
async pollForReceipt({
commitment = "confirmed",
...retryOpts
}: Omit<
TransactionWaitOptions,
"useWebsocket"
> = {}): Promise<TransactionReceipt> {
const receipt = await promiseRetry(
async (retry) => {
const result = await this.connection.getTransaction(this.signature, {
commitment,
});
if (!result) {
retry(new Error("Error fetching transaction"));
return;
}
return new TransactionReceipt(this.signature, result);
},
{
retries: 5,
minTimeout: 500,
...retryOpts,
}
);
if (!receipt) {
throw new Error("transaction could not be confirmed");
}
this._receipt = receipt;
return receipt;
}
/**
* Awaits the confirmation of the transaction, via onSignature subscription.
*
* @deprecated use {@link PendingTransaction#confirm}
* @returns
*/
async awaitSignatureConfirmation(
commitment: Finality = "confirmed"
): Promise<TransactionSignature> {
const { value } = await this.connection.confirmTransaction(
this.signature,
commitment
);
if (value.err) {
throw value.err;
}
return this.signature;
}
/**
* Awaits the confirmation of the transaction, via onSignature subscription.
* @returns
*/
async confirm({
commitment = "confirmed",
blockhash,
lastValidBlockHeight,
}: Pick<
TransactionWaitOptions,
"commitment" | "blockhash" | "lastValidBlockHeight"
>): Promise<TransactionSignature> {
let value: SignatureResult;
if (blockhash && lastValidBlockHeight) {
value = (
await this.connection.confirmTransaction(
{
signature: this.signature,
blockhash,
lastValidBlockHeight,
},
commitment
)
).value;
} else {
value = (
await this.connection.confirmTransaction(this.signature, commitment)
).value;
}
if (value.err) {
throw new Error(
`Transaction ${this.signature} failed (${JSON.stringify(value)})`
);
}
return this.signature;
}
/**
* Generates a link to view this {@link PendingTransaction} on the official Solana explorer.
* @param network
* @returns
*/
generateSolanaExplorerLink(cluster: Cluster = "mainnet-beta"): string {
return generateTXLink(this.signature, cluster);
}
}