@atomiqlabs/lp-lib 16.0.1 → 16.0.2

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.
@@ -306,8 +306,10 @@ class FromBtcLnAuto extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
306
306
  const minNativeTokenReserve = this.config.minNativeBalances?.[invoiceData.chainIdentifier] ?? 0n;
307
307
  //Pre-fetch data
308
308
  const balancePrefetch = this.getBalancePrefetch(invoiceData.chainIdentifier, useToken, abortController);
309
- const gasTokenBalancePrefetch = (invoiceData.getTotalOutputGasAmount() === 0n && minNativeTokenReserve === 0n) || useToken === gasToken ?
309
+ const gasTokenBalancePrefetch = invoiceData.getTotalOutputGasAmount() === 0n || useToken === gasToken ?
310
310
  null : this.getBalancePrefetch(invoiceData.chainIdentifier, gasToken, abortController);
311
+ const nativeBalancePrefetch = minNativeTokenReserve === 0n ?
312
+ null : this.getBalancePrefetch(invoiceData.chainIdentifier, chainInterface.getNativeCurrencyAddress(), abortController, false);
311
313
  if (await swapContract.getInitAuthorizationExpiry(invoiceData.data, invoiceData) < Date.now()) {
312
314
  if (invoiceData.metadata != null)
313
315
  invoiceData.metadata.htlcOfferError = "Init authorization expired, before being sent!";
@@ -316,14 +318,26 @@ class FromBtcLnAuto extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
316
318
  }
317
319
  return false;
318
320
  }
321
+ try {
322
+ await this.checkBalance(minNativeTokenReserve, nativeBalancePrefetch, abortController.signal);
323
+ }
324
+ catch (e) {
325
+ if (!abortController.signal.aborted) {
326
+ if ((0, Utils_1.isDefinedRuntimeError)(e) && invoiceData.metadata != null)
327
+ invoiceData.metadata.htlcOfferError = "Not enough native balance!";
328
+ if (invoiceData.state === FromBtcLnAutoSwap_1.FromBtcLnAutoSwapState.RECEIVED)
329
+ await this.cancelSwapAndInvoice(invoiceData);
330
+ }
331
+ throw e;
332
+ }
319
333
  try {
320
334
  //Check if we have enough liquidity to proceed
321
335
  if (useToken === gasToken) {
322
- await this.checkBalance(invoiceData.getTotalOutputAmount() + invoiceData.getTotalOutputGasAmount() + minNativeTokenReserve, balancePrefetch, abortController.signal);
336
+ await this.checkBalance(invoiceData.getTotalOutputAmount() + invoiceData.getTotalOutputGasAmount(), balancePrefetch, abortController.signal);
323
337
  }
324
338
  else {
325
339
  await this.checkBalance(invoiceData.getTotalOutputAmount(), balancePrefetch, abortController.signal);
326
- await this.checkBalance(invoiceData.getTotalOutputGasAmount() + minNativeTokenReserve, gasTokenBalancePrefetch, abortController.signal);
340
+ await this.checkBalance(invoiceData.getTotalOutputGasAmount(), gasTokenBalancePrefetch, abortController.signal);
327
341
  }
328
342
  if (invoiceData.metadata != null)
329
343
  invoiceData.metadata.times.offerHtlcChecked = Date.now();
@@ -597,8 +611,10 @@ class FromBtcLnAuto extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
597
611
  //Pre-fetch data
598
612
  const { pricePrefetchPromise, gasTokenPricePrefetchPromise } = this.getFromBtcPricePrefetches(chainIdentifier, useToken, gasToken, abortController);
599
613
  const balancePrefetch = this.getBalancePrefetch(chainIdentifier, useToken, abortController);
600
- const gasTokenBalancePrefetch = (gasTokenAmount.amount === 0n && minNativeTokenReserve == 0n) || useToken === gasToken ?
614
+ const gasTokenBalancePrefetch = gasTokenAmount.amount === 0n || useToken === gasToken ?
601
615
  null : this.getBalancePrefetch(chainIdentifier, gasToken, abortController);
616
+ const nativeTokenBalancePrefetch = minNativeTokenReserve === 0n ?
617
+ null : this.getBalancePrefetch(chainIdentifier, chainInterface.getNativeCurrencyAddress(), abortController, false);
602
618
  const channelsPrefetch = this.LightningAssertions.getChannelsPrefetch(abortController);
603
619
  //Asynchronously send the node's public key to the client
604
620
  this.sendPublicKeyAsync(responseStream);
@@ -606,13 +622,15 @@ class FromBtcLnAuto extends FromBtcBaseSwapHandler_1.FromBtcBaseSwapHandler {
606
622
  let { amountBD, swapFee, swapFeeInToken, totalInToken, amountBDgas, gasSwapFee, gasSwapFeeInToken, totalInGasToken } = await this.AmountAssertions.checkFromBtcAmount(this.type, request, { ...requestedAmount, pricePrefetch: pricePrefetchPromise }, fees, abortController.signal, { ...gasTokenAmount, pricePrefetch: gasTokenPricePrefetchPromise });
607
623
  metadata.times.priceCalculated = Date.now();
608
624
  const totalBtcInput = amountBD + amountBDgas;
625
+ //Check if we have at least the minimum needed native balance
626
+ await this.checkBalance(minNativeTokenReserve, nativeTokenBalancePrefetch, abortController.signal);
609
627
  //Check if we have enough funds to honor the request
610
628
  if (useToken === gasToken) {
611
- await this.checkBalance(totalInToken + totalInGasToken + minNativeTokenReserve, balancePrefetch, abortController.signal);
629
+ await this.checkBalance(totalInToken + totalInGasToken, balancePrefetch, abortController.signal);
612
630
  }
613
631
  else {
614
632
  await this.checkBalance(totalInToken, balancePrefetch, abortController.signal);
615
- await this.checkBalance(totalInGasToken + minNativeTokenReserve, gasTokenBalancePrefetch, abortController.signal);
633
+ await this.checkBalance(totalInGasToken, gasTokenBalancePrefetch, abortController.signal);
616
634
  }
617
635
  await this.LightningAssertions.checkInboundLiquidity(totalBtcInput, channelsPrefetch, abortController.signal);
618
636
  metadata.times.balanceChecked = Date.now();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atomiqlabs/lp-lib",
3
- "version": "16.0.1",
3
+ "version": "16.0.2",
4
4
  "description": "Main functionality implementation for atomiq LP node",
5
5
  "main": "./dist/index.js",
6
6
  "types:": "./dist/index.d.ts",
@@ -394,8 +394,10 @@ export class FromBtcLnAuto extends FromBtcBaseSwapHandler<FromBtcLnAutoSwap, Fro
394
394
 
395
395
  //Pre-fetch data
396
396
  const balancePrefetch: Promise<bigint> = this.getBalancePrefetch(invoiceData.chainIdentifier, useToken, abortController);
397
- const gasTokenBalancePrefetch: Promise<bigint> = (invoiceData.getTotalOutputGasAmount()===0n && minNativeTokenReserve===0n) || useToken===gasToken ?
397
+ const gasTokenBalancePrefetch: Promise<bigint> = invoiceData.getTotalOutputGasAmount()===0n || useToken===gasToken ?
398
398
  null : this.getBalancePrefetch(invoiceData.chainIdentifier, gasToken, abortController);
399
+ const nativeBalancePrefetch: Promise<bigint> = minNativeTokenReserve===0n ?
400
+ null : this.getBalancePrefetch(invoiceData.chainIdentifier, chainInterface.getNativeCurrencyAddress(), abortController, false);
399
401
 
400
402
  if(await swapContract.getInitAuthorizationExpiry(invoiceData.data, invoiceData) < Date.now()) {
401
403
  if(invoiceData.metadata!=null) invoiceData.metadata.htlcOfferError = "Init authorization expired, before being sent!";
@@ -405,13 +407,23 @@ export class FromBtcLnAuto extends FromBtcBaseSwapHandler<FromBtcLnAutoSwap, Fro
405
407
  return false;
406
408
  }
407
409
 
410
+ try {
411
+ await this.checkBalance(minNativeTokenReserve, nativeBalancePrefetch, abortController.signal);
412
+ } catch (e) {
413
+ if(!abortController.signal.aborted) {
414
+ if(isDefinedRuntimeError(e) && invoiceData.metadata!=null) invoiceData.metadata.htlcOfferError = "Not enough native balance!";
415
+ if(invoiceData.state===FromBtcLnAutoSwapState.RECEIVED) await this.cancelSwapAndInvoice(invoiceData);
416
+ }
417
+ throw e;
418
+ }
419
+
408
420
  try {
409
421
  //Check if we have enough liquidity to proceed
410
422
  if(useToken===gasToken) {
411
- await this.checkBalance(invoiceData.getTotalOutputAmount() + invoiceData.getTotalOutputGasAmount() + minNativeTokenReserve, balancePrefetch, abortController.signal);
423
+ await this.checkBalance(invoiceData.getTotalOutputAmount() + invoiceData.getTotalOutputGasAmount(), balancePrefetch, abortController.signal);
412
424
  } else {
413
425
  await this.checkBalance(invoiceData.getTotalOutputAmount(), balancePrefetch, abortController.signal);
414
- await this.checkBalance(invoiceData.getTotalOutputGasAmount() + minNativeTokenReserve, gasTokenBalancePrefetch, abortController.signal);
426
+ await this.checkBalance(invoiceData.getTotalOutputGasAmount(), gasTokenBalancePrefetch, abortController.signal);
415
427
  }
416
428
  if(invoiceData.metadata!=null) invoiceData.metadata.times.offerHtlcChecked = Date.now();
417
429
  } catch (e) {
@@ -702,8 +714,10 @@ export class FromBtcLnAuto extends FromBtcBaseSwapHandler<FromBtcLnAutoSwap, Fro
702
714
  gasTokenPricePrefetchPromise
703
715
  } = this.getFromBtcPricePrefetches(chainIdentifier, useToken, gasToken, abortController);
704
716
  const balancePrefetch: Promise<bigint> = this.getBalancePrefetch(chainIdentifier, useToken, abortController);
705
- const gasTokenBalancePrefetch: Promise<bigint> = (gasTokenAmount.amount===0n && minNativeTokenReserve==0n) || useToken===gasToken ?
717
+ const gasTokenBalancePrefetch: Promise<bigint> = gasTokenAmount.amount===0n || useToken===gasToken ?
706
718
  null : this.getBalancePrefetch(chainIdentifier, gasToken, abortController);
719
+ const nativeTokenBalancePrefetch: Promise<bigint> = minNativeTokenReserve===0n ?
720
+ null : this.getBalancePrefetch(chainIdentifier, chainInterface.getNativeCurrencyAddress(), abortController, false);
707
721
  const channelsPrefetch: Promise<LightningNetworkChannel[]> = this.LightningAssertions.getChannelsPrefetch(abortController);
708
722
 
709
723
  //Asynchronously send the node's public key to the client
@@ -729,12 +743,15 @@ export class FromBtcLnAuto extends FromBtcBaseSwapHandler<FromBtcLnAutoSwap, Fro
729
743
 
730
744
  const totalBtcInput = amountBD + amountBDgas;
731
745
 
746
+ //Check if we have at least the minimum needed native balance
747
+ await this.checkBalance(minNativeTokenReserve, nativeTokenBalancePrefetch, abortController.signal);
748
+
732
749
  //Check if we have enough funds to honor the request
733
750
  if(useToken===gasToken) {
734
- await this.checkBalance(totalInToken + totalInGasToken + minNativeTokenReserve, balancePrefetch, abortController.signal);
751
+ await this.checkBalance(totalInToken + totalInGasToken, balancePrefetch, abortController.signal);
735
752
  } else {
736
753
  await this.checkBalance(totalInToken, balancePrefetch, abortController.signal);
737
- await this.checkBalance(totalInGasToken + minNativeTokenReserve, gasTokenBalancePrefetch, abortController.signal);
754
+ await this.checkBalance(totalInGasToken, gasTokenBalancePrefetch, abortController.signal);
738
755
  }
739
756
  await this.LightningAssertions.checkInboundLiquidity(totalBtcInput, channelsPrefetch, abortController.signal);
740
757
  metadata.times.balanceChecked = Date.now();