@atomiqlabs/chain-solana 12.0.12 → 12.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -201
- package/dist/index.d.ts +29 -29
- package/dist/index.js +45 -45
- package/dist/solana/SolanaChainType.d.ts +11 -11
- package/dist/solana/SolanaChainType.js +2 -2
- package/dist/solana/SolanaChains.d.ts +20 -20
- package/dist/solana/SolanaChains.js +25 -25
- package/dist/solana/SolanaInitializer.d.ts +18 -18
- package/dist/solana/SolanaInitializer.js +63 -63
- package/dist/solana/btcrelay/SolanaBtcRelay.d.ts +228 -228
- package/dist/solana/btcrelay/SolanaBtcRelay.js +441 -441
- package/dist/solana/btcrelay/headers/SolanaBtcHeader.d.ts +29 -29
- package/dist/solana/btcrelay/headers/SolanaBtcHeader.js +34 -34
- package/dist/solana/btcrelay/headers/SolanaBtcStoredHeader.d.ts +46 -46
- package/dist/solana/btcrelay/headers/SolanaBtcStoredHeader.js +78 -78
- package/dist/solana/btcrelay/program/programIdl.json +671 -671
- package/dist/solana/chain/SolanaAction.d.ts +26 -26
- package/dist/solana/chain/SolanaAction.js +86 -86
- package/dist/solana/chain/SolanaChainInterface.d.ts +65 -65
- package/dist/solana/chain/SolanaChainInterface.js +125 -125
- package/dist/solana/chain/SolanaModule.d.ts +14 -14
- package/dist/solana/chain/SolanaModule.js +13 -13
- package/dist/solana/chain/modules/SolanaAddresses.d.ts +8 -8
- package/dist/solana/chain/modules/SolanaAddresses.js +22 -22
- package/dist/solana/chain/modules/SolanaBlocks.d.ts +28 -28
- package/dist/solana/chain/modules/SolanaBlocks.js +72 -72
- package/dist/solana/chain/modules/SolanaEvents.d.ts +68 -68
- package/dist/solana/chain/modules/SolanaEvents.js +238 -225
- package/dist/solana/chain/modules/SolanaFees.d.ts +121 -121
- package/dist/solana/chain/modules/SolanaFees.js +379 -379
- package/dist/solana/chain/modules/SolanaSignatures.d.ts +23 -23
- package/dist/solana/chain/modules/SolanaSignatures.js +39 -39
- package/dist/solana/chain/modules/SolanaSlots.d.ts +31 -31
- package/dist/solana/chain/modules/SolanaSlots.js +68 -68
- package/dist/solana/chain/modules/SolanaTokens.d.ts +136 -136
- package/dist/solana/chain/modules/SolanaTokens.js +248 -248
- package/dist/solana/chain/modules/SolanaTransactions.d.ts +124 -124
- package/dist/solana/chain/modules/SolanaTransactions.js +323 -323
- package/dist/solana/events/SolanaChainEvents.d.ts +88 -88
- package/dist/solana/events/SolanaChainEvents.js +256 -256
- package/dist/solana/events/SolanaChainEventsBrowser.d.ts +75 -75
- package/dist/solana/events/SolanaChainEventsBrowser.js +172 -172
- package/dist/solana/program/SolanaProgramBase.d.ts +40 -40
- package/dist/solana/program/SolanaProgramBase.js +43 -43
- package/dist/solana/program/SolanaProgramModule.d.ts +8 -8
- package/dist/solana/program/SolanaProgramModule.js +11 -11
- package/dist/solana/program/modules/SolanaProgramEvents.d.ts +53 -53
- package/dist/solana/program/modules/SolanaProgramEvents.js +114 -114
- package/dist/solana/swaps/SolanaSwapData.d.ts +71 -71
- package/dist/solana/swaps/SolanaSwapData.js +292 -292
- package/dist/solana/swaps/SolanaSwapModule.d.ts +10 -10
- package/dist/solana/swaps/SolanaSwapModule.js +11 -11
- package/dist/solana/swaps/SolanaSwapProgram.d.ts +224 -224
- package/dist/solana/swaps/SolanaSwapProgram.js +570 -570
- package/dist/solana/swaps/SwapTypeEnum.d.ts +11 -11
- package/dist/solana/swaps/SwapTypeEnum.js +42 -42
- package/dist/solana/swaps/modules/SolanaDataAccount.d.ts +94 -94
- package/dist/solana/swaps/modules/SolanaDataAccount.js +231 -231
- package/dist/solana/swaps/modules/SolanaLpVault.d.ts +71 -71
- package/dist/solana/swaps/modules/SolanaLpVault.js +173 -173
- package/dist/solana/swaps/modules/SwapClaim.d.ts +129 -129
- package/dist/solana/swaps/modules/SwapClaim.js +291 -291
- package/dist/solana/swaps/modules/SwapInit.d.ts +217 -217
- package/dist/solana/swaps/modules/SwapInit.js +519 -519
- package/dist/solana/swaps/modules/SwapRefund.d.ts +82 -82
- package/dist/solana/swaps/modules/SwapRefund.js +262 -262
- package/dist/solana/swaps/programIdl.json +945 -945
- package/dist/solana/swaps/programTypes.d.ts +943 -943
- package/dist/solana/swaps/programTypes.js +945 -945
- package/dist/solana/wallet/SolanaKeypairWallet.d.ts +9 -9
- package/dist/solana/wallet/SolanaKeypairWallet.js +33 -33
- package/dist/solana/wallet/SolanaSigner.d.ts +11 -11
- package/dist/solana/wallet/SolanaSigner.js +17 -17
- package/dist/utils/Utils.d.ts +53 -53
- package/dist/utils/Utils.js +170 -170
- package/package.json +41 -41
- package/src/index.ts +36 -36
- package/src/solana/SolanaChainType.ts +27 -27
- package/src/solana/SolanaChains.ts +23 -23
- package/src/solana/SolanaInitializer.ts +102 -102
- package/src/solana/btcrelay/SolanaBtcRelay.ts +589 -589
- package/src/solana/btcrelay/headers/SolanaBtcHeader.ts +57 -57
- package/src/solana/btcrelay/headers/SolanaBtcStoredHeader.ts +102 -102
- package/src/solana/btcrelay/program/programIdl.json +670 -670
- package/src/solana/chain/SolanaAction.ts +108 -108
- package/src/solana/chain/SolanaChainInterface.ts +192 -192
- package/src/solana/chain/SolanaModule.ts +20 -20
- package/src/solana/chain/modules/SolanaAddresses.ts +20 -20
- package/src/solana/chain/modules/SolanaBlocks.ts +78 -78
- package/src/solana/chain/modules/SolanaEvents.ts +270 -256
- package/src/solana/chain/modules/SolanaFees.ts +450 -450
- package/src/solana/chain/modules/SolanaSignatures.ts +39 -39
- package/src/solana/chain/modules/SolanaSlots.ts +82 -82
- package/src/solana/chain/modules/SolanaTokens.ts +307 -307
- package/src/solana/chain/modules/SolanaTransactions.ts +365 -365
- package/src/solana/events/SolanaChainEvents.ts +299 -299
- package/src/solana/events/SolanaChainEventsBrowser.ts +209 -209
- package/src/solana/program/SolanaProgramBase.ts +79 -79
- package/src/solana/program/SolanaProgramModule.ts +15 -15
- package/src/solana/program/modules/SolanaProgramEvents.ts +155 -155
- package/src/solana/swaps/SolanaSwapData.ts +430 -430
- package/src/solana/swaps/SolanaSwapModule.ts +16 -16
- package/src/solana/swaps/SolanaSwapProgram.ts +854 -854
- package/src/solana/swaps/SwapTypeEnum.ts +29 -29
- package/src/solana/swaps/modules/SolanaDataAccount.ts +307 -307
- package/src/solana/swaps/modules/SolanaLpVault.ts +215 -215
- package/src/solana/swaps/modules/SwapClaim.ts +389 -389
- package/src/solana/swaps/modules/SwapInit.ts +663 -663
- package/src/solana/swaps/modules/SwapRefund.ts +323 -323
- package/src/solana/swaps/programIdl.json +944 -944
- package/src/solana/swaps/programTypes.ts +1885 -1885
- package/src/solana/wallet/SolanaKeypairWallet.ts +36 -36
- package/src/solana/wallet/SolanaSigner.ts +24 -24
- package/src/utils/Utils.ts +180 -180
|
@@ -1,366 +1,366 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ComputeBudgetInstruction,
|
|
3
|
-
ComputeBudgetProgram, Finality, Keypair, RpcResponseAndContext,
|
|
4
|
-
SendOptions, SignatureResult, Signer, Transaction,
|
|
5
|
-
TransactionExpiredBlockheightExceededError
|
|
6
|
-
} from "@solana/web3.js";
|
|
7
|
-
import {SolanaModule} from "../SolanaModule";
|
|
8
|
-
import * as bs58 from "bs58";
|
|
9
|
-
import {tryWithRetries} from "../../../utils/Utils";
|
|
10
|
-
import {Buffer} from "buffer";
|
|
11
|
-
import {SolanaSigner} from "../../wallet/SolanaSigner";
|
|
12
|
-
import {TransactionRevertedError} from "@atomiqlabs/base";
|
|
13
|
-
|
|
14
|
-
export type SolanaTx = {tx: Transaction, signers: Signer[]};
|
|
15
|
-
|
|
16
|
-
export class SolanaTransactions extends SolanaModule {
|
|
17
|
-
|
|
18
|
-
private cbkBeforeTxSigned: (tx: SolanaTx) => Promise<void>;
|
|
19
|
-
/**
|
|
20
|
-
* Callback for sending transaction, returns not null if it was successfully able to send the transaction, and null
|
|
21
|
-
* if the transaction should be sent through other means)
|
|
22
|
-
* @private
|
|
23
|
-
*/
|
|
24
|
-
private cbkSendTransaction: (tx: Buffer, options?: SendOptions) => Promise<string>;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Sends raw solana transaction, first through the cbkSendTransaction callback (for e.g. sending the transaction
|
|
28
|
-
* to a different specific RPC), the through the Fees handler (for e.g. Jito transaction) and last through the
|
|
29
|
-
* underlying provider's Connection instance (the usual way). Only sends the transaction through one channel.
|
|
30
|
-
*
|
|
31
|
-
* @param data
|
|
32
|
-
* @param options
|
|
33
|
-
* @private
|
|
34
|
-
*/
|
|
35
|
-
private async sendRawTransaction(data: Buffer, options?: SendOptions): Promise<string> {
|
|
36
|
-
let result: string = null;
|
|
37
|
-
options ??= {};
|
|
38
|
-
options.maxRetries = 0;
|
|
39
|
-
if(this.cbkSendTransaction!=null) result = await this.cbkSendTransaction(data, options);
|
|
40
|
-
if(result==null) result = await this.root.Fees.submitTx(data, options);
|
|
41
|
-
if(result==null) result = await this.connection.sendRawTransaction(data, options);
|
|
42
|
-
// this.logger.debug("sendRawTransaction(): tx sent, signature: "+result);
|
|
43
|
-
return result;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Waits for the transaction to confirm by periodically checking the transaction status over HTTP, also
|
|
48
|
-
* re-sends the transaction at regular intervals
|
|
49
|
-
*
|
|
50
|
-
* @param solanaTx solana tx to wait for confirmation for
|
|
51
|
-
* @param finality wait for this finality
|
|
52
|
-
* @param abortSignal signal to abort waiting for tx confirmation
|
|
53
|
-
* @private
|
|
54
|
-
*/
|
|
55
|
-
private txConfirmationAndResendWatchdog(
|
|
56
|
-
solanaTx: SolanaTx,
|
|
57
|
-
finality?: Finality,
|
|
58
|
-
abortSignal?: AbortSignal
|
|
59
|
-
): Promise<string> {
|
|
60
|
-
const rawTx = solanaTx.tx.serialize();
|
|
61
|
-
const signature = bs58.encode(solanaTx.tx.signature);
|
|
62
|
-
return new Promise((resolve, reject) => {
|
|
63
|
-
let watchdogInterval: NodeJS.Timer;
|
|
64
|
-
watchdogInterval = setInterval(async () => {
|
|
65
|
-
const result = await this.sendRawTransaction(rawTx, {skipPreflight: true}).catch(
|
|
66
|
-
e => this.logger.error("txConfirmationAndResendWatchdog(): transaction re-sent error: ", e)
|
|
67
|
-
);
|
|
68
|
-
this.logger.debug("txConfirmationAndResendWatchdog(): transaction re-sent: "+result);
|
|
69
|
-
|
|
70
|
-
const status = await this.getTxIdStatus(signature, finality).catch(
|
|
71
|
-
e => this.logger.error("txConfirmationAndResendWatchdog(): get tx id status error: ", e)
|
|
72
|
-
);
|
|
73
|
-
if(status==null || status==="not_found") return;
|
|
74
|
-
if(status==="success") {
|
|
75
|
-
this.logger.info("txConfirmationAndResendWatchdog(): transaction confirmed from HTTP polling, signature: "+signature);
|
|
76
|
-
resolve(signature);
|
|
77
|
-
}
|
|
78
|
-
if(status==="reverted") reject(new TransactionRevertedError("Transaction reverted!"));
|
|
79
|
-
clearInterval(watchdogInterval);
|
|
80
|
-
}, this.retryPolicy?.transactionResendInterval || 3000);
|
|
81
|
-
|
|
82
|
-
if(abortSignal!=null) abortSignal.addEventListener("abort", () => {
|
|
83
|
-
clearInterval(watchdogInterval);
|
|
84
|
-
reject(abortSignal.reason);
|
|
85
|
-
});
|
|
86
|
-
})
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Waits for the transaction to confirm from WS, sometimes the WS rejects even though the transaction was confirmed
|
|
91
|
-
* this therefore also runs an ultimate check on the transaction in case the WS handler rejects, checking if it
|
|
92
|
-
* really was expired
|
|
93
|
-
*
|
|
94
|
-
* @param solanaTx solana tx to wait for confirmation for
|
|
95
|
-
* @param finality wait for this finality
|
|
96
|
-
* @param abortSignal signal to abort waiting for tx confirmation
|
|
97
|
-
* @private
|
|
98
|
-
*/
|
|
99
|
-
private async txConfirmFromWebsocket(
|
|
100
|
-
solanaTx: SolanaTx,
|
|
101
|
-
finality?: Finality,
|
|
102
|
-
abortSignal?: AbortSignal
|
|
103
|
-
): Promise<string> {
|
|
104
|
-
const signature = bs58.encode(solanaTx.tx.signature);
|
|
105
|
-
|
|
106
|
-
let result: RpcResponseAndContext<SignatureResult>;
|
|
107
|
-
try {
|
|
108
|
-
result = await this.connection.confirmTransaction({
|
|
109
|
-
signature: signature,
|
|
110
|
-
blockhash: solanaTx.tx.recentBlockhash,
|
|
111
|
-
lastValidBlockHeight: solanaTx.tx.lastValidBlockHeight,
|
|
112
|
-
abortSignal
|
|
113
|
-
}, finality);
|
|
114
|
-
this.logger.info("txConfirmFromWebsocket(): transaction confirmed from WS, signature: "+signature);
|
|
115
|
-
} catch (err) {
|
|
116
|
-
if(abortSignal!=null && abortSignal.aborted) throw err;
|
|
117
|
-
this.logger.debug("txConfirmFromWebsocket(): transaction rejected from WS, running ultimate check, expiry blockheight: "+solanaTx.tx.lastValidBlockHeight+" signature: "+signature+" error: "+err);
|
|
118
|
-
const status = await tryWithRetries(
|
|
119
|
-
() => this.getTxIdStatus(signature, finality)
|
|
120
|
-
);
|
|
121
|
-
this.logger.info("txConfirmFromWebsocket(): transaction status: "+status+" signature: "+signature);
|
|
122
|
-
if(status==="success") return signature;
|
|
123
|
-
if(status==="reverted") throw new TransactionRevertedError("Transaction reverted!");
|
|
124
|
-
if(err instanceof TransactionExpiredBlockheightExceededError || err.toString().startsWith("TransactionExpiredBlockheightExceededError")) {
|
|
125
|
-
throw new Error("Transaction expired before confirmation, please try again!");
|
|
126
|
-
} else {
|
|
127
|
-
throw err;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
if(result.value.err!=null) throw new TransactionRevertedError("Transaction reverted!");
|
|
131
|
-
return signature;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Waits for transaction confirmation using WS subscription and occasional HTTP polling, also re-sends
|
|
136
|
-
* the transaction at regular interval
|
|
137
|
-
*
|
|
138
|
-
* @param solanaTx solana transaction to wait for confirmation for & keep re-sending until it confirms
|
|
139
|
-
* @param abortSignal signal to abort waiting for tx confirmation
|
|
140
|
-
* @param finality wait for specific finality
|
|
141
|
-
* @private
|
|
142
|
-
*/
|
|
143
|
-
private async confirmTransaction(solanaTx: SolanaTx, abortSignal?: AbortSignal, finality?: Finality) {
|
|
144
|
-
const abortController = new AbortController();
|
|
145
|
-
if(abortSignal!=null) abortSignal.addEventListener("abort", () => {
|
|
146
|
-
abortController.abort();
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
let txSignature: string;
|
|
150
|
-
try {
|
|
151
|
-
txSignature = await Promise.race([
|
|
152
|
-
this.txConfirmationAndResendWatchdog(solanaTx, finality, abortController.signal),
|
|
153
|
-
this.txConfirmFromWebsocket(solanaTx, finality, abortController.signal)
|
|
154
|
-
]);
|
|
155
|
-
} catch (e) {
|
|
156
|
-
abortController.abort(e);
|
|
157
|
-
throw e;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// this.logger.info("confirmTransaction(): transaction confirmed, signature: "+txSignature);
|
|
161
|
-
|
|
162
|
-
abortController.abort();
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Prepares solana transactions, assigns recentBlockhash if needed, applies Phantom hotfix,
|
|
167
|
-
* sets feePayer to ourselves, calls beforeTxSigned callback & signs transaction with provided signers array
|
|
168
|
-
*
|
|
169
|
-
* @param signer
|
|
170
|
-
* @param txs
|
|
171
|
-
* @private
|
|
172
|
-
*/
|
|
173
|
-
private async prepareTransactions(signer: SolanaSigner, txs: SolanaTx[]): Promise<void> {
|
|
174
|
-
let latestBlockData: {blockhash: string, lastValidBlockHeight: number} = null;
|
|
175
|
-
|
|
176
|
-
for(let tx of txs) {
|
|
177
|
-
if(tx.tx.recentBlockhash==null) {
|
|
178
|
-
if(latestBlockData==null) {
|
|
179
|
-
latestBlockData = await tryWithRetries(
|
|
180
|
-
() => this.connection.getLatestBlockhash("confirmed"),
|
|
181
|
-
this.retryPolicy
|
|
182
|
-
);
|
|
183
|
-
this.logger.debug("prepareTransactions(): fetched latest block data for transactions," +
|
|
184
|
-
" blockhash: "+latestBlockData.blockhash+" expiry blockheight: "+latestBlockData.lastValidBlockHeight);
|
|
185
|
-
}
|
|
186
|
-
tx.tx.recentBlockhash = latestBlockData.blockhash;
|
|
187
|
-
tx.tx.lastValidBlockHeight = latestBlockData.lastValidBlockHeight;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
//This is a hotfix for Phantom adding compute unit price instruction on the first position & breaking
|
|
191
|
-
// required instructions order (e.g. btc relay verify needs to be 0th instruction in a tx)
|
|
192
|
-
if(signer.keypair==null && tx.tx.signatures.length===0) {
|
|
193
|
-
const foundIx = tx.tx.instructions.find(ix => ix.programId.equals(ComputeBudgetProgram.programId) && ComputeBudgetInstruction.decodeInstructionType(ix)==="SetComputeUnitPrice")
|
|
194
|
-
if(foundIx==null) tx.tx.instructions.splice(tx.tx.instructions.length-1, 0, ComputeBudgetProgram.setComputeUnitPrice({microLamports: 1}));
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
tx.tx.feePayer = signer.getPublicKey();
|
|
198
|
-
if(this.cbkBeforeTxSigned!=null) await this.cbkBeforeTxSigned(tx);
|
|
199
|
-
if(tx.signers!=null && tx.signers.length>0) for(let signer of tx.signers) tx.tx.sign(signer);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Sends out a signed transaction to the RPC
|
|
205
|
-
*
|
|
206
|
-
* @param solTx solana tx to send
|
|
207
|
-
* @param options send options to be passed to the RPC
|
|
208
|
-
* @param onBeforePublish a callback called before every transaction is published
|
|
209
|
-
* @private
|
|
210
|
-
*/
|
|
211
|
-
private async sendSignedTransaction(solTx: SolanaTx, options?: SendOptions, onBeforePublish?: (txId: string, rawTx: string) => Promise<void>): Promise<string> {
|
|
212
|
-
if(onBeforePublish!=null) await onBeforePublish(bs58.encode(solTx.tx.signature), await this.serializeTx(solTx));
|
|
213
|
-
const serializedTx = solTx.tx.serialize();
|
|
214
|
-
this.logger.debug("sendSignedTransaction(): sending transaction: "+serializedTx.toString("hex")+
|
|
215
|
-
" signature: "+bs58.encode(solTx.tx.signature));
|
|
216
|
-
const txResult = await tryWithRetries(() => this.sendRawTransaction(serializedTx, options), this.retryPolicy);
|
|
217
|
-
this.logger.info("sendSignedTransaction(): tx sent, signature: "+txResult);
|
|
218
|
-
return txResult;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Prepares (adds recent blockhash if required, applies Phantom hotfix),
|
|
223
|
-
* signs (all together using signAllTransactions() calls), sends (in parallel or sequentially) &
|
|
224
|
-
* optionally waits for confirmation of a batch of solana transactions
|
|
225
|
-
*
|
|
226
|
-
* @param signer
|
|
227
|
-
* @param _txs
|
|
228
|
-
* @param waitForConfirmation whether to wait for transaction confirmations (this also makes sure the transactions
|
|
229
|
-
* are re-sent at regular intervals)
|
|
230
|
-
* @param abortSignal abort signal to abort waiting for transaction confirmations
|
|
231
|
-
* @param parallel whether the send all the transaction at once in parallel or sequentially (such that transactions
|
|
232
|
-
* are executed in order)
|
|
233
|
-
* @param onBeforePublish a callback called before every transaction is published
|
|
234
|
-
*/
|
|
235
|
-
public async sendAndConfirm(signer: SolanaSigner, _txs: SolanaTx[], waitForConfirmation?: boolean, abortSignal?: AbortSignal, parallel?: boolean, onBeforePublish?: (txId: string, rawTx: string) => Promise<void>): Promise<string[]> {
|
|
236
|
-
const options = {
|
|
237
|
-
skipPreflight: true
|
|
238
|
-
};
|
|
239
|
-
|
|
240
|
-
this.logger.debug("sendAndConfirm(): sending transactions, count: "+_txs.length+
|
|
241
|
-
" waitForConfirmation: "+waitForConfirmation+" parallel: "+parallel);
|
|
242
|
-
|
|
243
|
-
const BATCH_SIZE = 50;
|
|
244
|
-
|
|
245
|
-
const signatures: string[] = [];
|
|
246
|
-
for(let e=0;e<_txs.length;e+=BATCH_SIZE) {
|
|
247
|
-
const txs = _txs.slice(e, e+BATCH_SIZE);
|
|
248
|
-
|
|
249
|
-
await this.prepareTransactions(signer, txs)
|
|
250
|
-
const signedTxs = await signer.wallet.signAllTransactions(txs.map(e => e.tx));
|
|
251
|
-
signedTxs.forEach((tx, index) => {
|
|
252
|
-
const solTx = txs[index];
|
|
253
|
-
tx.lastValidBlockHeight = solTx.tx.lastValidBlockHeight;
|
|
254
|
-
solTx.tx = tx
|
|
255
|
-
});
|
|
256
|
-
this.logger.debug("sendAndConfirm(): sending transaction batch ("+e+".."+(e+50)+"), count: "+txs.length);
|
|
257
|
-
|
|
258
|
-
//For solana we are forced to send txs one-by-one even with parallel, as we cannot determine their order upfront,
|
|
259
|
-
// however e.g. Jito could possibly handle sending a single package of up to 5 txns in order.
|
|
260
|
-
for(let i=0;i<txs.length;i++) {
|
|
261
|
-
const solTx = txs[i];
|
|
262
|
-
const signature = await this.sendSignedTransaction(solTx, options, onBeforePublish);
|
|
263
|
-
const confirmPromise = this.confirmTransaction(solTx, abortSignal, "confirmed");
|
|
264
|
-
//Don't await the last promise when !waitForConfirmation
|
|
265
|
-
if(i<txs.length-1 || e+50<_txs.length || waitForConfirmation) await confirmPromise;
|
|
266
|
-
signatures.push(signature);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
this.logger.info("sendAndConfirm(): sent transactions, count: "+_txs.length+
|
|
271
|
-
" waitForConfirmation: "+waitForConfirmation+" parallel: "+parallel);
|
|
272
|
-
|
|
273
|
-
return signatures;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
/**
|
|
277
|
-
* Serializes the solana transaction, saves the transaction, signers & last valid blockheight
|
|
278
|
-
*
|
|
279
|
-
* @param tx
|
|
280
|
-
*/
|
|
281
|
-
public serializeTx(tx: SolanaTx): Promise<string> {
|
|
282
|
-
return Promise.resolve(JSON.stringify({
|
|
283
|
-
tx: tx.tx.serialize().toString("hex"),
|
|
284
|
-
signers: tx.signers.map(e => Buffer.from(e.secretKey).toString("hex")),
|
|
285
|
-
lastValidBlockheight: tx.tx.lastValidBlockHeight
|
|
286
|
-
}));
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
/**
|
|
290
|
-
* Deserializes saved solana transaction, extracting the transaction, signers & last valid blockheight
|
|
291
|
-
*
|
|
292
|
-
* @param txData
|
|
293
|
-
*/
|
|
294
|
-
public deserializeTx(txData: string): Promise<SolanaTx> {
|
|
295
|
-
const jsonParsed: {
|
|
296
|
-
tx: string,
|
|
297
|
-
signers: string[],
|
|
298
|
-
lastValidBlockheight: number
|
|
299
|
-
} = JSON.parse(txData);
|
|
300
|
-
|
|
301
|
-
const transaction = Transaction.from(Buffer.from(jsonParsed.tx, "hex"));
|
|
302
|
-
transaction.lastValidBlockHeight = jsonParsed.lastValidBlockheight;
|
|
303
|
-
|
|
304
|
-
return Promise.resolve({
|
|
305
|
-
tx: transaction,
|
|
306
|
-
signers: jsonParsed.signers.map(e => Keypair.fromSecretKey(Buffer.from(e, "hex"))),
|
|
307
|
-
});
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
/**
|
|
311
|
-
* Gets the status of the raw solana transaction, this also checks transaction expiry & can therefore report tx
|
|
312
|
-
* in "pending" status, however pending status doesn't necessarily mean that the transaction was sent (again,
|
|
313
|
-
* no mempool on Solana, cannot check that), this function is preferred against getTxIdStatus
|
|
314
|
-
*
|
|
315
|
-
* @param tx
|
|
316
|
-
*/
|
|
317
|
-
public async getTxStatus(tx: string): Promise<"pending" | "success" | "not_found" | "reverted"> {
|
|
318
|
-
const parsedTx: SolanaTx = await this.deserializeTx(tx);
|
|
319
|
-
const txReceipt = await this.connection.getTransaction(bs58.encode(parsedTx.tx.signature), {
|
|
320
|
-
commitment: "confirmed",
|
|
321
|
-
maxSupportedTransactionVersion: 0
|
|
322
|
-
});
|
|
323
|
-
if(txReceipt==null) {
|
|
324
|
-
const currentBlockheight = await this.connection.getBlockHeight("processed");
|
|
325
|
-
if(currentBlockheight>parsedTx.tx.lastValidBlockHeight) return "not_found";
|
|
326
|
-
return "pending";
|
|
327
|
-
}
|
|
328
|
-
if(txReceipt.meta.err) return "reverted";
|
|
329
|
-
return "success";
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
/**
|
|
333
|
-
* Gets the status of the solana transaction with a specific txId, this cannot report whether the transaction is
|
|
334
|
-
* "pending" because Solana has no concept of mempool & only confirmed transactions are accessible
|
|
335
|
-
*
|
|
336
|
-
* @param txId
|
|
337
|
-
* @param finality
|
|
338
|
-
*/
|
|
339
|
-
public async getTxIdStatus(txId: string, finality?: Finality): Promise<"success" | "not_found" | "reverted"> {
|
|
340
|
-
const txReceipt = await this.connection.getTransaction(txId, {
|
|
341
|
-
commitment: finality || "confirmed",
|
|
342
|
-
maxSupportedTransactionVersion: 0
|
|
343
|
-
});
|
|
344
|
-
if(txReceipt==null) return "not_found";
|
|
345
|
-
if(txReceipt.meta.err) return "reverted";
|
|
346
|
-
return "success";
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
public onBeforeTxSigned(callback: (tx: SolanaTx) => Promise<void>): void {
|
|
350
|
-
this.cbkBeforeTxSigned = callback;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
public offBeforeTxSigned(callback: (tx: SolanaTx) => Promise<void>): boolean {
|
|
354
|
-
this.cbkBeforeTxSigned = null;
|
|
355
|
-
return true;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
public onSendTransaction(callback: (tx: Buffer, options?: SendOptions) => Promise<string>): void {
|
|
359
|
-
this.cbkSendTransaction = callback;
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
public offSendTransaction(callback: (tx: Buffer, options?: SendOptions) => Promise<string>): boolean {
|
|
363
|
-
this.cbkSendTransaction = null;
|
|
364
|
-
return true;
|
|
365
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
ComputeBudgetInstruction,
|
|
3
|
+
ComputeBudgetProgram, Finality, Keypair, RpcResponseAndContext,
|
|
4
|
+
SendOptions, SignatureResult, Signer, Transaction,
|
|
5
|
+
TransactionExpiredBlockheightExceededError
|
|
6
|
+
} from "@solana/web3.js";
|
|
7
|
+
import {SolanaModule} from "../SolanaModule";
|
|
8
|
+
import * as bs58 from "bs58";
|
|
9
|
+
import {tryWithRetries} from "../../../utils/Utils";
|
|
10
|
+
import {Buffer} from "buffer";
|
|
11
|
+
import {SolanaSigner} from "../../wallet/SolanaSigner";
|
|
12
|
+
import {TransactionRevertedError} from "@atomiqlabs/base";
|
|
13
|
+
|
|
14
|
+
export type SolanaTx = {tx: Transaction, signers: Signer[]};
|
|
15
|
+
|
|
16
|
+
export class SolanaTransactions extends SolanaModule {
|
|
17
|
+
|
|
18
|
+
private cbkBeforeTxSigned: (tx: SolanaTx) => Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* Callback for sending transaction, returns not null if it was successfully able to send the transaction, and null
|
|
21
|
+
* if the transaction should be sent through other means)
|
|
22
|
+
* @private
|
|
23
|
+
*/
|
|
24
|
+
private cbkSendTransaction: (tx: Buffer, options?: SendOptions) => Promise<string>;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Sends raw solana transaction, first through the cbkSendTransaction callback (for e.g. sending the transaction
|
|
28
|
+
* to a different specific RPC), the through the Fees handler (for e.g. Jito transaction) and last through the
|
|
29
|
+
* underlying provider's Connection instance (the usual way). Only sends the transaction through one channel.
|
|
30
|
+
*
|
|
31
|
+
* @param data
|
|
32
|
+
* @param options
|
|
33
|
+
* @private
|
|
34
|
+
*/
|
|
35
|
+
private async sendRawTransaction(data: Buffer, options?: SendOptions): Promise<string> {
|
|
36
|
+
let result: string = null;
|
|
37
|
+
options ??= {};
|
|
38
|
+
options.maxRetries = 0;
|
|
39
|
+
if(this.cbkSendTransaction!=null) result = await this.cbkSendTransaction(data, options);
|
|
40
|
+
if(result==null) result = await this.root.Fees.submitTx(data, options);
|
|
41
|
+
if(result==null) result = await this.connection.sendRawTransaction(data, options);
|
|
42
|
+
// this.logger.debug("sendRawTransaction(): tx sent, signature: "+result);
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Waits for the transaction to confirm by periodically checking the transaction status over HTTP, also
|
|
48
|
+
* re-sends the transaction at regular intervals
|
|
49
|
+
*
|
|
50
|
+
* @param solanaTx solana tx to wait for confirmation for
|
|
51
|
+
* @param finality wait for this finality
|
|
52
|
+
* @param abortSignal signal to abort waiting for tx confirmation
|
|
53
|
+
* @private
|
|
54
|
+
*/
|
|
55
|
+
private txConfirmationAndResendWatchdog(
|
|
56
|
+
solanaTx: SolanaTx,
|
|
57
|
+
finality?: Finality,
|
|
58
|
+
abortSignal?: AbortSignal
|
|
59
|
+
): Promise<string> {
|
|
60
|
+
const rawTx = solanaTx.tx.serialize();
|
|
61
|
+
const signature = bs58.encode(solanaTx.tx.signature);
|
|
62
|
+
return new Promise((resolve, reject) => {
|
|
63
|
+
let watchdogInterval: NodeJS.Timer;
|
|
64
|
+
watchdogInterval = setInterval(async () => {
|
|
65
|
+
const result = await this.sendRawTransaction(rawTx, {skipPreflight: true}).catch(
|
|
66
|
+
e => this.logger.error("txConfirmationAndResendWatchdog(): transaction re-sent error: ", e)
|
|
67
|
+
);
|
|
68
|
+
this.logger.debug("txConfirmationAndResendWatchdog(): transaction re-sent: "+result);
|
|
69
|
+
|
|
70
|
+
const status = await this.getTxIdStatus(signature, finality).catch(
|
|
71
|
+
e => this.logger.error("txConfirmationAndResendWatchdog(): get tx id status error: ", e)
|
|
72
|
+
);
|
|
73
|
+
if(status==null || status==="not_found") return;
|
|
74
|
+
if(status==="success") {
|
|
75
|
+
this.logger.info("txConfirmationAndResendWatchdog(): transaction confirmed from HTTP polling, signature: "+signature);
|
|
76
|
+
resolve(signature);
|
|
77
|
+
}
|
|
78
|
+
if(status==="reverted") reject(new TransactionRevertedError("Transaction reverted!"));
|
|
79
|
+
clearInterval(watchdogInterval);
|
|
80
|
+
}, this.retryPolicy?.transactionResendInterval || 3000);
|
|
81
|
+
|
|
82
|
+
if(abortSignal!=null) abortSignal.addEventListener("abort", () => {
|
|
83
|
+
clearInterval(watchdogInterval);
|
|
84
|
+
reject(abortSignal.reason);
|
|
85
|
+
});
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Waits for the transaction to confirm from WS, sometimes the WS rejects even though the transaction was confirmed
|
|
91
|
+
* this therefore also runs an ultimate check on the transaction in case the WS handler rejects, checking if it
|
|
92
|
+
* really was expired
|
|
93
|
+
*
|
|
94
|
+
* @param solanaTx solana tx to wait for confirmation for
|
|
95
|
+
* @param finality wait for this finality
|
|
96
|
+
* @param abortSignal signal to abort waiting for tx confirmation
|
|
97
|
+
* @private
|
|
98
|
+
*/
|
|
99
|
+
private async txConfirmFromWebsocket(
|
|
100
|
+
solanaTx: SolanaTx,
|
|
101
|
+
finality?: Finality,
|
|
102
|
+
abortSignal?: AbortSignal
|
|
103
|
+
): Promise<string> {
|
|
104
|
+
const signature = bs58.encode(solanaTx.tx.signature);
|
|
105
|
+
|
|
106
|
+
let result: RpcResponseAndContext<SignatureResult>;
|
|
107
|
+
try {
|
|
108
|
+
result = await this.connection.confirmTransaction({
|
|
109
|
+
signature: signature,
|
|
110
|
+
blockhash: solanaTx.tx.recentBlockhash,
|
|
111
|
+
lastValidBlockHeight: solanaTx.tx.lastValidBlockHeight,
|
|
112
|
+
abortSignal
|
|
113
|
+
}, finality);
|
|
114
|
+
this.logger.info("txConfirmFromWebsocket(): transaction confirmed from WS, signature: "+signature);
|
|
115
|
+
} catch (err) {
|
|
116
|
+
if(abortSignal!=null && abortSignal.aborted) throw err;
|
|
117
|
+
this.logger.debug("txConfirmFromWebsocket(): transaction rejected from WS, running ultimate check, expiry blockheight: "+solanaTx.tx.lastValidBlockHeight+" signature: "+signature+" error: "+err);
|
|
118
|
+
const status = await tryWithRetries(
|
|
119
|
+
() => this.getTxIdStatus(signature, finality)
|
|
120
|
+
);
|
|
121
|
+
this.logger.info("txConfirmFromWebsocket(): transaction status: "+status+" signature: "+signature);
|
|
122
|
+
if(status==="success") return signature;
|
|
123
|
+
if(status==="reverted") throw new TransactionRevertedError("Transaction reverted!");
|
|
124
|
+
if(err instanceof TransactionExpiredBlockheightExceededError || err.toString().startsWith("TransactionExpiredBlockheightExceededError")) {
|
|
125
|
+
throw new Error("Transaction expired before confirmation, please try again!");
|
|
126
|
+
} else {
|
|
127
|
+
throw err;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if(result.value.err!=null) throw new TransactionRevertedError("Transaction reverted!");
|
|
131
|
+
return signature;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Waits for transaction confirmation using WS subscription and occasional HTTP polling, also re-sends
|
|
136
|
+
* the transaction at regular interval
|
|
137
|
+
*
|
|
138
|
+
* @param solanaTx solana transaction to wait for confirmation for & keep re-sending until it confirms
|
|
139
|
+
* @param abortSignal signal to abort waiting for tx confirmation
|
|
140
|
+
* @param finality wait for specific finality
|
|
141
|
+
* @private
|
|
142
|
+
*/
|
|
143
|
+
private async confirmTransaction(solanaTx: SolanaTx, abortSignal?: AbortSignal, finality?: Finality) {
|
|
144
|
+
const abortController = new AbortController();
|
|
145
|
+
if(abortSignal!=null) abortSignal.addEventListener("abort", () => {
|
|
146
|
+
abortController.abort();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
let txSignature: string;
|
|
150
|
+
try {
|
|
151
|
+
txSignature = await Promise.race([
|
|
152
|
+
this.txConfirmationAndResendWatchdog(solanaTx, finality, abortController.signal),
|
|
153
|
+
this.txConfirmFromWebsocket(solanaTx, finality, abortController.signal)
|
|
154
|
+
]);
|
|
155
|
+
} catch (e) {
|
|
156
|
+
abortController.abort(e);
|
|
157
|
+
throw e;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// this.logger.info("confirmTransaction(): transaction confirmed, signature: "+txSignature);
|
|
161
|
+
|
|
162
|
+
abortController.abort();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Prepares solana transactions, assigns recentBlockhash if needed, applies Phantom hotfix,
|
|
167
|
+
* sets feePayer to ourselves, calls beforeTxSigned callback & signs transaction with provided signers array
|
|
168
|
+
*
|
|
169
|
+
* @param signer
|
|
170
|
+
* @param txs
|
|
171
|
+
* @private
|
|
172
|
+
*/
|
|
173
|
+
private async prepareTransactions(signer: SolanaSigner, txs: SolanaTx[]): Promise<void> {
|
|
174
|
+
let latestBlockData: {blockhash: string, lastValidBlockHeight: number} = null;
|
|
175
|
+
|
|
176
|
+
for(let tx of txs) {
|
|
177
|
+
if(tx.tx.recentBlockhash==null) {
|
|
178
|
+
if(latestBlockData==null) {
|
|
179
|
+
latestBlockData = await tryWithRetries(
|
|
180
|
+
() => this.connection.getLatestBlockhash("confirmed"),
|
|
181
|
+
this.retryPolicy
|
|
182
|
+
);
|
|
183
|
+
this.logger.debug("prepareTransactions(): fetched latest block data for transactions," +
|
|
184
|
+
" blockhash: "+latestBlockData.blockhash+" expiry blockheight: "+latestBlockData.lastValidBlockHeight);
|
|
185
|
+
}
|
|
186
|
+
tx.tx.recentBlockhash = latestBlockData.blockhash;
|
|
187
|
+
tx.tx.lastValidBlockHeight = latestBlockData.lastValidBlockHeight;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
//This is a hotfix for Phantom adding compute unit price instruction on the first position & breaking
|
|
191
|
+
// required instructions order (e.g. btc relay verify needs to be 0th instruction in a tx)
|
|
192
|
+
if(signer.keypair==null && tx.tx.signatures.length===0) {
|
|
193
|
+
const foundIx = tx.tx.instructions.find(ix => ix.programId.equals(ComputeBudgetProgram.programId) && ComputeBudgetInstruction.decodeInstructionType(ix)==="SetComputeUnitPrice")
|
|
194
|
+
if(foundIx==null) tx.tx.instructions.splice(tx.tx.instructions.length-1, 0, ComputeBudgetProgram.setComputeUnitPrice({microLamports: 1}));
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
tx.tx.feePayer = signer.getPublicKey();
|
|
198
|
+
if(this.cbkBeforeTxSigned!=null) await this.cbkBeforeTxSigned(tx);
|
|
199
|
+
if(tx.signers!=null && tx.signers.length>0) for(let signer of tx.signers) tx.tx.sign(signer);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Sends out a signed transaction to the RPC
|
|
205
|
+
*
|
|
206
|
+
* @param solTx solana tx to send
|
|
207
|
+
* @param options send options to be passed to the RPC
|
|
208
|
+
* @param onBeforePublish a callback called before every transaction is published
|
|
209
|
+
* @private
|
|
210
|
+
*/
|
|
211
|
+
private async sendSignedTransaction(solTx: SolanaTx, options?: SendOptions, onBeforePublish?: (txId: string, rawTx: string) => Promise<void>): Promise<string> {
|
|
212
|
+
if(onBeforePublish!=null) await onBeforePublish(bs58.encode(solTx.tx.signature), await this.serializeTx(solTx));
|
|
213
|
+
const serializedTx = solTx.tx.serialize();
|
|
214
|
+
this.logger.debug("sendSignedTransaction(): sending transaction: "+serializedTx.toString("hex")+
|
|
215
|
+
" signature: "+bs58.encode(solTx.tx.signature));
|
|
216
|
+
const txResult = await tryWithRetries(() => this.sendRawTransaction(serializedTx, options), this.retryPolicy);
|
|
217
|
+
this.logger.info("sendSignedTransaction(): tx sent, signature: "+txResult);
|
|
218
|
+
return txResult;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Prepares (adds recent blockhash if required, applies Phantom hotfix),
|
|
223
|
+
* signs (all together using signAllTransactions() calls), sends (in parallel or sequentially) &
|
|
224
|
+
* optionally waits for confirmation of a batch of solana transactions
|
|
225
|
+
*
|
|
226
|
+
* @param signer
|
|
227
|
+
* @param _txs
|
|
228
|
+
* @param waitForConfirmation whether to wait for transaction confirmations (this also makes sure the transactions
|
|
229
|
+
* are re-sent at regular intervals)
|
|
230
|
+
* @param abortSignal abort signal to abort waiting for transaction confirmations
|
|
231
|
+
* @param parallel whether the send all the transaction at once in parallel or sequentially (such that transactions
|
|
232
|
+
* are executed in order)
|
|
233
|
+
* @param onBeforePublish a callback called before every transaction is published
|
|
234
|
+
*/
|
|
235
|
+
public async sendAndConfirm(signer: SolanaSigner, _txs: SolanaTx[], waitForConfirmation?: boolean, abortSignal?: AbortSignal, parallel?: boolean, onBeforePublish?: (txId: string, rawTx: string) => Promise<void>): Promise<string[]> {
|
|
236
|
+
const options = {
|
|
237
|
+
skipPreflight: true
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
this.logger.debug("sendAndConfirm(): sending transactions, count: "+_txs.length+
|
|
241
|
+
" waitForConfirmation: "+waitForConfirmation+" parallel: "+parallel);
|
|
242
|
+
|
|
243
|
+
const BATCH_SIZE = 50;
|
|
244
|
+
|
|
245
|
+
const signatures: string[] = [];
|
|
246
|
+
for(let e=0;e<_txs.length;e+=BATCH_SIZE) {
|
|
247
|
+
const txs = _txs.slice(e, e+BATCH_SIZE);
|
|
248
|
+
|
|
249
|
+
await this.prepareTransactions(signer, txs)
|
|
250
|
+
const signedTxs = await signer.wallet.signAllTransactions(txs.map(e => e.tx));
|
|
251
|
+
signedTxs.forEach((tx, index) => {
|
|
252
|
+
const solTx = txs[index];
|
|
253
|
+
tx.lastValidBlockHeight = solTx.tx.lastValidBlockHeight;
|
|
254
|
+
solTx.tx = tx
|
|
255
|
+
});
|
|
256
|
+
this.logger.debug("sendAndConfirm(): sending transaction batch ("+e+".."+(e+50)+"), count: "+txs.length);
|
|
257
|
+
|
|
258
|
+
//For solana we are forced to send txs one-by-one even with parallel, as we cannot determine their order upfront,
|
|
259
|
+
// however e.g. Jito could possibly handle sending a single package of up to 5 txns in order.
|
|
260
|
+
for(let i=0;i<txs.length;i++) {
|
|
261
|
+
const solTx = txs[i];
|
|
262
|
+
const signature = await this.sendSignedTransaction(solTx, options, onBeforePublish);
|
|
263
|
+
const confirmPromise = this.confirmTransaction(solTx, abortSignal, "confirmed");
|
|
264
|
+
//Don't await the last promise when !waitForConfirmation
|
|
265
|
+
if(i<txs.length-1 || e+50<_txs.length || waitForConfirmation) await confirmPromise;
|
|
266
|
+
signatures.push(signature);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
this.logger.info("sendAndConfirm(): sent transactions, count: "+_txs.length+
|
|
271
|
+
" waitForConfirmation: "+waitForConfirmation+" parallel: "+parallel);
|
|
272
|
+
|
|
273
|
+
return signatures;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Serializes the solana transaction, saves the transaction, signers & last valid blockheight
|
|
278
|
+
*
|
|
279
|
+
* @param tx
|
|
280
|
+
*/
|
|
281
|
+
public serializeTx(tx: SolanaTx): Promise<string> {
|
|
282
|
+
return Promise.resolve(JSON.stringify({
|
|
283
|
+
tx: tx.tx.serialize().toString("hex"),
|
|
284
|
+
signers: tx.signers.map(e => Buffer.from(e.secretKey).toString("hex")),
|
|
285
|
+
lastValidBlockheight: tx.tx.lastValidBlockHeight
|
|
286
|
+
}));
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Deserializes saved solana transaction, extracting the transaction, signers & last valid blockheight
|
|
291
|
+
*
|
|
292
|
+
* @param txData
|
|
293
|
+
*/
|
|
294
|
+
public deserializeTx(txData: string): Promise<SolanaTx> {
|
|
295
|
+
const jsonParsed: {
|
|
296
|
+
tx: string,
|
|
297
|
+
signers: string[],
|
|
298
|
+
lastValidBlockheight: number
|
|
299
|
+
} = JSON.parse(txData);
|
|
300
|
+
|
|
301
|
+
const transaction = Transaction.from(Buffer.from(jsonParsed.tx, "hex"));
|
|
302
|
+
transaction.lastValidBlockHeight = jsonParsed.lastValidBlockheight;
|
|
303
|
+
|
|
304
|
+
return Promise.resolve({
|
|
305
|
+
tx: transaction,
|
|
306
|
+
signers: jsonParsed.signers.map(e => Keypair.fromSecretKey(Buffer.from(e, "hex"))),
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Gets the status of the raw solana transaction, this also checks transaction expiry & can therefore report tx
|
|
312
|
+
* in "pending" status, however pending status doesn't necessarily mean that the transaction was sent (again,
|
|
313
|
+
* no mempool on Solana, cannot check that), this function is preferred against getTxIdStatus
|
|
314
|
+
*
|
|
315
|
+
* @param tx
|
|
316
|
+
*/
|
|
317
|
+
public async getTxStatus(tx: string): Promise<"pending" | "success" | "not_found" | "reverted"> {
|
|
318
|
+
const parsedTx: SolanaTx = await this.deserializeTx(tx);
|
|
319
|
+
const txReceipt = await this.connection.getTransaction(bs58.encode(parsedTx.tx.signature), {
|
|
320
|
+
commitment: "confirmed",
|
|
321
|
+
maxSupportedTransactionVersion: 0
|
|
322
|
+
});
|
|
323
|
+
if(txReceipt==null) {
|
|
324
|
+
const currentBlockheight = await this.connection.getBlockHeight("processed");
|
|
325
|
+
if(currentBlockheight>parsedTx.tx.lastValidBlockHeight) return "not_found";
|
|
326
|
+
return "pending";
|
|
327
|
+
}
|
|
328
|
+
if(txReceipt.meta.err) return "reverted";
|
|
329
|
+
return "success";
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Gets the status of the solana transaction with a specific txId, this cannot report whether the transaction is
|
|
334
|
+
* "pending" because Solana has no concept of mempool & only confirmed transactions are accessible
|
|
335
|
+
*
|
|
336
|
+
* @param txId
|
|
337
|
+
* @param finality
|
|
338
|
+
*/
|
|
339
|
+
public async getTxIdStatus(txId: string, finality?: Finality): Promise<"success" | "not_found" | "reverted"> {
|
|
340
|
+
const txReceipt = await this.connection.getTransaction(txId, {
|
|
341
|
+
commitment: finality || "confirmed",
|
|
342
|
+
maxSupportedTransactionVersion: 0
|
|
343
|
+
});
|
|
344
|
+
if(txReceipt==null) return "not_found";
|
|
345
|
+
if(txReceipt.meta.err) return "reverted";
|
|
346
|
+
return "success";
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
public onBeforeTxSigned(callback: (tx: SolanaTx) => Promise<void>): void {
|
|
350
|
+
this.cbkBeforeTxSigned = callback;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
public offBeforeTxSigned(callback: (tx: SolanaTx) => Promise<void>): boolean {
|
|
354
|
+
this.cbkBeforeTxSigned = null;
|
|
355
|
+
return true;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
public onSendTransaction(callback: (tx: Buffer, options?: SendOptions) => Promise<string>): void {
|
|
359
|
+
this.cbkSendTransaction = callback;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
public offSendTransaction(callback: (tx: Buffer, options?: SendOptions) => Promise<string>): boolean {
|
|
363
|
+
this.cbkSendTransaction = null;
|
|
364
|
+
return true;
|
|
365
|
+
}
|
|
366
366
|
}
|