@atomiqlabs/lp-lib 14.0.0-dev.23 → 14.0.0-dev.25

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.
@@ -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
  }
@@ -8,7 +8,8 @@ const AmountAssertions_1 = require("../assertions/AmountAssertions");
8
8
  const btc_signer_1 = require("@scure/btc-signer");
9
9
  exports.VAULT_DUST_AMOUNT = 600;
10
10
  const VAULT_INIT_CONFIRMATIONS = 2;
11
- const BTC_FINALIZATION_CONFIRMATIONS = 6;
11
+ // const BTC_FINALIZATION_CONFIRMATIONS = 6;
12
+ const MAX_PARALLEL_VAULTS_OPENING = 10;
12
13
  class SpvVaults {
13
14
  constructor(vaultStorage, bitcoin, vaultSigner, bitcoinRpc, chains, config) {
14
15
  this.logger = (0, Utils_1.getLogger)("SpvVaults: ");
@@ -198,6 +199,7 @@ class SpvVaults {
198
199
  async checkVaults() {
199
200
  const vaults = Object.keys(this.vaultStorage.data).map(key => this.vaultStorage.data[key]);
200
201
  const claimWithdrawals = [];
202
+ let promises = [];
201
203
  for (let vault of vaults) {
202
204
  const { signer, spvVaultContract, chainInterface } = this.chains.chains[vault.chainId];
203
205
  if (vault.data.getOwner() !== signer.getAddress())
@@ -239,17 +241,23 @@ class SpvVaults {
239
241
  }
240
242
  const txs = await spvVaultContract.txsOpen(signer.getAddress(), vault.data);
241
243
  let numTx = 0;
242
- const txIds = await chainInterface.sendAndConfirm(signer, txs, true, undefined, true, async (txId, rawTx) => {
244
+ promises.push(chainInterface.sendAndConfirm(signer, txs, true, undefined, true, async (txId, rawTx) => {
243
245
  numTx++;
244
246
  if (numTx === txs.length) {
245
247
  //Final tx
246
248
  vault.scOpenTxs = { [txId]: rawTx };
247
249
  await this.saveVault(vault);
248
250
  }
249
- });
250
- this.logger.info("checkVaults(): Vault ID " + vault.data.getVaultId().toString(10) + " opened on " + vault.chainId + " txId: " + txIds.join(", "));
251
- vault.state = SpvVault_1.SpvVaultState.OPENED;
252
- await this.saveVault(vault);
251
+ }).then(txIds => {
252
+ this.logger.info("checkVaults(): Vault ID " + vault.data.getVaultId().toString(10) + " opened on " + vault.chainId + " txId: " + txIds.join(", "));
253
+ vault.state = SpvVault_1.SpvVaultState.OPENED;
254
+ return this.saveVault(vault);
255
+ }));
256
+ if (promises.length >= MAX_PARALLEL_VAULTS_OPENING) {
257
+ await Promise.all(promises);
258
+ promises = [];
259
+ }
260
+ continue;
253
261
  }
254
262
  if (vault.state === SpvVault_1.SpvVaultState.OPENED) {
255
263
  let changed = false;
@@ -259,29 +267,29 @@ class SpvVaults {
259
267
  for (let i = 0; i < vault.pendingWithdrawals.length; i++) {
260
268
  const pendingWithdrawal = vault.pendingWithdrawals[i];
261
269
  //Check all the pending withdrawals that were not finalized yet
262
- if (pendingWithdrawal.btcTx.confirmations == null || pendingWithdrawal.btcTx.confirmations < BTC_FINALIZATION_CONFIRMATIONS) {
263
- const btcTx = await this.bitcoinRpc.getTransaction(pendingWithdrawal.btcTx.txid);
264
- if (btcTx == null) {
265
- //Probable double-spend, remove from pending withdrawals
266
- const index = vault.pendingWithdrawals.indexOf(pendingWithdrawal);
267
- if (index === -1) {
268
- this.logger.warn("checkVaults(): Tried to remove pending withdrawal txId: " + pendingWithdrawal.btcTx.txid + ", but doesn't exist anymore!");
269
- }
270
- else {
271
- vault.pendingWithdrawals.splice(index, 1);
272
- }
273
- changed = true;
270
+ // if(pendingWithdrawal.btcTx.confirmations==null || pendingWithdrawal.btcTx.confirmations < BTC_FINALIZATION_CONFIRMATIONS) {
271
+ const btcTx = await this.bitcoinRpc.getTransaction(pendingWithdrawal.btcTx.txid);
272
+ if (btcTx == null) {
273
+ //Probable double-spend, remove from pending withdrawals
274
+ const index = vault.pendingWithdrawals.indexOf(pendingWithdrawal);
275
+ if (index === -1) {
276
+ this.logger.warn("checkVaults(): Tried to remove pending withdrawal txId: " + pendingWithdrawal.btcTx.txid + ", but doesn't exist anymore!");
274
277
  }
275
278
  else {
276
- //Update confirmations count
277
- if (pendingWithdrawal.btcTx.confirmations !== btcTx.confirmations ||
278
- pendingWithdrawal.btcTx.blockhash !== btcTx.blockhash) {
279
- pendingWithdrawal.btcTx.confirmations = btcTx.confirmations;
280
- pendingWithdrawal.btcTx.blockhash = btcTx.blockhash;
281
- changed = true;
282
- }
279
+ vault.pendingWithdrawals.splice(index, 1);
280
+ }
281
+ changed = true;
282
+ }
283
+ else {
284
+ //Update confirmations count
285
+ if (pendingWithdrawal.btcTx.confirmations !== btcTx.confirmations ||
286
+ pendingWithdrawal.btcTx.blockhash !== btcTx.blockhash) {
287
+ pendingWithdrawal.btcTx.confirmations = btcTx.confirmations;
288
+ pendingWithdrawal.btcTx.blockhash = btcTx.blockhash;
289
+ changed = true;
283
290
  }
284
291
  }
292
+ // }
285
293
  //Check it has enough confirmations
286
294
  if (pendingWithdrawal.btcTx.confirmations >= vault.data.getConfirmations()) {
287
295
  latestConfirmedWithdrawalIndex = i;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atomiqlabs/lp-lib",
3
- "version": "14.0.0-dev.23",
3
+ "version": "14.0.0-dev.25",
4
4
  "description": "Main functionality implementation for atomiq LP node",
5
5
  "main": "./dist/index.js",
6
6
  "types:": "./dist/index.d.ts",
@@ -22,7 +22,7 @@
22
22
  "author": "adambor",
23
23
  "license": "ISC",
24
24
  "dependencies": {
25
- "@atomiqlabs/base": "^10.0.0-dev.9",
25
+ "@atomiqlabs/base": "^10.0.0-dev.10",
26
26
  "@atomiqlabs/server-base": "2.0.0",
27
27
  "@scure/btc-signer": "1.6.0",
28
28
  "express": "4.21.1",
@@ -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
  }
@@ -18,7 +18,8 @@ import {Transaction} from "@scure/btc-signer";
18
18
 
19
19
  export const VAULT_DUST_AMOUNT = 600;
20
20
  const VAULT_INIT_CONFIRMATIONS = 2;
21
- const BTC_FINALIZATION_CONFIRMATIONS = 6;
21
+ // const BTC_FINALIZATION_CONFIRMATIONS = 6;
22
+ const MAX_PARALLEL_VAULTS_OPENING = 10;
22
23
 
23
24
  export class SpvVaults {
24
25
 
@@ -252,6 +253,8 @@ export class SpvVaults {
252
253
 
253
254
  const claimWithdrawals: {vault: SpvVault, withdrawals: SpvWithdrawalTransactionData[]}[] = [];
254
255
 
256
+ let promises: Promise<void>[] = [];
257
+
255
258
  for(let vault of vaults) {
256
259
  const {signer, spvVaultContract, chainInterface} = this.chains.chains[vault.chainId];
257
260
  if(vault.data.getOwner()!==signer.getAddress()) continue;
@@ -294,21 +297,29 @@ export class SpvVaults {
294
297
 
295
298
  const txs = await spvVaultContract.txsOpen(signer.getAddress(), vault.data);
296
299
  let numTx = 0;
297
- const txIds = await chainInterface.sendAndConfirm(
298
- signer, txs, true, undefined, true,
299
- async (txId: string, rawTx: string) => {
300
- numTx++;
301
- if(numTx===txs.length) {
302
- //Final tx
303
- vault.scOpenTxs = {[txId]: rawTx};
304
- await this.saveVault(vault);
300
+ promises.push(
301
+ chainInterface.sendAndConfirm(
302
+ signer, txs, true, undefined, true,
303
+ async (txId: string, rawTx: string) => {
304
+ numTx++;
305
+ if(numTx===txs.length) {
306
+ //Final tx
307
+ vault.scOpenTxs = {[txId]: rawTx};
308
+ await this.saveVault(vault);
309
+ }
305
310
  }
306
- }
307
- );
308
- this.logger.info("checkVaults(): Vault ID "+vault.data.getVaultId().toString(10)+" opened on "+vault.chainId+" txId: "+txIds.join(", "));
311
+ ).then(txIds => {
312
+ this.logger.info("checkVaults(): Vault ID "+vault.data.getVaultId().toString(10)+" opened on "+vault.chainId+" txId: "+txIds.join(", "));
309
313
 
310
- vault.state = SpvVaultState.OPENED;
311
- await this.saveVault(vault);
314
+ vault.state = SpvVaultState.OPENED;
315
+ return this.saveVault(vault);
316
+ })
317
+ );
318
+ if(promises.length>=MAX_PARALLEL_VAULTS_OPENING) {
319
+ await Promise.all(promises);
320
+ promises = [];
321
+ }
322
+ continue;
312
323
  }
313
324
 
314
325
  if(vault.state===SpvVaultState.OPENED) {
@@ -319,7 +330,7 @@ export class SpvVaults {
319
330
  for(let i=0; i<vault.pendingWithdrawals.length; i++) {
320
331
  const pendingWithdrawal = vault.pendingWithdrawals[i];
321
332
  //Check all the pending withdrawals that were not finalized yet
322
- if(pendingWithdrawal.btcTx.confirmations==null || pendingWithdrawal.btcTx.confirmations < BTC_FINALIZATION_CONFIRMATIONS) {
333
+ // if(pendingWithdrawal.btcTx.confirmations==null || pendingWithdrawal.btcTx.confirmations < BTC_FINALIZATION_CONFIRMATIONS) {
323
334
  const btcTx = await this.bitcoinRpc.getTransaction(pendingWithdrawal.btcTx.txid);
324
335
  if(btcTx==null) {
325
336
  //Probable double-spend, remove from pending withdrawals
@@ -341,7 +352,7 @@ export class SpvVaults {
341
352
  changed = true;
342
353
  }
343
354
  }
344
- }
355
+ // }
345
356
  //Check it has enough confirmations
346
357
  if(pendingWithdrawal.btcTx.confirmations >= vault.data.getConfirmations()) {
347
358
  latestConfirmedWithdrawalIndex = i;