@atomiqlabs/chain-solana 12.0.14 → 12.0.15

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 (114) hide show
  1. package/LICENSE +201 -201
  2. package/dist/index.d.ts +29 -29
  3. package/dist/index.js +45 -45
  4. package/dist/solana/SolanaChainType.d.ts +11 -11
  5. package/dist/solana/SolanaChainType.js +2 -2
  6. package/dist/solana/SolanaChains.d.ts +20 -20
  7. package/dist/solana/SolanaChains.js +25 -25
  8. package/dist/solana/SolanaInitializer.d.ts +18 -18
  9. package/dist/solana/SolanaInitializer.js +63 -63
  10. package/dist/solana/btcrelay/SolanaBtcRelay.d.ts +228 -228
  11. package/dist/solana/btcrelay/SolanaBtcRelay.js +441 -441
  12. package/dist/solana/btcrelay/headers/SolanaBtcHeader.d.ts +29 -29
  13. package/dist/solana/btcrelay/headers/SolanaBtcHeader.js +34 -34
  14. package/dist/solana/btcrelay/headers/SolanaBtcStoredHeader.d.ts +46 -46
  15. package/dist/solana/btcrelay/headers/SolanaBtcStoredHeader.js +78 -78
  16. package/dist/solana/btcrelay/program/programIdl.json +671 -671
  17. package/dist/solana/chain/SolanaAction.d.ts +26 -26
  18. package/dist/solana/chain/SolanaAction.js +86 -86
  19. package/dist/solana/chain/SolanaChainInterface.d.ts +65 -65
  20. package/dist/solana/chain/SolanaChainInterface.js +125 -125
  21. package/dist/solana/chain/SolanaModule.d.ts +14 -14
  22. package/dist/solana/chain/SolanaModule.js +13 -13
  23. package/dist/solana/chain/modules/SolanaAddresses.d.ts +8 -8
  24. package/dist/solana/chain/modules/SolanaAddresses.js +22 -22
  25. package/dist/solana/chain/modules/SolanaBlocks.d.ts +28 -28
  26. package/dist/solana/chain/modules/SolanaBlocks.js +72 -72
  27. package/dist/solana/chain/modules/SolanaEvents.d.ts +68 -68
  28. package/dist/solana/chain/modules/SolanaEvents.js +238 -238
  29. package/dist/solana/chain/modules/SolanaFees.d.ts +121 -121
  30. package/dist/solana/chain/modules/SolanaFees.js +379 -379
  31. package/dist/solana/chain/modules/SolanaSignatures.d.ts +23 -23
  32. package/dist/solana/chain/modules/SolanaSignatures.js +39 -39
  33. package/dist/solana/chain/modules/SolanaSlots.d.ts +31 -31
  34. package/dist/solana/chain/modules/SolanaSlots.js +68 -68
  35. package/dist/solana/chain/modules/SolanaTokens.d.ts +136 -136
  36. package/dist/solana/chain/modules/SolanaTokens.js +248 -248
  37. package/dist/solana/chain/modules/SolanaTransactions.d.ts +124 -124
  38. package/dist/solana/chain/modules/SolanaTransactions.js +323 -323
  39. package/dist/solana/events/SolanaChainEvents.d.ts +88 -88
  40. package/dist/solana/events/SolanaChainEvents.js +256 -256
  41. package/dist/solana/events/SolanaChainEventsBrowser.d.ts +75 -75
  42. package/dist/solana/events/SolanaChainEventsBrowser.js +172 -172
  43. package/dist/solana/program/SolanaProgramBase.d.ts +40 -40
  44. package/dist/solana/program/SolanaProgramBase.js +43 -43
  45. package/dist/solana/program/SolanaProgramModule.d.ts +8 -8
  46. package/dist/solana/program/SolanaProgramModule.js +11 -11
  47. package/dist/solana/program/modules/SolanaProgramEvents.d.ts +53 -53
  48. package/dist/solana/program/modules/SolanaProgramEvents.js +114 -114
  49. package/dist/solana/swaps/SolanaSwapData.d.ts +71 -71
  50. package/dist/solana/swaps/SolanaSwapData.js +292 -292
  51. package/dist/solana/swaps/SolanaSwapModule.d.ts +10 -10
  52. package/dist/solana/swaps/SolanaSwapModule.js +11 -11
  53. package/dist/solana/swaps/SolanaSwapProgram.d.ts +224 -224
  54. package/dist/solana/swaps/SolanaSwapProgram.js +570 -570
  55. package/dist/solana/swaps/SwapTypeEnum.d.ts +11 -11
  56. package/dist/solana/swaps/SwapTypeEnum.js +42 -42
  57. package/dist/solana/swaps/modules/SolanaDataAccount.d.ts +94 -94
  58. package/dist/solana/swaps/modules/SolanaDataAccount.js +231 -231
  59. package/dist/solana/swaps/modules/SolanaLpVault.d.ts +71 -71
  60. package/dist/solana/swaps/modules/SolanaLpVault.js +173 -173
  61. package/dist/solana/swaps/modules/SwapClaim.d.ts +129 -129
  62. package/dist/solana/swaps/modules/SwapClaim.js +291 -291
  63. package/dist/solana/swaps/modules/SwapInit.d.ts +217 -217
  64. package/dist/solana/swaps/modules/SwapInit.js +519 -519
  65. package/dist/solana/swaps/modules/SwapRefund.d.ts +82 -82
  66. package/dist/solana/swaps/modules/SwapRefund.js +262 -262
  67. package/dist/solana/swaps/programIdl.json +945 -945
  68. package/dist/solana/swaps/programTypes.d.ts +943 -943
  69. package/dist/solana/swaps/programTypes.js +945 -945
  70. package/dist/solana/wallet/SolanaKeypairWallet.d.ts +9 -9
  71. package/dist/solana/wallet/SolanaKeypairWallet.js +33 -33
  72. package/dist/solana/wallet/SolanaSigner.d.ts +11 -11
  73. package/dist/solana/wallet/SolanaSigner.js +17 -17
  74. package/dist/utils/Utils.d.ts +53 -53
  75. package/dist/utils/Utils.js +170 -170
  76. package/package.json +41 -41
  77. package/src/index.ts +36 -36
  78. package/src/solana/SolanaChainType.ts +27 -27
  79. package/src/solana/SolanaChains.ts +23 -23
  80. package/src/solana/SolanaInitializer.ts +102 -102
  81. package/src/solana/btcrelay/SolanaBtcRelay.ts +589 -589
  82. package/src/solana/btcrelay/headers/SolanaBtcHeader.ts +57 -57
  83. package/src/solana/btcrelay/headers/SolanaBtcStoredHeader.ts +102 -102
  84. package/src/solana/btcrelay/program/programIdl.json +670 -670
  85. package/src/solana/chain/SolanaAction.ts +108 -108
  86. package/src/solana/chain/SolanaChainInterface.ts +192 -192
  87. package/src/solana/chain/SolanaModule.ts +20 -20
  88. package/src/solana/chain/modules/SolanaAddresses.ts +20 -20
  89. package/src/solana/chain/modules/SolanaBlocks.ts +78 -78
  90. package/src/solana/chain/modules/SolanaEvents.ts +270 -270
  91. package/src/solana/chain/modules/SolanaFees.ts +450 -450
  92. package/src/solana/chain/modules/SolanaSignatures.ts +39 -39
  93. package/src/solana/chain/modules/SolanaSlots.ts +82 -82
  94. package/src/solana/chain/modules/SolanaTokens.ts +307 -307
  95. package/src/solana/chain/modules/SolanaTransactions.ts +365 -365
  96. package/src/solana/events/SolanaChainEvents.ts +299 -299
  97. package/src/solana/events/SolanaChainEventsBrowser.ts +209 -209
  98. package/src/solana/program/SolanaProgramBase.ts +79 -79
  99. package/src/solana/program/SolanaProgramModule.ts +15 -15
  100. package/src/solana/program/modules/SolanaProgramEvents.ts +155 -155
  101. package/src/solana/swaps/SolanaSwapData.ts +430 -430
  102. package/src/solana/swaps/SolanaSwapModule.ts +16 -16
  103. package/src/solana/swaps/SolanaSwapProgram.ts +854 -854
  104. package/src/solana/swaps/SwapTypeEnum.ts +29 -29
  105. package/src/solana/swaps/modules/SolanaDataAccount.ts +307 -307
  106. package/src/solana/swaps/modules/SolanaLpVault.ts +215 -215
  107. package/src/solana/swaps/modules/SwapClaim.ts +389 -389
  108. package/src/solana/swaps/modules/SwapInit.ts +663 -663
  109. package/src/solana/swaps/modules/SwapRefund.ts +323 -323
  110. package/src/solana/swaps/programIdl.json +944 -944
  111. package/src/solana/swaps/programTypes.ts +1885 -1885
  112. package/src/solana/wallet/SolanaKeypairWallet.ts +36 -36
  113. package/src/solana/wallet/SolanaSigner.ts +24 -24
  114. package/src/utils/Utils.ts +180 -180
@@ -1,854 +1,854 @@
1
- import {SolanaSwapData, InitInstruction} from "./SolanaSwapData";
2
- import {IdlAccounts} from "@coral-xyz/anchor";
3
- import {
4
- ParsedTransactionWithMeta,
5
- PublicKey,
6
- } from "@solana/web3.js";
7
- import {sha256} from "@noble/hashes/sha2";
8
- import {SolanaBtcRelay} from "../btcrelay/SolanaBtcRelay";
9
- import * as programIdl from "./programIdl.json";
10
- import {
11
- IStorageManager,
12
- SwapContract,
13
- ChainSwapType,
14
- IntermediaryReputationType,
15
- TransactionConfirmationOptions,
16
- SignatureData,
17
- RelaySynchronizer,
18
- BigIntBufferUtils,
19
- SwapCommitState,
20
- SwapCommitStateType, SwapNotCommitedState, SwapExpiredState, SwapPaidState
21
- } from "@atomiqlabs/base";
22
- import {SolanaBtcStoredHeader} from "../btcrelay/headers/SolanaBtcStoredHeader";
23
- import {
24
- getAssociatedTokenAddressSync,
25
- } from "@solana/spl-token";
26
- import {SwapProgram} from "./programTypes";
27
- import {SolanaChainInterface} from "../chain/SolanaChainInterface";
28
- import {SolanaProgramBase} from "../program/SolanaProgramBase";
29
- import {SolanaTx} from "../chain/modules/SolanaTransactions";
30
- import {SwapInit, SolanaPreFetchData, SolanaPreFetchVerification} from "./modules/SwapInit";
31
- import {SolanaDataAccount, StoredDataAccount} from "./modules/SolanaDataAccount";
32
- import {SwapRefund} from "./modules/SwapRefund";
33
- import {SwapClaim} from "./modules/SwapClaim";
34
- import {SolanaLpVault} from "./modules/SolanaLpVault";
35
- import {Buffer} from "buffer";
36
- import {SolanaSigner} from "../wallet/SolanaSigner";
37
- import {fromClaimHash, toBN, toClaimHash, toEscrowHash} from "../../utils/Utils";
38
- import {SolanaTokens} from "../chain/modules/SolanaTokens";
39
- import * as BN from "bn.js";
40
- import {ProgramEvent} from "../program/modules/SolanaProgramEvents";
41
-
42
- function toPublicKeyOrNull(str: string | null): PublicKey | null {
43
- return str==null ? null : new PublicKey(str);
44
- }
45
-
46
- const MAX_PARALLEL_COMMIT_STATUS_CHECKS = 5;
47
-
48
- export class SolanaSwapProgram
49
- extends SolanaProgramBase<SwapProgram>
50
- implements SwapContract<
51
- SolanaSwapData,
52
- SolanaTx,
53
- SolanaPreFetchData,
54
- SolanaPreFetchVerification,
55
- SolanaSigner,
56
- "SOLANA"
57
- > {
58
-
59
- ////////////////////////
60
- //// Constants
61
- public readonly ESCROW_STATE_RENT_EXEMPT = 2658720;
62
-
63
- ////////////////////////
64
- //// PDA accessors
65
- readonly SwapVaultAuthority = this.pda("authority");
66
- readonly SwapVault = this.pda("vault", (tokenAddress: PublicKey) => [tokenAddress.toBuffer()]);
67
- readonly SwapUserVault = this.pda("uservault",
68
- (publicKey: PublicKey, tokenAddress: PublicKey) => [publicKey.toBuffer(), tokenAddress.toBuffer()]
69
- );
70
- readonly SwapEscrowState = this.pda("state", (hash: Buffer) => [hash]);
71
-
72
- ////////////////////////
73
- //// Timeouts
74
- readonly chainId: "SOLANA" = "SOLANA";
75
- readonly claimWithSecretTimeout: number = 45;
76
- readonly claimWithTxDataTimeout: number = 120;
77
- readonly refundTimeout: number = 45;
78
- readonly claimGracePeriod: number = 10*60;
79
- readonly refundGracePeriod: number = 10*60;
80
- readonly authGracePeriod: number = 5*60;
81
-
82
- ////////////////////////
83
- //// Services
84
- readonly Init: SwapInit;
85
- readonly Refund: SwapRefund;
86
- readonly Claim: SwapClaim;
87
- readonly DataAccount: SolanaDataAccount;
88
- readonly LpVault: SolanaLpVault;
89
-
90
- constructor(
91
- chainInterface: SolanaChainInterface,
92
- btcRelay: SolanaBtcRelay<any>,
93
- storage: IStorageManager<StoredDataAccount>,
94
- programAddress?: string
95
- ) {
96
- super(chainInterface, programIdl, programAddress);
97
-
98
- this.Init = new SwapInit(chainInterface, this);
99
- this.Refund = new SwapRefund(chainInterface, this);
100
- this.Claim = new SwapClaim(chainInterface, this, btcRelay);
101
- this.DataAccount = new SolanaDataAccount(chainInterface, this, storage);
102
- this.LpVault = new SolanaLpVault(chainInterface, this);
103
- }
104
-
105
- async start(): Promise<void> {
106
- await this.DataAccount.init();
107
- }
108
-
109
- getClaimableDeposits(signer: string): Promise<{count: number, totalValue: bigint}> {
110
- return this.DataAccount.getDataAccountsInfo(new PublicKey(signer));
111
- }
112
-
113
- claimDeposits(signer: SolanaSigner): Promise<{txIds: string[], count: number, totalValue: bigint}> {
114
- return this.DataAccount.sweepDataAccounts(signer);
115
- }
116
-
117
- ////////////////////////////////////////////
118
- //// Signatures
119
- preFetchForInitSignatureVerification(data: SolanaPreFetchData): Promise<SolanaPreFetchVerification> {
120
- return this.Init.preFetchForInitSignatureVerification(data);
121
- }
122
-
123
- preFetchBlockDataForSignatures(): Promise<SolanaPreFetchData> {
124
- return this.Init.preFetchBlockDataForSignatures();
125
- }
126
-
127
- getInitSignature(signer: SolanaSigner, swapData: SolanaSwapData, authorizationTimeout: number, preFetchedBlockData?: SolanaPreFetchData, feeRate?: string): Promise<SignatureData> {
128
- return this.Init.signSwapInitialization(signer, swapData, authorizationTimeout, preFetchedBlockData, feeRate);
129
- }
130
-
131
- isValidInitAuthorization(signer: string, swapData: SolanaSwapData, {timeout, prefix, signature}, feeRate?: string, preFetchedData?: SolanaPreFetchVerification): Promise<Buffer> {
132
- return this.Init.isSignatureValid(signer, swapData, timeout, prefix, signature, feeRate, preFetchedData);
133
- }
134
-
135
- getInitAuthorizationExpiry(swapData: SolanaSwapData, {timeout, prefix, signature}, preFetchedData?: SolanaPreFetchVerification): Promise<number> {
136
- return this.Init.getSignatureExpiry(timeout, signature, preFetchedData);
137
- }
138
-
139
- isInitAuthorizationExpired(swapData: SolanaSwapData, {timeout, prefix, signature}): Promise<boolean> {
140
- return this.Init.isSignatureExpired(signature, timeout);
141
- }
142
-
143
- getRefundSignature(signer: SolanaSigner, swapData: SolanaSwapData, authorizationTimeout: number): Promise<SignatureData> {
144
- return this.Refund.signSwapRefund(signer, swapData, authorizationTimeout);
145
- }
146
-
147
- isValidRefundAuthorization(swapData: SolanaSwapData, {timeout, prefix, signature}): Promise<Buffer> {
148
- return this.Refund.isSignatureValid(swapData, timeout, prefix, signature);
149
- }
150
-
151
- getDataSignature(signer: SolanaSigner, data: Buffer): Promise<string> {
152
- return this.Chain.Signatures.getDataSignature(signer, data);
153
- }
154
-
155
- isValidDataSignature(data: Buffer, signature: string, publicKey: string): Promise<boolean> {
156
- return this.Chain.Signatures.isValidDataSignature(data, signature, publicKey);
157
- }
158
-
159
- ////////////////////////////////////////////
160
- //// Swap data utils
161
- /**
162
- * Checks whether the claim is claimable by us, that means not expired, we are claimer & the swap is commited
163
- *
164
- * @param signer
165
- * @param data
166
- */
167
- async isClaimable(signer: string, data: SolanaSwapData): Promise<boolean> {
168
- if(!data.isClaimer(signer)) return false;
169
- if(await this.isExpired(signer, data)) return false;
170
- return await this.isCommited(data);
171
- }
172
-
173
- /**
174
- * Checks whether a swap is commited, i.e. the swap still exists on-chain and was not claimed nor refunded
175
- *
176
- * @param swapData
177
- */
178
- async isCommited(swapData: SolanaSwapData): Promise<boolean> {
179
- const paymentHash = Buffer.from(swapData.paymentHash, "hex");
180
-
181
- const account: IdlAccounts<SwapProgram>["escrowState"] = await this.program.account.escrowState.fetchNullable(this.SwapEscrowState(paymentHash));
182
- if(account==null) return false;
183
-
184
- return swapData.correctPDA(account);
185
- }
186
-
187
- /**
188
- * Checks whether the swap is expired, takes into consideration possible on-chain time skew, therefore for claimer
189
- * the swap expires a bit sooner than it should've & for the offerer it expires a bit later
190
- *
191
- * @param signer
192
- * @param data
193
- */
194
- isExpired(signer: string, data: SolanaSwapData): Promise<boolean> {
195
- let currentTimestamp: BN = new BN(Math.floor(Date.now()/1000));
196
- if(data.isClaimer(signer)) currentTimestamp = currentTimestamp.add(new BN(this.claimGracePeriod));
197
- if(data.isOfferer(signer)) currentTimestamp = currentTimestamp.sub(new BN(this.refundGracePeriod));
198
- return Promise.resolve(data.expiry.lt(currentTimestamp));
199
- }
200
-
201
- /**
202
- * Checks if the swap is refundable by us, checks if we are offerer, if the swap is already expired & if the swap
203
- * is still commited
204
- *
205
- * @param signer
206
- * @param data
207
- */
208
- async isRequestRefundable(signer: string, data: SolanaSwapData): Promise<boolean> {
209
- //Swap can only be refunded by the offerer
210
- if(!data.isOfferer(signer)) return false;
211
- if(!(await this.isExpired(signer, data))) return false;
212
- return await this.isCommited(data);
213
- }
214
-
215
- /**
216
- * Get the swap payment hash to be used for an on-chain swap, this just uses a sha256 hash of the values
217
- *
218
- * @param outputScript output script required to claim the swap
219
- * @param amount sats sent required to claim the swap
220
- * @param confirmations
221
- * @param nonce swap nonce uniquely identifying the transaction to prevent replay attacks
222
- */
223
- getHashForOnchain(outputScript: Buffer, amount: bigint, confirmations: number, nonce?: bigint): Buffer {
224
- nonce ??= 0n;
225
- const paymentHash = Buffer.from(sha256(Buffer.concat([
226
- BigIntBufferUtils.toBuffer(nonce, "le", 8),
227
- BigIntBufferUtils.toBuffer(amount, "le", 8),
228
- outputScript
229
- ]))).toString("hex");
230
- return Buffer.from(toClaimHash(paymentHash, nonce, confirmations), "hex");
231
- }
232
-
233
- getHashForHtlc(swapHash: Buffer): Buffer {
234
- return Buffer.from(toClaimHash(
235
- swapHash.toString("hex"),
236
- 0n,
237
- 0
238
- ), "hex");
239
- }
240
-
241
- getHashForTxId(txId: string, confirmations: number): Buffer {
242
- return Buffer.from(toClaimHash(
243
- Buffer.from(txId, "hex").reverse().toString("hex"),
244
- 0n,
245
- confirmations
246
- ), "hex");
247
- }
248
-
249
- ////////////////////////////////////////////
250
- //// Swap data getters
251
- /**
252
- * Gets the status of the specific swap, this also checks if we are offerer/claimer & checks for expiry (to see
253
- * if swap is refundable)
254
- *
255
- * @param signer
256
- * @param data
257
- */
258
- async getCommitStatus(signer: string, data: SolanaSwapData): Promise<SwapCommitState> {
259
- const escrowStateKey = this.SwapEscrowState(Buffer.from(data.paymentHash, "hex"));
260
- const [escrowState, isExpired] = await Promise.all([
261
- this.program.account.escrowState.fetchNullable(escrowStateKey) as Promise<IdlAccounts<SwapProgram>["escrowState"]>,
262
- this.isExpired(signer,data)
263
- ]);
264
-
265
- if(escrowState!=null) {
266
- if(data.correctPDA(escrowState)) {
267
- if(data.isOfferer(signer) && isExpired) return {type: SwapCommitStateType.REFUNDABLE};
268
- return {type: SwapCommitStateType.COMMITED};
269
- }
270
-
271
- if(data.isOfferer(signer) && isExpired) return {type: SwapCommitStateType.EXPIRED};
272
- return {type: SwapCommitStateType.NOT_COMMITED};
273
- }
274
-
275
- //Check if paid or what
276
- const status: SwapNotCommitedState | SwapExpiredState | SwapPaidState = await this.Events.findInEvents(escrowStateKey, async (event, tx) => {
277
- if(event.name==="ClaimEvent") {
278
- const paymentHash = Buffer.from(event.data.hash).toString("hex");
279
- if(paymentHash!==data.paymentHash) return null;
280
- if(!event.data.sequence.eq(data.sequence)) return null;
281
- return {
282
- type: SwapCommitStateType.PAID,
283
- getClaimTxId: () => Promise.resolve(tx.transaction.signatures[0]),
284
- getClaimResult: () => Promise.resolve(Buffer.from(event.data.secret).toString("hex")),
285
- getTxBlock: () => Promise.resolve({
286
- blockHeight: tx.slot,
287
- blockTime: tx.blockTime
288
- })
289
- }
290
- }
291
- if(event.name==="RefundEvent") {
292
- const paymentHash = Buffer.from(event.data.hash).toString("hex");
293
- if(paymentHash!==data.paymentHash) return null;
294
- if(!event.data.sequence.eq(data.sequence)) return null;
295
- return {
296
- type: isExpired ? SwapCommitStateType.EXPIRED : SwapCommitStateType.NOT_COMMITED,
297
- getRefundTxId: () => Promise.resolve(tx.transaction.signatures[0]),
298
- getTxBlock: () => Promise.resolve({
299
- blockHeight: tx.slot,
300
- blockTime: tx.blockTime
301
- })
302
- };
303
- }
304
- });
305
- if(status!=null) return status;
306
-
307
- if(isExpired) return {type: SwapCommitStateType.EXPIRED};
308
- return {type: SwapCommitStateType.NOT_COMMITED};
309
- }
310
-
311
- async getCommitStatuses(request: { signer: string; swapData: SolanaSwapData }[]): Promise<{
312
- [p: string]: SwapCommitState
313
- }> {
314
- const result: {
315
- [p: string]: SwapCommitState
316
- } = {};
317
- let promises: Promise<void>[] = [];
318
- for(let {signer, swapData} of request) {
319
- promises.push(this.getCommitStatus(signer, swapData).then(val => {
320
- result[swapData.getEscrowHash()] = val;
321
- }));
322
- if(promises.length>=MAX_PARALLEL_COMMIT_STATUS_CHECKS) {
323
- await Promise.all(promises);
324
- promises = [];
325
- }
326
- }
327
- await Promise.all(promises);
328
- return result;
329
- }
330
-
331
- /**
332
- * Checks the status of the specific payment hash
333
- *
334
- * @param claimHash
335
- */
336
- async getClaimHashStatus(claimHash: string): Promise<SwapCommitStateType> {
337
- const {paymentHash} = fromClaimHash(claimHash);
338
- const escrowStateKey = this.SwapEscrowState(Buffer.from(paymentHash, "hex"));
339
- const abortController = new AbortController();
340
-
341
- //Start fetching events before checking escrow PDA, this call is used when quoting, so saving 100ms here helps a lot!
342
- const eventsPromise = this.Events.findInEvents(escrowStateKey, async (event) => {
343
- if(event.name==="ClaimEvent") return SwapCommitStateType.PAID;
344
- if(event.name==="RefundEvent") return SwapCommitStateType.NOT_COMMITED;
345
- }, abortController.signal).catch(e => {
346
- abortController.abort(e)
347
- return null;
348
- });
349
-
350
- const escrowState = await this.program.account.escrowState.fetchNullable(escrowStateKey);
351
- abortController.signal.throwIfAborted();
352
- if(escrowState!=null) {
353
- abortController.abort();
354
- return SwapCommitStateType.COMMITED;
355
- }
356
-
357
- //Check if paid or what
358
- const eventsStatus = await eventsPromise;
359
- abortController.signal.throwIfAborted();
360
- if(eventsStatus!=null) return eventsStatus;
361
-
362
- return SwapCommitStateType.NOT_COMMITED;
363
- }
364
-
365
- /**
366
- * Returns the data committed for a specific payment hash, or null if no data is currently commited for
367
- * the specific swap
368
- *
369
- * @param claimHashHex
370
- */
371
- async getCommitedData(claimHashHex: string): Promise<SolanaSwapData> {
372
- const {paymentHash} = fromClaimHash(claimHashHex);
373
- const paymentHashBuffer = Buffer.from(paymentHash, "hex");
374
-
375
- const account: IdlAccounts<SwapProgram>["escrowState"] = await this.program.account.escrowState.fetchNullable(this.SwapEscrowState(paymentHashBuffer));
376
- if(account==null) return null;
377
-
378
- return SolanaSwapData.fromEscrowState(account);
379
- }
380
-
381
- async getHistoricalSwaps(signer: string, startBlockheight?: number): Promise<{
382
- swaps: {
383
- [escrowHash: string]: {
384
- init?: {
385
- data: SolanaSwapData,
386
- getInitTxId: () => Promise<string>,
387
- getTxBlock: () => Promise<{
388
- blockTime: number,
389
- blockHeight: number
390
- }>
391
- },
392
- state: SwapCommitState
393
- }
394
- },
395
- latestBlockheight: number
396
- }> {
397
- let latestBlockheight: number;
398
-
399
- const events: {event: ProgramEvent<SwapProgram>, tx: ParsedTransactionWithMeta}[] = [];
400
-
401
- await this.Events.findInEvents(new PublicKey(signer), async (event, tx) => {
402
- if(latestBlockheight==null) latestBlockheight = tx.slot;
403
- events.push({event, tx});
404
- }, undefined, undefined, startBlockheight);
405
-
406
- this.logger.debug(`getHistoricalSwaps(): Found ${events.length} atomiq related events!`);
407
-
408
- const swapsOpened: {[escrowHash: string]: {
409
- data: SolanaSwapData,
410
- getInitTxId: () => Promise<string>,
411
- getTxBlock: () => Promise<{
412
- blockTime: number,
413
- blockHeight: number
414
- }>
415
- }} = {};
416
- const resultingSwaps: {
417
- [escrowHash: string]: {
418
- init?: {
419
- data: SolanaSwapData,
420
- getInitTxId: () => Promise<string>,
421
- getTxBlock: () => Promise<{
422
- blockTime: number,
423
- blockHeight: number
424
- }>
425
- },
426
- state: SwapCommitState
427
- }
428
- } = {};
429
-
430
- events.reverse();
431
- for(let {event, tx} of events) {
432
- const txSignature = tx.transaction.signatures[0];
433
- const paymentHash: string = Buffer.from(event.data.hash).toString("hex");
434
- const escrowHash = toEscrowHash(paymentHash, event.data.sequence);
435
-
436
- if(event.name==="InitializeEvent") {
437
- //Parse swap data from initialize event
438
- const txoHash: string = Buffer.from(event.data.txoHash).toString("hex");
439
- const instructions = this.Events.decodeInstructions(tx.transaction.message);
440
- if(instructions == null) {
441
- this.logger.warn(`getHistoricalSwaps(): Skipping tx ${txSignature} because cannot parse instructions!`);
442
- continue;
443
- }
444
-
445
- const initIx = instructions.find(
446
- ix => ix!=null && (ix.name === "offererInitializePayIn" || ix.name === "offererInitialize")
447
- ) as InitInstruction;
448
- if(initIx == null) {
449
- this.logger.warn(`getHistoricalSwaps(): Skipping tx ${txSignature} because init instruction not found!`);
450
- continue;
451
- }
452
-
453
- swapsOpened[escrowHash] = {
454
- data: SolanaSwapData.fromInstruction(initIx, txoHash),
455
- getInitTxId: () => Promise.resolve(txSignature),
456
- getTxBlock: () => Promise.resolve({
457
- blockHeight: tx.slot,
458
- blockTime: tx.blockTime
459
- })
460
- };
461
- }
462
-
463
- if(event.name==="ClaimEvent") {
464
- const foundSwapData = swapsOpened[escrowHash];
465
- delete swapsOpened[escrowHash];
466
- resultingSwaps[escrowHash] = {
467
- init: foundSwapData,
468
- state: {
469
- type: SwapCommitStateType.PAID,
470
- getClaimTxId: () => Promise.resolve(txSignature),
471
- getClaimResult: () => Promise.resolve(Buffer.from(event.data.secret).toString("hex")),
472
- getTxBlock: () => Promise.resolve({
473
- blockHeight: tx.slot,
474
- blockTime: tx.blockTime
475
- })
476
- }
477
- }
478
- }
479
-
480
- if(event.name==="RefundEvent") {
481
- const foundSwapData = swapsOpened[escrowHash];
482
- delete swapsOpened[escrowHash];
483
- const isExpired = foundSwapData!=null && await this.isExpired(signer, foundSwapData.data);
484
- resultingSwaps[escrowHash] = {
485
- init: foundSwapData,
486
- state: {
487
- type: isExpired ? SwapCommitStateType.EXPIRED : SwapCommitStateType.NOT_COMMITED,
488
- getRefundTxId: () => Promise.resolve(txSignature),
489
- getTxBlock: () => Promise.resolve({
490
- blockHeight: tx.slot,
491
- blockTime: tx.blockTime
492
- })
493
- }
494
- }
495
- }
496
- }
497
-
498
- this.logger.debug(`getHistoricalSwaps(): Found ${Object.keys(resultingSwaps).length} settled swaps!`);
499
- this.logger.debug(`getHistoricalSwaps(): Found ${Object.keys(swapsOpened).length} unsettled swaps!`);
500
-
501
- for(let escrowHash in swapsOpened) {
502
- const foundSwapData = swapsOpened[escrowHash];
503
- const isExpired = await this.isExpired(signer, foundSwapData.data);
504
- resultingSwaps[escrowHash] = {
505
- init: foundSwapData,
506
- state: foundSwapData.data.isOfferer(signer) && isExpired
507
- ? {type: SwapCommitStateType.REFUNDABLE}
508
- : {type: SwapCommitStateType.COMMITED}
509
- }
510
- }
511
-
512
- return {
513
- swaps: resultingSwaps,
514
- latestBlockheight
515
- }
516
- }
517
-
518
- ////////////////////////////////////////////
519
- //// Swap data initializer
520
- createSwapData(
521
- type: ChainSwapType,
522
- offerer: string,
523
- claimer: string,
524
- token: string,
525
- amount: bigint,
526
- claimHash: string,
527
- sequence: bigint,
528
- expiry: bigint,
529
- payIn: boolean,
530
- payOut: boolean,
531
- securityDeposit: bigint,
532
- claimerBounty: bigint,
533
- depositToken?: string
534
- ): Promise<SolanaSwapData> {
535
- if(depositToken!=null) {
536
- if(!new PublicKey(depositToken).equals(SolanaTokens.WSOL_ADDRESS)) throw new Error("Only SOL supported as deposit token!");
537
- }
538
- const tokenAddr: PublicKey = new PublicKey(token);
539
- const offererKey = offerer==null ? null : new PublicKey(offerer);
540
- const claimerKey = claimer==null ? null : new PublicKey(claimer);
541
- const {paymentHash, nonce, confirmations} = fromClaimHash(claimHash);
542
- return Promise.resolve(new SolanaSwapData(
543
- offererKey,
544
- claimerKey,
545
- tokenAddr,
546
- toBN(amount),
547
- paymentHash,
548
- toBN(sequence),
549
- toBN(expiry),
550
- nonce,
551
- confirmations,
552
- payOut,
553
- type==null ? null : SolanaSwapData.typeToKind(type),
554
- payIn,
555
- offererKey==null ? null : payIn ? getAssociatedTokenAddressSync(tokenAddr, offererKey) : PublicKey.default,
556
- claimerKey==null ? null : payOut ? getAssociatedTokenAddressSync(tokenAddr, claimerKey) : PublicKey.default,
557
- toBN(securityDeposit),
558
- toBN(claimerBounty),
559
- null
560
- ));
561
- }
562
-
563
- ////////////////////////////////////////////
564
- //// Utils
565
- async getBalance(signer: string, tokenAddress: string, inContract: boolean): Promise<bigint> {
566
- if(!inContract) {
567
- return await this.Chain.getBalance(signer, tokenAddress);
568
- }
569
-
570
- const token = new PublicKey(tokenAddress);
571
- const publicKey = new PublicKey(signer);
572
-
573
- return await this.getIntermediaryBalance(publicKey, token);
574
- }
575
-
576
- getIntermediaryData(address: string, token: string): Promise<{
577
- balance: bigint,
578
- reputation: IntermediaryReputationType
579
- }> {
580
- return this.LpVault.getIntermediaryData(new PublicKey(address), new PublicKey(token));
581
- }
582
-
583
- getIntermediaryReputation(address: string, token: string): Promise<IntermediaryReputationType> {
584
- return this.LpVault.getIntermediaryReputation(new PublicKey(address), new PublicKey(token));
585
- }
586
-
587
- getIntermediaryBalance(address: PublicKey, token: PublicKey): Promise<bigint> {
588
- return this.LpVault.getIntermediaryBalance(address, token);
589
- }
590
-
591
- ////////////////////////////////////////////
592
- //// Transaction initializers
593
- async txsClaimWithSecret(
594
- signer: string | SolanaSigner,
595
- swapData: SolanaSwapData,
596
- secret: string,
597
- checkExpiry?: boolean,
598
- initAta?: boolean,
599
- feeRate?: string,
600
- skipAtaCheck?: boolean
601
- ): Promise<SolanaTx[]> {
602
- return this.Claim.txsClaimWithSecret(typeof(signer)==="string" ? new PublicKey(signer) : signer.getPublicKey(), swapData, secret, checkExpiry, initAta, feeRate, skipAtaCheck)
603
- }
604
-
605
- async txsClaimWithTxData(
606
- signer: string | SolanaSigner,
607
- swapData: SolanaSwapData,
608
- tx: { blockhash: string, confirmations: number, txid: string, hex: string, height: number },
609
- requiredConfirmations: number,
610
- vout: number,
611
- commitedHeader?: SolanaBtcStoredHeader,
612
- synchronizer?: RelaySynchronizer<any, SolanaTx, any>,
613
- initAta?: boolean,
614
- feeRate?: string,
615
- storageAccHolder?: {storageAcc: PublicKey}
616
- ): Promise<SolanaTx[] | null> {
617
- if(swapData.confirmations!==requiredConfirmations) throw new Error("Invalid requiredConfirmations provided!");
618
- return this.Claim.txsClaimWithTxData(typeof(signer)==="string" ? new PublicKey(signer) : signer, swapData, tx, vout, commitedHeader, synchronizer, initAta, storageAccHolder, feeRate);
619
- }
620
-
621
- txsRefund(signer: string, swapData: SolanaSwapData, check?: boolean, initAta?: boolean, feeRate?: string): Promise<SolanaTx[]> {
622
- if(!swapData.isOfferer(signer)) throw new Error("Only offerer can refund on Solana");
623
- return this.Refund.txsRefund(swapData, check, initAta, feeRate);
624
- }
625
-
626
- txsRefundWithAuthorization(signer: string, swapData: SolanaSwapData, {timeout, prefix, signature}, check?: boolean, initAta?: boolean, feeRate?: string): Promise<SolanaTx[]> {
627
- if(!swapData.isOfferer(signer)) throw new Error("Only offerer can refund on Solana");
628
- return this.Refund.txsRefundWithAuthorization(swapData,timeout,prefix,signature,check,initAta,feeRate);
629
- }
630
-
631
- txsInit(sender: string, swapData: SolanaSwapData, {timeout, prefix, signature}, skipChecks?: boolean, feeRate?: string): Promise<SolanaTx[]> {
632
- if(swapData.isPayIn()) {
633
- if(!swapData.isOfferer(sender)) throw new Error("Only offerer can create payIn=true swap");
634
- return this.Init.txsInitPayIn(swapData, timeout, prefix, signature, skipChecks, feeRate);
635
- } else {
636
- if(!swapData.isClaimer(sender)) throw new Error("Only claimer can create payIn=false swap");
637
- return this.Init.txsInit(swapData, timeout, prefix, signature, skipChecks, feeRate);
638
- }
639
- }
640
-
641
- txsWithdraw(signer: string, token: string, amount: bigint, feeRate?: string): Promise<SolanaTx[]> {
642
- return this.LpVault.txsWithdraw(new PublicKey(signer), new PublicKey(token), amount, feeRate);
643
- }
644
-
645
- txsDeposit(signer: string, token: string, amount: bigint, feeRate?: string): Promise<SolanaTx[]> {
646
- return this.LpVault.txsDeposit(new PublicKey(signer), new PublicKey(token), amount, feeRate);
647
- }
648
-
649
- ////////////////////////////////////////////
650
- //// Executors
651
- async claimWithSecret(
652
- signer: SolanaSigner,
653
- swapData: SolanaSwapData,
654
- secret: string,
655
- checkExpiry?: boolean,
656
- initAta?: boolean,
657
- txOptions?: TransactionConfirmationOptions
658
- ): Promise<string> {
659
- const result = await this.Claim.txsClaimWithSecret(signer.getPublicKey(), swapData, secret, checkExpiry, initAta, txOptions?.feeRate);
660
- const [signature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
661
- return signature;
662
- }
663
-
664
- async claimWithTxData(
665
- signer: SolanaSigner,
666
- swapData: SolanaSwapData,
667
- tx: { blockhash: string, confirmations: number, txid: string, hex: string, height: number },
668
- requiredConfirmations: number,
669
- vout: number,
670
- commitedHeader?: SolanaBtcStoredHeader,
671
- synchronizer?: RelaySynchronizer<any, SolanaTx, any>,
672
- initAta?: boolean,
673
- txOptions?: TransactionConfirmationOptions
674
- ): Promise<string> {
675
- if(requiredConfirmations!==swapData.confirmations) throw new Error("Invalid requiredConfirmations provided!");
676
-
677
- const data: {storageAcc: PublicKey} = {
678
- storageAcc: null
679
- };
680
-
681
- const txs = await this.Claim.txsClaimWithTxData(
682
- signer, swapData, tx, vout,
683
- commitedHeader, synchronizer, initAta, data, txOptions?.feeRate
684
- );
685
- if(txs===null) throw new Error("Btc relay not synchronized to required blockheight!");
686
-
687
- //TODO: This doesn't return proper tx signature
688
- const [signature] = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal);
689
- await this.DataAccount.removeDataAccount(data.storageAcc);
690
-
691
- return signature;
692
- }
693
-
694
- async refund(
695
- signer: SolanaSigner,
696
- swapData: SolanaSwapData,
697
- check?: boolean,
698
- initAta?: boolean,
699
- txOptions?: TransactionConfirmationOptions
700
- ): Promise<string> {
701
- let result = await this.txsRefund(signer.getAddress(), swapData, check, initAta, txOptions?.feeRate);
702
-
703
- const [signature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
704
-
705
- return signature;
706
- }
707
-
708
- async refundWithAuthorization(
709
- signer: SolanaSigner,
710
- swapData: SolanaSwapData,
711
- signature: SignatureData,
712
- check?: boolean,
713
- initAta?: boolean,
714
- txOptions?: TransactionConfirmationOptions
715
- ): Promise<string> {
716
- let result = await this.txsRefundWithAuthorization(signer.getAddress(), swapData, signature, check, initAta, txOptions?.feeRate);
717
-
718
- const [txSignature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
719
-
720
- return txSignature;
721
- }
722
-
723
- async init(
724
- signer: SolanaSigner,
725
- swapData: SolanaSwapData,
726
- signature: SignatureData,
727
- skipChecks?: boolean,
728
- txOptions?: TransactionConfirmationOptions
729
- ): Promise<string> {
730
- if(swapData.isPayIn()) {
731
- if(!signer.getPublicKey().equals(swapData.offerer)) throw new Error("Invalid signer provided!");
732
- } else {
733
- if(!signer.getPublicKey().equals(swapData.claimer)) throw new Error("Invalid signer provided!");
734
- }
735
-
736
- const result = await this.txsInit(signer.getAddress(), swapData, signature, skipChecks, txOptions?.feeRate);
737
-
738
- const [txSignature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
739
-
740
- return txSignature;
741
- }
742
-
743
- async initAndClaimWithSecret(
744
- signer: SolanaSigner,
745
- swapData: SolanaSwapData,
746
- signature: SignatureData,
747
- secret: string,
748
- skipChecks?: boolean,
749
- txOptions?: TransactionConfirmationOptions
750
- ): Promise<string[]> {
751
- if(!signer.getPublicKey().equals(swapData.claimer)) throw new Error("Invalid signer provided!");
752
-
753
- const txsCommit = await this.txsInit(signer.getAddress(), swapData, signature, skipChecks, txOptions?.feeRate);
754
- const txsClaim = await this.Claim.txsClaimWithSecret(signer.getPublicKey(), swapData, secret, true, false, txOptions?.feeRate, true);
755
-
756
- return await this.Chain.sendAndConfirm(signer, txsCommit.concat(txsClaim), txOptions?.waitForConfirmation, txOptions?.abortSignal);
757
- }
758
-
759
- async withdraw(
760
- signer: SolanaSigner,
761
- token: string,
762
- amount: bigint,
763
- txOptions?: TransactionConfirmationOptions
764
- ): Promise<string> {
765
- const txs = await this.LpVault.txsWithdraw(signer.getPublicKey(), new PublicKey(token), amount, txOptions?.feeRate);
766
- const [txId] = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
767
- return txId;
768
- }
769
-
770
- async deposit(
771
- signer: SolanaSigner,
772
- token: string,
773
- amount: bigint,
774
- txOptions?: TransactionConfirmationOptions
775
- ): Promise<string> {
776
- const txs = await this.LpVault.txsDeposit(signer.getPublicKey(), new PublicKey(token), amount, txOptions?.feeRate);
777
- const [txId] = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
778
- return txId;
779
- }
780
-
781
- ////////////////////////////////////////////
782
- //// Fees
783
- getInitPayInFeeRate(offerer?: string, claimer?: string, token?: string, claimHash?: string): Promise<string> {
784
- const paymentHash = claimHash==null ? null : fromClaimHash(claimHash).paymentHash;
785
- return this.Init.getInitPayInFeeRate(
786
- toPublicKeyOrNull(offerer),
787
- toPublicKeyOrNull(claimer),
788
- toPublicKeyOrNull(token),
789
- paymentHash
790
- );
791
- }
792
-
793
- getInitFeeRate(offerer?: string, claimer?: string, token?: string, claimHash?: string): Promise<string> {
794
- const paymentHash = claimHash==null ? null : fromClaimHash(claimHash).paymentHash;
795
- return this.Init.getInitFeeRate(
796
- toPublicKeyOrNull(offerer),
797
- toPublicKeyOrNull(claimer),
798
- toPublicKeyOrNull(token),
799
- paymentHash
800
- );
801
- }
802
-
803
- getRefundFeeRate(swapData: SolanaSwapData): Promise<string> {
804
- return this.Refund.getRefundFeeRate(swapData);
805
- }
806
-
807
- getClaimFeeRate(signer: string, swapData: SolanaSwapData): Promise<string> {
808
- return this.Claim.getClaimFeeRate(new PublicKey(signer), swapData);
809
- }
810
-
811
- getClaimFee(signer: string, swapData: SolanaSwapData, feeRate?: string): Promise<bigint> {
812
- return this.Claim.getClaimFee(new PublicKey(signer), swapData, feeRate);
813
- }
814
-
815
- getRawClaimFee(signer: string, swapData: SolanaSwapData, feeRate?: string): Promise<bigint> {
816
- return this.Claim.getRawClaimFee(new PublicKey(signer), swapData, feeRate);
817
- }
818
-
819
- /**
820
- * Get the estimated solana fee of the commit transaction
821
- */
822
- getCommitFee(signer: string, swapData: SolanaSwapData, feeRate?: string): Promise<bigint> {
823
- return this.Init.getInitFee(swapData, feeRate);
824
- }
825
-
826
- /**
827
- * Get the estimated solana fee of the commit transaction, without any deposits
828
- */
829
- getRawCommitFee(signer: string, swapData: SolanaSwapData, feeRate?: string): Promise<bigint> {
830
- return this.Init.getRawInitFee(swapData, feeRate);
831
- }
832
-
833
- /**
834
- * Get the estimated solana transaction fee of the refund transaction
835
- */
836
- getRefundFee(signer: string, swapData: SolanaSwapData, feeRate?: string): Promise<bigint> {
837
- return this.Refund.getRefundFee(swapData, feeRate);
838
- }
839
-
840
- /**
841
- * Get the estimated solana transaction fee of the refund transaction
842
- */
843
- getRawRefundFee(signer: string, swapData: SolanaSwapData, feeRate?: string): Promise<bigint> {
844
- return this.Refund.getRawRefundFee(swapData, feeRate);
845
- }
846
-
847
- getExtraData(outputScript: Buffer, amount: bigint, confirmations: number, nonce?: bigint): Buffer {
848
- return Buffer.from(sha256(Buffer.concat([
849
- BigIntBufferUtils.toBuffer(amount, "le", 8),
850
- outputScript
851
- ])));
852
- }
853
-
854
- }
1
+ import {SolanaSwapData, InitInstruction} from "./SolanaSwapData";
2
+ import {IdlAccounts} from "@coral-xyz/anchor";
3
+ import {
4
+ ParsedTransactionWithMeta,
5
+ PublicKey,
6
+ } from "@solana/web3.js";
7
+ import {sha256} from "@noble/hashes/sha2";
8
+ import {SolanaBtcRelay} from "../btcrelay/SolanaBtcRelay";
9
+ import * as programIdl from "./programIdl.json";
10
+ import {
11
+ IStorageManager,
12
+ SwapContract,
13
+ ChainSwapType,
14
+ IntermediaryReputationType,
15
+ TransactionConfirmationOptions,
16
+ SignatureData,
17
+ RelaySynchronizer,
18
+ BigIntBufferUtils,
19
+ SwapCommitState,
20
+ SwapCommitStateType, SwapNotCommitedState, SwapExpiredState, SwapPaidState
21
+ } from "@atomiqlabs/base";
22
+ import {SolanaBtcStoredHeader} from "../btcrelay/headers/SolanaBtcStoredHeader";
23
+ import {
24
+ getAssociatedTokenAddressSync,
25
+ } from "@solana/spl-token";
26
+ import {SwapProgram} from "./programTypes";
27
+ import {SolanaChainInterface} from "../chain/SolanaChainInterface";
28
+ import {SolanaProgramBase} from "../program/SolanaProgramBase";
29
+ import {SolanaTx} from "../chain/modules/SolanaTransactions";
30
+ import {SwapInit, SolanaPreFetchData, SolanaPreFetchVerification} from "./modules/SwapInit";
31
+ import {SolanaDataAccount, StoredDataAccount} from "./modules/SolanaDataAccount";
32
+ import {SwapRefund} from "./modules/SwapRefund";
33
+ import {SwapClaim} from "./modules/SwapClaim";
34
+ import {SolanaLpVault} from "./modules/SolanaLpVault";
35
+ import {Buffer} from "buffer";
36
+ import {SolanaSigner} from "../wallet/SolanaSigner";
37
+ import {fromClaimHash, toBN, toClaimHash, toEscrowHash} from "../../utils/Utils";
38
+ import {SolanaTokens} from "../chain/modules/SolanaTokens";
39
+ import * as BN from "bn.js";
40
+ import {ProgramEvent} from "../program/modules/SolanaProgramEvents";
41
+
42
+ function toPublicKeyOrNull(str: string | null): PublicKey | null {
43
+ return str==null ? null : new PublicKey(str);
44
+ }
45
+
46
+ const MAX_PARALLEL_COMMIT_STATUS_CHECKS = 5;
47
+
48
+ export class SolanaSwapProgram
49
+ extends SolanaProgramBase<SwapProgram>
50
+ implements SwapContract<
51
+ SolanaSwapData,
52
+ SolanaTx,
53
+ SolanaPreFetchData,
54
+ SolanaPreFetchVerification,
55
+ SolanaSigner,
56
+ "SOLANA"
57
+ > {
58
+
59
+ ////////////////////////
60
+ //// Constants
61
+ public readonly ESCROW_STATE_RENT_EXEMPT = 2658720;
62
+
63
+ ////////////////////////
64
+ //// PDA accessors
65
+ readonly SwapVaultAuthority = this.pda("authority");
66
+ readonly SwapVault = this.pda("vault", (tokenAddress: PublicKey) => [tokenAddress.toBuffer()]);
67
+ readonly SwapUserVault = this.pda("uservault",
68
+ (publicKey: PublicKey, tokenAddress: PublicKey) => [publicKey.toBuffer(), tokenAddress.toBuffer()]
69
+ );
70
+ readonly SwapEscrowState = this.pda("state", (hash: Buffer) => [hash]);
71
+
72
+ ////////////////////////
73
+ //// Timeouts
74
+ readonly chainId: "SOLANA" = "SOLANA";
75
+ readonly claimWithSecretTimeout: number = 45;
76
+ readonly claimWithTxDataTimeout: number = 120;
77
+ readonly refundTimeout: number = 45;
78
+ readonly claimGracePeriod: number = 10*60;
79
+ readonly refundGracePeriod: number = 10*60;
80
+ readonly authGracePeriod: number = 5*60;
81
+
82
+ ////////////////////////
83
+ //// Services
84
+ readonly Init: SwapInit;
85
+ readonly Refund: SwapRefund;
86
+ readonly Claim: SwapClaim;
87
+ readonly DataAccount: SolanaDataAccount;
88
+ readonly LpVault: SolanaLpVault;
89
+
90
+ constructor(
91
+ chainInterface: SolanaChainInterface,
92
+ btcRelay: SolanaBtcRelay<any>,
93
+ storage: IStorageManager<StoredDataAccount>,
94
+ programAddress?: string
95
+ ) {
96
+ super(chainInterface, programIdl, programAddress);
97
+
98
+ this.Init = new SwapInit(chainInterface, this);
99
+ this.Refund = new SwapRefund(chainInterface, this);
100
+ this.Claim = new SwapClaim(chainInterface, this, btcRelay);
101
+ this.DataAccount = new SolanaDataAccount(chainInterface, this, storage);
102
+ this.LpVault = new SolanaLpVault(chainInterface, this);
103
+ }
104
+
105
+ async start(): Promise<void> {
106
+ await this.DataAccount.init();
107
+ }
108
+
109
+ getClaimableDeposits(signer: string): Promise<{count: number, totalValue: bigint}> {
110
+ return this.DataAccount.getDataAccountsInfo(new PublicKey(signer));
111
+ }
112
+
113
+ claimDeposits(signer: SolanaSigner): Promise<{txIds: string[], count: number, totalValue: bigint}> {
114
+ return this.DataAccount.sweepDataAccounts(signer);
115
+ }
116
+
117
+ ////////////////////////////////////////////
118
+ //// Signatures
119
+ preFetchForInitSignatureVerification(data: SolanaPreFetchData): Promise<SolanaPreFetchVerification> {
120
+ return this.Init.preFetchForInitSignatureVerification(data);
121
+ }
122
+
123
+ preFetchBlockDataForSignatures(): Promise<SolanaPreFetchData> {
124
+ return this.Init.preFetchBlockDataForSignatures();
125
+ }
126
+
127
+ getInitSignature(signer: SolanaSigner, swapData: SolanaSwapData, authorizationTimeout: number, preFetchedBlockData?: SolanaPreFetchData, feeRate?: string): Promise<SignatureData> {
128
+ return this.Init.signSwapInitialization(signer, swapData, authorizationTimeout, preFetchedBlockData, feeRate);
129
+ }
130
+
131
+ isValidInitAuthorization(signer: string, swapData: SolanaSwapData, {timeout, prefix, signature}, feeRate?: string, preFetchedData?: SolanaPreFetchVerification): Promise<Buffer> {
132
+ return this.Init.isSignatureValid(signer, swapData, timeout, prefix, signature, feeRate, preFetchedData);
133
+ }
134
+
135
+ getInitAuthorizationExpiry(swapData: SolanaSwapData, {timeout, prefix, signature}, preFetchedData?: SolanaPreFetchVerification): Promise<number> {
136
+ return this.Init.getSignatureExpiry(timeout, signature, preFetchedData);
137
+ }
138
+
139
+ isInitAuthorizationExpired(swapData: SolanaSwapData, {timeout, prefix, signature}): Promise<boolean> {
140
+ return this.Init.isSignatureExpired(signature, timeout);
141
+ }
142
+
143
+ getRefundSignature(signer: SolanaSigner, swapData: SolanaSwapData, authorizationTimeout: number): Promise<SignatureData> {
144
+ return this.Refund.signSwapRefund(signer, swapData, authorizationTimeout);
145
+ }
146
+
147
+ isValidRefundAuthorization(swapData: SolanaSwapData, {timeout, prefix, signature}): Promise<Buffer> {
148
+ return this.Refund.isSignatureValid(swapData, timeout, prefix, signature);
149
+ }
150
+
151
+ getDataSignature(signer: SolanaSigner, data: Buffer): Promise<string> {
152
+ return this.Chain.Signatures.getDataSignature(signer, data);
153
+ }
154
+
155
+ isValidDataSignature(data: Buffer, signature: string, publicKey: string): Promise<boolean> {
156
+ return this.Chain.Signatures.isValidDataSignature(data, signature, publicKey);
157
+ }
158
+
159
+ ////////////////////////////////////////////
160
+ //// Swap data utils
161
+ /**
162
+ * Checks whether the claim is claimable by us, that means not expired, we are claimer & the swap is commited
163
+ *
164
+ * @param signer
165
+ * @param data
166
+ */
167
+ async isClaimable(signer: string, data: SolanaSwapData): Promise<boolean> {
168
+ if(!data.isClaimer(signer)) return false;
169
+ if(await this.isExpired(signer, data)) return false;
170
+ return await this.isCommited(data);
171
+ }
172
+
173
+ /**
174
+ * Checks whether a swap is commited, i.e. the swap still exists on-chain and was not claimed nor refunded
175
+ *
176
+ * @param swapData
177
+ */
178
+ async isCommited(swapData: SolanaSwapData): Promise<boolean> {
179
+ const paymentHash = Buffer.from(swapData.paymentHash, "hex");
180
+
181
+ const account: IdlAccounts<SwapProgram>["escrowState"] = await this.program.account.escrowState.fetchNullable(this.SwapEscrowState(paymentHash));
182
+ if(account==null) return false;
183
+
184
+ return swapData.correctPDA(account);
185
+ }
186
+
187
+ /**
188
+ * Checks whether the swap is expired, takes into consideration possible on-chain time skew, therefore for claimer
189
+ * the swap expires a bit sooner than it should've & for the offerer it expires a bit later
190
+ *
191
+ * @param signer
192
+ * @param data
193
+ */
194
+ isExpired(signer: string, data: SolanaSwapData): Promise<boolean> {
195
+ let currentTimestamp: BN = new BN(Math.floor(Date.now()/1000));
196
+ if(data.isClaimer(signer)) currentTimestamp = currentTimestamp.add(new BN(this.claimGracePeriod));
197
+ if(data.isOfferer(signer)) currentTimestamp = currentTimestamp.sub(new BN(this.refundGracePeriod));
198
+ return Promise.resolve(data.expiry.lt(currentTimestamp));
199
+ }
200
+
201
+ /**
202
+ * Checks if the swap is refundable by us, checks if we are offerer, if the swap is already expired & if the swap
203
+ * is still commited
204
+ *
205
+ * @param signer
206
+ * @param data
207
+ */
208
+ async isRequestRefundable(signer: string, data: SolanaSwapData): Promise<boolean> {
209
+ //Swap can only be refunded by the offerer
210
+ if(!data.isOfferer(signer)) return false;
211
+ if(!(await this.isExpired(signer, data))) return false;
212
+ return await this.isCommited(data);
213
+ }
214
+
215
+ /**
216
+ * Get the swap payment hash to be used for an on-chain swap, this just uses a sha256 hash of the values
217
+ *
218
+ * @param outputScript output script required to claim the swap
219
+ * @param amount sats sent required to claim the swap
220
+ * @param confirmations
221
+ * @param nonce swap nonce uniquely identifying the transaction to prevent replay attacks
222
+ */
223
+ getHashForOnchain(outputScript: Buffer, amount: bigint, confirmations: number, nonce?: bigint): Buffer {
224
+ nonce ??= 0n;
225
+ const paymentHash = Buffer.from(sha256(Buffer.concat([
226
+ BigIntBufferUtils.toBuffer(nonce, "le", 8),
227
+ BigIntBufferUtils.toBuffer(amount, "le", 8),
228
+ outputScript
229
+ ]))).toString("hex");
230
+ return Buffer.from(toClaimHash(paymentHash, nonce, confirmations), "hex");
231
+ }
232
+
233
+ getHashForHtlc(swapHash: Buffer): Buffer {
234
+ return Buffer.from(toClaimHash(
235
+ swapHash.toString("hex"),
236
+ 0n,
237
+ 0
238
+ ), "hex");
239
+ }
240
+
241
+ getHashForTxId(txId: string, confirmations: number): Buffer {
242
+ return Buffer.from(toClaimHash(
243
+ Buffer.from(txId, "hex").reverse().toString("hex"),
244
+ 0n,
245
+ confirmations
246
+ ), "hex");
247
+ }
248
+
249
+ ////////////////////////////////////////////
250
+ //// Swap data getters
251
+ /**
252
+ * Gets the status of the specific swap, this also checks if we are offerer/claimer & checks for expiry (to see
253
+ * if swap is refundable)
254
+ *
255
+ * @param signer
256
+ * @param data
257
+ */
258
+ async getCommitStatus(signer: string, data: SolanaSwapData): Promise<SwapCommitState> {
259
+ const escrowStateKey = this.SwapEscrowState(Buffer.from(data.paymentHash, "hex"));
260
+ const [escrowState, isExpired] = await Promise.all([
261
+ this.program.account.escrowState.fetchNullable(escrowStateKey) as Promise<IdlAccounts<SwapProgram>["escrowState"]>,
262
+ this.isExpired(signer,data)
263
+ ]);
264
+
265
+ if(escrowState!=null) {
266
+ if(data.correctPDA(escrowState)) {
267
+ if(data.isOfferer(signer) && isExpired) return {type: SwapCommitStateType.REFUNDABLE};
268
+ return {type: SwapCommitStateType.COMMITED};
269
+ }
270
+
271
+ if(data.isOfferer(signer) && isExpired) return {type: SwapCommitStateType.EXPIRED};
272
+ return {type: SwapCommitStateType.NOT_COMMITED};
273
+ }
274
+
275
+ //Check if paid or what
276
+ const status: SwapNotCommitedState | SwapExpiredState | SwapPaidState = await this.Events.findInEvents(escrowStateKey, async (event, tx) => {
277
+ if(event.name==="ClaimEvent") {
278
+ const paymentHash = Buffer.from(event.data.hash).toString("hex");
279
+ if(paymentHash!==data.paymentHash) return null;
280
+ if(!event.data.sequence.eq(data.sequence)) return null;
281
+ return {
282
+ type: SwapCommitStateType.PAID,
283
+ getClaimTxId: () => Promise.resolve(tx.transaction.signatures[0]),
284
+ getClaimResult: () => Promise.resolve(Buffer.from(event.data.secret).toString("hex")),
285
+ getTxBlock: () => Promise.resolve({
286
+ blockHeight: tx.slot,
287
+ blockTime: tx.blockTime
288
+ })
289
+ }
290
+ }
291
+ if(event.name==="RefundEvent") {
292
+ const paymentHash = Buffer.from(event.data.hash).toString("hex");
293
+ if(paymentHash!==data.paymentHash) return null;
294
+ if(!event.data.sequence.eq(data.sequence)) return null;
295
+ return {
296
+ type: isExpired ? SwapCommitStateType.EXPIRED : SwapCommitStateType.NOT_COMMITED,
297
+ getRefundTxId: () => Promise.resolve(tx.transaction.signatures[0]),
298
+ getTxBlock: () => Promise.resolve({
299
+ blockHeight: tx.slot,
300
+ blockTime: tx.blockTime
301
+ })
302
+ };
303
+ }
304
+ });
305
+ if(status!=null) return status;
306
+
307
+ if(isExpired) return {type: SwapCommitStateType.EXPIRED};
308
+ return {type: SwapCommitStateType.NOT_COMMITED};
309
+ }
310
+
311
+ async getCommitStatuses(request: { signer: string; swapData: SolanaSwapData }[]): Promise<{
312
+ [p: string]: SwapCommitState
313
+ }> {
314
+ const result: {
315
+ [p: string]: SwapCommitState
316
+ } = {};
317
+ let promises: Promise<void>[] = [];
318
+ for(let {signer, swapData} of request) {
319
+ promises.push(this.getCommitStatus(signer, swapData).then(val => {
320
+ result[swapData.getEscrowHash()] = val;
321
+ }));
322
+ if(promises.length>=MAX_PARALLEL_COMMIT_STATUS_CHECKS) {
323
+ await Promise.all(promises);
324
+ promises = [];
325
+ }
326
+ }
327
+ await Promise.all(promises);
328
+ return result;
329
+ }
330
+
331
+ /**
332
+ * Checks the status of the specific payment hash
333
+ *
334
+ * @param claimHash
335
+ */
336
+ async getClaimHashStatus(claimHash: string): Promise<SwapCommitStateType> {
337
+ const {paymentHash} = fromClaimHash(claimHash);
338
+ const escrowStateKey = this.SwapEscrowState(Buffer.from(paymentHash, "hex"));
339
+ const abortController = new AbortController();
340
+
341
+ //Start fetching events before checking escrow PDA, this call is used when quoting, so saving 100ms here helps a lot!
342
+ const eventsPromise = this.Events.findInEvents(escrowStateKey, async (event) => {
343
+ if(event.name==="ClaimEvent") return SwapCommitStateType.PAID;
344
+ if(event.name==="RefundEvent") return SwapCommitStateType.NOT_COMMITED;
345
+ }, abortController.signal).catch(e => {
346
+ abortController.abort(e)
347
+ return null;
348
+ });
349
+
350
+ const escrowState = await this.program.account.escrowState.fetchNullable(escrowStateKey);
351
+ abortController.signal.throwIfAborted();
352
+ if(escrowState!=null) {
353
+ abortController.abort();
354
+ return SwapCommitStateType.COMMITED;
355
+ }
356
+
357
+ //Check if paid or what
358
+ const eventsStatus = await eventsPromise;
359
+ abortController.signal.throwIfAborted();
360
+ if(eventsStatus!=null) return eventsStatus;
361
+
362
+ return SwapCommitStateType.NOT_COMMITED;
363
+ }
364
+
365
+ /**
366
+ * Returns the data committed for a specific payment hash, or null if no data is currently commited for
367
+ * the specific swap
368
+ *
369
+ * @param claimHashHex
370
+ */
371
+ async getCommitedData(claimHashHex: string): Promise<SolanaSwapData> {
372
+ const {paymentHash} = fromClaimHash(claimHashHex);
373
+ const paymentHashBuffer = Buffer.from(paymentHash, "hex");
374
+
375
+ const account: IdlAccounts<SwapProgram>["escrowState"] = await this.program.account.escrowState.fetchNullable(this.SwapEscrowState(paymentHashBuffer));
376
+ if(account==null) return null;
377
+
378
+ return SolanaSwapData.fromEscrowState(account);
379
+ }
380
+
381
+ async getHistoricalSwaps(signer: string, startBlockheight?: number): Promise<{
382
+ swaps: {
383
+ [escrowHash: string]: {
384
+ init?: {
385
+ data: SolanaSwapData,
386
+ getInitTxId: () => Promise<string>,
387
+ getTxBlock: () => Promise<{
388
+ blockTime: number,
389
+ blockHeight: number
390
+ }>
391
+ },
392
+ state: SwapCommitState
393
+ }
394
+ },
395
+ latestBlockheight: number
396
+ }> {
397
+ let latestBlockheight: number;
398
+
399
+ const events: {event: ProgramEvent<SwapProgram>, tx: ParsedTransactionWithMeta}[] = [];
400
+
401
+ await this.Events.findInEvents(new PublicKey(signer), async (event, tx) => {
402
+ if(latestBlockheight==null) latestBlockheight = tx.slot;
403
+ events.push({event, tx});
404
+ }, undefined, undefined, startBlockheight);
405
+
406
+ this.logger.debug(`getHistoricalSwaps(): Found ${events.length} atomiq related events!`);
407
+
408
+ const swapsOpened: {[escrowHash: string]: {
409
+ data: SolanaSwapData,
410
+ getInitTxId: () => Promise<string>,
411
+ getTxBlock: () => Promise<{
412
+ blockTime: number,
413
+ blockHeight: number
414
+ }>
415
+ }} = {};
416
+ const resultingSwaps: {
417
+ [escrowHash: string]: {
418
+ init?: {
419
+ data: SolanaSwapData,
420
+ getInitTxId: () => Promise<string>,
421
+ getTxBlock: () => Promise<{
422
+ blockTime: number,
423
+ blockHeight: number
424
+ }>
425
+ },
426
+ state: SwapCommitState
427
+ }
428
+ } = {};
429
+
430
+ events.reverse();
431
+ for(let {event, tx} of events) {
432
+ const txSignature = tx.transaction.signatures[0];
433
+ const paymentHash: string = Buffer.from(event.data.hash).toString("hex");
434
+ const escrowHash = toEscrowHash(paymentHash, event.data.sequence);
435
+
436
+ if(event.name==="InitializeEvent") {
437
+ //Parse swap data from initialize event
438
+ const txoHash: string = Buffer.from(event.data.txoHash).toString("hex");
439
+ const instructions = this.Events.decodeInstructions(tx.transaction.message);
440
+ if(instructions == null) {
441
+ this.logger.warn(`getHistoricalSwaps(): Skipping tx ${txSignature} because cannot parse instructions!`);
442
+ continue;
443
+ }
444
+
445
+ const initIx = instructions.find(
446
+ ix => ix!=null && (ix.name === "offererInitializePayIn" || ix.name === "offererInitialize")
447
+ ) as InitInstruction;
448
+ if(initIx == null) {
449
+ this.logger.warn(`getHistoricalSwaps(): Skipping tx ${txSignature} because init instruction not found!`);
450
+ continue;
451
+ }
452
+
453
+ swapsOpened[escrowHash] = {
454
+ data: SolanaSwapData.fromInstruction(initIx, txoHash),
455
+ getInitTxId: () => Promise.resolve(txSignature),
456
+ getTxBlock: () => Promise.resolve({
457
+ blockHeight: tx.slot,
458
+ blockTime: tx.blockTime
459
+ })
460
+ };
461
+ }
462
+
463
+ if(event.name==="ClaimEvent") {
464
+ const foundSwapData = swapsOpened[escrowHash];
465
+ delete swapsOpened[escrowHash];
466
+ resultingSwaps[escrowHash] = {
467
+ init: foundSwapData,
468
+ state: {
469
+ type: SwapCommitStateType.PAID,
470
+ getClaimTxId: () => Promise.resolve(txSignature),
471
+ getClaimResult: () => Promise.resolve(Buffer.from(event.data.secret).toString("hex")),
472
+ getTxBlock: () => Promise.resolve({
473
+ blockHeight: tx.slot,
474
+ blockTime: tx.blockTime
475
+ })
476
+ }
477
+ }
478
+ }
479
+
480
+ if(event.name==="RefundEvent") {
481
+ const foundSwapData = swapsOpened[escrowHash];
482
+ delete swapsOpened[escrowHash];
483
+ const isExpired = foundSwapData!=null && await this.isExpired(signer, foundSwapData.data);
484
+ resultingSwaps[escrowHash] = {
485
+ init: foundSwapData,
486
+ state: {
487
+ type: isExpired ? SwapCommitStateType.EXPIRED : SwapCommitStateType.NOT_COMMITED,
488
+ getRefundTxId: () => Promise.resolve(txSignature),
489
+ getTxBlock: () => Promise.resolve({
490
+ blockHeight: tx.slot,
491
+ blockTime: tx.blockTime
492
+ })
493
+ }
494
+ }
495
+ }
496
+ }
497
+
498
+ this.logger.debug(`getHistoricalSwaps(): Found ${Object.keys(resultingSwaps).length} settled swaps!`);
499
+ this.logger.debug(`getHistoricalSwaps(): Found ${Object.keys(swapsOpened).length} unsettled swaps!`);
500
+
501
+ for(let escrowHash in swapsOpened) {
502
+ const foundSwapData = swapsOpened[escrowHash];
503
+ const isExpired = await this.isExpired(signer, foundSwapData.data);
504
+ resultingSwaps[escrowHash] = {
505
+ init: foundSwapData,
506
+ state: foundSwapData.data.isOfferer(signer) && isExpired
507
+ ? {type: SwapCommitStateType.REFUNDABLE}
508
+ : {type: SwapCommitStateType.COMMITED}
509
+ }
510
+ }
511
+
512
+ return {
513
+ swaps: resultingSwaps,
514
+ latestBlockheight
515
+ }
516
+ }
517
+
518
+ ////////////////////////////////////////////
519
+ //// Swap data initializer
520
+ createSwapData(
521
+ type: ChainSwapType,
522
+ offerer: string,
523
+ claimer: string,
524
+ token: string,
525
+ amount: bigint,
526
+ claimHash: string,
527
+ sequence: bigint,
528
+ expiry: bigint,
529
+ payIn: boolean,
530
+ payOut: boolean,
531
+ securityDeposit: bigint,
532
+ claimerBounty: bigint,
533
+ depositToken?: string
534
+ ): Promise<SolanaSwapData> {
535
+ if(depositToken!=null) {
536
+ if(!new PublicKey(depositToken).equals(SolanaTokens.WSOL_ADDRESS)) throw new Error("Only SOL supported as deposit token!");
537
+ }
538
+ const tokenAddr: PublicKey = new PublicKey(token);
539
+ const offererKey = offerer==null ? null : new PublicKey(offerer);
540
+ const claimerKey = claimer==null ? null : new PublicKey(claimer);
541
+ const {paymentHash, nonce, confirmations} = fromClaimHash(claimHash);
542
+ return Promise.resolve(new SolanaSwapData(
543
+ offererKey,
544
+ claimerKey,
545
+ tokenAddr,
546
+ toBN(amount),
547
+ paymentHash,
548
+ toBN(sequence),
549
+ toBN(expiry),
550
+ nonce,
551
+ confirmations,
552
+ payOut,
553
+ type==null ? null : SolanaSwapData.typeToKind(type),
554
+ payIn,
555
+ offererKey==null ? null : payIn ? getAssociatedTokenAddressSync(tokenAddr, offererKey) : PublicKey.default,
556
+ claimerKey==null ? null : payOut ? getAssociatedTokenAddressSync(tokenAddr, claimerKey) : PublicKey.default,
557
+ toBN(securityDeposit),
558
+ toBN(claimerBounty),
559
+ null
560
+ ));
561
+ }
562
+
563
+ ////////////////////////////////////////////
564
+ //// Utils
565
+ async getBalance(signer: string, tokenAddress: string, inContract: boolean): Promise<bigint> {
566
+ if(!inContract) {
567
+ return await this.Chain.getBalance(signer, tokenAddress);
568
+ }
569
+
570
+ const token = new PublicKey(tokenAddress);
571
+ const publicKey = new PublicKey(signer);
572
+
573
+ return await this.getIntermediaryBalance(publicKey, token);
574
+ }
575
+
576
+ getIntermediaryData(address: string, token: string): Promise<{
577
+ balance: bigint,
578
+ reputation: IntermediaryReputationType
579
+ }> {
580
+ return this.LpVault.getIntermediaryData(new PublicKey(address), new PublicKey(token));
581
+ }
582
+
583
+ getIntermediaryReputation(address: string, token: string): Promise<IntermediaryReputationType> {
584
+ return this.LpVault.getIntermediaryReputation(new PublicKey(address), new PublicKey(token));
585
+ }
586
+
587
+ getIntermediaryBalance(address: PublicKey, token: PublicKey): Promise<bigint> {
588
+ return this.LpVault.getIntermediaryBalance(address, token);
589
+ }
590
+
591
+ ////////////////////////////////////////////
592
+ //// Transaction initializers
593
+ async txsClaimWithSecret(
594
+ signer: string | SolanaSigner,
595
+ swapData: SolanaSwapData,
596
+ secret: string,
597
+ checkExpiry?: boolean,
598
+ initAta?: boolean,
599
+ feeRate?: string,
600
+ skipAtaCheck?: boolean
601
+ ): Promise<SolanaTx[]> {
602
+ return this.Claim.txsClaimWithSecret(typeof(signer)==="string" ? new PublicKey(signer) : signer.getPublicKey(), swapData, secret, checkExpiry, initAta, feeRate, skipAtaCheck)
603
+ }
604
+
605
+ async txsClaimWithTxData(
606
+ signer: string | SolanaSigner,
607
+ swapData: SolanaSwapData,
608
+ tx: { blockhash: string, confirmations: number, txid: string, hex: string, height: number },
609
+ requiredConfirmations: number,
610
+ vout: number,
611
+ commitedHeader?: SolanaBtcStoredHeader,
612
+ synchronizer?: RelaySynchronizer<any, SolanaTx, any>,
613
+ initAta?: boolean,
614
+ feeRate?: string,
615
+ storageAccHolder?: {storageAcc: PublicKey}
616
+ ): Promise<SolanaTx[] | null> {
617
+ if(swapData.confirmations!==requiredConfirmations) throw new Error("Invalid requiredConfirmations provided!");
618
+ return this.Claim.txsClaimWithTxData(typeof(signer)==="string" ? new PublicKey(signer) : signer, swapData, tx, vout, commitedHeader, synchronizer, initAta, storageAccHolder, feeRate);
619
+ }
620
+
621
+ txsRefund(signer: string, swapData: SolanaSwapData, check?: boolean, initAta?: boolean, feeRate?: string): Promise<SolanaTx[]> {
622
+ if(!swapData.isOfferer(signer)) throw new Error("Only offerer can refund on Solana");
623
+ return this.Refund.txsRefund(swapData, check, initAta, feeRate);
624
+ }
625
+
626
+ txsRefundWithAuthorization(signer: string, swapData: SolanaSwapData, {timeout, prefix, signature}, check?: boolean, initAta?: boolean, feeRate?: string): Promise<SolanaTx[]> {
627
+ if(!swapData.isOfferer(signer)) throw new Error("Only offerer can refund on Solana");
628
+ return this.Refund.txsRefundWithAuthorization(swapData,timeout,prefix,signature,check,initAta,feeRate);
629
+ }
630
+
631
+ txsInit(sender: string, swapData: SolanaSwapData, {timeout, prefix, signature}, skipChecks?: boolean, feeRate?: string): Promise<SolanaTx[]> {
632
+ if(swapData.isPayIn()) {
633
+ if(!swapData.isOfferer(sender)) throw new Error("Only offerer can create payIn=true swap");
634
+ return this.Init.txsInitPayIn(swapData, timeout, prefix, signature, skipChecks, feeRate);
635
+ } else {
636
+ if(!swapData.isClaimer(sender)) throw new Error("Only claimer can create payIn=false swap");
637
+ return this.Init.txsInit(swapData, timeout, prefix, signature, skipChecks, feeRate);
638
+ }
639
+ }
640
+
641
+ txsWithdraw(signer: string, token: string, amount: bigint, feeRate?: string): Promise<SolanaTx[]> {
642
+ return this.LpVault.txsWithdraw(new PublicKey(signer), new PublicKey(token), amount, feeRate);
643
+ }
644
+
645
+ txsDeposit(signer: string, token: string, amount: bigint, feeRate?: string): Promise<SolanaTx[]> {
646
+ return this.LpVault.txsDeposit(new PublicKey(signer), new PublicKey(token), amount, feeRate);
647
+ }
648
+
649
+ ////////////////////////////////////////////
650
+ //// Executors
651
+ async claimWithSecret(
652
+ signer: SolanaSigner,
653
+ swapData: SolanaSwapData,
654
+ secret: string,
655
+ checkExpiry?: boolean,
656
+ initAta?: boolean,
657
+ txOptions?: TransactionConfirmationOptions
658
+ ): Promise<string> {
659
+ const result = await this.Claim.txsClaimWithSecret(signer.getPublicKey(), swapData, secret, checkExpiry, initAta, txOptions?.feeRate);
660
+ const [signature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
661
+ return signature;
662
+ }
663
+
664
+ async claimWithTxData(
665
+ signer: SolanaSigner,
666
+ swapData: SolanaSwapData,
667
+ tx: { blockhash: string, confirmations: number, txid: string, hex: string, height: number },
668
+ requiredConfirmations: number,
669
+ vout: number,
670
+ commitedHeader?: SolanaBtcStoredHeader,
671
+ synchronizer?: RelaySynchronizer<any, SolanaTx, any>,
672
+ initAta?: boolean,
673
+ txOptions?: TransactionConfirmationOptions
674
+ ): Promise<string> {
675
+ if(requiredConfirmations!==swapData.confirmations) throw new Error("Invalid requiredConfirmations provided!");
676
+
677
+ const data: {storageAcc: PublicKey} = {
678
+ storageAcc: null
679
+ };
680
+
681
+ const txs = await this.Claim.txsClaimWithTxData(
682
+ signer, swapData, tx, vout,
683
+ commitedHeader, synchronizer, initAta, data, txOptions?.feeRate
684
+ );
685
+ if(txs===null) throw new Error("Btc relay not synchronized to required blockheight!");
686
+
687
+ //TODO: This doesn't return proper tx signature
688
+ const [signature] = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal);
689
+ await this.DataAccount.removeDataAccount(data.storageAcc);
690
+
691
+ return signature;
692
+ }
693
+
694
+ async refund(
695
+ signer: SolanaSigner,
696
+ swapData: SolanaSwapData,
697
+ check?: boolean,
698
+ initAta?: boolean,
699
+ txOptions?: TransactionConfirmationOptions
700
+ ): Promise<string> {
701
+ let result = await this.txsRefund(signer.getAddress(), swapData, check, initAta, txOptions?.feeRate);
702
+
703
+ const [signature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
704
+
705
+ return signature;
706
+ }
707
+
708
+ async refundWithAuthorization(
709
+ signer: SolanaSigner,
710
+ swapData: SolanaSwapData,
711
+ signature: SignatureData,
712
+ check?: boolean,
713
+ initAta?: boolean,
714
+ txOptions?: TransactionConfirmationOptions
715
+ ): Promise<string> {
716
+ let result = await this.txsRefundWithAuthorization(signer.getAddress(), swapData, signature, check, initAta, txOptions?.feeRate);
717
+
718
+ const [txSignature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
719
+
720
+ return txSignature;
721
+ }
722
+
723
+ async init(
724
+ signer: SolanaSigner,
725
+ swapData: SolanaSwapData,
726
+ signature: SignatureData,
727
+ skipChecks?: boolean,
728
+ txOptions?: TransactionConfirmationOptions
729
+ ): Promise<string> {
730
+ if(swapData.isPayIn()) {
731
+ if(!signer.getPublicKey().equals(swapData.offerer)) throw new Error("Invalid signer provided!");
732
+ } else {
733
+ if(!signer.getPublicKey().equals(swapData.claimer)) throw new Error("Invalid signer provided!");
734
+ }
735
+
736
+ const result = await this.txsInit(signer.getAddress(), swapData, signature, skipChecks, txOptions?.feeRate);
737
+
738
+ const [txSignature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
739
+
740
+ return txSignature;
741
+ }
742
+
743
+ async initAndClaimWithSecret(
744
+ signer: SolanaSigner,
745
+ swapData: SolanaSwapData,
746
+ signature: SignatureData,
747
+ secret: string,
748
+ skipChecks?: boolean,
749
+ txOptions?: TransactionConfirmationOptions
750
+ ): Promise<string[]> {
751
+ if(!signer.getPublicKey().equals(swapData.claimer)) throw new Error("Invalid signer provided!");
752
+
753
+ const txsCommit = await this.txsInit(signer.getAddress(), swapData, signature, skipChecks, txOptions?.feeRate);
754
+ const txsClaim = await this.Claim.txsClaimWithSecret(signer.getPublicKey(), swapData, secret, true, false, txOptions?.feeRate, true);
755
+
756
+ return await this.Chain.sendAndConfirm(signer, txsCommit.concat(txsClaim), txOptions?.waitForConfirmation, txOptions?.abortSignal);
757
+ }
758
+
759
+ async withdraw(
760
+ signer: SolanaSigner,
761
+ token: string,
762
+ amount: bigint,
763
+ txOptions?: TransactionConfirmationOptions
764
+ ): Promise<string> {
765
+ const txs = await this.LpVault.txsWithdraw(signer.getPublicKey(), new PublicKey(token), amount, txOptions?.feeRate);
766
+ const [txId] = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
767
+ return txId;
768
+ }
769
+
770
+ async deposit(
771
+ signer: SolanaSigner,
772
+ token: string,
773
+ amount: bigint,
774
+ txOptions?: TransactionConfirmationOptions
775
+ ): Promise<string> {
776
+ const txs = await this.LpVault.txsDeposit(signer.getPublicKey(), new PublicKey(token), amount, txOptions?.feeRate);
777
+ const [txId] = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
778
+ return txId;
779
+ }
780
+
781
+ ////////////////////////////////////////////
782
+ //// Fees
783
+ getInitPayInFeeRate(offerer?: string, claimer?: string, token?: string, claimHash?: string): Promise<string> {
784
+ const paymentHash = claimHash==null ? null : fromClaimHash(claimHash).paymentHash;
785
+ return this.Init.getInitPayInFeeRate(
786
+ toPublicKeyOrNull(offerer),
787
+ toPublicKeyOrNull(claimer),
788
+ toPublicKeyOrNull(token),
789
+ paymentHash
790
+ );
791
+ }
792
+
793
+ getInitFeeRate(offerer?: string, claimer?: string, token?: string, claimHash?: string): Promise<string> {
794
+ const paymentHash = claimHash==null ? null : fromClaimHash(claimHash).paymentHash;
795
+ return this.Init.getInitFeeRate(
796
+ toPublicKeyOrNull(offerer),
797
+ toPublicKeyOrNull(claimer),
798
+ toPublicKeyOrNull(token),
799
+ paymentHash
800
+ );
801
+ }
802
+
803
+ getRefundFeeRate(swapData: SolanaSwapData): Promise<string> {
804
+ return this.Refund.getRefundFeeRate(swapData);
805
+ }
806
+
807
+ getClaimFeeRate(signer: string, swapData: SolanaSwapData): Promise<string> {
808
+ return this.Claim.getClaimFeeRate(new PublicKey(signer), swapData);
809
+ }
810
+
811
+ getClaimFee(signer: string, swapData: SolanaSwapData, feeRate?: string): Promise<bigint> {
812
+ return this.Claim.getClaimFee(new PublicKey(signer), swapData, feeRate);
813
+ }
814
+
815
+ getRawClaimFee(signer: string, swapData: SolanaSwapData, feeRate?: string): Promise<bigint> {
816
+ return this.Claim.getRawClaimFee(new PublicKey(signer), swapData, feeRate);
817
+ }
818
+
819
+ /**
820
+ * Get the estimated solana fee of the commit transaction
821
+ */
822
+ getCommitFee(signer: string, swapData: SolanaSwapData, feeRate?: string): Promise<bigint> {
823
+ return this.Init.getInitFee(swapData, feeRate);
824
+ }
825
+
826
+ /**
827
+ * Get the estimated solana fee of the commit transaction, without any deposits
828
+ */
829
+ getRawCommitFee(signer: string, swapData: SolanaSwapData, feeRate?: string): Promise<bigint> {
830
+ return this.Init.getRawInitFee(swapData, feeRate);
831
+ }
832
+
833
+ /**
834
+ * Get the estimated solana transaction fee of the refund transaction
835
+ */
836
+ getRefundFee(signer: string, swapData: SolanaSwapData, feeRate?: string): Promise<bigint> {
837
+ return this.Refund.getRefundFee(swapData, feeRate);
838
+ }
839
+
840
+ /**
841
+ * Get the estimated solana transaction fee of the refund transaction
842
+ */
843
+ getRawRefundFee(signer: string, swapData: SolanaSwapData, feeRate?: string): Promise<bigint> {
844
+ return this.Refund.getRawRefundFee(swapData, feeRate);
845
+ }
846
+
847
+ getExtraData(outputScript: Buffer, amount: bigint, confirmations: number, nonce?: bigint): Buffer {
848
+ return Buffer.from(sha256(Buffer.concat([
849
+ BigIntBufferUtils.toBuffer(amount, "le", 8),
850
+ outputScript
851
+ ])));
852
+ }
853
+
854
+ }