@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,739 @@
1
+ import {SolanaSwapData} from "./SolanaSwapData";
2
+ import {IdlAccounts} from "@coral-xyz/anchor";
3
+ import * as BN from "bn.js";
4
+ import {
5
+ Connection, Keypair,
6
+ PublicKey,
7
+ SendOptions
8
+ } from "@solana/web3.js";
9
+ import * as createHash from "create-hash";
10
+ import {SolanaBtcRelay} from "../btcrelay/SolanaBtcRelay";
11
+ import * as programIdl from "./programIdl.json";
12
+ import {
13
+ IStorageManager, SwapContract, ChainSwapType, IntermediaryReputationType,
14
+ SwapCommitStatus, TransactionConfirmationOptions, SignatureData, RelaySynchronizer
15
+ } from "@atomiqlabs/base";
16
+ import {SolanaBtcStoredHeader} from "../btcrelay/headers/SolanaBtcStoredHeader";
17
+ import {
18
+ getAssociatedTokenAddressSync,
19
+ } from "@solana/spl-token";
20
+ import {SolanaFees} from "../base/modules/SolanaFees";
21
+ import {SwapProgram} from "./programTypes";
22
+ import {SolanaRetryPolicy} from "../base/SolanaBase";
23
+ import {SolanaProgramBase} from "../program/SolanaProgramBase";
24
+ import {SolanaTx} from "../base/modules/SolanaTransactions";
25
+ import {SwapInit, SolanaPreFetchData, SolanaPreFetchVerification} from "./modules/SwapInit";
26
+ import {SolanaDataAccount, StoredDataAccount} from "./modules/SolanaDataAccount";
27
+ import {SwapRefund} from "./modules/SwapRefund";
28
+ import {SwapClaim} from "./modules/SwapClaim";
29
+ import {SolanaLpVault} from "./modules/SolanaLpVault";
30
+ import {Buffer} from "buffer";
31
+ import {SolanaSigner} from "../wallet/SolanaSigner";
32
+ import {SolanaKeypairWallet} from "../wallet/SolanaKeypairWallet";
33
+
34
+ function toPublicKeyOrNull(str: string | null): PublicKey | null {
35
+ return str==null ? null : new PublicKey(str);
36
+ }
37
+
38
+ export class SolanaSwapProgram
39
+ extends SolanaProgramBase<SwapProgram>
40
+ implements SwapContract<
41
+ SolanaSwapData,
42
+ SolanaTx,
43
+ SolanaPreFetchData,
44
+ SolanaPreFetchVerification,
45
+ SolanaSigner,
46
+ "SOLANA"
47
+ > {
48
+
49
+ ////////////////////////
50
+ //// Constants
51
+ public readonly ESCROW_STATE_RENT_EXEMPT = 2658720;
52
+
53
+ ////////////////////////
54
+ //// PDA accessors
55
+ readonly SwapVaultAuthority = this.pda("authority");
56
+ readonly SwapVault = this.pda("vault", (tokenAddress: PublicKey) => [tokenAddress.toBuffer()]);
57
+ readonly SwapUserVault = this.pda("uservault",
58
+ (publicKey: PublicKey, tokenAddress: PublicKey) => [publicKey.toBuffer(), tokenAddress.toBuffer()]
59
+ );
60
+ readonly SwapEscrowState = this.pda("state", (hash: Buffer) => [hash]);
61
+
62
+ ////////////////////////
63
+ //// Timeouts
64
+ readonly chainId: "SOLANA" = "SOLANA";
65
+ readonly claimWithSecretTimeout: number = 45;
66
+ readonly claimWithTxDataTimeout: number = 120;
67
+ readonly refundTimeout: number = 45;
68
+ readonly claimGracePeriod: number = 10*60;
69
+ readonly refundGracePeriod: number = 10*60;
70
+ readonly authGracePeriod: number = 5*60;
71
+
72
+ ////////////////////////
73
+ //// Services
74
+ readonly Init: SwapInit;
75
+ readonly Refund: SwapRefund;
76
+ readonly Claim: SwapClaim;
77
+ readonly DataAccount: SolanaDataAccount;
78
+ readonly LpVault: SolanaLpVault;
79
+
80
+ constructor(
81
+ connection: Connection,
82
+ btcRelay: SolanaBtcRelay<any>,
83
+ storage: IStorageManager<StoredDataAccount>,
84
+ programAddress?: string,
85
+ retryPolicy?: SolanaRetryPolicy,
86
+ solanaFeeEstimator: SolanaFees = btcRelay.Fees || new SolanaFees(connection)
87
+ ) {
88
+ super(connection, programIdl, programAddress, retryPolicy, solanaFeeEstimator);
89
+
90
+ this.Init = new SwapInit(this);
91
+ this.Refund = new SwapRefund(this);
92
+ this.Claim = new SwapClaim(this, btcRelay);
93
+ this.DataAccount = new SolanaDataAccount(this, storage);
94
+ this.LpVault = new SolanaLpVault(this);
95
+ }
96
+
97
+ async start(): Promise<void> {
98
+ await this.DataAccount.init();
99
+ }
100
+
101
+ getClaimableDeposits(signer: string): Promise<{count: number, totalValue: BN}> {
102
+ return this.DataAccount.getDataAccountsInfo(new PublicKey(signer));
103
+ }
104
+
105
+ claimDeposits(signer: SolanaSigner): Promise<{txIds: string[], count: number, totalValue: BN}> {
106
+ return this.DataAccount.sweepDataAccounts(signer);
107
+ }
108
+
109
+ ////////////////////////////////////////////
110
+ //// Signatures
111
+ preFetchForInitSignatureVerification(data: SolanaPreFetchData): Promise<SolanaPreFetchVerification> {
112
+ return this.Init.preFetchForInitSignatureVerification(data);
113
+ }
114
+
115
+ preFetchBlockDataForSignatures(): Promise<SolanaPreFetchData> {
116
+ return this.Init.preFetchBlockDataForSignatures();
117
+ }
118
+
119
+ getInitSignature(signer: SolanaSigner, swapData: SolanaSwapData, authorizationTimeout: number, preFetchedBlockData?: SolanaPreFetchData, feeRate?: string): Promise<SignatureData> {
120
+ return this.Init.signSwapInitialization(signer, swapData, authorizationTimeout, preFetchedBlockData, feeRate);
121
+ }
122
+
123
+ isValidInitAuthorization(swapData: SolanaSwapData, {timeout, prefix, signature}, feeRate?: string, preFetchedData?: SolanaPreFetchVerification): Promise<Buffer> {
124
+ return this.Init.isSignatureValid(swapData, timeout, prefix, signature, feeRate, preFetchedData);
125
+ }
126
+
127
+ getInitAuthorizationExpiry(swapData: SolanaSwapData, {timeout, prefix, signature}, preFetchedData?: SolanaPreFetchVerification): Promise<number> {
128
+ return this.Init.getSignatureExpiry(timeout, signature, preFetchedData);
129
+ }
130
+
131
+ isInitAuthorizationExpired(swapData: SolanaSwapData, {timeout, prefix, signature}): Promise<boolean> {
132
+ return this.Init.isSignatureExpired(signature, timeout);
133
+ }
134
+
135
+ getRefundSignature(signer: SolanaSigner, swapData: SolanaSwapData, authorizationTimeout: number): Promise<SignatureData> {
136
+ return this.Refund.signSwapRefund(signer, swapData, authorizationTimeout);
137
+ }
138
+
139
+ isValidRefundAuthorization(swapData: SolanaSwapData, {timeout, prefix, signature}): Promise<Buffer> {
140
+ return this.Refund.isSignatureValid(swapData, timeout, prefix, signature);
141
+ }
142
+
143
+ getDataSignature(signer: SolanaSigner, data: Buffer): Promise<string> {
144
+ return this.Signatures.getDataSignature(signer, data);
145
+ }
146
+
147
+ isValidDataSignature(data: Buffer, signature: string, publicKey: string): Promise<boolean> {
148
+ return this.Signatures.isValidDataSignature(data, signature, publicKey);
149
+ }
150
+
151
+ ////////////////////////////////////////////
152
+ //// Swap data utils
153
+ /**
154
+ * Checks whether the claim is claimable by us, that means not expired, we are claimer & the swap is commited
155
+ *
156
+ * @param signer
157
+ * @param data
158
+ */
159
+ isClaimable(signer: string, data: SolanaSwapData): Promise<boolean> {
160
+ if(!data.isClaimer(signer)) return Promise.resolve(false);
161
+ if(this.isExpired(signer, data)) return Promise.resolve(false);
162
+ return this.isCommited(data);
163
+ }
164
+
165
+ /**
166
+ * Checks whether a swap is commited, i.e. the swap still exists on-chain and was not claimed nor refunded
167
+ *
168
+ * @param swapData
169
+ */
170
+ async isCommited(swapData: SolanaSwapData): Promise<boolean> {
171
+ const paymentHash = Buffer.from(swapData.paymentHash, "hex");
172
+
173
+ const account: IdlAccounts<SwapProgram>["escrowState"] = await this.program.account.escrowState.fetchNullable(this.SwapEscrowState(paymentHash));
174
+ if(account==null) return false;
175
+
176
+ return swapData.correctPDA(account);
177
+ }
178
+
179
+ /**
180
+ * Checks whether the swap is expired, takes into consideration possible on-chain time skew, therefore for claimer
181
+ * the swap expires a bit sooner than it should've & for the offerer it expires a bit later
182
+ *
183
+ * @param signer
184
+ * @param data
185
+ */
186
+ isExpired(signer: string, data: SolanaSwapData): boolean {
187
+ let currentTimestamp: BN = new BN(Math.floor(Date.now()/1000));
188
+ if(data.isClaimer(signer)) currentTimestamp = currentTimestamp.sub(new BN(this.refundGracePeriod));
189
+ if(data.isOfferer(signer)) currentTimestamp = currentTimestamp.add(new BN(this.claimGracePeriod));
190
+ return data.expiry.lt(currentTimestamp);
191
+ }
192
+
193
+ /**
194
+ * Checks if the swap is refundable by us, checks if we are offerer, if the swap is already expired & if the swap
195
+ * is still commited
196
+ *
197
+ * @param signer
198
+ * @param data
199
+ */
200
+ isRequestRefundable(signer: string, data: SolanaSwapData): Promise<boolean> {
201
+ //Swap can only be refunded by the offerer
202
+ if(!data.isOfferer(signer)) return Promise.resolve(false);
203
+ if(!this.isExpired(signer, data)) return Promise.resolve(false);
204
+ return this.isCommited(data);
205
+ }
206
+
207
+ /**
208
+ * Get the swap payment hash to be used for an on-chain swap, this just uses a sha256 hash of the values
209
+ *
210
+ * @param outputScript output script required to claim the swap
211
+ * @param amount sats sent required to claim the swap
212
+ * @param nonce swap nonce uniquely identifying the transaction to prevent replay attacks
213
+ */
214
+ getHashForOnchain(outputScript: Buffer, amount: BN, nonce: BN): Buffer {
215
+ return createHash("sha256").update(Buffer.concat([
216
+ Buffer.from(nonce.toArray("le", 8)),
217
+ Buffer.from(amount.toArray("le", 8)),
218
+ outputScript
219
+ ])).digest();
220
+ }
221
+
222
+ ////////////////////////////////////////////
223
+ //// Swap data getters
224
+ /**
225
+ * Gets the status of the specific swap, this also checks if we are offerer/claimer & checks for expiry (to see
226
+ * if swap is refundable)
227
+ *
228
+ * @param signer
229
+ * @param data
230
+ */
231
+ async getCommitStatus(signer: string, data: SolanaSwapData): Promise<SwapCommitStatus> {
232
+ const escrowStateKey = this.SwapEscrowState(Buffer.from(data.paymentHash, "hex"));
233
+ const escrowState: IdlAccounts<SwapProgram>["escrowState"] = await this.program.account.escrowState.fetchNullable(escrowStateKey);
234
+ if(escrowState!=null) {
235
+ if(data.correctPDA(escrowState)) {
236
+ if(data.isOfferer(signer) && this.isExpired(signer,data)) return SwapCommitStatus.REFUNDABLE;
237
+ return SwapCommitStatus.COMMITED;
238
+ }
239
+
240
+ if(data.isOfferer(signer) && this.isExpired(signer, data)) return SwapCommitStatus.EXPIRED;
241
+ return SwapCommitStatus.NOT_COMMITED;
242
+ }
243
+
244
+ //Check if paid or what
245
+ const status = await this.Events.findInEvents(escrowStateKey, async (event) => {
246
+ if(event.name==="ClaimEvent") {
247
+ if(!event.data.sequence.eq(data.sequence)) return null;
248
+ return SwapCommitStatus.PAID;
249
+ }
250
+ if(event.name==="RefundEvent") {
251
+ if(!event.data.sequence.eq(data.sequence)) return null;
252
+ if(this.isExpired(signer, data)) return SwapCommitStatus.EXPIRED;
253
+ return SwapCommitStatus.NOT_COMMITED;
254
+ }
255
+ });
256
+ if(status!=null) return status;
257
+
258
+ if(this.isExpired(signer, data)) {
259
+ return SwapCommitStatus.EXPIRED;
260
+ }
261
+ return SwapCommitStatus.NOT_COMMITED;
262
+ }
263
+
264
+ /**
265
+ * Checks the status of the specific payment hash
266
+ *
267
+ * @param paymentHash
268
+ */
269
+ async getPaymentHashStatus(paymentHash: string): Promise<SwapCommitStatus> {
270
+ const escrowStateKey = this.SwapEscrowState(Buffer.from(paymentHash, "hex"));
271
+ const abortController = new AbortController();
272
+
273
+ //Start fetching events before checking escrow PDA, this call is used when quoting, so saving 100ms here helps a lot!
274
+ const eventsPromise = this.Events.findInEvents(escrowStateKey, async (event) => {
275
+ if(event.name==="ClaimEvent") return SwapCommitStatus.PAID;
276
+ if(event.name==="RefundEvent") return SwapCommitStatus.NOT_COMMITED;
277
+ }, abortController.signal).catch(e => {
278
+ abortController.abort(e)
279
+ return null;
280
+ });
281
+
282
+ const escrowState = await this.program.account.escrowState.fetchNullable(escrowStateKey);
283
+ abortController.signal.throwIfAborted();
284
+ if(escrowState!=null) {
285
+ abortController.abort();
286
+ return SwapCommitStatus.COMMITED;
287
+ }
288
+
289
+ //Check if paid or what
290
+ const eventsStatus = await eventsPromise;
291
+ abortController.signal.throwIfAborted();
292
+ if(eventsStatus!=null) return eventsStatus;
293
+
294
+ return SwapCommitStatus.NOT_COMMITED;
295
+ }
296
+
297
+ /**
298
+ * Returns the data committed for a specific payment hash, or null if no data is currently commited for
299
+ * the specific swap
300
+ *
301
+ * @param paymentHashHex
302
+ */
303
+ async getCommitedData(paymentHashHex: string): Promise<SolanaSwapData> {
304
+ const paymentHash = Buffer.from(paymentHashHex, "hex");
305
+
306
+ const account: IdlAccounts<SwapProgram>["escrowState"] = await this.program.account.escrowState.fetchNullable(this.SwapEscrowState(paymentHash));
307
+ if(account==null) return null;
308
+
309
+ return SolanaSwapData.fromEscrowState(account);
310
+ }
311
+
312
+ ////////////////////////////////////////////
313
+ //// Swap data initializer
314
+ createSwapData(
315
+ type: ChainSwapType,
316
+ offerer: string,
317
+ claimer: string,
318
+ token: string,
319
+ amount: BN,
320
+ paymentHash: string,
321
+ sequence: BN,
322
+ expiry: BN,
323
+ escrowNonce: BN,
324
+ confirmations: number,
325
+ payIn: boolean,
326
+ payOut: boolean,
327
+ securityDeposit: BN,
328
+ claimerBounty: BN
329
+ ): Promise<SolanaSwapData> {
330
+ const tokenAddr: PublicKey = new PublicKey(token);
331
+ const offererKey = offerer==null ? null : new PublicKey(offerer);
332
+ const claimerKey = claimer==null ? null : new PublicKey(claimer);
333
+ return Promise.resolve(new SolanaSwapData(
334
+ offererKey,
335
+ claimerKey,
336
+ tokenAddr,
337
+ amount,
338
+ paymentHash,
339
+ sequence,
340
+ expiry,
341
+ escrowNonce,
342
+ confirmations,
343
+ payOut,
344
+ type==null ? null : SolanaSwapData.typeToKind(type),
345
+ payIn,
346
+ offererKey==null ? null : payIn ? getAssociatedTokenAddressSync(tokenAddr, offererKey) : PublicKey.default,
347
+ claimerKey==null ? null : payOut ? getAssociatedTokenAddressSync(tokenAddr, claimerKey) : PublicKey.default,
348
+ securityDeposit,
349
+ claimerBounty,
350
+ null
351
+ ));
352
+ }
353
+
354
+ ////////////////////////////////////////////
355
+ //// Utils
356
+ async getBalance(signer: string, tokenAddress: string, inContract: boolean): Promise<BN> {
357
+ const token = new PublicKey(tokenAddress);
358
+ const publicKey = new PublicKey(signer);
359
+
360
+ if(inContract) return await this.getIntermediaryBalance(publicKey, token);
361
+
362
+ let balance = await this.Tokens.getTokenBalance(publicKey, token);
363
+ if(token.equals(this.Tokens.WSOL_ADDRESS)) {
364
+ const feeCosts = new BN(5000).add(await this.getCommitFee(null));
365
+ balance = BN.max(balance.sub(feeCosts), new BN(0));
366
+ }
367
+ this.logger.debug("getBalance(): token balance, token: "+token.toBase58()+" balance: "+balance.toString(10));
368
+ return balance;
369
+ }
370
+
371
+ getIntermediaryData(address: string, token: string): Promise<{
372
+ balance: BN,
373
+ reputation: IntermediaryReputationType
374
+ }> {
375
+ return this.LpVault.getIntermediaryData(new PublicKey(address), new PublicKey(token));
376
+ }
377
+
378
+ getIntermediaryReputation(address: string, token: string): Promise<IntermediaryReputationType> {
379
+ return this.LpVault.getIntermediaryReputation(new PublicKey(address), new PublicKey(token));
380
+ }
381
+
382
+ getIntermediaryBalance(address: PublicKey, token: PublicKey): Promise<BN> {
383
+ return this.LpVault.getIntermediaryBalance(address, token);
384
+ }
385
+
386
+ isValidAddress(address: string): boolean {
387
+ return this.Addresses.isValidAddress(address);
388
+ }
389
+
390
+ getNativeCurrencyAddress(): string {
391
+ return this.Tokens.getNativeCurrencyAddress().toString();
392
+ }
393
+
394
+ ////////////////////////////////////////////
395
+ //// Transaction initializers
396
+ async txsClaimWithSecret(
397
+ signer: string | SolanaSigner,
398
+ swapData: SolanaSwapData,
399
+ secret: string,
400
+ checkExpiry?: boolean,
401
+ initAta?: boolean,
402
+ feeRate?: string,
403
+ skipAtaCheck?: boolean
404
+ ): Promise<SolanaTx[]> {
405
+ return this.Claim.txsClaimWithSecret(typeof(signer)==="string" ? new PublicKey(signer) : signer.getPublicKey(), swapData, secret, checkExpiry, initAta, feeRate, skipAtaCheck)
406
+ }
407
+
408
+ async txsClaimWithTxData(
409
+ signer: string | SolanaSigner,
410
+ swapData: SolanaSwapData,
411
+ blockheight: number,
412
+ tx: { blockhash: string, confirmations: number, txid: string, hex: string },
413
+ vout: number,
414
+ commitedHeader?: SolanaBtcStoredHeader,
415
+ synchronizer?: RelaySynchronizer<any, SolanaTx, any>,
416
+ initAta?: boolean,
417
+ feeRate?: string,
418
+ storageAccHolder?: {storageAcc: PublicKey}
419
+ ): Promise<SolanaTx[] | null> {
420
+ return this.Claim.txsClaimWithTxData(typeof(signer)==="string" ? new PublicKey(signer) : signer, swapData, blockheight, tx, vout, commitedHeader, synchronizer, initAta, storageAccHolder, feeRate);
421
+ }
422
+
423
+ txsRefund(swapData: SolanaSwapData, check?: boolean, initAta?: boolean, feeRate?: string): Promise<SolanaTx[]> {
424
+ return this.Refund.txsRefund(swapData, check, initAta, feeRate);
425
+ }
426
+
427
+ txsRefundWithAuthorization(swapData: SolanaSwapData, {timeout, prefix, signature}, check?: boolean, initAta?: boolean, feeRate?: string): Promise<SolanaTx[]> {
428
+ return this.Refund.txsRefundWithAuthorization(swapData,timeout,prefix,signature,check,initAta,feeRate);
429
+ }
430
+
431
+ txsInitPayIn(swapData: SolanaSwapData, {timeout, prefix, signature}, skipChecks?: boolean, feeRate?: string): Promise<SolanaTx[]> {
432
+ return this.Init.txsInitPayIn(swapData, timeout, prefix, signature, skipChecks, feeRate);
433
+ }
434
+
435
+ txsInit(swapData: SolanaSwapData, {timeout, prefix, signature}, txoHash?: Buffer, skipChecks?: boolean, feeRate?: string): Promise<SolanaTx[]> {
436
+ return this.Init.txsInit(swapData, timeout, prefix, signature, skipChecks, feeRate);
437
+ }
438
+
439
+ txsWithdraw(signer: string, token: string, amount: BN, feeRate?: string): Promise<SolanaTx[]> {
440
+ return this.LpVault.txsWithdraw(new PublicKey(signer), new PublicKey(token), amount, feeRate);
441
+ }
442
+
443
+ txsDeposit(signer: string, token: string, amount: BN, feeRate?: string): Promise<SolanaTx[]> {
444
+ return this.LpVault.txsDeposit(new PublicKey(signer), new PublicKey(token), amount, feeRate);
445
+ }
446
+
447
+ txsTransfer(signer: string, token: string, amount: BN, dstAddress: string, feeRate?: string): Promise<SolanaTx[]> {
448
+ return this.Tokens.txsTransfer(new PublicKey(signer), new PublicKey(token), amount, new PublicKey(dstAddress), feeRate);
449
+ }
450
+
451
+ ////////////////////////////////////////////
452
+ //// Executors
453
+ async claimWithSecret(
454
+ signer: SolanaSigner,
455
+ swapData: SolanaSwapData,
456
+ secret: string,
457
+ checkExpiry?: boolean,
458
+ initAta?: boolean,
459
+ txOptions?: TransactionConfirmationOptions
460
+ ): Promise<string> {
461
+ const result = await this.Claim.txsClaimWithSecret(signer.getPublicKey(), swapData, secret, checkExpiry, initAta, txOptions?.feeRate);
462
+ const [signature] = await this.Transactions.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
463
+ return signature;
464
+ }
465
+
466
+ async claimWithTxData(
467
+ signer: SolanaSigner,
468
+ swapData: SolanaSwapData,
469
+ blockheight: number,
470
+ tx: { blockhash: string, confirmations: number, txid: string, hex: string },
471
+ vout: number,
472
+ commitedHeader?: SolanaBtcStoredHeader,
473
+ synchronizer?: RelaySynchronizer<any, SolanaTx, any>,
474
+ initAta?: boolean,
475
+ txOptions?: TransactionConfirmationOptions
476
+ ): Promise<string> {
477
+ const data: {storageAcc: PublicKey} = {
478
+ storageAcc: null
479
+ };
480
+
481
+ const txs = await this.Claim.txsClaimWithTxData(
482
+ signer, swapData, blockheight, tx, vout,
483
+ commitedHeader, synchronizer, initAta, data, txOptions?.feeRate
484
+ );
485
+ if(txs===null) throw new Error("Btc relay not synchronized to required blockheight!");
486
+
487
+ //TODO: This doesn't return proper tx signature
488
+ const [signature] = await this.Transactions.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal);
489
+ await this.DataAccount.removeDataAccount(data.storageAcc);
490
+
491
+ return signature;
492
+ }
493
+
494
+ async refund(
495
+ signer: SolanaSigner,
496
+ swapData: SolanaSwapData,
497
+ check?: boolean,
498
+ initAta?: boolean,
499
+ txOptions?: TransactionConfirmationOptions
500
+ ): Promise<string> {
501
+ if(!signer.getPublicKey().equals(swapData.offerer)) throw new Error("Invalid signer provided!");
502
+
503
+ let result = await this.txsRefund(swapData, check, initAta, txOptions?.feeRate);
504
+
505
+ const [signature] = await this.Transactions.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
506
+
507
+ return signature;
508
+ }
509
+
510
+ async refundWithAuthorization(
511
+ signer: SolanaSigner,
512
+ swapData: SolanaSwapData,
513
+ signature: SignatureData,
514
+ check?: boolean,
515
+ initAta?: boolean,
516
+ txOptions?: TransactionConfirmationOptions
517
+ ): Promise<string> {
518
+ if(!signer.getPublicKey().equals(swapData.offerer)) throw new Error("Invalid signer provided!");
519
+
520
+ let result = await this.txsRefundWithAuthorization(swapData, signature, check, initAta, txOptions?.feeRate);
521
+
522
+ const [txSignature] = await this.Transactions.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
523
+
524
+ return txSignature;
525
+ }
526
+
527
+ async initPayIn(
528
+ signer: SolanaSigner,
529
+ swapData: SolanaSwapData,
530
+ signature: SignatureData,
531
+ skipChecks?: boolean,
532
+ txOptions?: TransactionConfirmationOptions
533
+ ): Promise<string> {
534
+ if(!signer.getPublicKey().equals(swapData.offerer)) throw new Error("Invalid signer provided!");
535
+
536
+ let result = await this.txsInitPayIn(swapData, signature, skipChecks, txOptions?.feeRate);
537
+
538
+ const signatures = await this.Transactions.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
539
+
540
+ return signatures[signatures.length-1];
541
+ }
542
+
543
+ async init(
544
+ signer: SolanaSigner,
545
+ swapData: SolanaSwapData,
546
+ signature: SignatureData,
547
+ txoHash?: Buffer,
548
+ skipChecks?: boolean,
549
+ txOptions?: TransactionConfirmationOptions
550
+ ): Promise<string> {
551
+ if(!signer.getPublicKey().equals(swapData.claimer)) throw new Error("Invalid signer provided!");
552
+
553
+ let result = await this.txsInit(swapData, signature, txoHash, skipChecks, txOptions?.feeRate);
554
+
555
+ const [txSignature] = await this.Transactions.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
556
+
557
+ return txSignature;
558
+ }
559
+
560
+ async initAndClaimWithSecret(
561
+ signer: SolanaSigner,
562
+ swapData: SolanaSwapData,
563
+ signature: SignatureData,
564
+ secret: string,
565
+ skipChecks?: boolean,
566
+ txOptions?: TransactionConfirmationOptions
567
+ ): Promise<string[]> {
568
+ if(!signer.getPublicKey().equals(swapData.claimer)) throw new Error("Invalid signer provided!");
569
+
570
+ const txsCommit = await this.txsInit(swapData, signature, null, skipChecks, txOptions?.feeRate);
571
+ const txsClaim = await this.Claim.txsClaimWithSecret(signer.getPublicKey(), swapData, secret, true, false, txOptions?.feeRate, true);
572
+
573
+ return await this.Transactions.sendAndConfirm(signer, txsCommit.concat(txsClaim), txOptions?.waitForConfirmation, txOptions?.abortSignal);
574
+ }
575
+
576
+ async withdraw(
577
+ signer: SolanaSigner,
578
+ token: string,
579
+ amount: BN,
580
+ txOptions?: TransactionConfirmationOptions
581
+ ): Promise<string> {
582
+ const txs = await this.LpVault.txsWithdraw(signer.getPublicKey(), new PublicKey(token), amount, txOptions?.feeRate);
583
+ const [txId] = await this.Transactions.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
584
+ return txId;
585
+ }
586
+
587
+ async deposit(
588
+ signer: SolanaSigner,
589
+ token: string,
590
+ amount: BN,
591
+ txOptions?: TransactionConfirmationOptions
592
+ ): Promise<string> {
593
+ const txs = await this.LpVault.txsDeposit(signer.getPublicKey(), new PublicKey(token), amount, txOptions?.feeRate);
594
+ const [txId] = await this.Transactions.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
595
+ return txId;
596
+ }
597
+
598
+ async transfer(
599
+ signer: SolanaSigner,
600
+ token: string,
601
+ amount: BN,
602
+ dstAddress: string,
603
+ txOptions?: TransactionConfirmationOptions
604
+ ): Promise<string> {
605
+ const txs = await this.Tokens.txsTransfer(signer.getPublicKey(), new PublicKey(token), amount, new PublicKey(dstAddress), txOptions?.feeRate);
606
+ const [txId] = await this.Transactions.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
607
+ return txId;
608
+ }
609
+
610
+ ////////////////////////////////////////////
611
+ //// Transactions
612
+ sendAndConfirm(
613
+ signer: SolanaSigner,
614
+ txs: SolanaTx[],
615
+ waitForConfirmation?: boolean,
616
+ abortSignal?: AbortSignal,
617
+ parallel?: boolean,
618
+ onBeforePublish?: (txId: string, rawTx: string) => Promise<void>
619
+ ): Promise<string[]> {
620
+ return this.Transactions.sendAndConfirm(signer, txs, waitForConfirmation, abortSignal, parallel, onBeforePublish);
621
+ }
622
+
623
+ serializeTx(tx: SolanaTx): Promise<string> {
624
+ return this.Transactions.serializeTx(tx);
625
+ }
626
+
627
+ deserializeTx(txData: string): Promise<SolanaTx> {
628
+ return this.Transactions.deserializeTx(txData);
629
+ }
630
+
631
+ getTxIdStatus(txId: string): Promise<"not_found" | "pending" | "success" | "reverted"> {
632
+ return this.Transactions.getTxIdStatus(txId);
633
+ }
634
+
635
+ getTxStatus(tx: string): Promise<"not_found" | "pending" | "success" | "reverted"> {
636
+ return this.Transactions.getTxStatus(tx);
637
+ }
638
+
639
+ ////////////////////////////////////////////
640
+ //// Fees
641
+ getInitPayInFeeRate(offerer?: string, claimer?: string, token?: string, paymentHash?: string): Promise<string> {
642
+ return this.Init.getInitPayInFeeRate(
643
+ toPublicKeyOrNull(offerer),
644
+ toPublicKeyOrNull(claimer),
645
+ toPublicKeyOrNull(token),
646
+ paymentHash
647
+ );
648
+ }
649
+
650
+ getInitFeeRate(offerer?: string, claimer?: string, token?: string, paymentHash?: string): Promise<string> {
651
+ return this.Init.getInitFeeRate(
652
+ toPublicKeyOrNull(offerer),
653
+ toPublicKeyOrNull(claimer),
654
+ toPublicKeyOrNull(token),
655
+ paymentHash
656
+ );
657
+ }
658
+
659
+ getRefundFeeRate(swapData: SolanaSwapData): Promise<string> {
660
+ return this.Refund.getRefundFeeRate(swapData);
661
+ }
662
+
663
+ getClaimFeeRate(signer: string, swapData: SolanaSwapData): Promise<string> {
664
+ return this.Claim.getClaimFeeRate(new PublicKey(signer), swapData);
665
+ }
666
+
667
+ getClaimFee(signer: string, swapData: SolanaSwapData, feeRate?: string): Promise<BN> {
668
+ return this.Claim.getClaimFee(new PublicKey(signer), swapData, feeRate);
669
+ }
670
+
671
+ getRawClaimFee(signer: string, swapData: SolanaSwapData, feeRate?: string): Promise<BN> {
672
+ return this.Claim.getRawClaimFee(new PublicKey(signer), swapData, feeRate);
673
+ }
674
+
675
+ /**
676
+ * Get the estimated solana fee of the commit transaction
677
+ */
678
+ getCommitFee(swapData: SolanaSwapData, feeRate?: string): Promise<BN> {
679
+ return this.Init.getInitFee(swapData, feeRate);
680
+ }
681
+
682
+ /**
683
+ * Get the estimated solana transaction fee of the refund transaction
684
+ */
685
+ getRefundFee(swapData: SolanaSwapData, feeRate?: string): Promise<BN> {
686
+ return this.Refund.getRefundFee(swapData, feeRate);
687
+ }
688
+
689
+ /**
690
+ * Get the estimated solana transaction fee of the refund transaction
691
+ */
692
+ getRawRefundFee(swapData: SolanaSwapData, feeRate?: string): Promise<BN> {
693
+ return this.Refund.getRawRefundFee(swapData, feeRate);
694
+ }
695
+
696
+ ///////////////////////////////////
697
+ //// Callbacks & handlers
698
+ offBeforeTxReplace(callback: (oldTx: string, oldTxId: string, newTx: string, newTxId: string) => Promise<void>): boolean {
699
+ return true;
700
+ }
701
+
702
+ onBeforeTxReplace(callback: (oldTx: string, oldTxId: string, newTx: string, newTxId: string) => Promise<void>): void {}
703
+
704
+ onBeforeTxSigned(callback: (tx: SolanaTx) => Promise<void>): void {
705
+ this.Transactions.onBeforeTxSigned(callback);
706
+ }
707
+
708
+ offBeforeTxSigned(callback: (tx: SolanaTx) => Promise<void>): boolean {
709
+ return this.Transactions.offBeforeTxSigned(callback);
710
+ }
711
+
712
+ onSendTransaction(callback: (tx: Buffer, options?: SendOptions) => Promise<string>): void {
713
+ this.Transactions.onSendTransaction(callback);
714
+ }
715
+
716
+ offSendTransaction(callback: (tx: Buffer, options?: SendOptions) => Promise<string>): boolean {
717
+ return this.Transactions.offSendTransaction(callback);
718
+ }
719
+
720
+ isValidToken(tokenIdentifier: string): boolean {
721
+ try {
722
+ new PublicKey(tokenIdentifier);
723
+ return true;
724
+ } catch (e) {
725
+ return false;
726
+ }
727
+ }
728
+
729
+ randomAddress(): string {
730
+ return Keypair.generate().publicKey.toString();
731
+ }
732
+
733
+ randomSigner(): SolanaSigner {
734
+ const keypair = Keypair.generate();
735
+ const wallet = new SolanaKeypairWallet(keypair);
736
+ return new SolanaSigner(wallet, keypair);
737
+ }
738
+
739
+ }