@atomiqlabs/lp-lib 14.0.0-dev.17 → 14.0.0-dev.20
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/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.d.ts +7 -0
- package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.js +29 -5
- package/dist/swaps/spv_vault_swap/SpvVaults.js +1 -1
- package/package.json +1 -1
- package/src/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.ts +34 -7
- package/src/swaps/spv_vault_swap/SpvVaults.ts +1 -1
|
@@ -32,6 +32,7 @@ export type FromBtcLnAutoRequestType = {
|
|
|
32
32
|
export declare class FromBtcLnAuto extends FromBtcBaseSwapHandler<FromBtcLnAutoSwap, FromBtcLnAutoSwapState> {
|
|
33
33
|
readonly type = SwapHandlerType.FROM_BTCLN_AUTO;
|
|
34
34
|
readonly swapType = ChainSwapType.HTLC;
|
|
35
|
+
activeSubscriptions: Set<string>;
|
|
35
36
|
readonly config: FromBtcLnAutoConfig;
|
|
36
37
|
readonly lightning: ILightningWallet;
|
|
37
38
|
readonly LightningAssertions: LightningAssertions;
|
|
@@ -46,6 +47,12 @@ export declare class FromBtcLnAuto extends FromBtcBaseSwapHandler<FromBtcLnAutoS
|
|
|
46
47
|
protected processInitializeEvent(chainIdentifier: string, savedSwap: FromBtcLnAutoSwap, event: InitializeEvent<SwapData>): Promise<void>;
|
|
47
48
|
protected processClaimEvent(chainIdentifier: string, savedSwap: FromBtcLnAutoSwap, event: ClaimEvent<SwapData>): Promise<void>;
|
|
48
49
|
protected processRefundEvent(chainIdentifier: string, savedSwap: FromBtcLnAutoSwap, event: RefundEvent<SwapData>): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Subscribe to a lightning network invoice
|
|
52
|
+
*
|
|
53
|
+
* @param swap
|
|
54
|
+
*/
|
|
55
|
+
private subscribeToInvoice;
|
|
49
56
|
/**
|
|
50
57
|
* Called when lightning HTLC is received, also signs an init transaction on the smart chain side, expiry of the
|
|
51
58
|
* smart chain authorization starts ticking as soon as this HTLC is received
|
|
@@ -19,6 +19,7 @@ class FromBtcLnAuto extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
|
|
|
19
19
|
super(storageDirectory, path, chains, swapPricing, config);
|
|
20
20
|
this.type = SwapHandler_1.SwapHandlerType.FROM_BTCLN_AUTO;
|
|
21
21
|
this.swapType = base_1.ChainSwapType.HTLC;
|
|
22
|
+
this.activeSubscriptions = new Set();
|
|
22
23
|
this.config = config;
|
|
23
24
|
this.config.invoiceTimeoutSeconds = this.config.invoiceTimeoutSeconds || 90;
|
|
24
25
|
this.lightning = lightning;
|
|
@@ -34,10 +35,12 @@ class FromBtcLnAuto extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
|
|
|
34
35
|
if (!isBeingPaid) {
|
|
35
36
|
//Not paid
|
|
36
37
|
const isInvoiceExpired = parsedPR.expiryEpochMillis < Date.now();
|
|
37
|
-
if (
|
|
38
|
+
if (isInvoiceExpired) {
|
|
39
|
+
this.swapLogger.info(swap, "processPastSwap(state=CREATED): swap LN invoice expired, cancelling, invoice: " + swap.pr);
|
|
40
|
+
await this.cancelSwapAndInvoice(swap);
|
|
38
41
|
return null;
|
|
39
|
-
|
|
40
|
-
|
|
42
|
+
}
|
|
43
|
+
this.subscribeToInvoice(swap);
|
|
41
44
|
return null;
|
|
42
45
|
}
|
|
43
46
|
//Adjust the state of the swap and expiry
|
|
@@ -219,6 +222,24 @@ class FromBtcLnAuto extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
|
|
|
219
222
|
// await this.lightning.cancelHodlInvoice(savedSwap.lnPaymentHash);
|
|
220
223
|
await this.removeSwapData(savedSwap, FromBtcLnAutoSwap_1.FromBtcLnAutoSwapState.REFUNDED);
|
|
221
224
|
}
|
|
225
|
+
/**
|
|
226
|
+
* Subscribe to a lightning network invoice
|
|
227
|
+
*
|
|
228
|
+
* @param swap
|
|
229
|
+
*/
|
|
230
|
+
subscribeToInvoice(swap) {
|
|
231
|
+
const paymentHash = swap.lnPaymentHash;
|
|
232
|
+
if (this.activeSubscriptions.has(paymentHash))
|
|
233
|
+
return false;
|
|
234
|
+
this.lightning.waitForInvoice(paymentHash).then(result => {
|
|
235
|
+
this.swapLogger.info(swap, "subscribeToInvoice(): result callback, outcome: " + result.status + " invoice: " + swap.pr);
|
|
236
|
+
this.htlcReceived(swap, result).catch(e => this.swapLogger.error(swap, "subscribeToInvoice(): HTLC received result", e));
|
|
237
|
+
this.activeSubscriptions.delete(paymentHash);
|
|
238
|
+
});
|
|
239
|
+
this.swapLogger.info(swap, "subscribeToInvoice(): subscribe to invoice: " + swap.pr);
|
|
240
|
+
this.activeSubscriptions.add(paymentHash);
|
|
241
|
+
return true;
|
|
242
|
+
}
|
|
222
243
|
/**
|
|
223
244
|
* Called when lightning HTLC is received, also signs an init transaction on the smart chain side, expiry of the
|
|
224
245
|
* smart chain authorization starts ticking as soon as this HTLC is received
|
|
@@ -227,6 +248,8 @@ class FromBtcLnAuto extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
|
|
|
227
248
|
* @param invoice
|
|
228
249
|
*/
|
|
229
250
|
async htlcReceived(invoiceData, invoice) {
|
|
251
|
+
if (invoiceData.state !== FromBtcLnAutoSwap_1.FromBtcLnAutoSwapState.CREATED)
|
|
252
|
+
return;
|
|
230
253
|
this.swapLogger.debug(invoiceData, "htlcReceived(): invoice: ", invoice);
|
|
231
254
|
if (invoiceData.metadata != null)
|
|
232
255
|
invoiceData.metadata.times.htlcReceived = Date.now();
|
|
@@ -257,8 +280,8 @@ class FromBtcLnAuto extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
|
|
|
257
280
|
//Setting the state variable is done outside the promise, so is done synchronously
|
|
258
281
|
await invoiceData.setState(FromBtcLnAutoSwap_1.FromBtcLnAutoSwapState.RECEIVED);
|
|
259
282
|
await this.saveSwapData(invoiceData);
|
|
283
|
+
await this.offerHtlc(invoiceData);
|
|
260
284
|
}
|
|
261
|
-
await this.offerHtlc(invoiceData);
|
|
262
285
|
}
|
|
263
286
|
async offerHtlc(invoiceData) {
|
|
264
287
|
if (invoiceData.state !== FromBtcLnAutoSwap_1.FromBtcLnAutoSwapState.RECEIVED)
|
|
@@ -310,7 +333,7 @@ class FromBtcLnAuto extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
|
|
|
310
333
|
//Setting the state variable is done outside the promise, so is done synchronously
|
|
311
334
|
await invoiceData.setState(FromBtcLnAutoSwap_1.FromBtcLnAutoSwapState.TXS_SENT);
|
|
312
335
|
await this.saveSwapData(invoiceData);
|
|
313
|
-
await chainInterface.sendAndConfirm(signer, [...txWithdraw, ...txInit], true);
|
|
336
|
+
await chainInterface.sendAndConfirm(signer, [...txWithdraw, ...txInit], true, undefined, true);
|
|
314
337
|
}
|
|
315
338
|
return true;
|
|
316
339
|
}
|
|
@@ -577,6 +600,7 @@ class FromBtcLnAuto extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
|
|
|
577
600
|
await PluginManager_1.PluginManager.swapCreate(createdSwap);
|
|
578
601
|
await this.saveSwapData(createdSwap);
|
|
579
602
|
this.swapLogger.info(createdSwap, "REST: /createInvoice: Created swap invoice: " + hodlInvoice.request + " amount: " + totalBtcInput.toString(10));
|
|
603
|
+
this.subscribeToInvoice(createdSwap);
|
|
580
604
|
await responseStream.writeParamsAndEnd({
|
|
581
605
|
code: 20000,
|
|
582
606
|
msg: "Success",
|
|
@@ -217,7 +217,7 @@ class SpvVaults {
|
|
|
217
217
|
}
|
|
218
218
|
const txs = await spvVaultContract.txsOpen(signer.getAddress(), vault.data);
|
|
219
219
|
let numTx = 0;
|
|
220
|
-
const txIds = await chainInterface.sendAndConfirm(signer, txs, true, undefined,
|
|
220
|
+
const txIds = await chainInterface.sendAndConfirm(signer, txs, true, undefined, true, async (txId, rawTx) => {
|
|
221
221
|
numTx++;
|
|
222
222
|
if (numTx === txs.length) {
|
|
223
223
|
//Final tx
|
package/package.json
CHANGED
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
} from "../../../wallets/ILightningWallet";
|
|
21
21
|
import {LightningAssertions} from "../../assertions/LightningAssertions";
|
|
22
22
|
import {FromBtcLnSwapState} from "../frombtcln_abstract/FromBtcLnSwapAbs";
|
|
23
|
+
import {ToBtcLnSwapAbs} from "../tobtcln_abstract/ToBtcLnSwapAbs";
|
|
23
24
|
|
|
24
25
|
export type FromBtcLnAutoConfig = FromBtcBaseConfig & {
|
|
25
26
|
invoiceTimeoutSeconds?: number,
|
|
@@ -47,6 +48,8 @@ export class FromBtcLnAuto extends FromBtcBaseSwapHandler<FromBtcLnAutoSwap, Fro
|
|
|
47
48
|
readonly type = SwapHandlerType.FROM_BTCLN_AUTO;
|
|
48
49
|
readonly swapType = ChainSwapType.HTLC;
|
|
49
50
|
|
|
51
|
+
activeSubscriptions: Set<string> = new Set<string>();
|
|
52
|
+
|
|
50
53
|
readonly config: FromBtcLnAutoConfig;
|
|
51
54
|
readonly lightning: ILightningWallet;
|
|
52
55
|
readonly LightningAssertions: LightningAssertions;
|
|
@@ -77,10 +80,12 @@ export class FromBtcLnAuto extends FromBtcBaseSwapHandler<FromBtcLnAutoSwap, Fro
|
|
|
77
80
|
if(!isBeingPaid) {
|
|
78
81
|
//Not paid
|
|
79
82
|
const isInvoiceExpired = parsedPR.expiryEpochMillis<Date.now();
|
|
80
|
-
if(
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
83
|
+
if(isInvoiceExpired) {
|
|
84
|
+
this.swapLogger.info(swap, "processPastSwap(state=CREATED): swap LN invoice expired, cancelling, invoice: "+swap.pr);
|
|
85
|
+
await this.cancelSwapAndInvoice(swap);
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
this.subscribeToInvoice(swap);
|
|
84
89
|
return null;
|
|
85
90
|
}
|
|
86
91
|
|
|
@@ -282,6 +287,26 @@ export class FromBtcLnAuto extends FromBtcBaseSwapHandler<FromBtcLnAutoSwap, Fro
|
|
|
282
287
|
await this.removeSwapData(savedSwap, FromBtcLnAutoSwapState.REFUNDED)
|
|
283
288
|
}
|
|
284
289
|
|
|
290
|
+
/**
|
|
291
|
+
* Subscribe to a lightning network invoice
|
|
292
|
+
*
|
|
293
|
+
* @param swap
|
|
294
|
+
*/
|
|
295
|
+
private subscribeToInvoice(swap: FromBtcLnAutoSwap): boolean {
|
|
296
|
+
const paymentHash = swap.lnPaymentHash;
|
|
297
|
+
if(this.activeSubscriptions.has(paymentHash)) return false;
|
|
298
|
+
|
|
299
|
+
this.lightning.waitForInvoice(paymentHash).then(result => {
|
|
300
|
+
this.swapLogger.info(swap, "subscribeToInvoice(): result callback, outcome: "+result.status+" invoice: "+swap.pr);
|
|
301
|
+
this.htlcReceived(swap, result).catch(e => this.swapLogger.error(swap, "subscribeToInvoice(): HTLC received result", e));
|
|
302
|
+
this.activeSubscriptions.delete(paymentHash);
|
|
303
|
+
});
|
|
304
|
+
this.swapLogger.info(swap, "subscribeToInvoice(): subscribe to invoice: "+swap.pr);
|
|
305
|
+
|
|
306
|
+
this.activeSubscriptions.add(paymentHash);
|
|
307
|
+
return true;
|
|
308
|
+
}
|
|
309
|
+
|
|
285
310
|
/**
|
|
286
311
|
* Called when lightning HTLC is received, also signs an init transaction on the smart chain side, expiry of the
|
|
287
312
|
* smart chain authorization starts ticking as soon as this HTLC is received
|
|
@@ -290,6 +315,7 @@ export class FromBtcLnAuto extends FromBtcBaseSwapHandler<FromBtcLnAutoSwap, Fro
|
|
|
290
315
|
* @param invoice
|
|
291
316
|
*/
|
|
292
317
|
private async htlcReceived(invoiceData: FromBtcLnAutoSwap, invoice: LightningNetworkInvoice) {
|
|
318
|
+
if(invoiceData.state!==FromBtcLnAutoSwapState.CREATED) return;
|
|
293
319
|
this.swapLogger.debug(invoiceData, "htlcReceived(): invoice: ", invoice);
|
|
294
320
|
if(invoiceData.metadata!=null) invoiceData.metadata.times.htlcReceived = Date.now();
|
|
295
321
|
|
|
@@ -336,9 +362,9 @@ export class FromBtcLnAuto extends FromBtcBaseSwapHandler<FromBtcLnAutoSwap, Fro
|
|
|
336
362
|
await invoiceData.setState(FromBtcLnAutoSwapState.RECEIVED);
|
|
337
363
|
|
|
338
364
|
await this.saveSwapData(invoiceData);
|
|
339
|
-
}
|
|
340
365
|
|
|
341
|
-
|
|
366
|
+
await this.offerHtlc(invoiceData);
|
|
367
|
+
}
|
|
342
368
|
}
|
|
343
369
|
|
|
344
370
|
private async offerHtlc(invoiceData: FromBtcLnAutoSwap) {
|
|
@@ -394,7 +420,7 @@ export class FromBtcLnAuto extends FromBtcBaseSwapHandler<FromBtcLnAutoSwap, Fro
|
|
|
394
420
|
//Setting the state variable is done outside the promise, so is done synchronously
|
|
395
421
|
await invoiceData.setState(FromBtcLnAutoSwapState.TXS_SENT);
|
|
396
422
|
await this.saveSwapData(invoiceData);
|
|
397
|
-
await chainInterface.sendAndConfirm(signer, [...txWithdraw, ...txInit], true);
|
|
423
|
+
await chainInterface.sendAndConfirm(signer, [...txWithdraw, ...txInit], true, undefined, true);
|
|
398
424
|
}
|
|
399
425
|
|
|
400
426
|
return true;
|
|
@@ -722,6 +748,7 @@ export class FromBtcLnAuto extends FromBtcBaseSwapHandler<FromBtcLnAutoSwap, Fro
|
|
|
722
748
|
await this.saveSwapData(createdSwap);
|
|
723
749
|
|
|
724
750
|
this.swapLogger.info(createdSwap, "REST: /createInvoice: Created swap invoice: "+hodlInvoice.request+" amount: "+totalBtcInput.toString(10));
|
|
751
|
+
this.subscribeToInvoice(createdSwap);
|
|
725
752
|
|
|
726
753
|
await responseStream.writeParamsAndEnd({
|
|
727
754
|
code: 20000,
|
|
@@ -272,7 +272,7 @@ export class SpvVaults {
|
|
|
272
272
|
const txs = await spvVaultContract.txsOpen(signer.getAddress(), vault.data);
|
|
273
273
|
let numTx = 0;
|
|
274
274
|
const txIds = await chainInterface.sendAndConfirm(
|
|
275
|
-
signer, txs, true, undefined,
|
|
275
|
+
signer, txs, true, undefined, true,
|
|
276
276
|
async (txId: string, rawTx: string) => {
|
|
277
277
|
numTx++;
|
|
278
278
|
if(numTx===txs.length) {
|