@atomiqlabs/lp-lib 11.0.2 → 11.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -60,9 +60,17 @@ class ToBtcLnAbs extends ToBtcBaseSwapHandler_1.ToBtcBaseSwapHandler {
60
60
  //Cancel the swaps where signature is expired
61
61
  const isSignatureExpired = yield swapContract.isInitAuthorizationExpired(swap.data, swap);
62
62
  if (isSignatureExpired) {
63
- this.swapLogger.info(swap, "processPastSwap(state=SAVED): signature expired, cancel uncommited swap, invoice: " + swap.pr);
64
- yield this.removeSwapData(swap, ToBtcLnSwapAbs_1.ToBtcLnSwapState.CANCELED);
65
- return;
63
+ const isCommitted = yield swapContract.isCommited(swap.data);
64
+ if (!isCommitted) {
65
+ this.swapLogger.info(swap, "processPastSwap(state=SAVED): authorization expired & swap not committed, cancelling swap, invoice: " + swap.pr);
66
+ yield this.removeSwapData(swap, ToBtcLnSwapAbs_1.ToBtcLnSwapState.CANCELED);
67
+ return;
68
+ }
69
+ else {
70
+ this.swapLogger.info(swap, "processPastSwap(state=SAVED): swap committed (detected from processPastSwap), invoice: " + swap.pr);
71
+ yield swap.setState(ToBtcLnSwapAbs_1.ToBtcLnSwapState.COMMITED);
72
+ yield this.storageManager.saveData(swap.data.getHash(), swap.getSequence(), swap);
73
+ }
66
74
  }
67
75
  //Cancel the swaps where lightning invoice is expired
68
76
  const decodedPR = yield this.lightning.parsePaymentRequest(swap.pr);
@@ -231,8 +239,6 @@ class ToBtcLnAbs extends ToBtcBaseSwapHandler_1.ToBtcBaseSwapHandler {
231
239
  //Compute max cltv delta
232
240
  const maxFee = swap.quotedNetworkFee;
233
241
  const maxUsableCLTVdelta = expiryTimestamp.sub(currentTimestamp).sub(this.config.gracePeriod).div(this.config.bitcoinBlocktime.mul(this.config.safetyFactor));
234
- yield swap.setState(ToBtcLnSwapAbs_1.ToBtcLnSwapState.COMMITED);
235
- yield this.storageManager.saveData(decodedPR.id, swap.data.getSequence(), swap);
236
242
  //Initiate payment
237
243
  this.swapLogger.info(swap, "sendLightningPayment(): paying lightning network invoice," +
238
244
  " cltvDelta: " + maxUsableCLTVdelta.toString(10) +
@@ -267,11 +273,28 @@ class ToBtcLnAbs extends ToBtcBaseSwapHandler_1.ToBtcBaseSwapHandler {
267
273
  processInitialized(swap) {
268
274
  return __awaiter(this, void 0, void 0, function* () {
269
275
  //Check if payment was already made
270
- let lnPaymentStatus = yield this.lightning.getPayment(swap.getHash());
271
- if (swap.metadata != null)
272
- swap.metadata.times.payPaymentChecked = Date.now();
273
- const paymentExists = lnPaymentStatus != null;
274
- if (!paymentExists) {
276
+ if (swap.state === ToBtcLnSwapAbs_1.ToBtcLnSwapState.COMMITED) {
277
+ let lnPaymentStatus = yield this.lightning.getPayment(swap.getHash());
278
+ if (lnPaymentStatus != null) {
279
+ if (lnPaymentStatus.status === "pending") {
280
+ //Payment still ongoing, process the result
281
+ this.subscribeToPayment(swap);
282
+ return;
283
+ }
284
+ else {
285
+ //Payment has already concluded, process the result
286
+ yield this.processPaymentResult(swap, lnPaymentStatus);
287
+ return;
288
+ }
289
+ }
290
+ else {
291
+ //Payment not founds, try to process again
292
+ yield swap.setState(ToBtcLnSwapAbs_1.ToBtcLnSwapState.SAVED);
293
+ }
294
+ }
295
+ if (swap.state === ToBtcLnSwapAbs_1.ToBtcLnSwapState.SAVED) {
296
+ yield swap.setState(ToBtcLnSwapAbs_1.ToBtcLnSwapState.COMMITED);
297
+ yield this.storageManager.saveData(swap.data.getHash(), swap.data.getSequence(), swap);
275
298
  try {
276
299
  yield this.sendLightningPayment(swap);
277
300
  }
@@ -290,12 +313,6 @@ class ToBtcLnAbs extends ToBtcBaseSwapHandler_1.ToBtcBaseSwapHandler {
290
313
  this.subscribeToPayment(swap);
291
314
  return;
292
315
  }
293
- if (lnPaymentStatus.status === "pending") {
294
- this.subscribeToPayment(swap);
295
- return;
296
- }
297
- //Payment has already concluded, process the result
298
- yield this.processPaymentResult(swap, lnPaymentStatus);
299
316
  });
300
317
  }
301
318
  processInitializeEvent(chainIdentifier, event) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atomiqlabs/lp-lib",
3
- "version": "11.0.2",
3
+ "version": "11.0.4",
4
4
  "description": "Main functionality implementation for atomiq LP node",
5
5
  "main": "./dist/index.js",
6
6
  "types:": "./dist/index.d.ts",
@@ -3,14 +3,7 @@ import {Express, Request, Response} from "express";
3
3
  import {ToBtcLnSwapAbs, ToBtcLnSwapState} from "./ToBtcLnSwapAbs";
4
4
  import {MultichainData, SwapHandlerType} from "../SwapHandler";
5
5
  import {ISwapPrice} from "../ISwapPrice";
6
- import {
7
- ChainSwapType,
8
- ClaimEvent,
9
- InitializeEvent,
10
- RefundEvent,
11
- SwapCommitStatus,
12
- SwapData
13
- } from "@atomiqlabs/base";
6
+ import {ChainSwapType, ClaimEvent, InitializeEvent, RefundEvent, SwapCommitStatus, SwapData} from "@atomiqlabs/base";
14
7
  import {expressHandlerWrapper, HEX_REGEX, isDefinedRuntimeError} from "../../utils/Utils";
15
8
  import {PluginManager} from "../../plugins/PluginManager";
16
9
  import {IIntermediaryStorage} from "../../storage/IIntermediaryStorage";
@@ -23,8 +16,10 @@ import {ToBtcBaseConfig, ToBtcBaseSwapHandler} from "../ToBtcBaseSwapHandler";
23
16
  import {
24
17
  ILightningWallet,
25
18
  OutgoingLightningNetworkPayment,
26
- ParsedPaymentRequest, ProbeAndRouteInit,
27
- ProbeAndRouteResponse, routesMatch
19
+ ParsedPaymentRequest,
20
+ ProbeAndRouteInit,
21
+ ProbeAndRouteResponse,
22
+ routesMatch
28
23
  } from "../../wallets/ILightningWallet";
29
24
 
30
25
  export type ToBtcLnConfig = ToBtcBaseConfig & {
@@ -142,11 +137,17 @@ export class ToBtcLnAbs extends ToBtcBaseSwapHandler<ToBtcLnSwapAbs, ToBtcLnSwap
142
137
  //Cancel the swaps where signature is expired
143
138
  const isSignatureExpired = await swapContract.isInitAuthorizationExpired(swap.data, swap);
144
139
  if(isSignatureExpired) {
145
- this.swapLogger.info(swap, "processPastSwap(state=SAVED): signature expired, cancel uncommited swap, invoice: "+swap.pr);
146
- await this.removeSwapData(swap, ToBtcLnSwapState.CANCELED);
147
- return;
140
+ const isCommitted = await swapContract.isCommited(swap.data);
141
+ if(!isCommitted) {
142
+ this.swapLogger.info(swap, "processPastSwap(state=SAVED): authorization expired & swap not committed, cancelling swap, invoice: "+swap.pr);
143
+ await this.removeSwapData(swap, ToBtcLnSwapState.CANCELED);
144
+ return;
145
+ } else {
146
+ this.swapLogger.info(swap, "processPastSwap(state=SAVED): swap committed (detected from processPastSwap), invoice: "+swap.pr);
147
+ await swap.setState(ToBtcLnSwapState.COMMITED);
148
+ await this.storageManager.saveData(swap.data.getHash(), swap.getSequence(), swap);
149
+ }
148
150
  }
149
-
150
151
  //Cancel the swaps where lightning invoice is expired
151
152
  const decodedPR = await this.lightning.parsePaymentRequest(swap.pr);
152
153
  const isInvoiceExpired = decodedPR.expiryEpochMillis < Date.now();
@@ -321,9 +322,6 @@ export class ToBtcLnAbs extends ToBtcBaseSwapHandler<ToBtcLnSwapAbs, ToBtcLnSwap
321
322
  const maxFee = swap.quotedNetworkFee;
322
323
  const maxUsableCLTVdelta = expiryTimestamp.sub(currentTimestamp).sub(this.config.gracePeriod).div(this.config.bitcoinBlocktime.mul(this.config.safetyFactor));
323
324
 
324
- await swap.setState(ToBtcLnSwapState.COMMITED);
325
- await this.storageManager.saveData(decodedPR.id, swap.data.getSequence(), swap);
326
-
327
325
  //Initiate payment
328
326
  this.swapLogger.info(swap, "sendLightningPayment(): paying lightning network invoice,"+
329
327
  " cltvDelta: "+maxUsableCLTVdelta.toString(10)+
@@ -357,11 +355,27 @@ export class ToBtcLnAbs extends ToBtcBaseSwapHandler<ToBtcLnSwapAbs, ToBtcLnSwap
357
355
  */
358
356
  private async processInitialized(swap: ToBtcLnSwapAbs) {
359
357
  //Check if payment was already made
360
- let lnPaymentStatus = await this.lightning.getPayment(swap.getHash());
361
- if(swap.metadata!=null) swap.metadata.times.payPaymentChecked = Date.now();
358
+ if(swap.state===ToBtcLnSwapState.COMMITED) {
359
+ let lnPaymentStatus = await this.lightning.getPayment(swap.getHash());
360
+ if(lnPaymentStatus!=null) {
361
+ if(lnPaymentStatus.status==="pending") {
362
+ //Payment still ongoing, process the result
363
+ this.subscribeToPayment(swap);
364
+ return;
365
+ } else {
366
+ //Payment has already concluded, process the result
367
+ await this.processPaymentResult(swap, lnPaymentStatus);
368
+ return;
369
+ }
370
+ } else {
371
+ //Payment not founds, try to process again
372
+ await swap.setState(ToBtcLnSwapState.SAVED);
373
+ }
374
+ }
362
375
 
363
- const paymentExists = lnPaymentStatus!=null;
364
- if(!paymentExists) {
376
+ if(swap.state===ToBtcLnSwapState.SAVED) {
377
+ await swap.setState(ToBtcLnSwapState.COMMITED);
378
+ await this.storageManager.saveData(swap.data.getHash(), swap.data.getSequence(), swap);
365
379
  try {
366
380
  await this.sendLightningPayment(swap);
367
381
  } catch (e) {
@@ -376,14 +390,6 @@ export class ToBtcLnAbs extends ToBtcBaseSwapHandler<ToBtcLnSwapAbs, ToBtcLnSwap
376
390
  this.subscribeToPayment(swap);
377
391
  return;
378
392
  }
379
-
380
- if(lnPaymentStatus.status==="pending") {
381
- this.subscribeToPayment(swap);
382
- return;
383
- }
384
-
385
- //Payment has already concluded, process the result
386
- await this.processPaymentResult(swap, lnPaymentStatus);
387
393
  }
388
394
 
389
395
  protected async processInitializeEvent(chainIdentifier: string, event: InitializeEvent<SwapData>): Promise<void> {