@atomiqlabs/lp-lib 16.0.7 → 16.0.9

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.
@@ -138,7 +138,7 @@ class FromBtcAmountAssertions extends AmountAssertions_1.AmountAssertions {
138
138
  this.checkBtcAmountInBounds(requestedAmount.amount);
139
139
  amountBD = requestedAmount.amount - amountBDgas;
140
140
  swapFee = fees.baseFee + ((amountBD * fees.feePPM + 999999n) / 1000000n);
141
- if (amountBD < 0n) {
141
+ if (amountBD - swapFee < 0n) {
142
142
  throw {
143
143
  code: 20003,
144
144
  msg: "Amount too low!",
@@ -163,6 +163,11 @@ class FromBtcAmountAssertions extends AmountAssertions_1.AmountAssertions {
163
163
  totalInToken = await this.swapPricing.getFromBtcSwapAmount(amountBD - swapFee, requestedAmount.token, chainIdentifier, null, requestedAmount.pricePrefetch);
164
164
  signal.throwIfAborted();
165
165
  }
166
+ if (totalInToken < 0n)
167
+ throw {
168
+ code: 20003,
169
+ msg: "Amount too low!"
170
+ };
166
171
  return {
167
172
  amountBD,
168
173
  swapFee,
@@ -586,6 +586,11 @@ class ToBtcAbs extends ToBtcBaseSwapHandler_1.ToBtcBaseSwapHandler {
586
586
  return resp;
587
587
  }, abortController.signal);
588
588
  metadata.times.priceCalculated = Date.now();
589
+ if (amountBD < 600n)
590
+ throw {
591
+ code: "20019",
592
+ msg: "Swap output amount is below bitcoin dust (600 sats)!"
593
+ };
589
594
  const claimHash = this.getHash(chainIdentifier, parsedBody.address, parsedBody.confirmations, parsedBody.nonce, amountBD).toString("hex");
590
595
  //Add grace period another time, so the user has 1 hour to commit
591
596
  const expirySeconds = this.getExpiryFromCLTV(parsedBody.confirmationTarget, parsedBody.confirmations) + this.config.gracePeriod;
@@ -167,11 +167,10 @@ export declare class ToBtcLnAbs extends ToBtcBaseSwapHandler<ToBtcLnSwapAbs, ToB
167
167
  * Checks if the newly submitted PR has the same parameters (destination, cltv_delta, routes) as the initial dummy
168
168
  * invoice sent for exactIn swap quote
169
169
  *
170
- * @param pr
170
+ * @param parsedRequest
171
171
  * @param parsedAuth
172
- * @throws {DefinedRuntimeError} will throw an error if the details don't match
173
172
  */
174
- private checkPaymentRequestMatchesInitial;
173
+ private isPaymentRequestMatchingInitial;
175
174
  startRestServer(restServer: Express): void;
176
175
  init(): Promise<void>;
177
176
  getInfoData(): any;
@@ -537,26 +537,13 @@ class ToBtcLnAbs extends ToBtcBaseSwapHandler_1.ToBtcBaseSwapHandler {
537
537
  * Checks if the newly submitted PR has the same parameters (destination, cltv_delta, routes) as the initial dummy
538
538
  * invoice sent for exactIn swap quote
539
539
  *
540
- * @param pr
540
+ * @param parsedRequest
541
541
  * @param parsedAuth
542
- * @throws {DefinedRuntimeError} will throw an error if the details don't match
543
542
  */
544
- async checkPaymentRequestMatchesInitial(pr, parsedAuth) {
545
- const parsedRequest = await this.lightning.parsePaymentRequest(pr);
546
- if (parsedRequest.destination !== parsedAuth.initialInvoice.destination ||
547
- parsedRequest.cltvDelta !== parsedAuth.initialInvoice.cltvDelta ||
548
- parsedRequest.mtokens !== parsedAuth.amount * 1000n) {
549
- throw {
550
- code: 20102,
551
- msg: "Provided PR doesn't match initial!"
552
- };
553
- }
554
- if (!(0, ILightningWallet_1.routesMatch)(parsedRequest.routes, parsedAuth.initialInvoice.routes)) {
555
- throw {
556
- code: 20102,
557
- msg: "Provided PR doesn't match initial (routes)!"
558
- };
559
- }
543
+ isPaymentRequestMatchingInitial(parsedRequest, parsedAuth) {
544
+ return parsedRequest.destination === parsedAuth.initialInvoice.destination &&
545
+ parsedRequest.cltvDelta === parsedAuth.initialInvoice.cltvDelta &&
546
+ (0, ILightningWallet_1.routesMatch)(parsedRequest.routes, parsedAuth.initialInvoice.routes);
560
547
  }
561
548
  startRestServer(restServer) {
562
549
  restServer.use(this.path + "/payInvoiceExactIn", (0, ServerParamDecoder_1.serverParamDecoder)(10 * 1000));
@@ -583,7 +570,25 @@ class ToBtcLnAbs extends ToBtcBaseSwapHandler_1.ToBtcBaseSwapHandler {
583
570
  const abortSignal = responseStream.getAbortSignal();
584
571
  //Check request params
585
572
  const { parsedPR, halfConfidence } = await this.checkPaymentRequest(parsedAuth.chainIdentifier, parsedBody.pr);
586
- await this.checkPaymentRequestMatchesInitial(parsedBody.pr, parsedAuth);
573
+ if (parsedPR.mtokens !== parsedAuth.amount * 1000n)
574
+ throw {
575
+ code: 20102,
576
+ msg: "Provided PR doesn't match requested (amount)!"
577
+ };
578
+ if (!this.isPaymentRequestMatchingInitial(parsedPR, parsedAuth)) {
579
+ //The provided payment request doesn't match the parameters from the initial one, try to probe/route again
580
+ // with the same max fee parameters
581
+ const currentTimestamp = BigInt(Math.floor(Date.now() / 1000));
582
+ const { networkFee, confidence } = await this.checkAndGetNetworkFee(parsedAuth.amount, parsedAuth.quotedNetworkFee, parsedAuth.swapExpiry, currentTimestamp, parsedBody.pr, parsedAuth.metadata, abortSignal);
583
+ this.logger.info("REST: /payInvoiceExactIn: re-checked network fee for exact-in swap," +
584
+ " reqId: " + parsedBody.reqId +
585
+ " initialNetworkFee: " + parsedAuth.quotedNetworkFee.toString(10) +
586
+ " newNetworkFee: " + networkFee.toString(10) +
587
+ " oldConfidence: " + parsedAuth.confidence.toString(10) +
588
+ " newConfidence: " + confidence.toString(10) +
589
+ " invoice: " + parsedBody.pr);
590
+ parsedAuth.confidence = confidence;
591
+ }
587
592
  const metadata = parsedAuth.metadata;
588
593
  const sequence = base_1.BigIntBufferUtils.fromBuffer((0, crypto_1.randomBytes)(8));
589
594
  const { swapContract, signer } = this.getChain(parsedAuth.chainIdentifier);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atomiqlabs/lp-lib",
3
- "version": "16.0.7",
3
+ "version": "16.0.9",
4
4
  "description": "Main functionality implementation for atomiq LP node",
5
5
  "main": "./dist/index.js",
6
6
  "types:": "./dist/index.d.ts",
@@ -201,7 +201,7 @@ export class FromBtcAmountAssertions extends AmountAssertions {
201
201
  this.checkBtcAmountInBounds(requestedAmount.amount);
202
202
  amountBD = requestedAmount.amount - amountBDgas;
203
203
  swapFee = fees.baseFee + ((amountBD * fees.feePPM + 999_999n) / 1000000n);
204
- if(amountBD < 0n) {
204
+ if(amountBD - swapFee < 0n) {
205
205
  throw {
206
206
  code: 20003,
207
207
  msg: "Amount too low!",
@@ -229,6 +229,11 @@ export class FromBtcAmountAssertions extends AmountAssertions {
229
229
  signal.throwIfAborted();
230
230
  }
231
231
 
232
+ if(totalInToken < 0n) throw {
233
+ code: 20003,
234
+ msg: "Amount too low!"
235
+ };
236
+
232
237
  return {
233
238
  amountBD,
234
239
  swapFee,
@@ -708,6 +708,11 @@ export class ToBtcAbs extends ToBtcBaseSwapHandler<ToBtcSwapAbs, ToBtcSwapState>
708
708
  }, abortController.signal);
709
709
  metadata.times.priceCalculated = Date.now();
710
710
 
711
+ if(amountBD < 600n) throw {
712
+ code: "20019",
713
+ msg: "Swap output amount is below bitcoin dust (600 sats)!"
714
+ };
715
+
711
716
  const claimHash = this.getHash(chainIdentifier, parsedBody.address, parsedBody.confirmations, parsedBody.nonce, amountBD).toString("hex");
712
717
 
713
718
  //Add grace period another time, so the user has 1 hour to commit
@@ -666,30 +666,13 @@ export class ToBtcLnAbs extends ToBtcBaseSwapHandler<ToBtcLnSwapAbs, ToBtcLnSwap
666
666
  * Checks if the newly submitted PR has the same parameters (destination, cltv_delta, routes) as the initial dummy
667
667
  * invoice sent for exactIn swap quote
668
668
  *
669
- * @param pr
669
+ * @param parsedRequest
670
670
  * @param parsedAuth
671
- * @throws {DefinedRuntimeError} will throw an error if the details don't match
672
671
  */
673
- private async checkPaymentRequestMatchesInitial(pr: string, parsedAuth: ExactInAuthorization): Promise<void> {
674
- const parsedRequest = await this.lightning.parsePaymentRequest(pr);
675
-
676
- if(
677
- parsedRequest.destination!==parsedAuth.initialInvoice.destination ||
678
- parsedRequest.cltvDelta!==parsedAuth.initialInvoice.cltvDelta ||
679
- parsedRequest.mtokens!==parsedAuth.amount * 1000n
680
- ) {
681
- throw {
682
- code: 20102,
683
- msg: "Provided PR doesn't match initial!"
684
- };
685
- }
686
-
687
- if(!routesMatch(parsedRequest.routes, parsedAuth.initialInvoice.routes)) {
688
- throw {
689
- code: 20102,
690
- msg: "Provided PR doesn't match initial (routes)!"
691
- };
692
- }
672
+ private isPaymentRequestMatchingInitial(parsedRequest: ParsedPaymentRequest, parsedAuth: ExactInAuthorization): boolean {
673
+ return parsedRequest.destination===parsedAuth.initialInvoice.destination &&
674
+ parsedRequest.cltvDelta===parsedAuth.initialInvoice.cltvDelta &&
675
+ routesMatch(parsedRequest.routes, parsedAuth.initialInvoice.routes);
693
676
  }
694
677
 
695
678
  startRestServer(restServer: Express) {
@@ -721,7 +704,27 @@ export class ToBtcLnAbs extends ToBtcBaseSwapHandler<ToBtcLnSwapAbs, ToBtcLnSwap
721
704
 
722
705
  //Check request params
723
706
  const {parsedPR, halfConfidence} = await this.checkPaymentRequest(parsedAuth.chainIdentifier, parsedBody.pr);
724
- await this.checkPaymentRequestMatchesInitial(parsedBody.pr, parsedAuth);
707
+ if(parsedPR.mtokens!==parsedAuth.amount*1000n) throw {
708
+ code: 20102,
709
+ msg: "Provided PR doesn't match requested (amount)!"
710
+ };
711
+ if(!this.isPaymentRequestMatchingInitial(parsedPR, parsedAuth)) {
712
+ //The provided payment request doesn't match the parameters from the initial one, try to probe/route again
713
+ // with the same max fee parameters
714
+ const currentTimestamp: bigint = BigInt(Math.floor(Date.now()/1000));
715
+ const {networkFee, confidence} = await this.checkAndGetNetworkFee(
716
+ parsedAuth.amount, parsedAuth.quotedNetworkFee, parsedAuth.swapExpiry,
717
+ currentTimestamp, parsedBody.pr, parsedAuth.metadata, abortSignal
718
+ );
719
+ this.logger.info("REST: /payInvoiceExactIn: re-checked network fee for exact-in swap,"+
720
+ " reqId: "+parsedBody.reqId+
721
+ " initialNetworkFee: "+parsedAuth.quotedNetworkFee.toString(10)+
722
+ " newNetworkFee: "+networkFee.toString(10)+
723
+ " oldConfidence: "+parsedAuth.confidence.toString(10)+
724
+ " newConfidence: "+confidence.toString(10)+
725
+ " invoice: "+parsedBody.pr);
726
+ parsedAuth.confidence = confidence;
727
+ }
725
728
 
726
729
  const metadata = parsedAuth.metadata;
727
730