@atomiqlabs/sdk 8.3.6 → 8.4.4

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 (58) hide show
  1. package/dist/bitcoin/wallet/BitcoinWallet.d.ts +3 -2
  2. package/dist/bitcoin/wallet/BitcoinWallet.js +15 -1
  3. package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.d.ts +25 -3
  4. package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.js +51 -2
  5. package/dist/index.d.ts +1 -0
  6. package/dist/index.js +1 -0
  7. package/dist/intermediaries/apis/IntermediaryAPI.d.ts +2 -0
  8. package/dist/intermediaries/apis/IntermediaryAPI.js +2 -0
  9. package/dist/swapper/Swapper.d.ts +2 -1
  10. package/dist/swapper/Swapper.js +4 -3
  11. package/dist/swaps/ISwap.d.ts +34 -0
  12. package/dist/swaps/ISwap.js +10 -0
  13. package/dist/swaps/escrow_swaps/IEscrowSwap.d.ts +12 -0
  14. package/dist/swaps/escrow_swaps/IEscrowSwap.js +18 -0
  15. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.d.ts +54 -4
  16. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +72 -8
  17. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +4 -1
  18. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +23 -4
  19. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.d.ts +89 -3
  20. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +118 -5
  21. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +2 -0
  22. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +30 -4
  23. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.d.ts +48 -3
  24. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +69 -4
  25. package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.d.ts +40 -2
  26. package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +72 -2
  27. package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +51 -1
  28. package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +71 -3
  29. package/dist/swaps/trusted/ln/LnForGasSwap.d.ts +28 -2
  30. package/dist/swaps/trusted/ln/LnForGasSwap.js +39 -1
  31. package/dist/swaps/trusted/onchain/OnchainForGasSwap.d.ts +28 -3
  32. package/dist/swaps/trusted/onchain/OnchainForGasSwap.js +44 -1
  33. package/dist/types/SwapStateInfo.d.ts +5 -0
  34. package/dist/types/SwapStateInfo.js +2 -0
  35. package/dist/types/lnurl/LNURLPay.d.ts +0 -1
  36. package/dist/types/lnurl/LNURLPay.js +0 -1
  37. package/dist/types/lnurl/LNURLWithdraw.d.ts +0 -1
  38. package/dist/types/lnurl/LNURLWithdraw.js +0 -1
  39. package/package.json +3 -1
  40. package/src/bitcoin/wallet/BitcoinWallet.ts +21 -4
  41. package/src/bitcoin/wallet/SingleAddressBitcoinWallet.ts +60 -5
  42. package/src/index.ts +1 -0
  43. package/src/intermediaries/apis/IntermediaryAPI.ts +5 -1
  44. package/src/swapper/Swapper.ts +5 -2
  45. package/src/swaps/ISwap.ts +45 -0
  46. package/src/swaps/escrow_swaps/IEscrowSwap.ts +20 -0
  47. package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.ts +86 -11
  48. package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +28 -3
  49. package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +137 -9
  50. package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +35 -4
  51. package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.ts +83 -6
  52. package/src/swaps/escrow_swaps/tobtc/IToBTCSwap.ts +77 -4
  53. package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +90 -4
  54. package/src/swaps/trusted/ln/LnForGasSwap.ts +51 -3
  55. package/src/swaps/trusted/onchain/OnchainForGasSwap.ts +58 -3
  56. package/src/types/SwapStateInfo.ts +6 -0
  57. package/src/types/lnurl/LNURLPay.ts +0 -1
  58. package/src/types/lnurl/LNURLWithdraw.ts +0 -1
@@ -100,6 +100,7 @@ class FromBTCLNAutoWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
100
100
  return false;
101
101
  }
102
102
  swap._commitTxId = event.meta?.txId;
103
+ swap._commitedAt ??= Date.now();
103
104
  swap._state = FromBTCLNAutoSwap_1.FromBTCLNAutoSwapState.CLAIM_COMMITED;
104
105
  swap._broadcastSecret().catch(e => {
105
106
  this.logger.error("processEventInitialize(" + swap.getId() + "): Error when broadcasting swap secret: ", e);
@@ -177,6 +178,8 @@ class FromBTCLNAutoWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
177
178
  throw new IntermediaryError_1.IntermediaryError("Invalid intermediary address/pubkey");
178
179
  if (options.descriptionHash != null && decodedPr.tagsObject.purpose_commit_hash !== options.descriptionHash.toString("hex"))
179
180
  throw new IntermediaryError_1.IntermediaryError("Invalid pr returned - description hash");
181
+ if (options.description != null && decodedPr.tagsObject.description !== options.description)
182
+ throw new IntermediaryError_1.IntermediaryError("Invalid pr returned - description");
180
183
  if (decodedPr.tagsObject.payment_hash == null ||
181
184
  !buffer_1.Buffer.from(decodedPr.tagsObject.payment_hash, "hex").equals(paymentHash))
182
185
  throw new IntermediaryError_1.IntermediaryError("Invalid pr returned - payment hash");
@@ -215,18 +218,33 @@ class FromBTCLNAutoWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
215
218
  * @param preFetches Optional pre-fetches for speeding up the quoting process (mainly used internally)
216
219
  */
217
220
  create(recipient, amountData, lps, options, additionalParams, abortSignal, preFetches) {
221
+ if (!this.isInitialized)
222
+ throw new Error("Not initialized, call init() first!");
218
223
  const _options = {
224
+ paymentHash: options?.paymentHash,
219
225
  unsafeSkipLnNodeCheck: options?.unsafeSkipLnNodeCheck ?? this._options.unsafeSkipLnNodeCheck,
220
226
  gasAmount: options?.gasAmount ?? 0n,
221
227
  feeSafetyFactor: options?.feeSafetyFactor ?? 1.25,
222
228
  unsafeZeroWatchtowerFee: options?.unsafeZeroWatchtowerFee ?? false,
229
+ description: options?.description,
223
230
  descriptionHash: options?.descriptionHash
224
231
  };
225
- if (preFetches == null)
226
- preFetches = {};
232
+ if (_options.paymentHash != null && _options.paymentHash.length !== 32)
233
+ throw new UserError_1.UserError("Invalid payment hash length, must be exactly 32 bytes!");
227
234
  if (_options.descriptionHash != null && _options.descriptionHash.length !== 32)
228
235
  throw new UserError_1.UserError("Invalid description hash length");
229
- const { secret, paymentHash } = this.getSecretAndHash();
236
+ if (_options.description != null && buffer_1.Buffer.byteLength(_options.description, "utf8") > 500)
237
+ throw new UserError_1.UserError("Invalid description length");
238
+ if (preFetches == null)
239
+ preFetches = {};
240
+ let secret;
241
+ let paymentHash;
242
+ if (_options?.paymentHash != null) {
243
+ paymentHash = _options.paymentHash;
244
+ }
245
+ else {
246
+ ({ secret, paymentHash } = this.getSecretAndHash());
247
+ }
230
248
  const claimHash = this._contract.getHashForHtlc(paymentHash);
231
249
  const nativeTokenAddress = this._chain.getNativeCurrencyAddress();
232
250
  const _abortController = (0, Utils_1.extendAbortController)(abortSignal);
@@ -252,6 +270,7 @@ class FromBTCLNAutoWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
252
270
  amount: amountData.amount,
253
271
  claimer: recipient,
254
272
  token: amountData.token.toString(),
273
+ description: _options.description,
255
274
  descriptionHash: _options.descriptionHash,
256
275
  exactOut: !amountData.exactIn,
257
276
  additionalParams,
@@ -293,7 +312,7 @@ class FromBTCLNAutoWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
293
312
  gasPricingInfo,
294
313
  initialSwapData: await this._contract.createSwapData(base_1.ChainSwapType.HTLC, lp.getAddress(this.chainIdentifier), recipient, amountData.token, resp.total, claimHash.toString("hex"), this.getRandomSequence(), BigInt(Math.floor(Date.now() / 1000)), false, true, _options.gasAmount + resp.claimerBounty, resp.claimerBounty, nativeTokenAddress),
295
314
  pr: resp.pr,
296
- secret: secret.toString("hex"),
315
+ secret: secret?.toString("hex"),
297
316
  exactIn: amountData.exactIn ?? true
298
317
  };
299
318
  const quote = new FromBTCLNAutoSwap_1.FromBTCLNAutoSwap(this, swapInit);
@@ -326,13 +345,19 @@ class FromBTCLNAutoWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
326
345
  * @param abortSignal Abort signal
327
346
  */
328
347
  async createViaLNURL(recipient, lnurl, amountData, lps, options, additionalParams, abortSignal) {
348
+ if (!this.isInitialized)
349
+ throw new Error("Not initialized, call init() first!");
329
350
  const _options = {
351
+ paymentHash: options?.paymentHash,
330
352
  unsafeSkipLnNodeCheck: options?.unsafeSkipLnNodeCheck ?? this._options.unsafeSkipLnNodeCheck,
331
353
  gasAmount: options?.gasAmount ?? 0n,
332
354
  feeSafetyFactor: options?.feeSafetyFactor ?? 1.25,
333
355
  unsafeZeroWatchtowerFee: options?.unsafeZeroWatchtowerFee ?? false,
356
+ description: options?.description,
334
357
  descriptionHash: options?.descriptionHash
335
358
  };
359
+ if (_options.paymentHash != null && _options.paymentHash.length !== 32)
360
+ throw new UserError_1.UserError("Invalid payment hash length, must be exactly 32 bytes!");
336
361
  const abortController = (0, Utils_1.extendAbortController)(abortSignal);
337
362
  const preFetches = {
338
363
  pricePrefetchPromise: this.preFetchPrice(amountData, abortController.signal),
@@ -472,6 +497,7 @@ class FromBTCLNAutoWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
472
497
  swap._commitTxId = await init.getInitTxId();
473
498
  const blockData = await init.getTxBlock();
474
499
  swap.createdAt = blockData.blockTime * 1000;
500
+ swap._commitedAt = blockData.blockTime * 1000;
475
501
  swap._setInitiated();
476
502
  swap._state = FromBTCLNAutoSwap_1.FromBTCLNAutoSwapState.CLAIM_COMMITED;
477
503
  await swap._sync(false, false, state);
@@ -12,6 +12,7 @@ import { IAddressSwap } from "../../../IAddressSwap";
12
12
  import { TokenAmount } from "../../../../types/TokenAmount";
13
13
  import { BtcToken, SCToken } from "../../../../types/Token";
14
14
  import { LoggerType } from "../../../../utils/Logger";
15
+ import { SwapExecutionAction } from "../../../../types/SwapExecutionAction";
15
16
  /**
16
17
  * State enum for legacy escrow based Bitcoin -> Smart chain swaps.
17
18
  *
@@ -50,9 +51,9 @@ export declare enum FromBTCSwapState {
50
51
  */
51
52
  CLAIM_COMMITED = 1,
52
53
  /**
53
- * Input bitcoin transaction was confirmed, wait for automatic settlement by the watchtower
54
- * or settle manually using the {@link FromBTCSwap.claim} or {@link FromBTCSwap.txsClaim}
55
- * function.
54
+ * Input bitcoin transaction was confirmed, wait for automatic settlement by the watchtowers
55
+ * using the {@link FromBTCSwap.waitTillClaimed} function or settle manually using the {@link FromBTCSwap.claim}
56
+ * or {@link FromBTCSwap.txsClaim} function.
56
57
  */
57
58
  BTC_TX_CONFIRMED = 2,
58
59
  /**
@@ -75,6 +76,23 @@ export declare function isFromBTCSwapInit<T extends SwapData>(obj: any): obj is
75
76
  */
76
77
  export declare class FromBTCSwap<T extends ChainType = ChainType> extends IFromBTCSelfInitSwap<T, FromBTCDefinition<T>, FromBTCSwapState> implements IBTCWalletSwap, IClaimableSwap<T, FromBTCDefinition<T>, FromBTCSwapState>, IAddressSwap {
77
78
  protected readonly TYPE: SwapType.FROM_BTC;
79
+ /**
80
+ * @internal
81
+ */
82
+ protected readonly swapStateName: (state: number) => string;
83
+ /**
84
+ * @internal
85
+ */
86
+ protected readonly swapStateDescription: {
87
+ [-4]: string;
88
+ [-3]: string;
89
+ [-2]: string;
90
+ [-1]: string;
91
+ 0: string;
92
+ 1: string;
93
+ 2: string;
94
+ 3: string;
95
+ };
78
96
  /**
79
97
  * @internal
80
98
  */
@@ -97,6 +115,7 @@ export declare class FromBTCSwap<T extends ChainType = ChainType> extends IFromB
97
115
  private senderAddress?;
98
116
  private txId?;
99
117
  private vout?;
118
+ private btcTxConfirmedAt?;
100
119
  constructor(wrapper: FromBTCWrapper<T>, init: FromBTCSwapInit<T["Data"]>);
101
120
  constructor(wrapper: FromBTCWrapper<T>, obj: any);
102
121
  /**
@@ -156,6 +175,10 @@ export declare class FromBTCSwap<T extends ChainType = ChainType> extends IFromB
156
175
  * @inheritDoc
157
176
  */
158
177
  isFailed(): boolean;
178
+ /**
179
+ * @inheritDoc
180
+ */
181
+ isInProgress(): boolean;
159
182
  /**
160
183
  * @inheritDoc
161
184
  */
@@ -293,6 +316,7 @@ export declare class FromBTCSwap<T extends ChainType = ChainType> extends IFromB
293
316
  /**
294
317
  * @inheritDoc
295
318
  *
319
+ * @param options.bitcoinFeeRate Optional fee rate to use for the created Bitcoin transaction
296
320
  * @param options.bitcoinWallet Bitcoin wallet to use, when provided the function returns a funded
297
321
  * psbt (`"FUNDED_PSBT"`), if not passed just a bitcoin receive address is returned (`"ADDRESS"`)
298
322
  * @param options.skipChecks Skip checks like making sure init signature is still valid and swap
@@ -302,6 +326,7 @@ export declare class FromBTCSwap<T extends ChainType = ChainType> extends IFromB
302
326
  * @throws {Error} if the swap or quote is expired, or if triggered in invalid state
303
327
  */
304
328
  txsExecute(options?: {
329
+ bitcoinFeeRate?: number;
305
330
  bitcoinWallet?: MinimalBitcoinWalletInterface;
306
331
  skipChecks?: boolean;
307
332
  }): Promise<({
@@ -329,6 +354,26 @@ export declare class FromBTCSwap<T extends ChainType = ChainType> extends IFromB
329
354
  hyperlink?: undefined;
330
355
  })[];
331
356
  })[]>;
357
+ /**
358
+ * @inheritDoc
359
+ *
360
+ * @param options.bitcoinFeeRate Optional fee rate to use for the created Bitcoin transaction
361
+ * @param options.bitcoinWallet Bitcoin wallet to use, when provided the function returns a funded
362
+ * psbt (`"FUNDED_PSBT"`), if not passed just a bitcoin receive address is returned (`"ADDRESS"`)
363
+ * @param options.skipChecks Skip checks like making sure init signature is still valid and swap
364
+ * wasn't commited yet (this is handled on swap creation, if you commit right after quoting, you
365
+ * can use `skipChecks=true`)
366
+ * @param options.manualSettlementSmartChainSigner Optional smart chain signer to create a manual claim (settlement) transaction
367
+ * @param options.maxWaitTillAutomaticSettlementSeconds Maximum time to wait for an automatic settlement after
368
+ * the bitcoin transaction is confirmed (defaults to 60 seconds)
369
+ */
370
+ getCurrentActions(options?: {
371
+ bitcoinFeeRate?: number;
372
+ bitcoinWallet?: MinimalBitcoinWalletInterface;
373
+ skipChecks?: boolean;
374
+ manualSettlementSmartChainSigner?: string | T["Signer"] | T["NativeSigner"];
375
+ maxWaitTillAutomaticSettlementSeconds?: number;
376
+ }): Promise<SwapExecutionAction<T>[]>;
332
377
  /**
333
378
  * @inheritDoc
334
379
  *
@@ -54,9 +54,9 @@ var FromBTCSwapState;
54
54
  */
55
55
  FromBTCSwapState[FromBTCSwapState["CLAIM_COMMITED"] = 1] = "CLAIM_COMMITED";
56
56
  /**
57
- * Input bitcoin transaction was confirmed, wait for automatic settlement by the watchtower
58
- * or settle manually using the {@link FromBTCSwap.claim} or {@link FromBTCSwap.txsClaim}
59
- * function.
57
+ * Input bitcoin transaction was confirmed, wait for automatic settlement by the watchtowers
58
+ * using the {@link FromBTCSwap.waitTillClaimed} function or settle manually using the {@link FromBTCSwap.claim}
59
+ * or {@link FromBTCSwap.txsClaim} function.
60
60
  */
61
61
  FromBTCSwapState[FromBTCSwapState["BTC_TX_CONFIRMED"] = 2] = "BTC_TX_CONFIRMED";
62
62
  /**
@@ -64,6 +64,16 @@ var FromBTCSwapState;
64
64
  */
65
65
  FromBTCSwapState[FromBTCSwapState["CLAIM_CLAIMED"] = 3] = "CLAIM_CLAIMED";
66
66
  })(FromBTCSwapState = exports.FromBTCSwapState || (exports.FromBTCSwapState = {}));
67
+ const FromBTCSwapStateDescription = {
68
+ [FromBTCSwapState.FAILED]: "Bitcoin swap address has expired and the intermediary (LP) has already refunded its funds. No BTC should be sent anymore!",
69
+ [FromBTCSwapState.EXPIRED]: "Bitcoin swap address has expired, user should not send any BTC anymore! Though the intermediary (LP) hasn't refunded yet. So if there is a transaction already in-flight the swap might still succeed.",
70
+ [FromBTCSwapState.QUOTE_EXPIRED]: "Swap has expired for good and there is no way how it can be executed anymore",
71
+ [FromBTCSwapState.QUOTE_SOFT_EXPIRED]: "The swap is expired, though there is still a chance that it will be processed",
72
+ [FromBTCSwapState.PR_CREATED]: "Swap quote was created, initiate it by creating the swap escrow on the destination smart chain",
73
+ [FromBTCSwapState.CLAIM_COMMITED]: "Swap escrow was initiated (committed) on the destination chain, user can send the BTC to the Bitcoin swap address.",
74
+ [FromBTCSwapState.BTC_TX_CONFIRMED]: "Input bitcoin transaction was confirmed, wait for automatic settlement by the watchtower or settle manually.",
75
+ [FromBTCSwapState.CLAIM_CLAIMED]: "Swap successfully settled and funds received on the destination chain"
76
+ };
67
77
  function isFromBTCSwapInit(obj) {
68
78
  return typeof (obj.data) === "object" &&
69
79
  (obj.address == null || typeof (obj.address) === "string") &&
@@ -84,6 +94,14 @@ class FromBTCSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
84
94
  initOrObject.url += "/frombtc";
85
95
  super(wrapper, initOrObject);
86
96
  this.TYPE = SwapType_1.SwapType.FROM_BTC;
97
+ /**
98
+ * @internal
99
+ */
100
+ this.swapStateName = (state) => FromBTCSwapState[state];
101
+ /**
102
+ * @internal
103
+ */
104
+ this.swapStateDescription = FromBTCSwapStateDescription;
87
105
  /**
88
106
  * @internal
89
107
  */
@@ -103,6 +121,7 @@ class FromBTCSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
103
121
  this.txId = initOrObject.txId;
104
122
  this.vout = initOrObject.vout;
105
123
  this.requiredConfirmations = initOrObject.requiredConfirmations ?? this._data.getConfirmationsHint();
124
+ this.btcTxConfirmedAt = initOrObject.btcTxConfirmedAt;
106
125
  }
107
126
  this.tryRecomputeSwapPrice();
108
127
  this.logger = (0, Logger_1.getLogger)("FromBTC(" + this.getIdentifierHashString() + "): ");
@@ -218,6 +237,13 @@ class FromBTCSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
218
237
  isFailed() {
219
238
  return this._state === FromBTCSwapState.FAILED || this._state === FromBTCSwapState.EXPIRED;
220
239
  }
240
+ /**
241
+ * @inheritDoc
242
+ */
243
+ isInProgress() {
244
+ return this._state === FromBTCSwapState.CLAIM_COMMITED ||
245
+ this._state === FromBTCSwapState.BTC_TX_CONFIRMED;
246
+ }
221
247
  /**
222
248
  * @inheritDoc
223
249
  */
@@ -415,6 +441,7 @@ class FromBTCSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
415
441
  this.senderAddress = result.tx.inputAddresses[0];
416
442
  if (this._state !== FromBTCSwapState.CLAIM_CLAIMED &&
417
443
  this._state !== FromBTCSwapState.FAILED) {
444
+ this.btcTxConfirmedAt ??= Date.now();
418
445
  this._state = FromBTCSwapState.BTC_TX_CONFIRMED;
419
446
  }
420
447
  await this._saveAndEmit();
@@ -598,6 +625,7 @@ class FromBTCSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
598
625
  /**
599
626
  * @inheritDoc
600
627
  *
628
+ * @param options.bitcoinFeeRate Optional fee rate to use for the created Bitcoin transaction
601
629
  * @param options.bitcoinWallet Bitcoin wallet to use, when provided the function returns a funded
602
630
  * psbt (`"FUNDED_PSBT"`), if not passed just a bitcoin receive address is returned (`"ADDRESS"`)
603
631
  * @param options.skipChecks Skip checks like making sure init signature is still valid and swap
@@ -661,6 +689,40 @@ class FromBTCSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
661
689
  }
662
690
  throw new Error("Invalid swap state to obtain execution txns, required PR_CREATED or CLAIM_COMMITED");
663
691
  }
692
+ /**
693
+ * @inheritDoc
694
+ *
695
+ * @param options.bitcoinFeeRate Optional fee rate to use for the created Bitcoin transaction
696
+ * @param options.bitcoinWallet Bitcoin wallet to use, when provided the function returns a funded
697
+ * psbt (`"FUNDED_PSBT"`), if not passed just a bitcoin receive address is returned (`"ADDRESS"`)
698
+ * @param options.skipChecks Skip checks like making sure init signature is still valid and swap
699
+ * wasn't commited yet (this is handled on swap creation, if you commit right after quoting, you
700
+ * can use `skipChecks=true`)
701
+ * @param options.manualSettlementSmartChainSigner Optional smart chain signer to create a manual claim (settlement) transaction
702
+ * @param options.maxWaitTillAutomaticSettlementSeconds Maximum time to wait for an automatic settlement after
703
+ * the bitcoin transaction is confirmed (defaults to 60 seconds)
704
+ */
705
+ async getCurrentActions(options) {
706
+ if (this._state === FromBTCSwapState.PR_CREATED || this._state === FromBTCSwapState.CLAIM_COMMITED) {
707
+ try {
708
+ return await this.txsExecute(options);
709
+ }
710
+ catch (e) { }
711
+ }
712
+ if (this.isClaimable()) {
713
+ if (this.btcTxConfirmedAt == null ||
714
+ options?.maxWaitTillAutomaticSettlementSeconds === 0 ||
715
+ (Date.now() - this.btcTxConfirmedAt) > (options?.maxWaitTillAutomaticSettlementSeconds ?? 60) * 1000) {
716
+ return [{
717
+ name: "Claim",
718
+ description: "Manually settle (claim) the swap on the destination smart chain",
719
+ chain: this.chainIdentifier,
720
+ txs: await this.txsClaim(options?.manualSettlementSmartChainSigner)
721
+ }];
722
+ }
723
+ }
724
+ return [];
725
+ }
664
726
  //////////////////////////////
665
727
  //// Commit
666
728
  /**
@@ -889,7 +951,8 @@ class FromBTCSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
889
951
  requiredConfirmations: this.requiredConfirmations,
890
952
  senderAddress: this.senderAddress,
891
953
  txId: this.txId,
892
- vout: this.vout
954
+ vout: this.vout,
955
+ btcTxConfirmedAt: this.btcTxConfirmedAt
893
956
  };
894
957
  }
895
958
  //////////////////////////////
@@ -992,6 +1055,7 @@ class FromBTCSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
992
1055
  save = true;
993
1056
  }
994
1057
  if (this.requiredConfirmations != null && res.confirmations >= this.requiredConfirmations) {
1058
+ this.btcTxConfirmedAt ??= Date.now();
995
1059
  this._state = FromBTCSwapState.BTC_TX_CONFIRMED;
996
1060
  save = true;
997
1061
  }
@@ -1037,6 +1101,7 @@ class FromBTCSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
1037
1101
  shouldSave = true;
1038
1102
  }
1039
1103
  if (this.requiredConfirmations != null && res.confirmations >= this.requiredConfirmations) {
1104
+ this.btcTxConfirmedAt ??= Date.now();
1040
1105
  this._state = FromBTCSwapState.BTC_TX_CONFIRMED;
1041
1106
  if (save)
1042
1107
  await this._saveAndEmit();
@@ -7,7 +7,7 @@ import { IRefundableSwap } from "../../IRefundableSwap";
7
7
  import { FeeType } from "../../../enums/FeeType";
8
8
  import { TokenAmount } from "../../../types/TokenAmount";
9
9
  import { BtcToken, SCToken } from "../../../types/Token";
10
- import { SwapExecutionActionCommit } from "../../../types/SwapExecutionAction";
10
+ import { SwapExecutionAction, SwapExecutionActionCommit } from "../../../types/SwapExecutionAction";
11
11
  export type IToBTCSwapInit<T extends SwapData> = IEscrowSelfInitSwapInit<T> & {
12
12
  signatureData?: SignatureData;
13
13
  data: T;
@@ -67,6 +67,23 @@ export declare enum ToBTCSwapState {
67
67
  * @category Swaps/Smart chain → Bitcoin
68
68
  */
69
69
  export declare abstract class IToBTCSwap<T extends ChainType = ChainType, D extends IToBTCDefinition<T, IToBTCWrapper<T, D>, IToBTCSwap<T, D>> = IToBTCDefinition<T, IToBTCWrapper<T, any>, IToBTCSwap<T, any>>> extends IEscrowSelfInitSwap<T, D, ToBTCSwapState> implements IRefundableSwap<T, D, ToBTCSwapState> {
70
+ /**
71
+ * @internal
72
+ */
73
+ protected readonly swapStateDescription: {
74
+ [-3]: string;
75
+ [-2]: string;
76
+ [-1]: string;
77
+ 0: string;
78
+ 1: string;
79
+ 2: string;
80
+ 3: string;
81
+ 4: string;
82
+ };
83
+ /**
84
+ * @internal
85
+ */
86
+ protected readonly swapStateName: (state: number) => string;
70
87
  /**
71
88
  * @internal
72
89
  */
@@ -155,6 +172,15 @@ export declare abstract class IToBTCSwap<T extends ChainType = ChainType, D exte
155
172
  * @inheritDoc
156
173
  */
157
174
  isFailed(): boolean;
175
+ /**
176
+ * @inheritDoc
177
+ */
178
+ isInProgress(): boolean;
179
+ /**
180
+ * Returns the time (in UNIX milliseconds) at which the swap expires and the user is able to unilaterally
181
+ * refund it with the {@link refund} or {@link txsRefund} function.
182
+ */
183
+ getExpiry(): number;
158
184
  /**
159
185
  * @inheritDoc
160
186
  * @internal
@@ -239,6 +265,7 @@ export declare abstract class IToBTCSwap<T extends ChainType = ChainType, D exte
239
265
  }): Promise<boolean>;
240
266
  /**
241
267
  * @inheritDoc
268
+ *
242
269
  * @param options.skipChecks Skip checks like making sure init signature is still valid and swap wasn't commited yet
243
270
  * (this is handled on swap creation, if you commit right after quoting, you can use `skipChecks=true`)
244
271
  */
@@ -247,6 +274,17 @@ export declare abstract class IToBTCSwap<T extends ChainType = ChainType, D exte
247
274
  }): Promise<[
248
275
  SwapExecutionActionCommit<T>
249
276
  ]>;
277
+ /**
278
+ * @inheritDoc
279
+ *
280
+ * @param options.skipChecks Skip checks like making sure init signature is still valid and swap wasn't commited yet
281
+ * (this is handled on swap creation, if you commit right after quoting, you can use `skipChecks=true`)
282
+ * @param options.refundSmartChainSigner Optional smart chain signer to use when creating refunds transactions
283
+ */
284
+ getCurrentActions(options?: {
285
+ skipChecks?: boolean;
286
+ refundSmartChainSigner?: string | T["Signer"] | T["NativeSigner"];
287
+ }): Promise<SwapExecutionAction<T>[]>;
250
288
  /**
251
289
  * @inheritDoc
252
290
  *
@@ -311,7 +349,7 @@ export declare abstract class IToBTCSwap<T extends ChainType = ChainType, D exte
311
349
  * @throws {SignatureVerificationError} If intermediary returned invalid cooperative refund signature
312
350
  * @throws {Error} When state is not refundable
313
351
  */
314
- txsRefund(signer?: string): Promise<T["TX"][]>;
352
+ txsRefund(_signer?: string | T["Signer"] | T["NativeSigner"]): Promise<T["TX"][]>;
315
353
  /**
316
354
  * @inheritDoc
317
355
  *
@@ -68,6 +68,16 @@ var ToBTCSwapState;
68
68
  */
69
69
  ToBTCSwapState[ToBTCSwapState["REFUNDABLE"] = 4] = "REFUNDABLE";
70
70
  })(ToBTCSwapState = exports.ToBTCSwapState || (exports.ToBTCSwapState = {}));
71
+ const ToBTCSwapStateDescription = {
72
+ [ToBTCSwapState.REFUNDED]: "Intermediary (LP) was unable to process the swap and the funds were refunded on the source chain",
73
+ [ToBTCSwapState.QUOTE_EXPIRED]: "Swap has expired for good and there is no way how it can be executed anymore",
74
+ [ToBTCSwapState.QUOTE_SOFT_EXPIRED]: "A swap is expired, though there is still a chance that it will be processed",
75
+ [ToBTCSwapState.CREATED]: "Swap was created, initiate it by creating the swap escrow on the source chain",
76
+ [ToBTCSwapState.COMMITED]: "Swap escrow was initiated (committed) on the source chain, the intermediary (LP) will now process the swap.",
77
+ [ToBTCSwapState.SOFT_CLAIMED]: "The intermediary (LP) has processed the transaction and sent out the funds on the destination chain, but hasn't yet settled the escrow on the source chain.",
78
+ [ToBTCSwapState.CLAIMED]: "Swap was successfully settled by the intermediary (LP) on the source chain",
79
+ [ToBTCSwapState.REFUNDABLE]: "Intermediary (LP) was unable to process the swap and the swap escrow on the source chain is refundable."
80
+ };
71
81
  /**
72
82
  * Base class for escrow-based Smart chain -> Bitcoin (on-chain & lightning) swaps
73
83
  *
@@ -76,6 +86,14 @@ var ToBTCSwapState;
76
86
  class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
77
87
  constructor(wrapper, initOrObject) {
78
88
  super(wrapper, initOrObject);
89
+ /**
90
+ * @internal
91
+ */
92
+ this.swapStateDescription = ToBTCSwapStateDescription;
93
+ /**
94
+ * @internal
95
+ */
96
+ this.swapStateName = (state) => ToBTCSwapState[state];
79
97
  if (isIToBTCSwapInit(initOrObject)) {
80
98
  this._state = ToBTCSwapState.CREATED;
81
99
  this.networkFee = initOrObject.networkFee;
@@ -203,6 +221,19 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
203
221
  isFailed() {
204
222
  return this._state === ToBTCSwapState.REFUNDED;
205
223
  }
224
+ /**
225
+ * @inheritDoc
226
+ */
227
+ isInProgress() {
228
+ return this._state === ToBTCSwapState.COMMITED || this._state === ToBTCSwapState.SOFT_CLAIMED;
229
+ }
230
+ /**
231
+ * Returns the time (in UNIX milliseconds) at which the swap expires and the user is able to unilaterally
232
+ * refund it with the {@link refund} or {@link txsRefund} function.
233
+ */
234
+ getExpiry() {
235
+ return Number(this._data.getExpiry()) * 1000;
236
+ }
206
237
  /**
207
238
  * @inheritDoc
208
239
  * @internal
@@ -373,6 +404,7 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
373
404
  }
374
405
  /**
375
406
  * @inheritDoc
407
+ *
376
408
  * @param options.skipChecks Skip checks like making sure init signature is still valid and swap wasn't commited yet
377
409
  * (this is handled on swap creation, if you commit right after quoting, you can use `skipChecks=true`)
378
410
  */
@@ -389,6 +421,30 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
389
421
  }
390
422
  ];
391
423
  }
424
+ /**
425
+ * @inheritDoc
426
+ *
427
+ * @param options.skipChecks Skip checks like making sure init signature is still valid and swap wasn't commited yet
428
+ * (this is handled on swap creation, if you commit right after quoting, you can use `skipChecks=true`)
429
+ * @param options.refundSmartChainSigner Optional smart chain signer to use when creating refunds transactions
430
+ */
431
+ async getCurrentActions(options) {
432
+ if (this._state === ToBTCSwapState.CREATED) {
433
+ try {
434
+ return await this.txsExecute(options);
435
+ }
436
+ catch (e) { }
437
+ }
438
+ if (this.isRefundable()) {
439
+ return [{
440
+ name: "Refund",
441
+ description: "Refund the swap after it failed to execute",
442
+ chain: this.chainIdentifier,
443
+ txs: await this.txsRefund(options?.refundSmartChainSigner)
444
+ }];
445
+ }
446
+ return [];
447
+ }
392
448
  //////////////////////////////
393
449
  //// Commit
394
450
  /**
@@ -623,10 +679,24 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
623
679
  * @throws {SignatureVerificationError} If intermediary returned invalid cooperative refund signature
624
680
  * @throws {Error} When state is not refundable
625
681
  */
626
- async txsRefund(signer) {
682
+ async txsRefund(_signer) {
627
683
  if (!this.isRefundable())
628
684
  throw new Error("Must be in REFUNDABLE state or expired!");
629
- signer ??= this._getInitiator();
685
+ let signer;
686
+ if (_signer != null) {
687
+ if (typeof (_signer) === "string") {
688
+ signer = _signer;
689
+ }
690
+ else if ((0, base_1.isAbstractSigner)(_signer)) {
691
+ signer = _signer.getAddress();
692
+ }
693
+ else {
694
+ signer = (await this.wrapper._chain.wrapSigner(_signer)).getAddress();
695
+ }
696
+ }
697
+ else {
698
+ signer = this._getInitiator();
699
+ }
630
700
  if (await this.wrapper._contract.isExpired(this._getInitiator(), this._data)) {
631
701
  return await this.wrapper._contract.txsRefund(signer, this._data, true, true);
632
702
  }
@@ -14,7 +14,7 @@ import { TokenAmount } from "../../types/TokenAmount";
14
14
  import { BtcToken, SCToken } from "../../types/Token";
15
15
  import { LoggerType } from "../../utils/Logger";
16
16
  import { PriceInfoType } from "../../types/PriceInfoType";
17
- import { SwapExecutionActionBitcoin } from "../../types/SwapExecutionAction";
17
+ import { SwapExecutionAction, SwapExecutionActionBitcoin } from "../../types/SwapExecutionAction";
18
18
  /**
19
19
  * State enum for SPV vault (UTXO-controlled vault) based swaps
20
20
  * @category Swaps/Bitcoin → Smart chain
@@ -120,6 +120,27 @@ export declare function isSpvFromBTCSwapInit(obj: any): obj is SpvFromBTCSwapIni
120
120
  */
121
121
  export declare class SpvFromBTCSwap<T extends ChainType> extends ISwap<T, SpvFromBTCTypeDefinition<T>> implements IBTCWalletSwap, ISwapWithGasDrop<T>, IClaimableSwap<T, SpvFromBTCTypeDefinition<T>, SpvFromBTCSwapState> {
122
122
  readonly TYPE: SwapType.SPV_VAULT_FROM_BTC;
123
+ /**
124
+ * @internal
125
+ */
126
+ protected readonly swapStateDescription: {
127
+ [-5]: string;
128
+ [-4]: string;
129
+ [-3]: string;
130
+ [-2]: string;
131
+ [-1]: string;
132
+ 0: string;
133
+ 1: string;
134
+ 2: string;
135
+ 3: string;
136
+ 4: string;
137
+ 5: string;
138
+ 6: string;
139
+ };
140
+ /**
141
+ * @internal
142
+ */
143
+ protected readonly swapStateName: (state: number) => string;
123
144
  /**
124
145
  * @inheritDoc
125
146
  * @internal
@@ -172,6 +193,11 @@ export declare class SpvFromBTCSwap<T extends ChainType> extends ISwap<T, SpvFro
172
193
  * Minimum fee rate in sats/vB that the input bitcoin transaction needs to pay
173
194
  */
174
195
  readonly minimumBtcFeeRate: number;
196
+ /**
197
+ * Time at which the SDK realized the bitcoin transaction was confirmed
198
+ * @private
199
+ */
200
+ private btcTxConfirmedAt?;
175
201
  constructor(wrapper: SpvFromBTCWrapper<T>, init: SpvFromBTCSwapInit);
176
202
  constructor(wrapper: SpvFromBTCWrapper<T>, obj: any);
177
203
  /**
@@ -252,6 +278,10 @@ export declare class SpvFromBTCSwap<T extends ChainType> extends ISwap<T, SpvFro
252
278
  * @inheritDoc
253
279
  */
254
280
  isFailed(): boolean;
281
+ /**
282
+ * @inheritDoc
283
+ */
284
+ isInProgress(): boolean;
255
285
  /**
256
286
  * @inheritDoc
257
287
  */
@@ -440,16 +470,36 @@ export declare class SpvFromBTCSwap<T extends ChainType> extends ISwap<T, SpvFro
440
470
  /**
441
471
  * @inheritDoc
442
472
  *
473
+ * @param options.bitcoinFeeRate Optional fee rate to use for the created Bitcoin transaction
443
474
  * @param options.bitcoinWallet Optional bitcoin wallet address specification to return a funded PSBT,
444
475
  * if not provided a raw PSBT is returned instead which necessitates the implementor to manually add
445
476
  * inputs to the bitcoin transaction and **set the nSequence field of the 2nd input** (input 1 -
446
477
  * indexing from 0) to the value returned in `in1sequence`
447
478
  */
448
479
  txsExecute(options?: {
480
+ bitcoinFeeRate?: number;
449
481
  bitcoinWallet?: MinimalBitcoinWalletInterface;
450
482
  }): Promise<[
451
483
  SwapExecutionActionBitcoin<"RAW_PSBT" | "FUNDED_PSBT">
452
484
  ]>;
485
+ /**
486
+ * @inheritDoc
487
+ *
488
+ * @param options.bitcoinFeeRate Optional fee rate to use for the created Bitcoin transaction
489
+ * @param options.bitcoinWallet Optional bitcoin wallet address specification to return a funded PSBT,
490
+ * if not provided a raw PSBT is returned instead which necessitates the implementor to manually add
491
+ * inputs to the bitcoin transaction and **set the nSequence field of the 2nd input** (input 1 -
492
+ * indexing from 0) to the value returned in `in1sequence`
493
+ * @param options.manualSettlementSmartChainSigner Optional smart chain signer to create a manual claim (settlement) transaction
494
+ * @param options.maxWaitTillAutomaticSettlementSeconds Maximum time to wait for an automatic settlement after
495
+ * the bitcoin transaction is confirmed (defaults to 60 seconds)
496
+ */
497
+ getCurrentActions(options?: {
498
+ bitcoinFeeRate?: number;
499
+ bitcoinWallet?: MinimalBitcoinWalletInterface;
500
+ manualSettlementSmartChainSigner?: string | T["Signer"] | T["NativeSigner"];
501
+ maxWaitTillAutomaticSettlementSeconds?: number;
502
+ }): Promise<SwapExecutionAction<T>[]>;
453
503
  /**
454
504
  * Checks whether a bitcoin payment was already made, returns the payment or null when no payment has been made.
455
505
  * @internal