@atomiqlabs/lp-lib 14.0.0-dev.24 → 14.0.0-dev.26

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.
@@ -12,6 +12,7 @@ const ServerParamDecoder_1 = require("../../../utils/paramcoders/server/ServerPa
12
12
  const ToBtcBaseSwapHandler_1 = require("../ToBtcBaseSwapHandler");
13
13
  const promise_queue_ts_1 = require("promise-queue-ts");
14
14
  const OUTPUT_SCRIPT_MAX_LENGTH = 200;
15
+ const MAX_PARALLEL_TX_PROCESSED = 10;
15
16
  /**
16
17
  * Handler for to BTC swaps, utilizing PTLCs (proof-time locked contracts) using btc relay (on-chain bitcoin SPV)
17
18
  */
@@ -170,22 +171,30 @@ class ToBtcAbs extends ToBtcBaseSwapHandler_1.ToBtcBaseSwapHandler {
170
171
  */
171
172
  async processBtcTxs() {
172
173
  const unsubscribeSwaps = [];
174
+ let promises = [];
173
175
  for (let txId in this.activeSubscriptions) {
174
176
  const swap = this.activeSubscriptions[txId];
175
177
  //TODO: RBF the transaction if it's already taking too long to confirm
176
- try {
177
- let tx = await this.bitcoin.getWalletTransaction(txId);
178
- if (tx == null)
179
- continue;
180
- if (await this.processBtcTx(swap, tx)) {
181
- this.swapLogger.info(swap, "processBtcTxs(): swap claimed successfully, txId: " + tx.txid + " address: " + swap.address);
182
- unsubscribeSwaps.push(swap);
178
+ promises.push((async () => {
179
+ try {
180
+ let tx = await this.bitcoin.getWalletTransaction(txId);
181
+ if (tx == null)
182
+ return;
183
+ if (await this.processBtcTx(swap, tx)) {
184
+ this.swapLogger.info(swap, "processBtcTxs(): swap claimed successfully, txId: " + tx.txid + " address: " + swap.address);
185
+ unsubscribeSwaps.push(swap);
186
+ }
183
187
  }
184
- }
185
- catch (e) {
186
- this.swapLogger.error(swap, "processBtcTxs(): error processing btc transaction", e);
188
+ catch (e) {
189
+ this.swapLogger.error(swap, "processBtcTxs(): error processing btc transaction", e);
190
+ }
191
+ })());
192
+ if (promises.length >= MAX_PARALLEL_TX_PROCESSED) {
193
+ await Promise.all(promises);
194
+ promises = [];
187
195
  }
188
196
  }
197
+ await Promise.all(promises);
189
198
  unsubscribeSwaps.forEach(swap => {
190
199
  this.unsubscribePayment(swap);
191
200
  });
@@ -115,7 +115,7 @@ class SpvVaultSwapHandler extends SwapHandler_1.SwapHandler {
115
115
  await this.saveSwapData(swap);
116
116
  }
117
117
  }
118
- if (swap.state === SpvVaultSwap_1.SpvVaultSwapState.SENT) {
118
+ if (swap.state === SpvVaultSwap_1.SpvVaultSwapState.SENT || swap.state === SpvVaultSwap_1.SpvVaultSwapState.BTC_CONFIRMED) {
119
119
  //Check if confirmed or double-spent
120
120
  const tx = await this.bitcoinRpc.getTransaction(swap.btcTxId);
121
121
  if (tx == null) {
@@ -123,8 +123,10 @@ class SpvVaultSwapHandler extends SwapHandler_1.SwapHandler {
123
123
  return;
124
124
  }
125
125
  else if (tx.confirmations > 0) {
126
- await swap.setState(SpvVaultSwap_1.SpvVaultSwapState.BTC_CONFIRMED);
127
- await this.saveSwapData(swap);
126
+ if (swap.state !== SpvVaultSwap_1.SpvVaultSwapState.BTC_CONFIRMED) {
127
+ await swap.setState(SpvVaultSwap_1.SpvVaultSwapState.BTC_CONFIRMED);
128
+ await this.saveSwapData(swap);
129
+ }
128
130
  }
129
131
  }
130
132
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atomiqlabs/lp-lib",
3
- "version": "14.0.0-dev.24",
3
+ "version": "14.0.0-dev.26",
4
4
  "description": "Main functionality implementation for atomiq LP node",
5
5
  "main": "./dist/index.js",
6
6
  "types:": "./dist/index.d.ts",
@@ -51,6 +51,8 @@ export type ToBtcRequestType = {
51
51
  exactIn?: boolean
52
52
  };
53
53
 
54
+ const MAX_PARALLEL_TX_PROCESSED = 10;
55
+
54
56
  /**
55
57
  * Handler for to BTC swaps, utilizing PTLCs (proof-time locked contracts) using btc relay (on-chain bitcoin SPV)
56
58
  */
@@ -248,21 +250,29 @@ export class ToBtcAbs extends ToBtcBaseSwapHandler<ToBtcSwapAbs, ToBtcSwapState>
248
250
  private async processBtcTxs() {
249
251
  const unsubscribeSwaps: ToBtcSwapAbs[] = [];
250
252
 
253
+ let promises: Promise<void>[] = [];
251
254
  for(let txId in this.activeSubscriptions) {
252
255
  const swap: ToBtcSwapAbs = this.activeSubscriptions[txId];
253
256
  //TODO: RBF the transaction if it's already taking too long to confirm
254
- try {
255
- let tx: BtcTx = await this.bitcoin.getWalletTransaction(txId);
256
- if(tx==null) continue;
257
-
258
- if(await this.processBtcTx(swap, tx)) {
259
- this.swapLogger.info(swap, "processBtcTxs(): swap claimed successfully, txId: "+tx.txid+" address: "+swap.address);
260
- unsubscribeSwaps.push(swap);
257
+ promises.push((async () => {
258
+ try {
259
+ let tx: BtcTx = await this.bitcoin.getWalletTransaction(txId);
260
+ if(tx==null) return;
261
+
262
+ if(await this.processBtcTx(swap, tx)) {
263
+ this.swapLogger.info(swap, "processBtcTxs(): swap claimed successfully, txId: "+tx.txid+" address: "+swap.address);
264
+ unsubscribeSwaps.push(swap);
265
+ }
266
+ } catch (e) {
267
+ this.swapLogger.error(swap, "processBtcTxs(): error processing btc transaction", e);
261
268
  }
262
- } catch (e) {
263
- this.swapLogger.error(swap, "processBtcTxs(): error processing btc transaction", e);
269
+ })());
270
+ if(promises.length >= MAX_PARALLEL_TX_PROCESSED) {
271
+ await Promise.all(promises);
272
+ promises = [];
264
273
  }
265
274
  }
275
+ await Promise.all(promises);
266
276
 
267
277
  unsubscribeSwaps.forEach(swap => {
268
278
  this.unsubscribePayment(swap);
@@ -181,15 +181,17 @@ export class SpvVaultSwapHandler extends SwapHandler<SpvVaultSwap, SpvVaultSwapS
181
181
  }
182
182
  }
183
183
 
184
- if(swap.state===SpvVaultSwapState.SENT) {
184
+ if(swap.state===SpvVaultSwapState.SENT || swap.state===SpvVaultSwapState.BTC_CONFIRMED) {
185
185
  //Check if confirmed or double-spent
186
186
  const tx = await this.bitcoinRpc.getTransaction(swap.btcTxId);
187
187
  if(tx==null) {
188
188
  await this.removeSwapData(swap, SpvVaultSwapState.DOUBLE_SPENT);
189
189
  return;
190
190
  } else if(tx.confirmations > 0) {
191
- await swap.setState(SpvVaultSwapState.BTC_CONFIRMED)
192
- await this.saveSwapData(swap);
191
+ if(swap.state!==SpvVaultSwapState.BTC_CONFIRMED) {
192
+ await swap.setState(SpvVaultSwapState.BTC_CONFIRMED)
193
+ await this.saveSwapData(swap);
194
+ }
193
195
  }
194
196
  }
195
197
  }