/
tiered.ts
143 lines (133 loc) · 3.89 KB
/
tiered.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
import type {
Blockhash,
BlockhashWithExpiryBlockHeight,
Commitment,
ConfirmOptions,
Connection,
RpcResponseAndContext,
SendOptions,
SimulatedTransactionResponse,
Transaction,
} from "@solana/web3.js";
import type { Broadcaster } from "../interfaces.js";
import { DEFAULT_PROVIDER_OPTIONS } from "../provider.js";
import { PendingTransaction } from "../transaction/index.js";
import { suppressConsoleErrorAsync } from "../utils/index.js";
import type { BroadcastOptions } from "./index.js";
import {
DEFAULT_FALLBACK_RETRY_OPTIONS,
DEFAULT_RETRY_OPTIONS,
SingleConnectionBroadcaster,
} from "./index.js";
import { sendAndSpamRawTx } from "./sendAndSpamRawTx.js";
/**
* Broadcasts transactions to multiple connections simultaneously.
*/
export class TieredBroadcaster implements Broadcaster {
readonly premiumBroadcaster: SingleConnectionBroadcaster;
constructor(
readonly primaryConnection: Connection,
/**
* Connections to send to in addition to the primary.
*/
readonly fallbackConnections: readonly Connection[],
readonly opts: ConfirmOptions = DEFAULT_PROVIDER_OPTIONS
) {
this.premiumBroadcaster = new SingleConnectionBroadcaster(
primaryConnection,
opts
);
}
async getLatestBlockhash(
commitment: Commitment = this.opts.preflightCommitment ?? "confirmed"
): Promise<BlockhashWithExpiryBlockHeight> {
return await this.premiumBroadcaster.getLatestBlockhash(commitment);
}
async getRecentBlockhash(
commitment: Commitment = this.opts.preflightCommitment ?? "confirmed"
): Promise<Blockhash> {
return await this.premiumBroadcaster.getRecentBlockhash(commitment);
}
private async _sendRawTransaction(
encoded: Buffer,
options?: SendOptions & Omit<BroadcastOptions, "printLogs">
): Promise<PendingTransaction> {
const pending = new PendingTransaction(
this.primaryConnection,
await sendAndSpamRawTx(
this.primaryConnection,
encoded,
options ?? this.opts,
options ?? DEFAULT_RETRY_OPTIONS
)
);
void (async () => {
await Promise.all(
this.fallbackConnections.map(async (fc) => {
try {
await sendAndSpamRawTx(
fc,
encoded,
options ?? this.opts,
options?.fallbackRetryOptions ?? DEFAULT_FALLBACK_RETRY_OPTIONS
);
} catch (e) {
console.warn(`[Broadcaster] _sendRawTransaction error`, e);
}
})
);
})();
return pending;
}
/**
* Broadcasts a signed transaction.
*
* @param tx
* @param confirm
* @param opts
* @returns
*/
async broadcast(
tx: Transaction,
{ printLogs = true, ...opts }: BroadcastOptions = this.opts
): Promise<PendingTransaction> {
if (tx.signatures.length === 0) {
throw new Error("Transaction must be signed before broadcasting.");
}
const rawTx = tx.serialize();
if (printLogs) {
return await this._sendRawTransaction(rawTx, opts);
}
return await suppressConsoleErrorAsync(async () => {
// hide the logs of TX errors if printLogs = false
return await this._sendRawTransaction(rawTx, opts);
});
}
/**
* Simulates a transaction with a commitment.
* @param tx
* @param commitment
* @returns
*/
async simulate(
tx: Transaction,
{
commitment = this.opts.preflightCommitment ?? "confirmed",
verifySigners = true,
}: {
commitment?: Commitment;
verifySigners?: boolean;
} = {
commitment: this.opts.preflightCommitment ?? "confirmed",
verifySigners: true,
}
): Promise<RpcResponseAndContext<SimulatedTransactionResponse>> {
if (verifySigners && tx.signatures.length === 0) {
throw new Error("Transaction must be signed before simulating.");
}
return this.premiumBroadcaster.simulate(tx, {
commitment,
verifySigners,
});
}
}