@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.
- 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/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 +171 -14
- package/dist/intermediaries/apis/IntermediaryAPI.js +174 -28
- 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 +36 -3
- package/dist/swapper/Swapper.js +54 -18
- 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 +76 -19
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +290 -51
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.d.ts +3 -1
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.js +5 -5
- 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 +2 -0
- package/dist/utils/BitcoinUtils.js +34 -1
- 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/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 +314 -30
- 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 +87 -18
- 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 +393 -57
- package/src/swaps/spv_swaps/SpvFromBTCWrapper.ts +5 -3
- 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 +32 -0
- package/src/utils/Utils.ts +10 -1
- 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
|
|
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, "
|
|
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 {
|
|
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
|
|
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
|
|
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
|
|
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
|
-
*
|
|
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
|
|
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
|
-
|
|
230
|
+
_submitExecutionTransactions(): Promise<string[]>;
|
|
216
231
|
/**
|
|
217
|
-
* @
|
|
232
|
+
* @internal
|
|
218
233
|
*/
|
|
219
|
-
|
|
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
|
-
|
|
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}
|
|
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
|
|
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
|
*
|