@atomiqlabs/sdk 8.7.7 → 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.
- package/api/index.d.ts +1 -0
- package/api/index.js +3 -0
- package/dist/ApiList.d.ts +37 -0
- package/dist/ApiList.js +30 -0
- package/dist/api/ApiEndpoints.d.ts +393 -0
- package/dist/api/ApiEndpoints.js +2 -0
- package/dist/api/ApiParser.d.ts +10 -0
- package/dist/api/ApiParser.js +134 -0
- package/dist/api/ApiTypes.d.ts +157 -0
- package/dist/api/ApiTypes.js +75 -0
- package/dist/api/SerializedAction.d.ts +40 -0
- package/dist/api/SerializedAction.js +59 -0
- package/dist/api/SwapperApi.d.ts +50 -0
- package/dist/api/SwapperApi.js +431 -0
- package/dist/api/index.d.ts +5 -0
- package/dist/api/index.js +24 -0
- package/dist/bitcoin/coinselect2/accumulative.d.ts +1 -0
- package/dist/bitcoin/coinselect2/accumulative.js +1 -1
- package/dist/bitcoin/coinselect2/blackjack.d.ts +1 -0
- package/dist/bitcoin/coinselect2/blackjack.js +1 -1
- package/dist/bitcoin/coinselect2/index.d.ts +3 -2
- package/dist/bitcoin/coinselect2/index.js +2 -2
- package/dist/bitcoin/coinselect2/utils.d.ts +7 -2
- package/dist/bitcoin/coinselect2/utils.js +45 -10
- package/dist/bitcoin/wallet/BitcoinWallet.d.ts +8 -25
- package/dist/bitcoin/wallet/BitcoinWallet.js +31 -18
- package/dist/bitcoin/wallet/IBitcoinWallet.d.ts +40 -2
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.d.ts +7 -2
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.js +10 -4
- package/dist/events/UnifiedSwapEventListener.d.ts +4 -3
- package/dist/events/UnifiedSwapEventListener.js +8 -2
- package/dist/http/HttpUtils.d.ts +4 -2
- package/dist/http/HttpUtils.js +10 -4
- package/dist/http/paramcoders/client/StreamingFetchPromise.d.ts +2 -1
- package/dist/http/paramcoders/client/StreamingFetchPromise.js +3 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/intermediaries/IntermediaryDiscovery.d.ts +7 -2
- package/dist/intermediaries/IntermediaryDiscovery.js +4 -4
- package/dist/intermediaries/apis/IntermediaryAPI.d.ts +182 -15
- package/dist/intermediaries/apis/IntermediaryAPI.js +192 -31
- package/dist/intermediaries/auth/SignedKeyBasedAuth.d.ts +14 -0
- package/dist/intermediaries/auth/SignedKeyBasedAuth.js +68 -0
- package/dist/storage/IUnifiedStorage.d.ts +45 -3
- package/dist/storage/UnifiedSwapStorage.d.ts +8 -2
- package/dist/storage/UnifiedSwapStorage.js +46 -8
- package/dist/swapper/Swapper.d.ts +77 -4
- package/dist/swapper/Swapper.js +117 -25
- package/dist/swapper/SwapperUtils.d.ts +18 -2
- package/dist/swapper/SwapperUtils.js +39 -1
- package/dist/swaps/ISwap.d.ts +70 -9
- package/dist/swaps/ISwap.js +28 -6
- package/dist/swaps/ISwapWrapper.d.ts +11 -1
- package/dist/swaps/ISwapWrapper.js +23 -3
- package/dist/swaps/escrow_swaps/IEscrowSwap.d.ts +1 -1
- package/dist/swaps/escrow_swaps/IEscrowSwap.js +4 -2
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.d.ts +2 -1
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.js +2 -2
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.d.ts +3 -1
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.js +3 -2
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.d.ts +47 -31
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +201 -67
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +3 -1
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +6 -6
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.d.ts +82 -15
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +304 -98
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +3 -1
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +6 -6
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.d.ts +75 -42
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +424 -87
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.d.ts +3 -1
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.js +7 -7
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.d.ts +54 -11
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +214 -41
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.d.ts +2 -1
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.js +7 -8
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.d.ts +3 -1
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.js +5 -5
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +85 -22
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +299 -56
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.d.ts +41 -7
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.js +183 -58
- package/dist/swaps/trusted/ln/LnForGasSwap.d.ts +53 -12
- package/dist/swaps/trusted/ln/LnForGasSwap.js +163 -49
- package/dist/swaps/trusted/ln/LnForGasWrapper.js +1 -2
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.d.ts +14 -13
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.js +30 -47
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.d.ts +3 -1
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.js +4 -4
- package/dist/types/SwapExecutionAction.d.ts +141 -34
- package/dist/types/SwapExecutionAction.js +104 -0
- package/dist/types/SwapExecutionStep.d.ts +144 -0
- package/dist/types/SwapExecutionStep.js +87 -0
- package/dist/types/TokenAmount.d.ts +6 -0
- package/dist/types/TokenAmount.js +26 -1
- package/dist/utils/BitcoinUtils.d.ts +4 -0
- package/dist/utils/BitcoinUtils.js +73 -1
- package/dist/utils/BitcoinWalletUtils.d.ts +2 -2
- package/dist/utils/Utils.d.ts +3 -1
- package/dist/utils/Utils.js +7 -1
- package/package.json +7 -4
- package/src/api/ApiEndpoints.ts +427 -0
- package/src/api/ApiParser.ts +138 -0
- package/src/api/ApiTypes.ts +229 -0
- package/src/api/SerializedAction.ts +97 -0
- package/src/api/SwapperApi.ts +545 -0
- package/src/api/index.ts +5 -0
- package/src/bitcoin/coinselect2/accumulative.ts +2 -1
- package/src/bitcoin/coinselect2/blackjack.ts +2 -1
- package/src/bitcoin/coinselect2/index.ts +5 -4
- package/src/bitcoin/coinselect2/utils.ts +55 -14
- package/src/bitcoin/wallet/BitcoinWallet.ts +69 -57
- package/src/bitcoin/wallet/IBitcoinWallet.ts +44 -3
- package/src/bitcoin/wallet/SingleAddressBitcoinWallet.ts +12 -4
- package/src/events/UnifiedSwapEventListener.ts +11 -3
- package/src/http/HttpUtils.ts +10 -4
- package/src/http/paramcoders/client/StreamingFetchPromise.ts +4 -2
- package/src/index.ts +1 -0
- package/src/intermediaries/IntermediaryDiscovery.ts +9 -2
- package/src/intermediaries/apis/IntermediaryAPI.ts +335 -35
- package/src/intermediaries/auth/SignedKeyBasedAuth.ts +69 -0
- package/src/storage/IUnifiedStorage.ts +45 -4
- package/src/storage/UnifiedSwapStorage.ts +42 -8
- package/src/swapper/Swapper.ts +165 -24
- package/src/swapper/SwapperUtils.ts +42 -2
- package/src/swaps/ISwap.ts +88 -16
- package/src/swaps/ISwapWrapper.ts +28 -3
- package/src/swaps/escrow_swaps/IEscrowSwap.ts +5 -3
- package/src/swaps/escrow_swaps/IEscrowSwapWrapper.ts +3 -1
- package/src/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.ts +4 -1
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.ts +264 -67
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +6 -4
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +390 -89
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +6 -4
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.ts +548 -94
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.ts +7 -5
- package/src/swaps/escrow_swaps/tobtc/IToBTCSwap.ts +276 -45
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.ts +7 -6
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.ts +5 -3
- package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +413 -64
- package/src/swaps/spv_swaps/SpvFromBTCWrapper.ts +239 -61
- package/src/swaps/trusted/ln/LnForGasSwap.ts +211 -47
- package/src/swaps/trusted/ln/LnForGasWrapper.ts +1 -2
- package/src/swaps/trusted/onchain/OnchainForGasSwap.ts +32 -51
- package/src/swaps/trusted/onchain/OnchainForGasWrapper.ts +5 -3
- package/src/types/SwapExecutionAction.ts +266 -43
- package/src/types/SwapExecutionStep.ts +224 -0
- package/src/types/TokenAmount.ts +36 -2
- package/src/utils/BitcoinUtils.ts +73 -0
- package/src/utils/BitcoinWalletUtils.ts +2 -2
- package/src/utils/Utils.ts +10 -1
- package/src/intermediaries/apis/TrustedIntermediaryAPI.ts +0 -258
|
@@ -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
|
|
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
|
|
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
|
|
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
|
|
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
|
-
*
|
|
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
|
|
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
|
|
45
|
-
[LnForGasSwapState.FAILED]: "The swap
|
|
46
|
-
[LnForGasSwapState.PR_CREATED]: "Swap was created, pay the provided
|
|
47
|
-
[LnForGasSwapState.PR_PAID]: "
|
|
48
|
-
[LnForGasSwapState.FINISHED]: "The swap is finished after the
|
|
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
|
-
* @
|
|
333
|
+
* @remarks Not supported
|
|
334
334
|
*/
|
|
335
|
-
async
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
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
|
-
|
|
342
|
-
|
|
375
|
+
type: "Payment",
|
|
376
|
+
side: "source",
|
|
343
377
|
chain: "LIGHTNING",
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
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
|
-
|
|
390
|
+
],
|
|
391
|
+
buildCurrentAction,
|
|
392
|
+
state
|
|
393
|
+
};
|
|
355
394
|
}
|
|
356
395
|
/**
|
|
357
|
-
* @
|
|
396
|
+
* @internal
|
|
397
|
+
* @inheritDoc
|
|
358
398
|
*/
|
|
359
|
-
|
|
360
|
-
throw new Error("
|
|
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
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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}
|
|
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
|
|
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
|
-
|
|
268
|
+
getExecutionAction(options?: {
|
|
270
269
|
bitcoinWallet?: MinimalBitcoinWalletInterface;
|
|
271
|
-
}): Promise<
|
|
272
|
-
SwapExecutionActionBitcoin<"ADDRESS" | "FUNDED_PSBT">
|
|
273
|
-
]>;
|
|
270
|
+
}): Promise<never>;
|
|
274
271
|
/**
|
|
275
|
-
* @
|
|
272
|
+
* @inheritDoc
|
|
276
273
|
*/
|
|
277
|
-
|
|
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
|
-
|
|
285
|
-
|
|
286
|
-
|
|
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
|
*
|
|
@@ -5,7 +5,7 @@ const SwapType_1 = require("../../../enums/SwapType");
|
|
|
5
5
|
const Utils_1 = require("../../../utils/Utils");
|
|
6
6
|
const BitcoinUtils_1 = require("../../../utils/BitcoinUtils");
|
|
7
7
|
const ISwap_1 = require("../../ISwap");
|
|
8
|
-
const
|
|
8
|
+
const IntermediaryAPI_1 = require("../../../intermediaries/apis/IntermediaryAPI");
|
|
9
9
|
const IBitcoinWallet_1 = require("../../../bitcoin/wallet/IBitcoinWallet");
|
|
10
10
|
const btc_signer_1 = require("@scure/btc-signer");
|
|
11
11
|
const SingleAddressBitcoinWallet_1 = require("../../../bitcoin/wallet/SingleAddressBitcoinWallet");
|
|
@@ -435,50 +435,33 @@ class OnchainForGasSwap extends ISwap_1.ISwap {
|
|
|
435
435
|
* @param options.bitcoinWallet Optional bitcoin wallet address specification to return a funded PSBT,
|
|
436
436
|
* if not provided an address is returned instead.
|
|
437
437
|
*/
|
|
438
|
-
async
|
|
439
|
-
|
|
440
|
-
if (!await this._verifyQuoteValid())
|
|
441
|
-
throw new Error("Quote already expired or close to expiry!");
|
|
442
|
-
return [
|
|
443
|
-
{
|
|
444
|
-
name: "Payment",
|
|
445
|
-
description: "Send funds to the bitcoin swap address",
|
|
446
|
-
chain: "BITCOIN",
|
|
447
|
-
txs: [
|
|
448
|
-
options?.bitcoinWallet == null ? {
|
|
449
|
-
address: this.address,
|
|
450
|
-
amount: Number(this.inputAmount),
|
|
451
|
-
hyperlink: this.getHyperlink(),
|
|
452
|
-
type: "ADDRESS"
|
|
453
|
-
} : {
|
|
454
|
-
...await this.getFundedPsbt(options.bitcoinWallet),
|
|
455
|
-
type: "FUNDED_PSBT"
|
|
456
|
-
}
|
|
457
|
-
]
|
|
458
|
-
}
|
|
459
|
-
];
|
|
460
|
-
}
|
|
461
|
-
throw new Error("Invalid swap state to obtain execution txns, required PR_CREATED or CLAIM_COMMITED");
|
|
438
|
+
async getExecutionAction(options) {
|
|
439
|
+
throw new Error("Not supported");
|
|
462
440
|
}
|
|
463
441
|
/**
|
|
464
|
-
* @
|
|
442
|
+
* @inheritDoc
|
|
465
443
|
*/
|
|
466
|
-
async
|
|
444
|
+
async getExecutionSteps() {
|
|
467
445
|
throw new Error("Not supported");
|
|
468
446
|
}
|
|
469
447
|
/**
|
|
470
448
|
* @inheritDoc
|
|
471
|
-
*
|
|
472
|
-
* @param options.bitcoinWallet Optional bitcoin wallet address specification to return a funded PSBT,
|
|
473
|
-
* if not provided an address is returned instead.
|
|
474
449
|
*/
|
|
475
|
-
async
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
450
|
+
async getExecutionStatus() {
|
|
451
|
+
throw new Error("Not supported");
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* @internal
|
|
455
|
+
* @inheritDoc
|
|
456
|
+
*/
|
|
457
|
+
_submitExecutionTransactions() {
|
|
458
|
+
throw new Error("Not supported");
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* @remarks Not supported
|
|
462
|
+
*/
|
|
463
|
+
async execute() {
|
|
464
|
+
throw new Error("Not supported");
|
|
482
465
|
}
|
|
483
466
|
//////////////////////////////
|
|
484
467
|
//// Payment
|
|
@@ -499,9 +482,9 @@ class OnchainForGasSwap extends ISwap_1.ISwap {
|
|
|
499
482
|
return false;
|
|
500
483
|
if (this.url == null)
|
|
501
484
|
return false;
|
|
502
|
-
const response = await
|
|
485
|
+
const response = await this.wrapper._lpApi.getTrustedAddressStatus(this.url, this.paymentHash, this.sequence, this.wrapper._options.getRequestTimeout);
|
|
503
486
|
switch (response.code) {
|
|
504
|
-
case
|
|
487
|
+
case IntermediaryAPI_1.TrustedAddressStatusResponseCodes.AWAIT_PAYMENT:
|
|
505
488
|
if (this.txId != null) {
|
|
506
489
|
this.txId = undefined;
|
|
507
490
|
if (save)
|
|
@@ -509,9 +492,9 @@ class OnchainForGasSwap extends ISwap_1.ISwap {
|
|
|
509
492
|
return true;
|
|
510
493
|
}
|
|
511
494
|
return false;
|
|
512
|
-
case
|
|
513
|
-
case
|
|
514
|
-
case
|
|
495
|
+
case IntermediaryAPI_1.TrustedAddressStatusResponseCodes.AWAIT_CONFIRMATION:
|
|
496
|
+
case IntermediaryAPI_1.TrustedAddressStatusResponseCodes.PENDING:
|
|
497
|
+
case IntermediaryAPI_1.TrustedAddressStatusResponseCodes.TX_SENT:
|
|
515
498
|
const inputAmount = BigInt(response.data.adjustedAmount);
|
|
516
499
|
const outputAmount = BigInt(response.data.adjustedTotal);
|
|
517
500
|
const adjustedFee = response.data.adjustedFee == null ? null : BigInt(response.data.adjustedFee);
|
|
@@ -532,7 +515,7 @@ class OnchainForGasSwap extends ISwap_1.ISwap {
|
|
|
532
515
|
return true;
|
|
533
516
|
}
|
|
534
517
|
return false;
|
|
535
|
-
case
|
|
518
|
+
case IntermediaryAPI_1.TrustedAddressStatusResponseCodes.PAID:
|
|
536
519
|
const txStatus = await this.wrapper._chain.getTxIdStatus(response.data.txId);
|
|
537
520
|
if (txStatus === "success") {
|
|
538
521
|
this._state = OnchainForGasSwapState.FINISHED;
|
|
@@ -542,19 +525,19 @@ class OnchainForGasSwap extends ISwap_1.ISwap {
|
|
|
542
525
|
return true;
|
|
543
526
|
}
|
|
544
527
|
return false;
|
|
545
|
-
case
|
|
528
|
+
case IntermediaryAPI_1.TrustedAddressStatusResponseCodes.EXPIRED:
|
|
546
529
|
this._state = OnchainForGasSwapState.EXPIRED;
|
|
547
530
|
if (save)
|
|
548
531
|
await this._saveAndEmit();
|
|
549
532
|
return true;
|
|
550
|
-
case
|
|
533
|
+
case IntermediaryAPI_1.TrustedAddressStatusResponseCodes.REFUNDABLE:
|
|
551
534
|
if (this._state === OnchainForGasSwapState.REFUNDABLE)
|
|
552
535
|
return null;
|
|
553
536
|
this._state = OnchainForGasSwapState.REFUNDABLE;
|
|
554
537
|
if (save)
|
|
555
538
|
await this._saveAndEmit();
|
|
556
539
|
return true;
|
|
557
|
-
case
|
|
540
|
+
case IntermediaryAPI_1.TrustedAddressStatusResponseCodes.REFUNDED:
|
|
558
541
|
this._state = OnchainForGasSwapState.REFUNDED;
|
|
559
542
|
this.refundTxId = response.data.txId;
|
|
560
543
|
if (save)
|
|
@@ -581,7 +564,7 @@ class OnchainForGasSwap extends ISwap_1.ISwap {
|
|
|
581
564
|
}
|
|
582
565
|
if (this.url == null)
|
|
583
566
|
throw new Error("LP URL not known, cannot set refund address!");
|
|
584
|
-
await
|
|
567
|
+
await this.wrapper._lpApi.setTrustedRefundAddress(this.url, this.paymentHash, this.sequence, refundAddress, this.wrapper._options.getRequestTimeout);
|
|
585
568
|
this.refundAddress = refundAddress;
|
|
586
569
|
}
|
|
587
570
|
/**
|
|
@@ -10,6 +10,7 @@ import { UnifiedSwapEventListener } from "../../../events/UnifiedSwapEventListen
|
|
|
10
10
|
import { UnifiedSwapStorage } from "../../../storage/UnifiedSwapStorage";
|
|
11
11
|
import { ISwap } from "../../ISwap";
|
|
12
12
|
import { BTC_NETWORK } from "@scure/btc-signer/utils";
|
|
13
|
+
import { IntermediaryAPI } from "../../../intermediaries/apis/IntermediaryAPI";
|
|
13
14
|
export type OnchainForGasWrapperOptions = ISwapWrapperOptions & {
|
|
14
15
|
bitcoinNetwork: BTC_NETWORK;
|
|
15
16
|
};
|
|
@@ -50,10 +51,11 @@ export declare class OnchainForGasWrapper<T extends ChainType> extends ISwapWrap
|
|
|
50
51
|
* @param prices Pricing to use
|
|
51
52
|
* @param tokens
|
|
52
53
|
* @param btcRpc Bitcoin RPC which also supports getting transactions by txoHash
|
|
54
|
+
* @param lpApi
|
|
53
55
|
* @param options
|
|
54
56
|
* @param events Instance to use for emitting events
|
|
55
57
|
*/
|
|
56
|
-
constructor(chainIdentifier: string, unifiedStorage: UnifiedSwapStorage<T>, unifiedChainEvents: UnifiedSwapEventListener<T>, chain: T["ChainInterface"], prices: ISwapPrice, tokens: WrapperCtorTokens, btcRpc: BitcoinRpcWithAddressIndex<any>, options: OnchainForGasWrapperOptions, events?: EventEmitter<{
|
|
58
|
+
constructor(chainIdentifier: string, unifiedStorage: UnifiedSwapStorage<T>, unifiedChainEvents: UnifiedSwapEventListener<T>, chain: T["ChainInterface"], prices: ISwapPrice, tokens: WrapperCtorTokens, btcRpc: BitcoinRpcWithAddressIndex<any>, lpApi: IntermediaryAPI, options: OnchainForGasWrapperOptions, events?: EventEmitter<{
|
|
57
59
|
swapState: [ISwap];
|
|
58
60
|
}>);
|
|
59
61
|
/**
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.OnchainForGasWrapper = void 0;
|
|
4
4
|
const ISwapWrapper_1 = require("../../ISwapWrapper");
|
|
5
|
-
const TrustedIntermediaryAPI_1 = require("../../../intermediaries/apis/TrustedIntermediaryAPI");
|
|
6
5
|
const IntermediaryError_1 = require("../../../errors/IntermediaryError");
|
|
7
6
|
const OnchainForGasSwap_1 = require("./OnchainForGasSwap");
|
|
8
7
|
const SwapType_1 = require("../../../enums/SwapType");
|
|
@@ -21,11 +20,12 @@ class OnchainForGasWrapper extends ISwapWrapper_1.ISwapWrapper {
|
|
|
21
20
|
* @param prices Pricing to use
|
|
22
21
|
* @param tokens
|
|
23
22
|
* @param btcRpc Bitcoin RPC which also supports getting transactions by txoHash
|
|
23
|
+
* @param lpApi
|
|
24
24
|
* @param options
|
|
25
25
|
* @param events Instance to use for emitting events
|
|
26
26
|
*/
|
|
27
|
-
constructor(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, prices, tokens, btcRpc, options, events) {
|
|
28
|
-
super(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, prices, tokens, options, events);
|
|
27
|
+
constructor(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, prices, tokens, btcRpc, lpApi, options, events) {
|
|
28
|
+
super(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, prices, tokens, lpApi, options, events);
|
|
29
29
|
this.TYPE = SwapType_1.SwapType.TRUSTED_FROM_BTC;
|
|
30
30
|
/**
|
|
31
31
|
* @internal
|
|
@@ -59,7 +59,7 @@ class OnchainForGasWrapper extends ISwapWrapper_1.ISwapWrapper {
|
|
|
59
59
|
throw new Error("Not initialized, call init() first!");
|
|
60
60
|
const lpUrl = typeof (lpOrUrl) === "string" ? lpOrUrl : lpOrUrl.url;
|
|
61
61
|
const token = this._chain.getNativeCurrencyAddress();
|
|
62
|
-
const resp = await
|
|
62
|
+
const resp = await this._lpApi.initTrustedFromBTC(this.chainIdentifier, lpUrl, {
|
|
63
63
|
address: recipient,
|
|
64
64
|
amount,
|
|
65
65
|
refundAddress,
|