@atomiqlabs/lp-lib 14.0.0-dev.23 → 14.0.0-dev.24
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.
|
@@ -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
|
-
|
|
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
|
-
|
|
251
|
-
|
|
252
|
-
|
|
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
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
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
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
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.
|
|
3
|
+
"version": "14.0.0-dev.24",
|
|
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.
|
|
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",
|
|
@@ -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
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
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
|
-
|
|
311
|
-
|
|
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;
|