@atomiqlabs/chain-solana 7.2.0

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.
Files changed (105) hide show
  1. package/LICENSE +201 -0
  2. package/dist/index.d.ts +28 -0
  3. package/dist/index.js +44 -0
  4. package/dist/solana/SolanaChainType.d.ts +9 -0
  5. package/dist/solana/SolanaChainType.js +2 -0
  6. package/dist/solana/base/SolanaAction.d.ts +26 -0
  7. package/dist/solana/base/SolanaAction.js +99 -0
  8. package/dist/solana/base/SolanaBase.d.ts +36 -0
  9. package/dist/solana/base/SolanaBase.js +30 -0
  10. package/dist/solana/base/SolanaModule.d.ts +14 -0
  11. package/dist/solana/base/SolanaModule.js +13 -0
  12. package/dist/solana/base/modules/SolanaAddresses.d.ts +9 -0
  13. package/dist/solana/base/modules/SolanaAddresses.js +23 -0
  14. package/dist/solana/base/modules/SolanaBlocks.d.ts +28 -0
  15. package/dist/solana/base/modules/SolanaBlocks.js +83 -0
  16. package/dist/solana/base/modules/SolanaEvents.d.ts +25 -0
  17. package/dist/solana/base/modules/SolanaEvents.js +69 -0
  18. package/dist/solana/base/modules/SolanaFees.d.ts +121 -0
  19. package/dist/solana/base/modules/SolanaFees.js +393 -0
  20. package/dist/solana/base/modules/SolanaSignatures.d.ts +23 -0
  21. package/dist/solana/base/modules/SolanaSignatures.js +39 -0
  22. package/dist/solana/base/modules/SolanaSlots.d.ts +31 -0
  23. package/dist/solana/base/modules/SolanaSlots.js +81 -0
  24. package/dist/solana/base/modules/SolanaTokens.d.ts +134 -0
  25. package/dist/solana/base/modules/SolanaTokens.js +269 -0
  26. package/dist/solana/base/modules/SolanaTransactions.d.ts +124 -0
  27. package/dist/solana/base/modules/SolanaTransactions.js +354 -0
  28. package/dist/solana/btcrelay/SolanaBtcRelay.d.ts +229 -0
  29. package/dist/solana/btcrelay/SolanaBtcRelay.js +477 -0
  30. package/dist/solana/btcrelay/headers/SolanaBtcHeader.d.ts +29 -0
  31. package/dist/solana/btcrelay/headers/SolanaBtcHeader.js +34 -0
  32. package/dist/solana/btcrelay/headers/SolanaBtcStoredHeader.d.ts +46 -0
  33. package/dist/solana/btcrelay/headers/SolanaBtcStoredHeader.js +78 -0
  34. package/dist/solana/btcrelay/program/programIdl.json +671 -0
  35. package/dist/solana/events/SolanaChainEvents.d.ts +84 -0
  36. package/dist/solana/events/SolanaChainEvents.js +268 -0
  37. package/dist/solana/events/SolanaChainEventsBrowser.d.ts +85 -0
  38. package/dist/solana/events/SolanaChainEventsBrowser.js +202 -0
  39. package/dist/solana/program/SolanaProgramBase.d.ts +34 -0
  40. package/dist/solana/program/SolanaProgramBase.js +43 -0
  41. package/dist/solana/program/modules/SolanaProgramEvents.d.ts +58 -0
  42. package/dist/solana/program/modules/SolanaProgramEvents.js +114 -0
  43. package/dist/solana/swaps/SolanaSwapData.d.ts +55 -0
  44. package/dist/solana/swaps/SolanaSwapData.js +251 -0
  45. package/dist/solana/swaps/SolanaSwapModule.d.ts +9 -0
  46. package/dist/solana/swaps/SolanaSwapModule.js +12 -0
  47. package/dist/solana/swaps/SolanaSwapProgram.d.ts +218 -0
  48. package/dist/solana/swaps/SolanaSwapProgram.js +523 -0
  49. package/dist/solana/swaps/SwapTypeEnum.d.ts +11 -0
  50. package/dist/solana/swaps/SwapTypeEnum.js +42 -0
  51. package/dist/solana/swaps/modules/SolanaDataAccount.d.ts +94 -0
  52. package/dist/solana/swaps/modules/SolanaDataAccount.js +255 -0
  53. package/dist/solana/swaps/modules/SolanaLpVault.d.ts +72 -0
  54. package/dist/solana/swaps/modules/SolanaLpVault.js +196 -0
  55. package/dist/solana/swaps/modules/SwapClaim.d.ts +129 -0
  56. package/dist/solana/swaps/modules/SwapClaim.js +307 -0
  57. package/dist/solana/swaps/modules/SwapInit.d.ts +212 -0
  58. package/dist/solana/swaps/modules/SwapInit.js +508 -0
  59. package/dist/solana/swaps/modules/SwapRefund.d.ts +83 -0
  60. package/dist/solana/swaps/modules/SwapRefund.js +264 -0
  61. package/dist/solana/swaps/programIdl.json +945 -0
  62. package/dist/solana/swaps/programTypes.d.ts +943 -0
  63. package/dist/solana/swaps/programTypes.js +945 -0
  64. package/dist/solana/wallet/SolanaKeypairWallet.d.ts +9 -0
  65. package/dist/solana/wallet/SolanaKeypairWallet.js +33 -0
  66. package/dist/solana/wallet/SolanaSigner.d.ts +10 -0
  67. package/dist/solana/wallet/SolanaSigner.js +16 -0
  68. package/dist/utils/Utils.d.ts +43 -0
  69. package/dist/utils/Utils.js +143 -0
  70. package/package.json +40 -0
  71. package/src/index.ts +35 -0
  72. package/src/solana/SolanaChainType.ts +20 -0
  73. package/src/solana/base/SolanaAction.ts +109 -0
  74. package/src/solana/base/SolanaBase.ts +57 -0
  75. package/src/solana/base/SolanaModule.ts +21 -0
  76. package/src/solana/base/modules/SolanaAddresses.ts +22 -0
  77. package/src/solana/base/modules/SolanaBlocks.ts +79 -0
  78. package/src/solana/base/modules/SolanaEvents.ts +58 -0
  79. package/src/solana/base/modules/SolanaFees.ts +445 -0
  80. package/src/solana/base/modules/SolanaSignatures.ts +40 -0
  81. package/src/solana/base/modules/SolanaSlots.ts +83 -0
  82. package/src/solana/base/modules/SolanaTokens.ts +310 -0
  83. package/src/solana/base/modules/SolanaTransactions.ts +366 -0
  84. package/src/solana/btcrelay/SolanaBtcRelay.ts +591 -0
  85. package/src/solana/btcrelay/headers/SolanaBtcHeader.ts +58 -0
  86. package/src/solana/btcrelay/headers/SolanaBtcStoredHeader.ts +103 -0
  87. package/src/solana/btcrelay/program/programIdl.json +671 -0
  88. package/src/solana/events/SolanaChainEvents.ts +286 -0
  89. package/src/solana/events/SolanaChainEventsBrowser.ts +251 -0
  90. package/src/solana/program/SolanaProgramBase.ts +77 -0
  91. package/src/solana/program/modules/SolanaProgramEvents.ts +140 -0
  92. package/src/solana/swaps/SolanaSwapData.ts +360 -0
  93. package/src/solana/swaps/SolanaSwapModule.ts +17 -0
  94. package/src/solana/swaps/SolanaSwapProgram.ts +739 -0
  95. package/src/solana/swaps/SwapTypeEnum.ts +30 -0
  96. package/src/solana/swaps/modules/SolanaDataAccount.ts +309 -0
  97. package/src/solana/swaps/modules/SolanaLpVault.ts +216 -0
  98. package/src/solana/swaps/modules/SwapClaim.ts +397 -0
  99. package/src/solana/swaps/modules/SwapInit.ts +621 -0
  100. package/src/solana/swaps/modules/SwapRefund.ts +316 -0
  101. package/src/solana/swaps/programIdl.json +945 -0
  102. package/src/solana/swaps/programTypes.ts +1885 -0
  103. package/src/solana/wallet/SolanaKeypairWallet.ts +36 -0
  104. package/src/solana/wallet/SolanaSigner.ts +23 -0
  105. package/src/utils/Utils.ts +145 -0
@@ -0,0 +1,397 @@
1
+ import {SolanaSwapModule} from "../SolanaSwapModule";
2
+ import {SolanaSwapData} from "../SolanaSwapData";
3
+ import {SolanaAction} from "../../base/SolanaAction";
4
+ import {TOKEN_PROGRAM_ID} from "@solana/spl-token";
5
+ import {ChainSwapType, RelaySynchronizer, SwapDataVerificationError} from "@atomiqlabs/base";
6
+ import {PublicKey, SYSVAR_INSTRUCTIONS_PUBKEY} from "@solana/web3.js";
7
+ import {SolanaTx} from "../../base/modules/SolanaTransactions";
8
+ import {SolanaBtcStoredHeader} from "../../btcrelay/headers/SolanaBtcStoredHeader";
9
+ import {tryWithRetries} from "../../../utils/Utils";
10
+ import {SolanaBtcRelay} from "../../btcrelay/SolanaBtcRelay";
11
+ import {SolanaSwapProgram} from "../SolanaSwapProgram";
12
+ import * as BN from "bn.js";
13
+ import {SolanaSigner} from "../../wallet/SolanaSigner";
14
+
15
+ export class SwapClaim extends SolanaSwapModule {
16
+
17
+ private static readonly CUCosts = {
18
+ CLAIM: 25000,
19
+ CLAIM_PAY_OUT: 50000,
20
+ CLAIM_ONCHAIN: 600000,
21
+ CLAIM_ONCHAIN_PAY_OUT: 600000
22
+ };
23
+
24
+ readonly btcRelay: SolanaBtcRelay<any>;
25
+
26
+ /**
27
+ * Claim action which uses the provided hex encoded secret for claiming the swap
28
+ *
29
+ * @param signer
30
+ * @param swapData
31
+ * @param secret
32
+ * @constructor
33
+ * @private
34
+ */
35
+ private Claim(signer: PublicKey, swapData: SolanaSwapData, secret: string): Promise<SolanaAction>;
36
+ /**
37
+ * Claim action which uses data in the provided data ccount with dataKey to claim the swap
38
+ *
39
+ * @param signer
40
+ * @param swapData
41
+ * @param dataKey
42
+ * @constructor
43
+ * @private
44
+ */
45
+ private Claim(signer: PublicKey, swapData: SolanaSwapData, dataKey: PublicKey): Promise<SolanaAction>;
46
+ private async Claim(
47
+ signer: PublicKey,
48
+ swapData: SolanaSwapData,
49
+ secretOrDataKey: string | PublicKey
50
+ ): Promise<SolanaAction> {
51
+ const isDataKey = typeof(secretOrDataKey)!=="string";
52
+
53
+ const accounts = {
54
+ signer,
55
+ initializer: swapData.isPayIn() ? swapData.offerer : swapData.claimer,
56
+ escrowState: this.root.SwapEscrowState(Buffer.from(swapData.paymentHash, "hex")),
57
+ ixSysvar: SYSVAR_INSTRUCTIONS_PUBKEY,
58
+ data: isDataKey ? secretOrDataKey : null,
59
+ };
60
+ let secretBuffer = isDataKey ?
61
+ Buffer.alloc(0) :
62
+ Buffer.from(secretOrDataKey, "hex");
63
+
64
+ if(swapData.isPayOut()) {
65
+ return new SolanaAction(signer, this.root,
66
+ await this.program.methods
67
+ .claimerClaimPayOut(secretBuffer)
68
+ .accounts({
69
+ ...accounts,
70
+ claimerAta: swapData.claimerAta,
71
+ vault: this.root.SwapVault(swapData.token),
72
+ vaultAuthority: this.root.SwapVaultAuthority,
73
+ tokenProgram: TOKEN_PROGRAM_ID
74
+ })
75
+ .instruction(),
76
+ this.getComputeBudget(swapData)
77
+ );
78
+ } else {
79
+ return new SolanaAction(signer, this.root,
80
+ await this.program.methods
81
+ .claimerClaim(secretBuffer)
82
+ .accounts({
83
+ ...accounts,
84
+ claimerUserData: this.root.SwapUserVault(swapData.claimer, swapData.token)
85
+ })
86
+ .instruction(),
87
+ this.getComputeBudget(swapData)
88
+ );
89
+ }
90
+ }
91
+
92
+ /**
93
+ * Verify and claim action required for BTC on-chain swaps verified through btc relay, adds the btc relay verify
94
+ * instruction to the 0th index in the transaction, also intentionally sets compute budget to null such that no
95
+ * compute budget instruction is added, since that takes up too much space and txs are limited to 1232 bytes
96
+ *
97
+ * @param signer
98
+ * @param swapData
99
+ * @param storeDataKey
100
+ * @param merkleProof
101
+ * @param commitedHeader
102
+ * @constructor
103
+ * @private
104
+ */
105
+ private async VerifyAndClaim(
106
+ signer: PublicKey,
107
+ swapData: SolanaSwapData,
108
+ storeDataKey: PublicKey,
109
+ merkleProof: {reversedTxId: Buffer, pos: number, merkle: Buffer[]},
110
+ commitedHeader: SolanaBtcStoredHeader
111
+ ): Promise<SolanaAction> {
112
+ const action = await this.btcRelay.Verify(
113
+ signer,
114
+ merkleProof.reversedTxId,
115
+ swapData.confirmations,
116
+ merkleProof.pos,
117
+ merkleProof.merkle,
118
+ commitedHeader
119
+ );
120
+ action.addAction(await this.Claim(signer, swapData, storeDataKey));
121
+ action.computeBudget = null;
122
+ return action;
123
+ }
124
+
125
+ constructor(root: SolanaSwapProgram, btcRelay: SolanaBtcRelay<any>) {
126
+ super(root);
127
+ this.btcRelay = btcRelay;
128
+ }
129
+
130
+ /**
131
+ * Gets the compute budget required for claiming the swap
132
+ *
133
+ * @param swapData
134
+ * @private
135
+ */
136
+ private getComputeBudget(swapData: SolanaSwapData) {
137
+ if(swapData.isPayOut()) {
138
+ return SwapClaim.CUCosts[swapData.getType()===ChainSwapType.HTLC ? "CLAIM_PAY_OUT" : "CLAIM_ONCHAIN_PAY_OUT"]
139
+ } else {
140
+ return SwapClaim.CUCosts[swapData.getType()===ChainSwapType.HTLC ? "CLAIM" : "CLAIM_ONCHAIN"];
141
+ }
142
+ }
143
+
144
+ /**
145
+ * Gets committed header, identified by blockhash & blockheight, determines required BTC relay blockheight based on
146
+ * requiredConfirmations
147
+ * If synchronizer is passed & blockhash is not found, it produces transactions to sync up the btc relay to the
148
+ * current chain tip & adds them to the txs array
149
+ *
150
+ * @param signer
151
+ * @param txBlockheight transaction blockheight
152
+ * @param requiredConfirmations required confirmation for the swap to be claimable with that TX
153
+ * @param blockhash blockhash of the block which includes the transaction
154
+ * @param txs solana transaction array, in case we need to synchronize the btc relay ourselves the synchronization
155
+ * txns are added here
156
+ * @param synchronizer optional synchronizer to use to synchronize the btc relay in case it is not yet synchronized
157
+ * to the required blockheight
158
+ * @private
159
+ */
160
+ private async getCommitedHeaderAndSynchronize(
161
+ signer: PublicKey,
162
+ txBlockheight: number,
163
+ requiredConfirmations: number,
164
+ blockhash: string,
165
+ txs: SolanaTx[],
166
+ synchronizer?: RelaySynchronizer<SolanaBtcStoredHeader, SolanaTx, any>,
167
+ ): Promise<SolanaBtcStoredHeader> {
168
+ const requiredBlockheight = txBlockheight+requiredConfirmations-1;
169
+
170
+ const result = await tryWithRetries(
171
+ () => this.btcRelay.retrieveLogAndBlockheight({
172
+ blockhash: blockhash
173
+ }, requiredBlockheight),
174
+ this.retryPolicy
175
+ );
176
+
177
+ if(result!=null) return result.header;
178
+
179
+ //Need to synchronize
180
+ if(synchronizer==null) return null;
181
+
182
+ //TODO: We don't have to synchronize to tip, only to our required blockheight
183
+ const resp = await synchronizer.syncToLatestTxs(signer.toString());
184
+ this.logger.debug("getCommitedHeaderAndSynchronize(): BTC Relay not synchronized to required blockheight, "+
185
+ "synchronizing ourselves in "+resp.txs.length+" txs");
186
+ this.logger.debug("getCommitedHeaderAndSynchronize(): BTC Relay computed header map: ",resp.computedHeaderMap);
187
+ resp.txs.forEach(tx => txs.push(tx));
188
+
189
+ //Retrieve computed header
190
+ return resp.computedHeaderMap[txBlockheight];
191
+ }
192
+
193
+ /**
194
+ * Adds the transactions required for initialization and writing of transaction data to the data account
195
+ *
196
+ * @param signer
197
+ * @param tx transaction to be written
198
+ * @param vout vout of the transaction to use to satisfy swap conditions
199
+ * @param feeRate fee rate for the transactions
200
+ * @param txs solana transaction array, init & write transactions are added here
201
+ * @private
202
+ * @returns {Promise<PublicKey>} publicKey/address of the data account
203
+ */
204
+ private addTxsWriteTransactionData(
205
+ signer: PublicKey | SolanaSigner,
206
+ tx: {hex: string, txid: string},
207
+ vout: number,
208
+ feeRate: string,
209
+ txs: SolanaTx[]
210
+ ): Promise<PublicKey> {
211
+ const reversedTxId = Buffer.from(tx.txid, "hex").reverse();
212
+ const writeData: Buffer = Buffer.concat([
213
+ Buffer.from(new BN(vout).toArray("le", 4)),
214
+ Buffer.from(tx.hex, "hex")
215
+ ]);
216
+ this.logger.debug("addTxsWriteTransactionData(): writing transaction data: ", writeData.toString("hex"));
217
+
218
+ return this.root.DataAccount.addTxsWriteData(signer, reversedTxId, writeData, txs, feeRate);
219
+ }
220
+
221
+ /**
222
+ * Checks whether we should unwrap the WSOL to SOL when claiming the swap
223
+ *
224
+ * @param signer
225
+ * @param swapData
226
+ * @private
227
+ */
228
+ private shouldUnwrap(signer: PublicKey, swapData: SolanaSwapData): boolean {
229
+ return swapData.isPayOut() &&
230
+ swapData.token.equals(this.root.Tokens.WSOL_ADDRESS) &&
231
+ swapData.claimer.equals(signer);
232
+ }
233
+
234
+ /**
235
+ * Creates transactions claiming the swap using a secret (for HTLC swaps)
236
+ *
237
+ * @param signer
238
+ * @param swapData swap to claim
239
+ * @param secret hex encoded secret pre-image to the HTLC hash
240
+ * @param checkExpiry whether to check if the swap is already expired (trying to claim an expired swap with a secret
241
+ * is dangerous because we might end up revealing the secret to the counterparty without being able to claim the swap)
242
+ * @param initAta whether to init the claimer's ATA if it doesn't exist
243
+ * @param feeRate fee rate to use for the transaction
244
+ * @param skipAtaCheck whether to check if ATA exists
245
+ */
246
+ async txsClaimWithSecret(
247
+ signer: PublicKey,
248
+ swapData: SolanaSwapData,
249
+ secret: string,
250
+ checkExpiry?: boolean,
251
+ initAta?: boolean,
252
+ feeRate?: string,
253
+ skipAtaCheck?: boolean
254
+ ): Promise<SolanaTx[]> {
255
+ //We need to be sure that this transaction confirms in time, otherwise we reveal the secret to the counterparty
256
+ // and won't claim the funds
257
+ if(checkExpiry && this.root.isExpired(swapData.claimer.toString(), swapData)) {
258
+ throw new SwapDataVerificationError("Not enough time to reliably pay the invoice");
259
+ }
260
+ const shouldInitAta = !skipAtaCheck && swapData.isPayOut() && !await this.root.Tokens.ataExists(swapData.claimerAta);
261
+ if(shouldInitAta && !initAta) throw new SwapDataVerificationError("ATA not initialized");
262
+
263
+ if(feeRate==null) feeRate = await this.getClaimFeeRate(signer, swapData);
264
+
265
+ const action = new SolanaAction(signer, this.root);
266
+
267
+ const shouldUnwrap = this.shouldUnwrap(signer, swapData);
268
+ if(shouldInitAta) {
269
+ const initAction = this.root.Tokens.InitAta(signer, swapData.claimer, swapData.token, swapData.claimerAta);
270
+ if(initAction==null) throw new SwapDataVerificationError("Invalid claimer token account address");
271
+ action.add(initAction);
272
+ }
273
+ action.add(await this.Claim(signer, swapData, secret));
274
+ if(shouldUnwrap) action.add(this.root.Tokens.Unwrap(signer));
275
+
276
+ this.logger.debug("txsClaimWithSecret(): creating claim transaction, swap: "+swapData.getHash()+
277
+ " initializingAta: "+shouldInitAta+" unwrapping: "+shouldUnwrap);
278
+
279
+ return [await action.tx(feeRate)];
280
+ }
281
+
282
+ /**
283
+ * Creates transaction claiming the swap using a confirmed transaction data (for BTC on-chain swaps)
284
+ *
285
+ * @param signer
286
+ * @param swapData swap to claim
287
+ * @param blockheight blockheight of the bitcoin transaction
288
+ * @param tx bitcoin transaction that satisfies the swap condition
289
+ * @param vout vout of the bitcoin transaction that satisfies the swap condition
290
+ * @param commitedHeader commited header data from btc relay (fetched internally if null)
291
+ * @param synchronizer optional synchronizer to use in case we need to sync up the btc relay ourselves
292
+ * @param initAta whether to initialize claimer's ATA
293
+ * @param storageAccHolder an object holder filled in with the created data account where tx data is written
294
+ * @param feeRate fee rate to be used for the transactions
295
+ */
296
+ async txsClaimWithTxData(
297
+ signer: PublicKey | SolanaSigner,
298
+ swapData: SolanaSwapData,
299
+ blockheight: number,
300
+ tx: { blockhash: string, confirmations: number, txid: string, hex: string },
301
+ vout: number,
302
+ commitedHeader?: SolanaBtcStoredHeader,
303
+ synchronizer?: RelaySynchronizer<any, SolanaTx, any>,
304
+ initAta?: boolean,
305
+ storageAccHolder?: {storageAcc: PublicKey},
306
+ feeRate?: string
307
+ ): Promise<SolanaTx[] | null> {
308
+ const shouldInitAta = swapData.isPayOut() && !await this.root.Tokens.ataExists(swapData.claimerAta);
309
+ if(shouldInitAta && !initAta) throw new SwapDataVerificationError("ATA not initialized");
310
+
311
+ const signerKey = signer instanceof SolanaSigner ? signer.getPublicKey() : signer;
312
+
313
+ if(feeRate==null) feeRate = await this.getClaimFeeRate(signerKey, swapData);
314
+
315
+ const merkleProof = await this.btcRelay.bitcoinRpc.getMerkleProof(tx.txid, tx.blockhash);
316
+ this.logger.debug("txsClaimWithTxData(): merkle proof computed: ", merkleProof);
317
+
318
+ const txs: SolanaTx[] = [];
319
+ if(commitedHeader==null) commitedHeader = await this.getCommitedHeaderAndSynchronize(
320
+ signerKey, blockheight, swapData.getConfirmations(),
321
+ tx.blockhash, txs, synchronizer
322
+ );
323
+
324
+ const storeDataKey = await this.addTxsWriteTransactionData(signer, tx, vout, feeRate, txs);
325
+ if(storageAccHolder!=null) storageAccHolder.storageAcc = storeDataKey;
326
+
327
+ const shouldUnwrap = this.shouldUnwrap(signerKey, swapData);
328
+ if(shouldInitAta) {
329
+ const initAction = this.root.Tokens.InitAta(signerKey, swapData.claimer, swapData.token, swapData.claimerAta);
330
+ if(initAction==null) throw new SwapDataVerificationError("Invalid claimer token account address");
331
+ await initAction.addToTxs(txs, feeRate);
332
+ }
333
+ const claimAction = await this.VerifyAndClaim(signerKey, swapData, storeDataKey, merkleProof, commitedHeader);
334
+ await claimAction.addToTxs(txs, feeRate);
335
+ if(shouldUnwrap) await this.root.Tokens.Unwrap(signerKey).addToTxs(txs, feeRate);
336
+
337
+ this.logger.debug("txsClaimWithTxData(): creating claim transaction, swap: "+swapData.getHash()+
338
+ " initializingAta: "+shouldInitAta+" unwrapping: "+shouldUnwrap+" num txns: "+txs.length);
339
+
340
+ return txs;
341
+ }
342
+
343
+ public getClaimFeeRate(signer: PublicKey, swapData: SolanaSwapData): Promise<string> {
344
+ const accounts: PublicKey[] = [signer];
345
+ if(swapData.payOut) {
346
+ if(swapData.token!=null) accounts.push(this.root.SwapVault(swapData.token));
347
+ if(swapData.payIn) {
348
+ if(swapData.offerer!=null) accounts.push(swapData.offerer);
349
+ } else {
350
+ if(swapData.claimer!=null) accounts.push(swapData.claimer);
351
+ }
352
+ if(swapData.claimerAta!=null && !swapData.claimerAta.equals(PublicKey.default)) accounts.push(swapData.claimerAta);
353
+ } else {
354
+ if(swapData.claimer!=null && swapData.token!=null) accounts.push(this.root.SwapUserVault(swapData.claimer, swapData.token));
355
+
356
+ if(swapData.payIn) {
357
+ if(swapData.offerer!=null) accounts.push(swapData.offerer);
358
+ } else {
359
+ if(swapData.claimer!=null) accounts.push(swapData.claimer);
360
+ }
361
+ }
362
+
363
+ if(swapData.paymentHash!=null) accounts.push(this.root.SwapEscrowState(Buffer.from(swapData.paymentHash, "hex")));
364
+
365
+ return this.root.Fees.getFeeRate(accounts);
366
+ }
367
+
368
+ /**
369
+ * Get the estimated solana transaction fee of the claim transaction, this fee might be negative since it
370
+ * includes the rebate for closing the swap PDA
371
+ */
372
+ public async getClaimFee(signer: PublicKey, swapData: SolanaSwapData, feeRate?: string): Promise<BN> {
373
+ if(swapData==null) return new BN(-this.root.ESCROW_STATE_RENT_EXEMPT+5000);
374
+
375
+ feeRate = feeRate || await this.getClaimFeeRate(signer, swapData);
376
+
377
+ return new BN(-this.root.ESCROW_STATE_RENT_EXEMPT+5000).add(
378
+ this.root.Fees.getPriorityFee(this.getComputeBudget(swapData), feeRate)
379
+ );
380
+ }
381
+
382
+ /**
383
+ * Get the estimated solana transaction fee of the claim transaction in the worst case scenario in case where the
384
+ * ATA needs to be initialized again (i.e. adding the ATA rent exempt lamports to the fee)
385
+ */
386
+ public async getRawClaimFee(signer: PublicKey, swapData: SolanaSwapData, feeRate?: string): Promise<BN> {
387
+ if(swapData==null) return new BN(5000);
388
+
389
+ feeRate = feeRate || await this.getClaimFeeRate(signer, swapData);
390
+
391
+ //Include rent exempt in claim fee, to take into consideration worst case cost when user destroys ATA
392
+ return new BN(this.root.Tokens.SPL_ATA_RENT_EXEMPT+5000).add(
393
+ this.root.Fees.getPriorityFee(this.getComputeBudget(swapData), feeRate)
394
+ );
395
+ }
396
+
397
+ }