@atomiqlabs/chain-solana 12.0.12 → 12.0.13

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 +225 -225
  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 +256 -256
  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,570 +1,570 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SolanaSwapProgram = void 0;
4
- const SolanaSwapData_1 = require("./SolanaSwapData");
5
- const web3_js_1 = require("@solana/web3.js");
6
- const sha2_1 = require("@noble/hashes/sha2");
7
- const programIdl = require("./programIdl.json");
8
- const base_1 = require("@atomiqlabs/base");
9
- const spl_token_1 = require("@solana/spl-token");
10
- const SolanaProgramBase_1 = require("../program/SolanaProgramBase");
11
- const SwapInit_1 = require("./modules/SwapInit");
12
- const SolanaDataAccount_1 = require("./modules/SolanaDataAccount");
13
- const SwapRefund_1 = require("./modules/SwapRefund");
14
- const SwapClaim_1 = require("./modules/SwapClaim");
15
- const SolanaLpVault_1 = require("./modules/SolanaLpVault");
16
- const buffer_1 = require("buffer");
17
- const Utils_1 = require("../../utils/Utils");
18
- const SolanaTokens_1 = require("../chain/modules/SolanaTokens");
19
- const BN = require("bn.js");
20
- function toPublicKeyOrNull(str) {
21
- return str == null ? null : new web3_js_1.PublicKey(str);
22
- }
23
- const MAX_PARALLEL_COMMIT_STATUS_CHECKS = 5;
24
- class SolanaSwapProgram extends SolanaProgramBase_1.SolanaProgramBase {
25
- constructor(chainInterface, btcRelay, storage, programAddress) {
26
- super(chainInterface, programIdl, programAddress);
27
- ////////////////////////
28
- //// Constants
29
- this.ESCROW_STATE_RENT_EXEMPT = 2658720;
30
- ////////////////////////
31
- //// PDA accessors
32
- this.SwapVaultAuthority = this.pda("authority");
33
- this.SwapVault = this.pda("vault", (tokenAddress) => [tokenAddress.toBuffer()]);
34
- this.SwapUserVault = this.pda("uservault", (publicKey, tokenAddress) => [publicKey.toBuffer(), tokenAddress.toBuffer()]);
35
- this.SwapEscrowState = this.pda("state", (hash) => [hash]);
36
- ////////////////////////
37
- //// Timeouts
38
- this.chainId = "SOLANA";
39
- this.claimWithSecretTimeout = 45;
40
- this.claimWithTxDataTimeout = 120;
41
- this.refundTimeout = 45;
42
- this.claimGracePeriod = 10 * 60;
43
- this.refundGracePeriod = 10 * 60;
44
- this.authGracePeriod = 5 * 60;
45
- this.Init = new SwapInit_1.SwapInit(chainInterface, this);
46
- this.Refund = new SwapRefund_1.SwapRefund(chainInterface, this);
47
- this.Claim = new SwapClaim_1.SwapClaim(chainInterface, this, btcRelay);
48
- this.DataAccount = new SolanaDataAccount_1.SolanaDataAccount(chainInterface, this, storage);
49
- this.LpVault = new SolanaLpVault_1.SolanaLpVault(chainInterface, this);
50
- }
51
- async start() {
52
- await this.DataAccount.init();
53
- }
54
- getClaimableDeposits(signer) {
55
- return this.DataAccount.getDataAccountsInfo(new web3_js_1.PublicKey(signer));
56
- }
57
- claimDeposits(signer) {
58
- return this.DataAccount.sweepDataAccounts(signer);
59
- }
60
- ////////////////////////////////////////////
61
- //// Signatures
62
- preFetchForInitSignatureVerification(data) {
63
- return this.Init.preFetchForInitSignatureVerification(data);
64
- }
65
- preFetchBlockDataForSignatures() {
66
- return this.Init.preFetchBlockDataForSignatures();
67
- }
68
- getInitSignature(signer, swapData, authorizationTimeout, preFetchedBlockData, feeRate) {
69
- return this.Init.signSwapInitialization(signer, swapData, authorizationTimeout, preFetchedBlockData, feeRate);
70
- }
71
- isValidInitAuthorization(signer, swapData, { timeout, prefix, signature }, feeRate, preFetchedData) {
72
- return this.Init.isSignatureValid(signer, swapData, timeout, prefix, signature, feeRate, preFetchedData);
73
- }
74
- getInitAuthorizationExpiry(swapData, { timeout, prefix, signature }, preFetchedData) {
75
- return this.Init.getSignatureExpiry(timeout, signature, preFetchedData);
76
- }
77
- isInitAuthorizationExpired(swapData, { timeout, prefix, signature }) {
78
- return this.Init.isSignatureExpired(signature, timeout);
79
- }
80
- getRefundSignature(signer, swapData, authorizationTimeout) {
81
- return this.Refund.signSwapRefund(signer, swapData, authorizationTimeout);
82
- }
83
- isValidRefundAuthorization(swapData, { timeout, prefix, signature }) {
84
- return this.Refund.isSignatureValid(swapData, timeout, prefix, signature);
85
- }
86
- getDataSignature(signer, data) {
87
- return this.Chain.Signatures.getDataSignature(signer, data);
88
- }
89
- isValidDataSignature(data, signature, publicKey) {
90
- return this.Chain.Signatures.isValidDataSignature(data, signature, publicKey);
91
- }
92
- ////////////////////////////////////////////
93
- //// Swap data utils
94
- /**
95
- * Checks whether the claim is claimable by us, that means not expired, we are claimer & the swap is commited
96
- *
97
- * @param signer
98
- * @param data
99
- */
100
- async isClaimable(signer, data) {
101
- if (!data.isClaimer(signer))
102
- return false;
103
- if (await this.isExpired(signer, data))
104
- return false;
105
- return await this.isCommited(data);
106
- }
107
- /**
108
- * Checks whether a swap is commited, i.e. the swap still exists on-chain and was not claimed nor refunded
109
- *
110
- * @param swapData
111
- */
112
- async isCommited(swapData) {
113
- const paymentHash = buffer_1.Buffer.from(swapData.paymentHash, "hex");
114
- const account = await this.program.account.escrowState.fetchNullable(this.SwapEscrowState(paymentHash));
115
- if (account == null)
116
- return false;
117
- return swapData.correctPDA(account);
118
- }
119
- /**
120
- * Checks whether the swap is expired, takes into consideration possible on-chain time skew, therefore for claimer
121
- * the swap expires a bit sooner than it should've & for the offerer it expires a bit later
122
- *
123
- * @param signer
124
- * @param data
125
- */
126
- isExpired(signer, data) {
127
- let currentTimestamp = new BN(Math.floor(Date.now() / 1000));
128
- if (data.isClaimer(signer))
129
- currentTimestamp = currentTimestamp.add(new BN(this.claimGracePeriod));
130
- if (data.isOfferer(signer))
131
- currentTimestamp = currentTimestamp.sub(new BN(this.refundGracePeriod));
132
- return Promise.resolve(data.expiry.lt(currentTimestamp));
133
- }
134
- /**
135
- * Checks if the swap is refundable by us, checks if we are offerer, if the swap is already expired & if the swap
136
- * is still commited
137
- *
138
- * @param signer
139
- * @param data
140
- */
141
- async isRequestRefundable(signer, data) {
142
- //Swap can only be refunded by the offerer
143
- if (!data.isOfferer(signer))
144
- return false;
145
- if (!(await this.isExpired(signer, data)))
146
- return false;
147
- return await this.isCommited(data);
148
- }
149
- /**
150
- * Get the swap payment hash to be used for an on-chain swap, this just uses a sha256 hash of the values
151
- *
152
- * @param outputScript output script required to claim the swap
153
- * @param amount sats sent required to claim the swap
154
- * @param confirmations
155
- * @param nonce swap nonce uniquely identifying the transaction to prevent replay attacks
156
- */
157
- getHashForOnchain(outputScript, amount, confirmations, nonce) {
158
- nonce ?? (nonce = 0n);
159
- const paymentHash = buffer_1.Buffer.from((0, sha2_1.sha256)(buffer_1.Buffer.concat([
160
- base_1.BigIntBufferUtils.toBuffer(nonce, "le", 8),
161
- base_1.BigIntBufferUtils.toBuffer(amount, "le", 8),
162
- outputScript
163
- ]))).toString("hex");
164
- return buffer_1.Buffer.from((0, Utils_1.toClaimHash)(paymentHash, nonce, confirmations), "hex");
165
- }
166
- getHashForHtlc(swapHash) {
167
- return buffer_1.Buffer.from((0, Utils_1.toClaimHash)(swapHash.toString("hex"), 0n, 0), "hex");
168
- }
169
- getHashForTxId(txId, confirmations) {
170
- return buffer_1.Buffer.from((0, Utils_1.toClaimHash)(buffer_1.Buffer.from(txId, "hex").reverse().toString("hex"), 0n, confirmations), "hex");
171
- }
172
- ////////////////////////////////////////////
173
- //// Swap data getters
174
- /**
175
- * Gets the status of the specific swap, this also checks if we are offerer/claimer & checks for expiry (to see
176
- * if swap is refundable)
177
- *
178
- * @param signer
179
- * @param data
180
- */
181
- async getCommitStatus(signer, data) {
182
- const escrowStateKey = this.SwapEscrowState(buffer_1.Buffer.from(data.paymentHash, "hex"));
183
- const [escrowState, isExpired] = await Promise.all([
184
- this.program.account.escrowState.fetchNullable(escrowStateKey),
185
- this.isExpired(signer, data)
186
- ]);
187
- if (escrowState != null) {
188
- if (data.correctPDA(escrowState)) {
189
- if (data.isOfferer(signer) && isExpired)
190
- return { type: base_1.SwapCommitStateType.REFUNDABLE };
191
- return { type: base_1.SwapCommitStateType.COMMITED };
192
- }
193
- if (data.isOfferer(signer) && isExpired)
194
- return { type: base_1.SwapCommitStateType.EXPIRED };
195
- return { type: base_1.SwapCommitStateType.NOT_COMMITED };
196
- }
197
- //Check if paid or what
198
- const status = await this.Events.findInEvents(escrowStateKey, async (event, tx) => {
199
- if (event.name === "ClaimEvent") {
200
- const paymentHash = buffer_1.Buffer.from(event.data.hash).toString("hex");
201
- if (paymentHash !== data.paymentHash)
202
- return null;
203
- if (!event.data.sequence.eq(data.sequence))
204
- return null;
205
- return {
206
- type: base_1.SwapCommitStateType.PAID,
207
- getClaimTxId: () => Promise.resolve(tx.transaction.signatures[0]),
208
- getClaimResult: () => Promise.resolve(buffer_1.Buffer.from(event.data.secret).toString("hex")),
209
- getTxBlock: () => Promise.resolve({
210
- blockHeight: tx.slot,
211
- blockTime: tx.blockTime
212
- })
213
- };
214
- }
215
- if (event.name === "RefundEvent") {
216
- const paymentHash = buffer_1.Buffer.from(event.data.hash).toString("hex");
217
- if (paymentHash !== data.paymentHash)
218
- return null;
219
- if (!event.data.sequence.eq(data.sequence))
220
- return null;
221
- return {
222
- type: isExpired ? base_1.SwapCommitStateType.EXPIRED : base_1.SwapCommitStateType.NOT_COMMITED,
223
- getRefundTxId: () => Promise.resolve(tx.transaction.signatures[0]),
224
- getTxBlock: () => Promise.resolve({
225
- blockHeight: tx.slot,
226
- blockTime: tx.blockTime
227
- })
228
- };
229
- }
230
- });
231
- if (status != null)
232
- return status;
233
- if (isExpired)
234
- return { type: base_1.SwapCommitStateType.EXPIRED };
235
- return { type: base_1.SwapCommitStateType.NOT_COMMITED };
236
- }
237
- async getCommitStatuses(request) {
238
- const result = {};
239
- let promises = [];
240
- for (let { signer, swapData } of request) {
241
- promises.push(this.getCommitStatus(signer, swapData).then(val => {
242
- result[swapData.getEscrowHash()] = val;
243
- }));
244
- if (promises.length >= MAX_PARALLEL_COMMIT_STATUS_CHECKS) {
245
- await Promise.all(promises);
246
- promises = [];
247
- }
248
- }
249
- await Promise.all(promises);
250
- return result;
251
- }
252
- /**
253
- * Checks the status of the specific payment hash
254
- *
255
- * @param claimHash
256
- */
257
- async getClaimHashStatus(claimHash) {
258
- const { paymentHash } = (0, Utils_1.fromClaimHash)(claimHash);
259
- const escrowStateKey = this.SwapEscrowState(buffer_1.Buffer.from(paymentHash, "hex"));
260
- const abortController = new AbortController();
261
- //Start fetching events before checking escrow PDA, this call is used when quoting, so saving 100ms here helps a lot!
262
- const eventsPromise = this.Events.findInEvents(escrowStateKey, async (event) => {
263
- if (event.name === "ClaimEvent")
264
- return base_1.SwapCommitStateType.PAID;
265
- if (event.name === "RefundEvent")
266
- return base_1.SwapCommitStateType.NOT_COMMITED;
267
- }, abortController.signal).catch(e => {
268
- abortController.abort(e);
269
- return null;
270
- });
271
- const escrowState = await this.program.account.escrowState.fetchNullable(escrowStateKey);
272
- abortController.signal.throwIfAborted();
273
- if (escrowState != null) {
274
- abortController.abort();
275
- return base_1.SwapCommitStateType.COMMITED;
276
- }
277
- //Check if paid or what
278
- const eventsStatus = await eventsPromise;
279
- abortController.signal.throwIfAborted();
280
- if (eventsStatus != null)
281
- return eventsStatus;
282
- return base_1.SwapCommitStateType.NOT_COMMITED;
283
- }
284
- /**
285
- * Returns the data committed for a specific payment hash, or null if no data is currently commited for
286
- * the specific swap
287
- *
288
- * @param claimHashHex
289
- */
290
- async getCommitedData(claimHashHex) {
291
- const { paymentHash } = (0, Utils_1.fromClaimHash)(claimHashHex);
292
- const paymentHashBuffer = buffer_1.Buffer.from(paymentHash, "hex");
293
- const account = await this.program.account.escrowState.fetchNullable(this.SwapEscrowState(paymentHashBuffer));
294
- if (account == null)
295
- return null;
296
- return SolanaSwapData_1.SolanaSwapData.fromEscrowState(account);
297
- }
298
- async getHistoricalSwaps(signer, startBlockheight) {
299
- let latestBlockheight;
300
- const events = [];
301
- await this.Events.findInEvents(new web3_js_1.PublicKey(signer), async (event, tx) => {
302
- if (latestBlockheight == null)
303
- latestBlockheight = tx.slot;
304
- events.push({ event, tx });
305
- }, undefined, undefined, startBlockheight);
306
- this.logger.debug(`getHistoricalSwaps(): Found ${events.length} atomiq related events!`);
307
- const swapsOpened = {};
308
- const resultingSwaps = {};
309
- events.reverse();
310
- for (let { event, tx } of events) {
311
- const txSignature = tx.transaction.signatures[0];
312
- const paymentHash = buffer_1.Buffer.from(event.data.hash).toString("hex");
313
- const escrowHash = (0, Utils_1.toEscrowHash)(paymentHash, event.data.sequence);
314
- if (event.name === "InitializeEvent") {
315
- //Parse swap data from initialize event
316
- const txoHash = buffer_1.Buffer.from(event.data.txoHash).toString("hex");
317
- const instructions = this.Events.decodeInstructions(tx.transaction.message);
318
- if (instructions == null) {
319
- this.logger.warn(`getHistoricalSwaps(): Skipping tx ${txSignature} because cannot parse instructions!`);
320
- continue;
321
- }
322
- const initIx = instructions.find(ix => ix != null && (ix.name === "offererInitializePayIn" || ix.name === "offererInitialize"));
323
- if (initIx == null) {
324
- this.logger.warn(`getHistoricalSwaps(): Skipping tx ${txSignature} because init instruction not found!`);
325
- continue;
326
- }
327
- swapsOpened[escrowHash] = {
328
- data: SolanaSwapData_1.SolanaSwapData.fromInstruction(initIx, txoHash),
329
- getInitTxId: () => Promise.resolve(txSignature),
330
- getTxBlock: () => Promise.resolve({
331
- blockHeight: tx.slot,
332
- blockTime: tx.blockTime
333
- })
334
- };
335
- }
336
- if (event.name === "ClaimEvent") {
337
- const foundSwapData = swapsOpened[escrowHash];
338
- delete swapsOpened[escrowHash];
339
- resultingSwaps[escrowHash] = {
340
- init: foundSwapData,
341
- state: {
342
- type: base_1.SwapCommitStateType.PAID,
343
- getClaimTxId: () => Promise.resolve(txSignature),
344
- getClaimResult: () => Promise.resolve(buffer_1.Buffer.from(event.data.secret).toString("hex")),
345
- getTxBlock: () => Promise.resolve({
346
- blockHeight: tx.slot,
347
- blockTime: tx.blockTime
348
- })
349
- }
350
- };
351
- }
352
- if (event.name === "RefundEvent") {
353
- const foundSwapData = swapsOpened[escrowHash];
354
- delete swapsOpened[escrowHash];
355
- const isExpired = foundSwapData != null && await this.isExpired(signer, foundSwapData.data);
356
- resultingSwaps[escrowHash] = {
357
- init: foundSwapData,
358
- state: {
359
- type: isExpired ? base_1.SwapCommitStateType.EXPIRED : base_1.SwapCommitStateType.NOT_COMMITED,
360
- getRefundTxId: () => Promise.resolve(txSignature),
361
- getTxBlock: () => Promise.resolve({
362
- blockHeight: tx.slot,
363
- blockTime: tx.blockTime
364
- })
365
- }
366
- };
367
- }
368
- }
369
- this.logger.debug(`getHistoricalSwaps(): Found ${Object.keys(resultingSwaps).length} settled swaps!`);
370
- this.logger.debug(`getHistoricalSwaps(): Found ${Object.keys(swapsOpened).length} unsettled swaps!`);
371
- for (let escrowHash in swapsOpened) {
372
- const foundSwapData = swapsOpened[escrowHash];
373
- const isExpired = await this.isExpired(signer, foundSwapData.data);
374
- resultingSwaps[escrowHash] = {
375
- init: foundSwapData,
376
- state: foundSwapData.data.isOfferer(signer) && isExpired
377
- ? { type: base_1.SwapCommitStateType.REFUNDABLE }
378
- : { type: base_1.SwapCommitStateType.COMMITED }
379
- };
380
- }
381
- return {
382
- swaps: resultingSwaps,
383
- latestBlockheight
384
- };
385
- }
386
- ////////////////////////////////////////////
387
- //// Swap data initializer
388
- createSwapData(type, offerer, claimer, token, amount, claimHash, sequence, expiry, payIn, payOut, securityDeposit, claimerBounty, depositToken) {
389
- if (depositToken != null) {
390
- if (!new web3_js_1.PublicKey(depositToken).equals(SolanaTokens_1.SolanaTokens.WSOL_ADDRESS))
391
- throw new Error("Only SOL supported as deposit token!");
392
- }
393
- const tokenAddr = new web3_js_1.PublicKey(token);
394
- const offererKey = offerer == null ? null : new web3_js_1.PublicKey(offerer);
395
- const claimerKey = claimer == null ? null : new web3_js_1.PublicKey(claimer);
396
- const { paymentHash, nonce, confirmations } = (0, Utils_1.fromClaimHash)(claimHash);
397
- return Promise.resolve(new SolanaSwapData_1.SolanaSwapData(offererKey, claimerKey, tokenAddr, (0, Utils_1.toBN)(amount), paymentHash, (0, Utils_1.toBN)(sequence), (0, Utils_1.toBN)(expiry), nonce, confirmations, payOut, type == null ? null : SolanaSwapData_1.SolanaSwapData.typeToKind(type), payIn, offererKey == null ? null : payIn ? (0, spl_token_1.getAssociatedTokenAddressSync)(tokenAddr, offererKey) : web3_js_1.PublicKey.default, claimerKey == null ? null : payOut ? (0, spl_token_1.getAssociatedTokenAddressSync)(tokenAddr, claimerKey) : web3_js_1.PublicKey.default, (0, Utils_1.toBN)(securityDeposit), (0, Utils_1.toBN)(claimerBounty), null));
398
- }
399
- ////////////////////////////////////////////
400
- //// Utils
401
- async getBalance(signer, tokenAddress, inContract) {
402
- if (!inContract) {
403
- return await this.Chain.getBalance(signer, tokenAddress);
404
- }
405
- const token = new web3_js_1.PublicKey(tokenAddress);
406
- const publicKey = new web3_js_1.PublicKey(signer);
407
- return await this.getIntermediaryBalance(publicKey, token);
408
- }
409
- getIntermediaryData(address, token) {
410
- return this.LpVault.getIntermediaryData(new web3_js_1.PublicKey(address), new web3_js_1.PublicKey(token));
411
- }
412
- getIntermediaryReputation(address, token) {
413
- return this.LpVault.getIntermediaryReputation(new web3_js_1.PublicKey(address), new web3_js_1.PublicKey(token));
414
- }
415
- getIntermediaryBalance(address, token) {
416
- return this.LpVault.getIntermediaryBalance(address, token);
417
- }
418
- ////////////////////////////////////////////
419
- //// Transaction initializers
420
- async txsClaimWithSecret(signer, swapData, secret, checkExpiry, initAta, feeRate, skipAtaCheck) {
421
- return this.Claim.txsClaimWithSecret(typeof (signer) === "string" ? new web3_js_1.PublicKey(signer) : signer.getPublicKey(), swapData, secret, checkExpiry, initAta, feeRate, skipAtaCheck);
422
- }
423
- async txsClaimWithTxData(signer, swapData, tx, requiredConfirmations, vout, commitedHeader, synchronizer, initAta, feeRate, storageAccHolder) {
424
- if (swapData.confirmations !== requiredConfirmations)
425
- throw new Error("Invalid requiredConfirmations provided!");
426
- return this.Claim.txsClaimWithTxData(typeof (signer) === "string" ? new web3_js_1.PublicKey(signer) : signer, swapData, tx, vout, commitedHeader, synchronizer, initAta, storageAccHolder, feeRate);
427
- }
428
- txsRefund(signer, swapData, check, initAta, feeRate) {
429
- if (!swapData.isOfferer(signer))
430
- throw new Error("Only offerer can refund on Solana");
431
- return this.Refund.txsRefund(swapData, check, initAta, feeRate);
432
- }
433
- txsRefundWithAuthorization(signer, swapData, { timeout, prefix, signature }, check, initAta, feeRate) {
434
- if (!swapData.isOfferer(signer))
435
- throw new Error("Only offerer can refund on Solana");
436
- return this.Refund.txsRefundWithAuthorization(swapData, timeout, prefix, signature, check, initAta, feeRate);
437
- }
438
- txsInit(sender, swapData, { timeout, prefix, signature }, skipChecks, feeRate) {
439
- if (swapData.isPayIn()) {
440
- if (!swapData.isOfferer(sender))
441
- throw new Error("Only offerer can create payIn=true swap");
442
- return this.Init.txsInitPayIn(swapData, timeout, prefix, signature, skipChecks, feeRate);
443
- }
444
- else {
445
- if (!swapData.isClaimer(sender))
446
- throw new Error("Only claimer can create payIn=false swap");
447
- return this.Init.txsInit(swapData, timeout, prefix, signature, skipChecks, feeRate);
448
- }
449
- }
450
- txsWithdraw(signer, token, amount, feeRate) {
451
- return this.LpVault.txsWithdraw(new web3_js_1.PublicKey(signer), new web3_js_1.PublicKey(token), amount, feeRate);
452
- }
453
- txsDeposit(signer, token, amount, feeRate) {
454
- return this.LpVault.txsDeposit(new web3_js_1.PublicKey(signer), new web3_js_1.PublicKey(token), amount, feeRate);
455
- }
456
- ////////////////////////////////////////////
457
- //// Executors
458
- async claimWithSecret(signer, swapData, secret, checkExpiry, initAta, txOptions) {
459
- const result = await this.Claim.txsClaimWithSecret(signer.getPublicKey(), swapData, secret, checkExpiry, initAta, txOptions?.feeRate);
460
- const [signature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
461
- return signature;
462
- }
463
- async claimWithTxData(signer, swapData, tx, requiredConfirmations, vout, commitedHeader, synchronizer, initAta, txOptions) {
464
- if (requiredConfirmations !== swapData.confirmations)
465
- throw new Error("Invalid requiredConfirmations provided!");
466
- const data = {
467
- storageAcc: null
468
- };
469
- const txs = await this.Claim.txsClaimWithTxData(signer, swapData, tx, vout, commitedHeader, synchronizer, initAta, data, txOptions?.feeRate);
470
- if (txs === null)
471
- throw new Error("Btc relay not synchronized to required blockheight!");
472
- //TODO: This doesn't return proper tx signature
473
- const [signature] = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal);
474
- await this.DataAccount.removeDataAccount(data.storageAcc);
475
- return signature;
476
- }
477
- async refund(signer, swapData, check, initAta, txOptions) {
478
- let result = await this.txsRefund(signer.getAddress(), swapData, check, initAta, txOptions?.feeRate);
479
- const [signature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
480
- return signature;
481
- }
482
- async refundWithAuthorization(signer, swapData, signature, check, initAta, txOptions) {
483
- let result = await this.txsRefundWithAuthorization(signer.getAddress(), swapData, signature, check, initAta, txOptions?.feeRate);
484
- const [txSignature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
485
- return txSignature;
486
- }
487
- async init(signer, swapData, signature, skipChecks, txOptions) {
488
- if (swapData.isPayIn()) {
489
- if (!signer.getPublicKey().equals(swapData.offerer))
490
- throw new Error("Invalid signer provided!");
491
- }
492
- else {
493
- if (!signer.getPublicKey().equals(swapData.claimer))
494
- throw new Error("Invalid signer provided!");
495
- }
496
- const result = await this.txsInit(signer.getAddress(), swapData, signature, skipChecks, txOptions?.feeRate);
497
- const [txSignature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
498
- return txSignature;
499
- }
500
- async initAndClaimWithSecret(signer, swapData, signature, secret, skipChecks, txOptions) {
501
- if (!signer.getPublicKey().equals(swapData.claimer))
502
- throw new Error("Invalid signer provided!");
503
- const txsCommit = await this.txsInit(signer.getAddress(), swapData, signature, skipChecks, txOptions?.feeRate);
504
- const txsClaim = await this.Claim.txsClaimWithSecret(signer.getPublicKey(), swapData, secret, true, false, txOptions?.feeRate, true);
505
- return await this.Chain.sendAndConfirm(signer, txsCommit.concat(txsClaim), txOptions?.waitForConfirmation, txOptions?.abortSignal);
506
- }
507
- async withdraw(signer, token, amount, txOptions) {
508
- const txs = await this.LpVault.txsWithdraw(signer.getPublicKey(), new web3_js_1.PublicKey(token), amount, txOptions?.feeRate);
509
- const [txId] = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
510
- return txId;
511
- }
512
- async deposit(signer, token, amount, txOptions) {
513
- const txs = await this.LpVault.txsDeposit(signer.getPublicKey(), new web3_js_1.PublicKey(token), amount, txOptions?.feeRate);
514
- const [txId] = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
515
- return txId;
516
- }
517
- ////////////////////////////////////////////
518
- //// Fees
519
- getInitPayInFeeRate(offerer, claimer, token, claimHash) {
520
- const paymentHash = claimHash == null ? null : (0, Utils_1.fromClaimHash)(claimHash).paymentHash;
521
- return this.Init.getInitPayInFeeRate(toPublicKeyOrNull(offerer), toPublicKeyOrNull(claimer), toPublicKeyOrNull(token), paymentHash);
522
- }
523
- getInitFeeRate(offerer, claimer, token, claimHash) {
524
- const paymentHash = claimHash == null ? null : (0, Utils_1.fromClaimHash)(claimHash).paymentHash;
525
- return this.Init.getInitFeeRate(toPublicKeyOrNull(offerer), toPublicKeyOrNull(claimer), toPublicKeyOrNull(token), paymentHash);
526
- }
527
- getRefundFeeRate(swapData) {
528
- return this.Refund.getRefundFeeRate(swapData);
529
- }
530
- getClaimFeeRate(signer, swapData) {
531
- return this.Claim.getClaimFeeRate(new web3_js_1.PublicKey(signer), swapData);
532
- }
533
- getClaimFee(signer, swapData, feeRate) {
534
- return this.Claim.getClaimFee(new web3_js_1.PublicKey(signer), swapData, feeRate);
535
- }
536
- getRawClaimFee(signer, swapData, feeRate) {
537
- return this.Claim.getRawClaimFee(new web3_js_1.PublicKey(signer), swapData, feeRate);
538
- }
539
- /**
540
- * Get the estimated solana fee of the commit transaction
541
- */
542
- getCommitFee(signer, swapData, feeRate) {
543
- return this.Init.getInitFee(swapData, feeRate);
544
- }
545
- /**
546
- * Get the estimated solana fee of the commit transaction, without any deposits
547
- */
548
- getRawCommitFee(signer, swapData, feeRate) {
549
- return this.Init.getRawInitFee(swapData, feeRate);
550
- }
551
- /**
552
- * Get the estimated solana transaction fee of the refund transaction
553
- */
554
- getRefundFee(signer, swapData, feeRate) {
555
- return this.Refund.getRefundFee(swapData, feeRate);
556
- }
557
- /**
558
- * Get the estimated solana transaction fee of the refund transaction
559
- */
560
- getRawRefundFee(signer, swapData, feeRate) {
561
- return this.Refund.getRawRefundFee(swapData, feeRate);
562
- }
563
- getExtraData(outputScript, amount, confirmations, nonce) {
564
- return buffer_1.Buffer.from((0, sha2_1.sha256)(buffer_1.Buffer.concat([
565
- base_1.BigIntBufferUtils.toBuffer(amount, "le", 8),
566
- outputScript
567
- ])));
568
- }
569
- }
570
- exports.SolanaSwapProgram = SolanaSwapProgram;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SolanaSwapProgram = void 0;
4
+ const SolanaSwapData_1 = require("./SolanaSwapData");
5
+ const web3_js_1 = require("@solana/web3.js");
6
+ const sha2_1 = require("@noble/hashes/sha2");
7
+ const programIdl = require("./programIdl.json");
8
+ const base_1 = require("@atomiqlabs/base");
9
+ const spl_token_1 = require("@solana/spl-token");
10
+ const SolanaProgramBase_1 = require("../program/SolanaProgramBase");
11
+ const SwapInit_1 = require("./modules/SwapInit");
12
+ const SolanaDataAccount_1 = require("./modules/SolanaDataAccount");
13
+ const SwapRefund_1 = require("./modules/SwapRefund");
14
+ const SwapClaim_1 = require("./modules/SwapClaim");
15
+ const SolanaLpVault_1 = require("./modules/SolanaLpVault");
16
+ const buffer_1 = require("buffer");
17
+ const Utils_1 = require("../../utils/Utils");
18
+ const SolanaTokens_1 = require("../chain/modules/SolanaTokens");
19
+ const BN = require("bn.js");
20
+ function toPublicKeyOrNull(str) {
21
+ return str == null ? null : new web3_js_1.PublicKey(str);
22
+ }
23
+ const MAX_PARALLEL_COMMIT_STATUS_CHECKS = 5;
24
+ class SolanaSwapProgram extends SolanaProgramBase_1.SolanaProgramBase {
25
+ constructor(chainInterface, btcRelay, storage, programAddress) {
26
+ super(chainInterface, programIdl, programAddress);
27
+ ////////////////////////
28
+ //// Constants
29
+ this.ESCROW_STATE_RENT_EXEMPT = 2658720;
30
+ ////////////////////////
31
+ //// PDA accessors
32
+ this.SwapVaultAuthority = this.pda("authority");
33
+ this.SwapVault = this.pda("vault", (tokenAddress) => [tokenAddress.toBuffer()]);
34
+ this.SwapUserVault = this.pda("uservault", (publicKey, tokenAddress) => [publicKey.toBuffer(), tokenAddress.toBuffer()]);
35
+ this.SwapEscrowState = this.pda("state", (hash) => [hash]);
36
+ ////////////////////////
37
+ //// Timeouts
38
+ this.chainId = "SOLANA";
39
+ this.claimWithSecretTimeout = 45;
40
+ this.claimWithTxDataTimeout = 120;
41
+ this.refundTimeout = 45;
42
+ this.claimGracePeriod = 10 * 60;
43
+ this.refundGracePeriod = 10 * 60;
44
+ this.authGracePeriod = 5 * 60;
45
+ this.Init = new SwapInit_1.SwapInit(chainInterface, this);
46
+ this.Refund = new SwapRefund_1.SwapRefund(chainInterface, this);
47
+ this.Claim = new SwapClaim_1.SwapClaim(chainInterface, this, btcRelay);
48
+ this.DataAccount = new SolanaDataAccount_1.SolanaDataAccount(chainInterface, this, storage);
49
+ this.LpVault = new SolanaLpVault_1.SolanaLpVault(chainInterface, this);
50
+ }
51
+ async start() {
52
+ await this.DataAccount.init();
53
+ }
54
+ getClaimableDeposits(signer) {
55
+ return this.DataAccount.getDataAccountsInfo(new web3_js_1.PublicKey(signer));
56
+ }
57
+ claimDeposits(signer) {
58
+ return this.DataAccount.sweepDataAccounts(signer);
59
+ }
60
+ ////////////////////////////////////////////
61
+ //// Signatures
62
+ preFetchForInitSignatureVerification(data) {
63
+ return this.Init.preFetchForInitSignatureVerification(data);
64
+ }
65
+ preFetchBlockDataForSignatures() {
66
+ return this.Init.preFetchBlockDataForSignatures();
67
+ }
68
+ getInitSignature(signer, swapData, authorizationTimeout, preFetchedBlockData, feeRate) {
69
+ return this.Init.signSwapInitialization(signer, swapData, authorizationTimeout, preFetchedBlockData, feeRate);
70
+ }
71
+ isValidInitAuthorization(signer, swapData, { timeout, prefix, signature }, feeRate, preFetchedData) {
72
+ return this.Init.isSignatureValid(signer, swapData, timeout, prefix, signature, feeRate, preFetchedData);
73
+ }
74
+ getInitAuthorizationExpiry(swapData, { timeout, prefix, signature }, preFetchedData) {
75
+ return this.Init.getSignatureExpiry(timeout, signature, preFetchedData);
76
+ }
77
+ isInitAuthorizationExpired(swapData, { timeout, prefix, signature }) {
78
+ return this.Init.isSignatureExpired(signature, timeout);
79
+ }
80
+ getRefundSignature(signer, swapData, authorizationTimeout) {
81
+ return this.Refund.signSwapRefund(signer, swapData, authorizationTimeout);
82
+ }
83
+ isValidRefundAuthorization(swapData, { timeout, prefix, signature }) {
84
+ return this.Refund.isSignatureValid(swapData, timeout, prefix, signature);
85
+ }
86
+ getDataSignature(signer, data) {
87
+ return this.Chain.Signatures.getDataSignature(signer, data);
88
+ }
89
+ isValidDataSignature(data, signature, publicKey) {
90
+ return this.Chain.Signatures.isValidDataSignature(data, signature, publicKey);
91
+ }
92
+ ////////////////////////////////////////////
93
+ //// Swap data utils
94
+ /**
95
+ * Checks whether the claim is claimable by us, that means not expired, we are claimer & the swap is commited
96
+ *
97
+ * @param signer
98
+ * @param data
99
+ */
100
+ async isClaimable(signer, data) {
101
+ if (!data.isClaimer(signer))
102
+ return false;
103
+ if (await this.isExpired(signer, data))
104
+ return false;
105
+ return await this.isCommited(data);
106
+ }
107
+ /**
108
+ * Checks whether a swap is commited, i.e. the swap still exists on-chain and was not claimed nor refunded
109
+ *
110
+ * @param swapData
111
+ */
112
+ async isCommited(swapData) {
113
+ const paymentHash = buffer_1.Buffer.from(swapData.paymentHash, "hex");
114
+ const account = await this.program.account.escrowState.fetchNullable(this.SwapEscrowState(paymentHash));
115
+ if (account == null)
116
+ return false;
117
+ return swapData.correctPDA(account);
118
+ }
119
+ /**
120
+ * Checks whether the swap is expired, takes into consideration possible on-chain time skew, therefore for claimer
121
+ * the swap expires a bit sooner than it should've & for the offerer it expires a bit later
122
+ *
123
+ * @param signer
124
+ * @param data
125
+ */
126
+ isExpired(signer, data) {
127
+ let currentTimestamp = new BN(Math.floor(Date.now() / 1000));
128
+ if (data.isClaimer(signer))
129
+ currentTimestamp = currentTimestamp.add(new BN(this.claimGracePeriod));
130
+ if (data.isOfferer(signer))
131
+ currentTimestamp = currentTimestamp.sub(new BN(this.refundGracePeriod));
132
+ return Promise.resolve(data.expiry.lt(currentTimestamp));
133
+ }
134
+ /**
135
+ * Checks if the swap is refundable by us, checks if we are offerer, if the swap is already expired & if the swap
136
+ * is still commited
137
+ *
138
+ * @param signer
139
+ * @param data
140
+ */
141
+ async isRequestRefundable(signer, data) {
142
+ //Swap can only be refunded by the offerer
143
+ if (!data.isOfferer(signer))
144
+ return false;
145
+ if (!(await this.isExpired(signer, data)))
146
+ return false;
147
+ return await this.isCommited(data);
148
+ }
149
+ /**
150
+ * Get the swap payment hash to be used for an on-chain swap, this just uses a sha256 hash of the values
151
+ *
152
+ * @param outputScript output script required to claim the swap
153
+ * @param amount sats sent required to claim the swap
154
+ * @param confirmations
155
+ * @param nonce swap nonce uniquely identifying the transaction to prevent replay attacks
156
+ */
157
+ getHashForOnchain(outputScript, amount, confirmations, nonce) {
158
+ nonce ?? (nonce = 0n);
159
+ const paymentHash = buffer_1.Buffer.from((0, sha2_1.sha256)(buffer_1.Buffer.concat([
160
+ base_1.BigIntBufferUtils.toBuffer(nonce, "le", 8),
161
+ base_1.BigIntBufferUtils.toBuffer(amount, "le", 8),
162
+ outputScript
163
+ ]))).toString("hex");
164
+ return buffer_1.Buffer.from((0, Utils_1.toClaimHash)(paymentHash, nonce, confirmations), "hex");
165
+ }
166
+ getHashForHtlc(swapHash) {
167
+ return buffer_1.Buffer.from((0, Utils_1.toClaimHash)(swapHash.toString("hex"), 0n, 0), "hex");
168
+ }
169
+ getHashForTxId(txId, confirmations) {
170
+ return buffer_1.Buffer.from((0, Utils_1.toClaimHash)(buffer_1.Buffer.from(txId, "hex").reverse().toString("hex"), 0n, confirmations), "hex");
171
+ }
172
+ ////////////////////////////////////////////
173
+ //// Swap data getters
174
+ /**
175
+ * Gets the status of the specific swap, this also checks if we are offerer/claimer & checks for expiry (to see
176
+ * if swap is refundable)
177
+ *
178
+ * @param signer
179
+ * @param data
180
+ */
181
+ async getCommitStatus(signer, data) {
182
+ const escrowStateKey = this.SwapEscrowState(buffer_1.Buffer.from(data.paymentHash, "hex"));
183
+ const [escrowState, isExpired] = await Promise.all([
184
+ this.program.account.escrowState.fetchNullable(escrowStateKey),
185
+ this.isExpired(signer, data)
186
+ ]);
187
+ if (escrowState != null) {
188
+ if (data.correctPDA(escrowState)) {
189
+ if (data.isOfferer(signer) && isExpired)
190
+ return { type: base_1.SwapCommitStateType.REFUNDABLE };
191
+ return { type: base_1.SwapCommitStateType.COMMITED };
192
+ }
193
+ if (data.isOfferer(signer) && isExpired)
194
+ return { type: base_1.SwapCommitStateType.EXPIRED };
195
+ return { type: base_1.SwapCommitStateType.NOT_COMMITED };
196
+ }
197
+ //Check if paid or what
198
+ const status = await this.Events.findInEvents(escrowStateKey, async (event, tx) => {
199
+ if (event.name === "ClaimEvent") {
200
+ const paymentHash = buffer_1.Buffer.from(event.data.hash).toString("hex");
201
+ if (paymentHash !== data.paymentHash)
202
+ return null;
203
+ if (!event.data.sequence.eq(data.sequence))
204
+ return null;
205
+ return {
206
+ type: base_1.SwapCommitStateType.PAID,
207
+ getClaimTxId: () => Promise.resolve(tx.transaction.signatures[0]),
208
+ getClaimResult: () => Promise.resolve(buffer_1.Buffer.from(event.data.secret).toString("hex")),
209
+ getTxBlock: () => Promise.resolve({
210
+ blockHeight: tx.slot,
211
+ blockTime: tx.blockTime
212
+ })
213
+ };
214
+ }
215
+ if (event.name === "RefundEvent") {
216
+ const paymentHash = buffer_1.Buffer.from(event.data.hash).toString("hex");
217
+ if (paymentHash !== data.paymentHash)
218
+ return null;
219
+ if (!event.data.sequence.eq(data.sequence))
220
+ return null;
221
+ return {
222
+ type: isExpired ? base_1.SwapCommitStateType.EXPIRED : base_1.SwapCommitStateType.NOT_COMMITED,
223
+ getRefundTxId: () => Promise.resolve(tx.transaction.signatures[0]),
224
+ getTxBlock: () => Promise.resolve({
225
+ blockHeight: tx.slot,
226
+ blockTime: tx.blockTime
227
+ })
228
+ };
229
+ }
230
+ });
231
+ if (status != null)
232
+ return status;
233
+ if (isExpired)
234
+ return { type: base_1.SwapCommitStateType.EXPIRED };
235
+ return { type: base_1.SwapCommitStateType.NOT_COMMITED };
236
+ }
237
+ async getCommitStatuses(request) {
238
+ const result = {};
239
+ let promises = [];
240
+ for (let { signer, swapData } of request) {
241
+ promises.push(this.getCommitStatus(signer, swapData).then(val => {
242
+ result[swapData.getEscrowHash()] = val;
243
+ }));
244
+ if (promises.length >= MAX_PARALLEL_COMMIT_STATUS_CHECKS) {
245
+ await Promise.all(promises);
246
+ promises = [];
247
+ }
248
+ }
249
+ await Promise.all(promises);
250
+ return result;
251
+ }
252
+ /**
253
+ * Checks the status of the specific payment hash
254
+ *
255
+ * @param claimHash
256
+ */
257
+ async getClaimHashStatus(claimHash) {
258
+ const { paymentHash } = (0, Utils_1.fromClaimHash)(claimHash);
259
+ const escrowStateKey = this.SwapEscrowState(buffer_1.Buffer.from(paymentHash, "hex"));
260
+ const abortController = new AbortController();
261
+ //Start fetching events before checking escrow PDA, this call is used when quoting, so saving 100ms here helps a lot!
262
+ const eventsPromise = this.Events.findInEvents(escrowStateKey, async (event) => {
263
+ if (event.name === "ClaimEvent")
264
+ return base_1.SwapCommitStateType.PAID;
265
+ if (event.name === "RefundEvent")
266
+ return base_1.SwapCommitStateType.NOT_COMMITED;
267
+ }, abortController.signal).catch(e => {
268
+ abortController.abort(e);
269
+ return null;
270
+ });
271
+ const escrowState = await this.program.account.escrowState.fetchNullable(escrowStateKey);
272
+ abortController.signal.throwIfAborted();
273
+ if (escrowState != null) {
274
+ abortController.abort();
275
+ return base_1.SwapCommitStateType.COMMITED;
276
+ }
277
+ //Check if paid or what
278
+ const eventsStatus = await eventsPromise;
279
+ abortController.signal.throwIfAborted();
280
+ if (eventsStatus != null)
281
+ return eventsStatus;
282
+ return base_1.SwapCommitStateType.NOT_COMMITED;
283
+ }
284
+ /**
285
+ * Returns the data committed for a specific payment hash, or null if no data is currently commited for
286
+ * the specific swap
287
+ *
288
+ * @param claimHashHex
289
+ */
290
+ async getCommitedData(claimHashHex) {
291
+ const { paymentHash } = (0, Utils_1.fromClaimHash)(claimHashHex);
292
+ const paymentHashBuffer = buffer_1.Buffer.from(paymentHash, "hex");
293
+ const account = await this.program.account.escrowState.fetchNullable(this.SwapEscrowState(paymentHashBuffer));
294
+ if (account == null)
295
+ return null;
296
+ return SolanaSwapData_1.SolanaSwapData.fromEscrowState(account);
297
+ }
298
+ async getHistoricalSwaps(signer, startBlockheight) {
299
+ let latestBlockheight;
300
+ const events = [];
301
+ await this.Events.findInEvents(new web3_js_1.PublicKey(signer), async (event, tx) => {
302
+ if (latestBlockheight == null)
303
+ latestBlockheight = tx.slot;
304
+ events.push({ event, tx });
305
+ }, undefined, undefined, startBlockheight);
306
+ this.logger.debug(`getHistoricalSwaps(): Found ${events.length} atomiq related events!`);
307
+ const swapsOpened = {};
308
+ const resultingSwaps = {};
309
+ events.reverse();
310
+ for (let { event, tx } of events) {
311
+ const txSignature = tx.transaction.signatures[0];
312
+ const paymentHash = buffer_1.Buffer.from(event.data.hash).toString("hex");
313
+ const escrowHash = (0, Utils_1.toEscrowHash)(paymentHash, event.data.sequence);
314
+ if (event.name === "InitializeEvent") {
315
+ //Parse swap data from initialize event
316
+ const txoHash = buffer_1.Buffer.from(event.data.txoHash).toString("hex");
317
+ const instructions = this.Events.decodeInstructions(tx.transaction.message);
318
+ if (instructions == null) {
319
+ this.logger.warn(`getHistoricalSwaps(): Skipping tx ${txSignature} because cannot parse instructions!`);
320
+ continue;
321
+ }
322
+ const initIx = instructions.find(ix => ix != null && (ix.name === "offererInitializePayIn" || ix.name === "offererInitialize"));
323
+ if (initIx == null) {
324
+ this.logger.warn(`getHistoricalSwaps(): Skipping tx ${txSignature} because init instruction not found!`);
325
+ continue;
326
+ }
327
+ swapsOpened[escrowHash] = {
328
+ data: SolanaSwapData_1.SolanaSwapData.fromInstruction(initIx, txoHash),
329
+ getInitTxId: () => Promise.resolve(txSignature),
330
+ getTxBlock: () => Promise.resolve({
331
+ blockHeight: tx.slot,
332
+ blockTime: tx.blockTime
333
+ })
334
+ };
335
+ }
336
+ if (event.name === "ClaimEvent") {
337
+ const foundSwapData = swapsOpened[escrowHash];
338
+ delete swapsOpened[escrowHash];
339
+ resultingSwaps[escrowHash] = {
340
+ init: foundSwapData,
341
+ state: {
342
+ type: base_1.SwapCommitStateType.PAID,
343
+ getClaimTxId: () => Promise.resolve(txSignature),
344
+ getClaimResult: () => Promise.resolve(buffer_1.Buffer.from(event.data.secret).toString("hex")),
345
+ getTxBlock: () => Promise.resolve({
346
+ blockHeight: tx.slot,
347
+ blockTime: tx.blockTime
348
+ })
349
+ }
350
+ };
351
+ }
352
+ if (event.name === "RefundEvent") {
353
+ const foundSwapData = swapsOpened[escrowHash];
354
+ delete swapsOpened[escrowHash];
355
+ const isExpired = foundSwapData != null && await this.isExpired(signer, foundSwapData.data);
356
+ resultingSwaps[escrowHash] = {
357
+ init: foundSwapData,
358
+ state: {
359
+ type: isExpired ? base_1.SwapCommitStateType.EXPIRED : base_1.SwapCommitStateType.NOT_COMMITED,
360
+ getRefundTxId: () => Promise.resolve(txSignature),
361
+ getTxBlock: () => Promise.resolve({
362
+ blockHeight: tx.slot,
363
+ blockTime: tx.blockTime
364
+ })
365
+ }
366
+ };
367
+ }
368
+ }
369
+ this.logger.debug(`getHistoricalSwaps(): Found ${Object.keys(resultingSwaps).length} settled swaps!`);
370
+ this.logger.debug(`getHistoricalSwaps(): Found ${Object.keys(swapsOpened).length} unsettled swaps!`);
371
+ for (let escrowHash in swapsOpened) {
372
+ const foundSwapData = swapsOpened[escrowHash];
373
+ const isExpired = await this.isExpired(signer, foundSwapData.data);
374
+ resultingSwaps[escrowHash] = {
375
+ init: foundSwapData,
376
+ state: foundSwapData.data.isOfferer(signer) && isExpired
377
+ ? { type: base_1.SwapCommitStateType.REFUNDABLE }
378
+ : { type: base_1.SwapCommitStateType.COMMITED }
379
+ };
380
+ }
381
+ return {
382
+ swaps: resultingSwaps,
383
+ latestBlockheight
384
+ };
385
+ }
386
+ ////////////////////////////////////////////
387
+ //// Swap data initializer
388
+ createSwapData(type, offerer, claimer, token, amount, claimHash, sequence, expiry, payIn, payOut, securityDeposit, claimerBounty, depositToken) {
389
+ if (depositToken != null) {
390
+ if (!new web3_js_1.PublicKey(depositToken).equals(SolanaTokens_1.SolanaTokens.WSOL_ADDRESS))
391
+ throw new Error("Only SOL supported as deposit token!");
392
+ }
393
+ const tokenAddr = new web3_js_1.PublicKey(token);
394
+ const offererKey = offerer == null ? null : new web3_js_1.PublicKey(offerer);
395
+ const claimerKey = claimer == null ? null : new web3_js_1.PublicKey(claimer);
396
+ const { paymentHash, nonce, confirmations } = (0, Utils_1.fromClaimHash)(claimHash);
397
+ return Promise.resolve(new SolanaSwapData_1.SolanaSwapData(offererKey, claimerKey, tokenAddr, (0, Utils_1.toBN)(amount), paymentHash, (0, Utils_1.toBN)(sequence), (0, Utils_1.toBN)(expiry), nonce, confirmations, payOut, type == null ? null : SolanaSwapData_1.SolanaSwapData.typeToKind(type), payIn, offererKey == null ? null : payIn ? (0, spl_token_1.getAssociatedTokenAddressSync)(tokenAddr, offererKey) : web3_js_1.PublicKey.default, claimerKey == null ? null : payOut ? (0, spl_token_1.getAssociatedTokenAddressSync)(tokenAddr, claimerKey) : web3_js_1.PublicKey.default, (0, Utils_1.toBN)(securityDeposit), (0, Utils_1.toBN)(claimerBounty), null));
398
+ }
399
+ ////////////////////////////////////////////
400
+ //// Utils
401
+ async getBalance(signer, tokenAddress, inContract) {
402
+ if (!inContract) {
403
+ return await this.Chain.getBalance(signer, tokenAddress);
404
+ }
405
+ const token = new web3_js_1.PublicKey(tokenAddress);
406
+ const publicKey = new web3_js_1.PublicKey(signer);
407
+ return await this.getIntermediaryBalance(publicKey, token);
408
+ }
409
+ getIntermediaryData(address, token) {
410
+ return this.LpVault.getIntermediaryData(new web3_js_1.PublicKey(address), new web3_js_1.PublicKey(token));
411
+ }
412
+ getIntermediaryReputation(address, token) {
413
+ return this.LpVault.getIntermediaryReputation(new web3_js_1.PublicKey(address), new web3_js_1.PublicKey(token));
414
+ }
415
+ getIntermediaryBalance(address, token) {
416
+ return this.LpVault.getIntermediaryBalance(address, token);
417
+ }
418
+ ////////////////////////////////////////////
419
+ //// Transaction initializers
420
+ async txsClaimWithSecret(signer, swapData, secret, checkExpiry, initAta, feeRate, skipAtaCheck) {
421
+ return this.Claim.txsClaimWithSecret(typeof (signer) === "string" ? new web3_js_1.PublicKey(signer) : signer.getPublicKey(), swapData, secret, checkExpiry, initAta, feeRate, skipAtaCheck);
422
+ }
423
+ async txsClaimWithTxData(signer, swapData, tx, requiredConfirmations, vout, commitedHeader, synchronizer, initAta, feeRate, storageAccHolder) {
424
+ if (swapData.confirmations !== requiredConfirmations)
425
+ throw new Error("Invalid requiredConfirmations provided!");
426
+ return this.Claim.txsClaimWithTxData(typeof (signer) === "string" ? new web3_js_1.PublicKey(signer) : signer, swapData, tx, vout, commitedHeader, synchronizer, initAta, storageAccHolder, feeRate);
427
+ }
428
+ txsRefund(signer, swapData, check, initAta, feeRate) {
429
+ if (!swapData.isOfferer(signer))
430
+ throw new Error("Only offerer can refund on Solana");
431
+ return this.Refund.txsRefund(swapData, check, initAta, feeRate);
432
+ }
433
+ txsRefundWithAuthorization(signer, swapData, { timeout, prefix, signature }, check, initAta, feeRate) {
434
+ if (!swapData.isOfferer(signer))
435
+ throw new Error("Only offerer can refund on Solana");
436
+ return this.Refund.txsRefundWithAuthorization(swapData, timeout, prefix, signature, check, initAta, feeRate);
437
+ }
438
+ txsInit(sender, swapData, { timeout, prefix, signature }, skipChecks, feeRate) {
439
+ if (swapData.isPayIn()) {
440
+ if (!swapData.isOfferer(sender))
441
+ throw new Error("Only offerer can create payIn=true swap");
442
+ return this.Init.txsInitPayIn(swapData, timeout, prefix, signature, skipChecks, feeRate);
443
+ }
444
+ else {
445
+ if (!swapData.isClaimer(sender))
446
+ throw new Error("Only claimer can create payIn=false swap");
447
+ return this.Init.txsInit(swapData, timeout, prefix, signature, skipChecks, feeRate);
448
+ }
449
+ }
450
+ txsWithdraw(signer, token, amount, feeRate) {
451
+ return this.LpVault.txsWithdraw(new web3_js_1.PublicKey(signer), new web3_js_1.PublicKey(token), amount, feeRate);
452
+ }
453
+ txsDeposit(signer, token, amount, feeRate) {
454
+ return this.LpVault.txsDeposit(new web3_js_1.PublicKey(signer), new web3_js_1.PublicKey(token), amount, feeRate);
455
+ }
456
+ ////////////////////////////////////////////
457
+ //// Executors
458
+ async claimWithSecret(signer, swapData, secret, checkExpiry, initAta, txOptions) {
459
+ const result = await this.Claim.txsClaimWithSecret(signer.getPublicKey(), swapData, secret, checkExpiry, initAta, txOptions?.feeRate);
460
+ const [signature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
461
+ return signature;
462
+ }
463
+ async claimWithTxData(signer, swapData, tx, requiredConfirmations, vout, commitedHeader, synchronizer, initAta, txOptions) {
464
+ if (requiredConfirmations !== swapData.confirmations)
465
+ throw new Error("Invalid requiredConfirmations provided!");
466
+ const data = {
467
+ storageAcc: null
468
+ };
469
+ const txs = await this.Claim.txsClaimWithTxData(signer, swapData, tx, vout, commitedHeader, synchronizer, initAta, data, txOptions?.feeRate);
470
+ if (txs === null)
471
+ throw new Error("Btc relay not synchronized to required blockheight!");
472
+ //TODO: This doesn't return proper tx signature
473
+ const [signature] = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal);
474
+ await this.DataAccount.removeDataAccount(data.storageAcc);
475
+ return signature;
476
+ }
477
+ async refund(signer, swapData, check, initAta, txOptions) {
478
+ let result = await this.txsRefund(signer.getAddress(), swapData, check, initAta, txOptions?.feeRate);
479
+ const [signature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
480
+ return signature;
481
+ }
482
+ async refundWithAuthorization(signer, swapData, signature, check, initAta, txOptions) {
483
+ let result = await this.txsRefundWithAuthorization(signer.getAddress(), swapData, signature, check, initAta, txOptions?.feeRate);
484
+ const [txSignature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
485
+ return txSignature;
486
+ }
487
+ async init(signer, swapData, signature, skipChecks, txOptions) {
488
+ if (swapData.isPayIn()) {
489
+ if (!signer.getPublicKey().equals(swapData.offerer))
490
+ throw new Error("Invalid signer provided!");
491
+ }
492
+ else {
493
+ if (!signer.getPublicKey().equals(swapData.claimer))
494
+ throw new Error("Invalid signer provided!");
495
+ }
496
+ const result = await this.txsInit(signer.getAddress(), swapData, signature, skipChecks, txOptions?.feeRate);
497
+ const [txSignature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
498
+ return txSignature;
499
+ }
500
+ async initAndClaimWithSecret(signer, swapData, signature, secret, skipChecks, txOptions) {
501
+ if (!signer.getPublicKey().equals(swapData.claimer))
502
+ throw new Error("Invalid signer provided!");
503
+ const txsCommit = await this.txsInit(signer.getAddress(), swapData, signature, skipChecks, txOptions?.feeRate);
504
+ const txsClaim = await this.Claim.txsClaimWithSecret(signer.getPublicKey(), swapData, secret, true, false, txOptions?.feeRate, true);
505
+ return await this.Chain.sendAndConfirm(signer, txsCommit.concat(txsClaim), txOptions?.waitForConfirmation, txOptions?.abortSignal);
506
+ }
507
+ async withdraw(signer, token, amount, txOptions) {
508
+ const txs = await this.LpVault.txsWithdraw(signer.getPublicKey(), new web3_js_1.PublicKey(token), amount, txOptions?.feeRate);
509
+ const [txId] = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
510
+ return txId;
511
+ }
512
+ async deposit(signer, token, amount, txOptions) {
513
+ const txs = await this.LpVault.txsDeposit(signer.getPublicKey(), new web3_js_1.PublicKey(token), amount, txOptions?.feeRate);
514
+ const [txId] = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
515
+ return txId;
516
+ }
517
+ ////////////////////////////////////////////
518
+ //// Fees
519
+ getInitPayInFeeRate(offerer, claimer, token, claimHash) {
520
+ const paymentHash = claimHash == null ? null : (0, Utils_1.fromClaimHash)(claimHash).paymentHash;
521
+ return this.Init.getInitPayInFeeRate(toPublicKeyOrNull(offerer), toPublicKeyOrNull(claimer), toPublicKeyOrNull(token), paymentHash);
522
+ }
523
+ getInitFeeRate(offerer, claimer, token, claimHash) {
524
+ const paymentHash = claimHash == null ? null : (0, Utils_1.fromClaimHash)(claimHash).paymentHash;
525
+ return this.Init.getInitFeeRate(toPublicKeyOrNull(offerer), toPublicKeyOrNull(claimer), toPublicKeyOrNull(token), paymentHash);
526
+ }
527
+ getRefundFeeRate(swapData) {
528
+ return this.Refund.getRefundFeeRate(swapData);
529
+ }
530
+ getClaimFeeRate(signer, swapData) {
531
+ return this.Claim.getClaimFeeRate(new web3_js_1.PublicKey(signer), swapData);
532
+ }
533
+ getClaimFee(signer, swapData, feeRate) {
534
+ return this.Claim.getClaimFee(new web3_js_1.PublicKey(signer), swapData, feeRate);
535
+ }
536
+ getRawClaimFee(signer, swapData, feeRate) {
537
+ return this.Claim.getRawClaimFee(new web3_js_1.PublicKey(signer), swapData, feeRate);
538
+ }
539
+ /**
540
+ * Get the estimated solana fee of the commit transaction
541
+ */
542
+ getCommitFee(signer, swapData, feeRate) {
543
+ return this.Init.getInitFee(swapData, feeRate);
544
+ }
545
+ /**
546
+ * Get the estimated solana fee of the commit transaction, without any deposits
547
+ */
548
+ getRawCommitFee(signer, swapData, feeRate) {
549
+ return this.Init.getRawInitFee(swapData, feeRate);
550
+ }
551
+ /**
552
+ * Get the estimated solana transaction fee of the refund transaction
553
+ */
554
+ getRefundFee(signer, swapData, feeRate) {
555
+ return this.Refund.getRefundFee(swapData, feeRate);
556
+ }
557
+ /**
558
+ * Get the estimated solana transaction fee of the refund transaction
559
+ */
560
+ getRawRefundFee(signer, swapData, feeRate) {
561
+ return this.Refund.getRawRefundFee(swapData, feeRate);
562
+ }
563
+ getExtraData(outputScript, amount, confirmations, nonce) {
564
+ return buffer_1.Buffer.from((0, sha2_1.sha256)(buffer_1.Buffer.concat([
565
+ base_1.BigIntBufferUtils.toBuffer(amount, "le", 8),
566
+ outputScript
567
+ ])));
568
+ }
569
+ }
570
+ exports.SolanaSwapProgram = SolanaSwapProgram;