@atomiqlabs/sdk 8.8.3 → 8.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (130) hide show
  1. package/api/index.d.ts +1 -0
  2. package/api/index.js +3 -0
  3. package/dist/ApiList.d.ts +37 -0
  4. package/dist/ApiList.js +30 -0
  5. package/dist/api/ApiEndpoints.d.ts +393 -0
  6. package/dist/api/ApiEndpoints.js +2 -0
  7. package/dist/api/ApiParser.d.ts +10 -0
  8. package/dist/api/ApiParser.js +134 -0
  9. package/dist/api/ApiTypes.d.ts +157 -0
  10. package/dist/api/ApiTypes.js +75 -0
  11. package/dist/api/SerializedAction.d.ts +40 -0
  12. package/dist/api/SerializedAction.js +59 -0
  13. package/dist/api/SwapperApi.d.ts +50 -0
  14. package/dist/api/SwapperApi.js +431 -0
  15. package/dist/api/index.d.ts +5 -0
  16. package/dist/api/index.js +24 -0
  17. package/dist/events/UnifiedSwapEventListener.d.ts +4 -3
  18. package/dist/events/UnifiedSwapEventListener.js +8 -2
  19. package/dist/http/HttpUtils.d.ts +4 -2
  20. package/dist/http/HttpUtils.js +10 -4
  21. package/dist/http/paramcoders/client/StreamingFetchPromise.d.ts +2 -1
  22. package/dist/http/paramcoders/client/StreamingFetchPromise.js +3 -2
  23. package/dist/index.d.ts +1 -0
  24. package/dist/index.js +1 -0
  25. package/dist/intermediaries/IntermediaryDiscovery.d.ts +7 -2
  26. package/dist/intermediaries/IntermediaryDiscovery.js +4 -4
  27. package/dist/intermediaries/apis/IntermediaryAPI.d.ts +171 -14
  28. package/dist/intermediaries/apis/IntermediaryAPI.js +174 -28
  29. package/dist/intermediaries/auth/SignedKeyBasedAuth.d.ts +14 -0
  30. package/dist/intermediaries/auth/SignedKeyBasedAuth.js +68 -0
  31. package/dist/storage/IUnifiedStorage.d.ts +45 -3
  32. package/dist/storage/UnifiedSwapStorage.d.ts +8 -2
  33. package/dist/storage/UnifiedSwapStorage.js +46 -8
  34. package/dist/swapper/Swapper.d.ts +36 -3
  35. package/dist/swapper/Swapper.js +54 -18
  36. package/dist/swapper/SwapperUtils.d.ts +18 -2
  37. package/dist/swapper/SwapperUtils.js +39 -1
  38. package/dist/swaps/ISwap.d.ts +70 -9
  39. package/dist/swaps/ISwap.js +28 -6
  40. package/dist/swaps/ISwapWrapper.d.ts +11 -1
  41. package/dist/swaps/ISwapWrapper.js +23 -3
  42. package/dist/swaps/escrow_swaps/IEscrowSwap.d.ts +1 -1
  43. package/dist/swaps/escrow_swaps/IEscrowSwap.js +4 -2
  44. package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.d.ts +2 -1
  45. package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.js +2 -2
  46. package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.d.ts +3 -1
  47. package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.js +3 -2
  48. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.d.ts +47 -31
  49. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +201 -67
  50. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +3 -1
  51. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +6 -6
  52. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.d.ts +82 -15
  53. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +304 -98
  54. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +3 -1
  55. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +6 -6
  56. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.d.ts +75 -42
  57. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +424 -87
  58. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.d.ts +3 -1
  59. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.js +7 -7
  60. package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.d.ts +54 -11
  61. package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +214 -41
  62. package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.d.ts +2 -1
  63. package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.js +7 -8
  64. package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.d.ts +3 -1
  65. package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.js +5 -5
  66. package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +76 -19
  67. package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +290 -51
  68. package/dist/swaps/spv_swaps/SpvFromBTCWrapper.d.ts +3 -1
  69. package/dist/swaps/spv_swaps/SpvFromBTCWrapper.js +5 -5
  70. package/dist/swaps/trusted/ln/LnForGasSwap.d.ts +53 -12
  71. package/dist/swaps/trusted/ln/LnForGasSwap.js +163 -49
  72. package/dist/swaps/trusted/ln/LnForGasWrapper.js +1 -2
  73. package/dist/swaps/trusted/onchain/OnchainForGasSwap.d.ts +14 -13
  74. package/dist/swaps/trusted/onchain/OnchainForGasSwap.js +30 -47
  75. package/dist/swaps/trusted/onchain/OnchainForGasWrapper.d.ts +3 -1
  76. package/dist/swaps/trusted/onchain/OnchainForGasWrapper.js +4 -4
  77. package/dist/types/SwapExecutionAction.d.ts +141 -34
  78. package/dist/types/SwapExecutionAction.js +104 -0
  79. package/dist/types/SwapExecutionStep.d.ts +144 -0
  80. package/dist/types/SwapExecutionStep.js +87 -0
  81. package/dist/types/TokenAmount.d.ts +6 -0
  82. package/dist/types/TokenAmount.js +26 -1
  83. package/dist/utils/BitcoinUtils.d.ts +2 -0
  84. package/dist/utils/BitcoinUtils.js +34 -1
  85. package/dist/utils/Utils.d.ts +3 -1
  86. package/dist/utils/Utils.js +7 -1
  87. package/package.json +7 -4
  88. package/src/api/ApiEndpoints.ts +427 -0
  89. package/src/api/ApiParser.ts +138 -0
  90. package/src/api/ApiTypes.ts +229 -0
  91. package/src/api/SerializedAction.ts +97 -0
  92. package/src/api/SwapperApi.ts +545 -0
  93. package/src/api/index.ts +5 -0
  94. package/src/events/UnifiedSwapEventListener.ts +11 -3
  95. package/src/http/HttpUtils.ts +10 -4
  96. package/src/http/paramcoders/client/StreamingFetchPromise.ts +4 -2
  97. package/src/index.ts +1 -0
  98. package/src/intermediaries/IntermediaryDiscovery.ts +9 -2
  99. package/src/intermediaries/apis/IntermediaryAPI.ts +314 -30
  100. package/src/intermediaries/auth/SignedKeyBasedAuth.ts +69 -0
  101. package/src/storage/IUnifiedStorage.ts +45 -4
  102. package/src/storage/UnifiedSwapStorage.ts +42 -8
  103. package/src/swapper/Swapper.ts +87 -18
  104. package/src/swapper/SwapperUtils.ts +42 -2
  105. package/src/swaps/ISwap.ts +88 -16
  106. package/src/swaps/ISwapWrapper.ts +28 -3
  107. package/src/swaps/escrow_swaps/IEscrowSwap.ts +5 -3
  108. package/src/swaps/escrow_swaps/IEscrowSwapWrapper.ts +3 -1
  109. package/src/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.ts +4 -1
  110. package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.ts +264 -67
  111. package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +6 -4
  112. package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +390 -89
  113. package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +6 -4
  114. package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.ts +548 -94
  115. package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.ts +7 -5
  116. package/src/swaps/escrow_swaps/tobtc/IToBTCSwap.ts +276 -45
  117. package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.ts +7 -6
  118. package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.ts +5 -3
  119. package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +393 -57
  120. package/src/swaps/spv_swaps/SpvFromBTCWrapper.ts +5 -3
  121. package/src/swaps/trusted/ln/LnForGasSwap.ts +211 -47
  122. package/src/swaps/trusted/ln/LnForGasWrapper.ts +1 -2
  123. package/src/swaps/trusted/onchain/OnchainForGasSwap.ts +32 -51
  124. package/src/swaps/trusted/onchain/OnchainForGasWrapper.ts +5 -3
  125. package/src/types/SwapExecutionAction.ts +266 -43
  126. package/src/types/SwapExecutionStep.ts +224 -0
  127. package/src/types/TokenAmount.ts +36 -2
  128. package/src/utils/BitcoinUtils.ts +32 -0
  129. package/src/utils/Utils.ts +10 -1
  130. package/src/intermediaries/apis/TrustedIntermediaryAPI.ts +0 -258
@@ -9,7 +9,6 @@ const SwapType_1 = require("../../enums/SwapType");
9
9
  const Intermediary_1 = require("../../intermediaries/Intermediary");
10
10
  const Utils_1 = require("../../utils/Utils");
11
11
  const BitcoinUtils_1 = require("../../utils/BitcoinUtils");
12
- const IntermediaryAPI_1 = require("../../intermediaries/apis/IntermediaryAPI");
13
12
  const RequestError_1 = require("../../errors/RequestError");
14
13
  const IntermediaryError_1 = require("../../errors/IntermediaryError");
15
14
  const btc_signer_1 = require("@scure/btc-signer");
@@ -37,11 +36,12 @@ class SpvFromBTCWrapper extends ISwapWrapper_1.ISwapWrapper {
37
36
  * @param versionedContracts
38
37
  * @param versionedSynchronizer
39
38
  * @param btcRpc Bitcoin RPC which also supports getting transactions by txoHash
39
+ * @param lpApi
40
40
  * @param options
41
41
  * @param events Instance to use for emitting events
42
42
  */
43
- constructor(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, prices, tokens, versionedContracts, versionedSynchronizer, btcRpc, options, events) {
44
- super(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, prices, tokens, {
43
+ constructor(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, prices, tokens, versionedContracts, versionedSynchronizer, btcRpc, lpApi, options, events) {
44
+ super(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, prices, tokens, lpApi, {
45
45
  ...options,
46
46
  bitcoinNetwork: options?.bitcoinNetwork ?? utils_1.TEST_NETWORK,
47
47
  maxConfirmations: options?.maxConfirmations ?? 6,
@@ -602,7 +602,7 @@ class SpvFromBTCWrapper extends ISwapWrapper_1.ISwapWrapper {
602
602
  const callerFeeRatePromise = this.computeCallerFeeShare(amountPromise, callerFeePrefetchPromise[version], amountData, _options, pricePrefetchPromise, gasTokenPricePrefetchPromise, abortController.signal);
603
603
  try {
604
604
  const resp = await (0, RetryUtils_1.tryWithRetries)(async (retryCount) => {
605
- return await IntermediaryAPI_1.IntermediaryAPI.prepareSpvFromBTC(this.chainIdentifier, lp.url, {
605
+ return await this._lpApi.prepareSpvFromBTC(this.chainIdentifier, lp.url, {
606
606
  address: recipient,
607
607
  amount: (0, Utils_1.throwIfUndefined)(amountPromise, "Failed to compute swap amount"),
608
608
  token: amountData.token.toString(),
@@ -670,7 +670,7 @@ class SpvFromBTCWrapper extends ISwapWrapper_1.ISwapWrapper {
670
670
  callerFeeShare: resp.callerFeeShare,
671
671
  frontingFeeShare: resp.frontingFeeShare,
672
672
  executionFeeShare: resp.executionFeeShare,
673
- genesisSmartChainBlockHeight: await (0, Utils_1.throwIfUndefined)(finalizedBlockHeightPrefetchPromise, "Finalize block height promise failed!"),
673
+ genesisSmartChainBlockHeight: await (0, Utils_1.throwIfUndefined)(finalizedBlockHeightPrefetchPromise, "Network finalized blockheight pre-fetch failed!"),
674
674
  contractVersion: version
675
675
  };
676
676
  const quote = new SpvFromBTCSwap_1.SpvFromBTCSwap(this, swapInit);
@@ -8,7 +8,9 @@ import { FeeType } from "../../../enums/FeeType";
8
8
  import { TokenAmount } from "../../../types/TokenAmount";
9
9
  import { BtcToken, SCToken } from "../../../types/Token";
10
10
  import { LoggerType } from "../../../utils/Logger";
11
- import { SwapExecutionAction, SwapExecutionActionLightning } from "../../../types/SwapExecutionAction";
11
+ import { SwapExecutionActionSendToAddress, SwapExecutionActionWait } from "../../../types/SwapExecutionAction";
12
+ import { SwapExecutionStepPayment, SwapExecutionStepSettlement } from "../../../types/SwapExecutionStep";
13
+ import { SwapStateInfo } from "../../../types/SwapStateInfo";
12
14
  /**
13
15
  * State enum for trusted Lightning gas swaps
14
16
  *
@@ -16,23 +18,23 @@ import { SwapExecutionAction, SwapExecutionActionLightning } from "../../../type
16
18
  */
17
19
  export declare enum LnForGasSwapState {
18
20
  /**
19
- * The swap quote expired without user sending in the lightning network payment
21
+ * The swap quote expired before the user paid the Lightning invoice
20
22
  */
21
23
  EXPIRED = -2,
22
24
  /**
23
- * The swap has failed after the intermediary already received a lightning network payment on the source
25
+ * The swap has failed before the destination payout completed, and the held Lightning invoice was released
24
26
  */
25
27
  FAILED = -1,
26
28
  /**
27
- * Swap was created, pay the provided lightning network invoice
29
+ * Swap was created, pay the provided Lightning invoice which will remain held until destination payout succeeds
28
30
  */
29
31
  PR_CREATED = 0,
30
32
  /**
31
- * User paid the lightning network invoice on the source
33
+ * The Lightning invoice was paid and is currently held until the user receives the destination funds
32
34
  */
33
35
  PR_PAID = 1,
34
36
  /**
35
- * The swap is finished after the intermediary sent funds on the destination chain
37
+ * The swap is finished after the destination payout succeeded and the held Lightning invoice was settled
36
38
  */
37
39
  FINISHED = 2
38
40
  }
@@ -210,17 +212,54 @@ export declare class LnForGasSwap<T extends ChainType = ChainType> extends ISwap
210
212
  fee: Fee<T["ChainId"], BtcToken<true>, SCToken<T["ChainId"]>>;
211
213
  }];
212
214
  /**
215
+ * @remarks Not supported
216
+ */
217
+ execute(): Promise<boolean>;
218
+ /**
219
+ * @internal
220
+ */
221
+ protected _getExecutionStatus(): Promise<{
222
+ steps: [SwapExecutionStepPayment<"LIGHTNING">, SwapExecutionStepSettlement<T["ChainId"], never>];
223
+ buildCurrentAction: () => Promise<SwapExecutionActionSendToAddress<true> | SwapExecutionActionWait<"LP"> | undefined>;
224
+ state: LnForGasSwapState;
225
+ }>;
226
+ /**
227
+ * @internal
213
228
  * @inheritDoc
214
229
  */
215
- txsExecute(): Promise<[SwapExecutionActionLightning]>;
230
+ _submitExecutionTransactions(): Promise<string[]>;
216
231
  /**
217
- * @remark Not supported
232
+ * @internal
218
233
  */
219
- execute(): Promise<boolean>;
234
+ private _buildLightningPaymentAction;
235
+ /**
236
+ * @internal
237
+ */
238
+ private _buildWaitLpAction;
239
+ /**
240
+ * @inheritDoc
241
+ */
242
+ getExecutionAction(): Promise<SwapExecutionActionSendToAddress<true> | SwapExecutionActionWait<"LP"> | undefined>;
243
+ /**
244
+ * @inheritDoc
245
+ */
246
+ getExecutionStatus(options?: {
247
+ skipBuildingAction?: boolean;
248
+ }): Promise<{
249
+ steps: [
250
+ SwapExecutionStepPayment<"LIGHTNING">,
251
+ SwapExecutionStepSettlement<T["ChainId"], never>
252
+ ];
253
+ currentAction: SwapExecutionActionSendToAddress<true> | SwapExecutionActionWait<"LP"> | undefined;
254
+ stateInfo: SwapStateInfo<LnForGasSwapState>;
255
+ }>;
220
256
  /**
221
257
  * @inheritDoc
222
258
  */
223
- getCurrentActions(): Promise<SwapExecutionAction<T>[]>;
259
+ getExecutionSteps(): Promise<[
260
+ SwapExecutionStepPayment<"LIGHTNING">,
261
+ SwapExecutionStepSettlement<T["ChainId"], never>
262
+ ]>;
224
263
  /**
225
264
  * Queries the intermediary (LP) node for the state of the swap
226
265
  *
@@ -232,13 +271,15 @@ export declare class LnForGasSwap<T extends ChainType = ChainType> extends ISwap
232
271
  protected checkInvoicePaid(save?: boolean): Promise<boolean | null>;
233
272
  /**
234
273
  * A blocking promise resolving when payment was received by the intermediary and client can continue,
235
- * rejecting in case of failure. The swap must be in {@link LnForGasSwapState.PR_CREATED} state!
274
+ * rejecting in case of failure. The swap must be in {@link LnForGasSwapState.PR_CREATED} or
275
+ * {@link LnForGasSwapState.PR_PAID} state!
236
276
  *
237
277
  * @param checkIntervalSeconds How often to poll the intermediary for answer (default 5 seconds)
238
278
  * @param abortSignal Abort signal
279
+ * @param onPaymentReceived Callback as for when the LP reports having received the ln payment
239
280
  * @throws {Error} When in invalid state (not PR_CREATED)
240
281
  */
241
- waitForPayment(checkIntervalSeconds?: number, abortSignal?: AbortSignal): Promise<boolean>;
282
+ waitForPayment(checkIntervalSeconds?: number, abortSignal?: AbortSignal, onPaymentReceived?: (txId: string) => void): Promise<boolean>;
242
283
  /**
243
284
  * @inheritDoc
244
285
  */
@@ -5,7 +5,7 @@ const bolt11_1 = require("@atomiqlabs/bolt11");
5
5
  const SwapType_1 = require("../../../enums/SwapType");
6
6
  const Utils_1 = require("../../../utils/Utils");
7
7
  const ISwap_1 = require("../../ISwap");
8
- const TrustedIntermediaryAPI_1 = require("../../../intermediaries/apis/TrustedIntermediaryAPI");
8
+ const IntermediaryAPI_1 = require("../../../intermediaries/apis/IntermediaryAPI");
9
9
  const FeeType_1 = require("../../../enums/FeeType");
10
10
  const PercentagePPM_1 = require("../../../types/fees/PercentagePPM");
11
11
  const TokenAmount_1 = require("../../../types/TokenAmount");
@@ -20,32 +20,32 @@ const TimeoutUtils_1 = require("../../../utils/TimeoutUtils");
20
20
  var LnForGasSwapState;
21
21
  (function (LnForGasSwapState) {
22
22
  /**
23
- * The swap quote expired without user sending in the lightning network payment
23
+ * The swap quote expired before the user paid the Lightning invoice
24
24
  */
25
25
  LnForGasSwapState[LnForGasSwapState["EXPIRED"] = -2] = "EXPIRED";
26
26
  /**
27
- * The swap has failed after the intermediary already received a lightning network payment on the source
27
+ * The swap has failed before the destination payout completed, and the held Lightning invoice was released
28
28
  */
29
29
  LnForGasSwapState[LnForGasSwapState["FAILED"] = -1] = "FAILED";
30
30
  /**
31
- * Swap was created, pay the provided lightning network invoice
31
+ * Swap was created, pay the provided Lightning invoice which will remain held until destination payout succeeds
32
32
  */
33
33
  LnForGasSwapState[LnForGasSwapState["PR_CREATED"] = 0] = "PR_CREATED";
34
34
  /**
35
- * User paid the lightning network invoice on the source
35
+ * The Lightning invoice was paid and is currently held until the user receives the destination funds
36
36
  */
37
37
  LnForGasSwapState[LnForGasSwapState["PR_PAID"] = 1] = "PR_PAID";
38
38
  /**
39
- * The swap is finished after the intermediary sent funds on the destination chain
39
+ * The swap is finished after the destination payout succeeded and the held Lightning invoice was settled
40
40
  */
41
41
  LnForGasSwapState[LnForGasSwapState["FINISHED"] = 2] = "FINISHED";
42
42
  })(LnForGasSwapState = exports.LnForGasSwapState || (exports.LnForGasSwapState = {}));
43
43
  const LnForGasSwapStateDescription = {
44
- [LnForGasSwapState.EXPIRED]: "The swap quote expired without user sending in the lightning network payment",
45
- [LnForGasSwapState.FAILED]: "The swap has failed after the intermediary already received a lightning network payment on the source",
46
- [LnForGasSwapState.PR_CREATED]: "Swap was created, pay the provided lightning network invoice",
47
- [LnForGasSwapState.PR_PAID]: "User paid the lightning network invoice on the source",
48
- [LnForGasSwapState.FINISHED]: "The swap is finished after the intermediary sent funds on the destination chain"
44
+ [LnForGasSwapState.EXPIRED]: "The swap quote expired before the user paid the Lightning invoice",
45
+ [LnForGasSwapState.FAILED]: "The swap failed before destination payout completed, and the held Lightning invoice was released back to the user",
46
+ [LnForGasSwapState.PR_CREATED]: "Swap was created, pay the provided Lightning invoice. The invoice will remain held until destination payout succeeds",
47
+ [LnForGasSwapState.PR_PAID]: "The Lightning invoice was paid and is currently held. It will only settle once the user receives the destination funds",
48
+ [LnForGasSwapState.FINISHED]: "The swap is finished after the destination payout succeeded and the held Lightning invoice was settled"
49
49
  };
50
50
  function isLnForGasSwapInit(obj) {
51
51
  return typeof (obj.pr) === "string" &&
@@ -330,45 +330,149 @@ class LnForGasSwap extends ISwap_1.ISwap {
330
330
  //////////////////////////////
331
331
  //// Payment
332
332
  /**
333
- * @inheritDoc
333
+ * @remarks Not supported
334
334
  */
335
- async txsExecute() {
336
- if (this._state === LnForGasSwapState.PR_CREATED) {
337
- if (!await this._verifyQuoteValid())
338
- throw new Error("Quote already expired or close to expiry!");
339
- return [
335
+ async execute() {
336
+ throw new Error("Not supported");
337
+ }
338
+ /**
339
+ * @internal
340
+ */
341
+ async _getExecutionStatus() {
342
+ const state = this._state;
343
+ let lightningPaymentStatus = "inactive";
344
+ let destinationSettlementStatus = "inactive";
345
+ let buildCurrentAction = async () => undefined;
346
+ switch (state) {
347
+ case LnForGasSwapState.PR_CREATED: {
348
+ const quoteValid = await this._verifyQuoteValid();
349
+ lightningPaymentStatus = quoteValid ? "awaiting" : "soft_expired";
350
+ if (quoteValid) {
351
+ buildCurrentAction = this._buildLightningPaymentAction.bind(this);
352
+ }
353
+ break;
354
+ }
355
+ case LnForGasSwapState.EXPIRED:
356
+ lightningPaymentStatus = "expired";
357
+ break;
358
+ case LnForGasSwapState.PR_PAID:
359
+ lightningPaymentStatus = "received";
360
+ destinationSettlementStatus = "waiting_lp";
361
+ buildCurrentAction = this._buildWaitLpAction.bind(this);
362
+ break;
363
+ case LnForGasSwapState.FAILED:
364
+ lightningPaymentStatus = "expired";
365
+ destinationSettlementStatus = "expired";
366
+ break;
367
+ case LnForGasSwapState.FINISHED:
368
+ lightningPaymentStatus = "confirmed";
369
+ destinationSettlementStatus = "settled";
370
+ break;
371
+ }
372
+ return {
373
+ steps: [
340
374
  {
341
- name: "Payment",
342
- description: "Initiates the swap by paying up the lightning network invoice",
375
+ type: "Payment",
376
+ side: "source",
343
377
  chain: "LIGHTNING",
344
- txs: [
345
- {
346
- type: "BOLT11_PAYMENT_REQUEST",
347
- address: this.pr,
348
- hyperlink: this.getHyperlink()
349
- }
350
- ]
378
+ title: "Lightning payment",
379
+ description: "Pay the Lightning network invoice to initiate the swap",
380
+ status: lightningPaymentStatus
381
+ },
382
+ {
383
+ type: "Settlement",
384
+ side: "destination",
385
+ chain: this.chainIdentifier,
386
+ title: "Destination payout",
387
+ description: "Wait for the intermediary to send the gas tokens on the destination smart chain",
388
+ status: destinationSettlementStatus
351
389
  }
352
- ];
353
- }
354
- throw new Error("Invalid swap state to obtain execution txns, required PR_CREATED");
390
+ ],
391
+ buildCurrentAction,
392
+ state
393
+ };
355
394
  }
356
395
  /**
357
- * @remark Not supported
396
+ * @internal
397
+ * @inheritDoc
358
398
  */
359
- async execute() {
360
- throw new Error("Not supported");
399
+ _submitExecutionTransactions() {
400
+ throw new Error("Invalid swap state for transaction submission!");
401
+ }
402
+ /**
403
+ * @internal
404
+ */
405
+ async _buildLightningPaymentAction() {
406
+ return {
407
+ type: "SendToAddress",
408
+ name: "Deposit on Lightning",
409
+ description: "Pay the lightning network invoice to initiate the swap",
410
+ chain: "LIGHTNING",
411
+ txs: [{
412
+ type: "BOLT11_PAYMENT_REQUEST",
413
+ address: this.pr,
414
+ hyperlink: this.getHyperlink(),
415
+ amount: this.getInput()
416
+ }],
417
+ waitForTransactions: async (maxWaitTimeSeconds, pollIntervalSeconds, abortSignal) => {
418
+ const abortController = (0, Utils_1.extendAbortController)(abortSignal, maxWaitTimeSeconds, "Timed out waiting for lightning payment");
419
+ let lightningTxId;
420
+ try {
421
+ const success = await this.waitForPayment(pollIntervalSeconds, abortController.signal, (txId) => {
422
+ lightningTxId = txId;
423
+ abortController.abort();
424
+ });
425
+ if (!success)
426
+ throw new Error("Quote expired while waiting for lightning payment");
427
+ }
428
+ catch (e) {
429
+ if (lightningTxId != null)
430
+ return lightningTxId;
431
+ throw e;
432
+ }
433
+ return this.getInputTxId();
434
+ }
435
+ };
436
+ }
437
+ /**
438
+ * @internal
439
+ */
440
+ async _buildWaitLpAction() {
441
+ return {
442
+ type: "Wait",
443
+ name: "Awaiting LP payout",
444
+ description: "Wait for the intermediary to send the gas tokens on the destination smart chain",
445
+ pollTimeSeconds: 5,
446
+ expectedTimeSeconds: 10,
447
+ wait: async (maxWaitTimeSeconds, pollIntervalSeconds, abortSignal) => {
448
+ const abortController = (0, Utils_1.extendAbortController)(abortSignal, maxWaitTimeSeconds, "Timed out waiting for LP payout");
449
+ await this.waitForPayment(pollIntervalSeconds, abortController.signal);
450
+ }
451
+ };
361
452
  }
362
453
  /**
363
454
  * @inheritDoc
364
455
  */
365
- async getCurrentActions() {
366
- try {
367
- return await this.txsExecute();
368
- }
369
- catch (e) {
370
- return [];
371
- }
456
+ async getExecutionAction() {
457
+ const executionStatus = await this._getExecutionStatus();
458
+ return executionStatus.buildCurrentAction();
459
+ }
460
+ /**
461
+ * @inheritDoc
462
+ */
463
+ async getExecutionStatus(options) {
464
+ const executionStatus = await this._getExecutionStatus();
465
+ return {
466
+ steps: executionStatus.steps,
467
+ currentAction: options?.skipBuildingAction ? undefined : await executionStatus.buildCurrentAction(),
468
+ stateInfo: this._getStateInfo(executionStatus.state)
469
+ };
470
+ }
471
+ /**
472
+ * @inheritDoc
473
+ */
474
+ async getExecutionSteps() {
475
+ return (await this._getExecutionStatus()).steps;
372
476
  }
373
477
  /**
374
478
  * Queries the intermediary (LP) node for the state of the swap
@@ -389,10 +493,10 @@ class LnForGasSwap extends ISwap_1.ISwap {
389
493
  const paymentHash = decodedPR.tagsObject.payment_hash;
390
494
  if (paymentHash == null)
391
495
  throw new Error("Invalid swap invoice, payment hash not found!");
392
- const response = await TrustedIntermediaryAPI_1.TrustedIntermediaryAPI.getInvoiceStatus(this.url, paymentHash, this.wrapper._options.getRequestTimeout);
496
+ const response = await this.wrapper._lpApi.getTrustedInvoiceStatus(this.url, paymentHash, this.wrapper._options.getRequestTimeout);
393
497
  this.logger.debug("checkInvoicePaid(): LP response: ", response);
394
498
  switch (response.code) {
395
- case TrustedIntermediaryAPI_1.InvoiceStatusResponseCodes.PAID:
499
+ case IntermediaryAPI_1.TrustedInvoiceStatusResponseCodes.PAID:
396
500
  this.scTxId = response.data.txId;
397
501
  const txStatus = await this.wrapper._chain.getTxIdStatus(this.scTxId);
398
502
  if (txStatus === "success") {
@@ -402,7 +506,7 @@ class LnForGasSwap extends ISwap_1.ISwap {
402
506
  return true;
403
507
  }
404
508
  return null;
405
- case TrustedIntermediaryAPI_1.InvoiceStatusResponseCodes.EXPIRED:
509
+ case IntermediaryAPI_1.TrustedInvoiceStatusResponseCodes.EXPIRED:
406
510
  if (this._state === LnForGasSwapState.PR_CREATED) {
407
511
  this._state = LnForGasSwapState.EXPIRED;
408
512
  }
@@ -412,7 +516,7 @@ class LnForGasSwap extends ISwap_1.ISwap {
412
516
  if (save)
413
517
  await this._saveAndEmit();
414
518
  return false;
415
- case TrustedIntermediaryAPI_1.InvoiceStatusResponseCodes.TX_SENT:
519
+ case IntermediaryAPI_1.TrustedInvoiceStatusResponseCodes.TX_SENT:
416
520
  this.scTxId = response.data.txId;
417
521
  if (this._state === LnForGasSwapState.PR_CREATED) {
418
522
  this._state = LnForGasSwapState.PR_PAID;
@@ -420,14 +524,14 @@ class LnForGasSwap extends ISwap_1.ISwap {
420
524
  await this._saveAndEmit();
421
525
  }
422
526
  return null;
423
- case TrustedIntermediaryAPI_1.InvoiceStatusResponseCodes.PENDING:
527
+ case IntermediaryAPI_1.TrustedInvoiceStatusResponseCodes.PENDING:
424
528
  if (this._state === LnForGasSwapState.PR_CREATED) {
425
529
  this._state = LnForGasSwapState.PR_PAID;
426
530
  if (save)
427
531
  await this._saveAndEmit();
428
532
  }
429
533
  return null;
430
- case TrustedIntermediaryAPI_1.InvoiceStatusResponseCodes.AWAIT_PAYMENT:
534
+ case IntermediaryAPI_1.TrustedInvoiceStatusResponseCodes.AWAIT_PAYMENT:
431
535
  return null;
432
536
  default:
433
537
  this._state = LnForGasSwapState.FAILED;
@@ -438,24 +542,34 @@ class LnForGasSwap extends ISwap_1.ISwap {
438
542
  }
439
543
  /**
440
544
  * A blocking promise resolving when payment was received by the intermediary and client can continue,
441
- * rejecting in case of failure. The swap must be in {@link LnForGasSwapState.PR_CREATED} state!
545
+ * rejecting in case of failure. The swap must be in {@link LnForGasSwapState.PR_CREATED} or
546
+ * {@link LnForGasSwapState.PR_PAID} state!
442
547
  *
443
548
  * @param checkIntervalSeconds How often to poll the intermediary for answer (default 5 seconds)
444
549
  * @param abortSignal Abort signal
550
+ * @param onPaymentReceived Callback as for when the LP reports having received the ln payment
445
551
  * @throws {Error} When in invalid state (not PR_CREATED)
446
552
  */
447
- async waitForPayment(checkIntervalSeconds, abortSignal) {
448
- if (this._state !== LnForGasSwapState.PR_CREATED)
449
- throw new Error("Must be in PR_CREATED state!");
553
+ async waitForPayment(checkIntervalSeconds, abortSignal, onPaymentReceived) {
554
+ if (this._state !== LnForGasSwapState.PR_CREATED && this._state !== LnForGasSwapState.PR_PAID)
555
+ throw new Error("Must be in PR_CREATED or PR_PAID state!");
450
556
  if (!this.initiated) {
451
557
  this.initiated = true;
452
558
  await this._saveAndEmit();
453
559
  }
454
560
  while (!abortSignal?.aborted && (this._state === LnForGasSwapState.PR_CREATED || this._state === LnForGasSwapState.PR_PAID)) {
455
561
  await this.checkInvoicePaid(true);
562
+ if (this._state === LnForGasSwapState.PR_PAID) {
563
+ if (onPaymentReceived != null) {
564
+ onPaymentReceived(this.getInputTxId());
565
+ onPaymentReceived = undefined; // Set to null so it only triggers once
566
+ }
567
+ }
456
568
  if (this._state === LnForGasSwapState.PR_CREATED || this._state === LnForGasSwapState.PR_PAID)
457
569
  await (0, TimeoutUtils_1.timeoutPromise)((checkIntervalSeconds ?? 5) * 1000, abortSignal);
458
570
  }
571
+ if (abortSignal != null)
572
+ abortSignal.throwIfAborted();
459
573
  if (this.isFailed())
460
574
  throw new Error("Swap failed");
461
575
  return !this.isQuoteExpired();
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LnForGasWrapper = void 0;
4
4
  const LnForGasSwap_1 = require("./LnForGasSwap");
5
5
  const ISwapWrapper_1 = require("../../ISwapWrapper");
6
- const TrustedIntermediaryAPI_1 = require("../../../intermediaries/apis/TrustedIntermediaryAPI");
7
6
  const bolt11_1 = require("@atomiqlabs/bolt11");
8
7
  const IntermediaryError_1 = require("../../../errors/IntermediaryError");
9
8
  const SwapType_1 = require("../../../enums/SwapType");
@@ -47,7 +46,7 @@ class LnForGasWrapper extends ISwapWrapper_1.ISwapWrapper {
47
46
  throw new Error("Not initialized, call init() first!");
48
47
  const lpUrl = typeof (lpOrUrl) === "string" ? lpOrUrl : lpOrUrl.url;
49
48
  const token = this._chain.getNativeCurrencyAddress();
50
- const resp = await TrustedIntermediaryAPI_1.TrustedIntermediaryAPI.initTrustedFromBTCLN(this.chainIdentifier, lpUrl, {
49
+ const resp = await this._lpApi.initTrustedFromBTCLN(this.chainIdentifier, lpUrl, {
51
50
  address: recipient,
52
51
  amount,
53
52
  token
@@ -12,7 +12,6 @@ import { FeeType } from "../../../enums/FeeType";
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, SwapExecutionActionBitcoin } from "../../../types/SwapExecutionAction";
16
15
  /**
17
16
  * State enum for trusted on-chain gas swaps
18
17
  *
@@ -266,24 +265,26 @@ export declare class OnchainForGasSwap<T extends ChainType = ChainType> extends
266
265
  * @param options.bitcoinWallet Optional bitcoin wallet address specification to return a funded PSBT,
267
266
  * if not provided an address is returned instead.
268
267
  */
269
- txsExecute(options?: {
268
+ getExecutionAction(options?: {
270
269
  bitcoinWallet?: MinimalBitcoinWalletInterface;
271
- }): Promise<[
272
- SwapExecutionActionBitcoin<"ADDRESS" | "FUNDED_PSBT">
273
- ]>;
270
+ }): Promise<never>;
274
271
  /**
275
- * @remark Not supported
272
+ * @inheritDoc
276
273
  */
277
- execute(): Promise<boolean>;
274
+ getExecutionSteps(): Promise<never>;
278
275
  /**
279
276
  * @inheritDoc
280
- *
281
- * @param options.bitcoinWallet Optional bitcoin wallet address specification to return a funded PSBT,
282
- * if not provided an address is returned instead.
283
277
  */
284
- getCurrentActions(options?: {
285
- bitcoinWallet?: MinimalBitcoinWalletInterface;
286
- }): Promise<SwapExecutionAction<T>[]>;
278
+ getExecutionStatus(): Promise<never>;
279
+ /**
280
+ * @internal
281
+ * @inheritDoc
282
+ */
283
+ _submitExecutionTransactions(): Promise<string[]>;
284
+ /**
285
+ * @remarks Not supported
286
+ */
287
+ execute(): Promise<boolean>;
287
288
  /**
288
289
  * Queries the intermediary (LP) node for the state of the swap
289
290
  *