@atomiqlabs/lp-lib 16.0.2 → 16.0.4
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.
- package/dist/swaps/SwapHandler.d.ts +28 -0
- package/dist/swaps/SwapHandler.js +66 -0
- package/dist/swaps/escrow/frombtc_abstract/FromBtcAbs.d.ts +1 -0
- package/dist/swaps/escrow/frombtc_abstract/FromBtcAbs.js +4 -0
- package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.d.ts +1 -0
- package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.js +27 -5
- package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.d.ts +1 -7
- package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.js +7 -41
- package/dist/swaps/escrow/tobtc_abstract/ToBtcAbs.d.ts +1 -0
- package/dist/swaps/escrow/tobtc_abstract/ToBtcAbs.js +5 -0
- package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.d.ts +2 -1
- package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.js +23 -2
- package/dist/swaps/spv_vault_swap/SpvVaultSwapHandler.d.ts +1 -0
- package/dist/swaps/spv_vault_swap/SpvVaultSwapHandler.js +9 -0
- package/dist/swaps/trusted/frombtc_trusted/FromBtcTrusted.d.ts +1 -0
- package/dist/swaps/trusted/frombtc_trusted/FromBtcTrusted.js +12 -0
- package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.d.ts +1 -0
- package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.js +12 -0
- package/package.json +1 -1
- package/src/swaps/SwapHandler.ts +72 -1
- package/src/swaps/escrow/frombtc_abstract/FromBtcAbs.ts +5 -0
- package/src/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.ts +24 -6
- package/src/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.ts +7 -46
- package/src/swaps/escrow/tobtc_abstract/ToBtcAbs.ts +6 -0
- package/src/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.ts +23 -4
- package/src/swaps/spv_vault_swap/SpvVaultSwapHandler.ts +16 -3
- package/src/swaps/trusted/frombtc_trusted/FromBtcTrusted.ts +15 -0
- package/src/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.ts +15 -1
|
@@ -3,7 +3,7 @@ import {Express, Request, Response} from "express";
|
|
|
3
3
|
import {IBitcoinWallet} from "../../wallets/IBitcoinWallet";
|
|
4
4
|
import {
|
|
5
5
|
BitcoinRpc,
|
|
6
|
-
BtcBlock,
|
|
6
|
+
BtcBlock,
|
|
7
7
|
ChainEvent,
|
|
8
8
|
IStorageManager,
|
|
9
9
|
SpvVaultClaimEvent,
|
|
@@ -21,7 +21,7 @@ import {ISpvVaultSigner} from "../../wallets/ISpvVaultSigner";
|
|
|
21
21
|
import {PluginManager} from "../../plugins/PluginManager";
|
|
22
22
|
import {SpvVault} from "./SpvVault";
|
|
23
23
|
import {serverParamDecoder} from "../../utils/paramcoders/server/ServerParamDecoder";
|
|
24
|
-
import {expressHandlerWrapper, getAbortController, HEX_REGEX} from "../../utils/Utils";
|
|
24
|
+
import {expressHandlerWrapper, getAbortController, HEX_REGEX, isDefinedRuntimeError} from "../../utils/Utils";
|
|
25
25
|
import {IParamReader} from "../../utils/paramcoders/IParamReader";
|
|
26
26
|
import {ServerParamEncoder} from "../../utils/paramcoders/server/ServerParamEncoder";
|
|
27
27
|
import {FieldTypeEnum} from "../../utils/paramcoders/SchemaVerifier";
|
|
@@ -57,8 +57,8 @@ export type SpvVaultPostQuote = {
|
|
|
57
57
|
const TX_MAX_VSIZE = 16*1024;
|
|
58
58
|
|
|
59
59
|
export class SpvVaultSwapHandler extends SwapHandler<SpvVaultSwap, SpvVaultSwapState> {
|
|
60
|
-
|
|
61
60
|
readonly type = SwapHandlerType.FROM_BTC_SPV;
|
|
61
|
+
readonly inflightSwapStates = new Set([SpvVaultSwapState.SIGNED, SpvVaultSwapState.SENT, SpvVaultSwapState.BTC_CONFIRMED]);
|
|
62
62
|
|
|
63
63
|
readonly bitcoin: IBitcoinWallet;
|
|
64
64
|
readonly bitcoinRpc: BitcoinRpc<BtcBlock>;
|
|
@@ -318,6 +318,8 @@ export class SpvVaultSwapHandler extends SwapHandler<SpvVaultSwap, SpvVaultSwapS
|
|
|
318
318
|
const useToken = parsedBody.token;
|
|
319
319
|
const gasToken = parsedBody.gasToken;
|
|
320
320
|
|
|
321
|
+
this.checkTooManyInflightSwaps();
|
|
322
|
+
|
|
321
323
|
//Check request params
|
|
322
324
|
const fees = await this.AmountAssertions.preCheckFromBtcAmounts(this.type, request, requestedAmount, gasTokenAmount);
|
|
323
325
|
metadata.times.requestChecked = Date.now();
|
|
@@ -331,6 +333,9 @@ export class SpvVaultSwapHandler extends SwapHandler<SpvVaultSwap, SpvVaultSwapS
|
|
|
331
333
|
pricePrefetchPromise,
|
|
332
334
|
gasTokenPricePrefetchPromise
|
|
333
335
|
} = this.getPricePrefetches(chainIdentifier, useToken, gasToken, abortController);
|
|
336
|
+
const nativeBalancePrefetch = this.prefetchNativeBalanceIfNeeded(chainIdentifier, abortController);
|
|
337
|
+
|
|
338
|
+
await this.checkNativeBalance(chainIdentifier, nativeBalancePrefetch, abortController.signal);
|
|
334
339
|
|
|
335
340
|
//Check valid amount specified (min/max)
|
|
336
341
|
let {
|
|
@@ -465,6 +470,8 @@ export class SpvVaultSwapHandler extends SwapHandler<SpvVaultSwap, SpvVaultSwapS
|
|
|
465
470
|
metadata.times ??= {};
|
|
466
471
|
metadata.times.requestReceived = requestReceived;
|
|
467
472
|
|
|
473
|
+
this.checkTooManyInflightSwaps();
|
|
474
|
+
|
|
468
475
|
const vault = await this.Vaults.getVault(swap.chainIdentifier, swap.vaultOwner, swap.vaultId);
|
|
469
476
|
if(vault==null || !vault.isReady()) {
|
|
470
477
|
throw {
|
|
@@ -593,6 +600,9 @@ export class SpvVaultSwapHandler extends SwapHandler<SpvVaultSwap, SpvVaultSwapS
|
|
|
593
600
|
};
|
|
594
601
|
}
|
|
595
602
|
|
|
603
|
+
//Double check in-flight swap count
|
|
604
|
+
this.checkTooManyInflightSwaps();
|
|
605
|
+
|
|
596
606
|
swap.btcTxId = signedTx.id;
|
|
597
607
|
swap.state = SpvVaultSwapState.SIGNED;
|
|
598
608
|
swap.sending = true;
|
|
@@ -622,7 +632,10 @@ export class SpvVaultSwapHandler extends SwapHandler<SpvVaultSwap, SpvVaultSwapS
|
|
|
622
632
|
swap.sending = false;
|
|
623
633
|
vault.removeWithdrawal(data);
|
|
624
634
|
await this.Vaults.saveVault(vault);
|
|
635
|
+
|
|
636
|
+
if(isDefinedRuntimeError(e) && swap.metadata!=null) swap.metadata.postQuoteError = e;
|
|
625
637
|
await this.removeSwapData(swap, SpvVaultSwapState.FAILED);
|
|
638
|
+
|
|
626
639
|
throw e;
|
|
627
640
|
}
|
|
628
641
|
|
|
@@ -28,6 +28,7 @@ export type FromBtcTrustedRequestType = {
|
|
|
28
28
|
|
|
29
29
|
export class FromBtcTrusted extends SwapHandler<FromBtcTrustedSwap, FromBtcTrustedSwapState> {
|
|
30
30
|
readonly type = SwapHandlerType.FROM_BTC_TRUSTED;
|
|
31
|
+
readonly inflightSwapStates = new Set([FromBtcTrustedSwapState.RECEIVED, FromBtcTrustedSwapState.BTC_CONFIRMED, FromBtcTrustedSwapState.SENT, FromBtcTrustedSwapState.CONFIRMED]);
|
|
31
32
|
|
|
32
33
|
readonly config: FromBtcTrustedConfig;
|
|
33
34
|
readonly bitcoin: IBitcoinWallet;
|
|
@@ -199,6 +200,14 @@ export class FromBtcTrusted extends SwapHandler<FromBtcTrustedSwap, FromBtcTrust
|
|
|
199
200
|
swap.txId = tx.txid;
|
|
200
201
|
swap.vout = vout;
|
|
201
202
|
this.subscriptions.delete(outputScript);
|
|
203
|
+
|
|
204
|
+
try {
|
|
205
|
+
this.checkTooManyInflightSwaps();
|
|
206
|
+
} catch (e) {
|
|
207
|
+
await this.refundSwap(swap);
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
|
|
202
211
|
await swap.setState(FromBtcTrustedSwapState.RECEIVED);
|
|
203
212
|
await this.storageManager.saveData(swap.getIdentifierHash(), swap.getSequence(), swap);
|
|
204
213
|
}
|
|
@@ -435,6 +444,8 @@ export class FromBtcTrusted extends SwapHandler<FromBtcTrustedSwap, FromBtcTrust
|
|
|
435
444
|
};
|
|
436
445
|
const useToken = parsedBody.token;
|
|
437
446
|
|
|
447
|
+
this.checkTooManyInflightSwaps();
|
|
448
|
+
|
|
438
449
|
//Check request params
|
|
439
450
|
const fees = await this.AmountAssertions.preCheckFromBtcAmounts(this.type, request, requestedAmount);
|
|
440
451
|
metadata.times.requestChecked = Date.now();
|
|
@@ -454,6 +465,10 @@ export class FromBtcTrusted extends SwapHandler<FromBtcTrustedSwap, FromBtcTrust
|
|
|
454
465
|
abortController.abort(e);
|
|
455
466
|
return null;
|
|
456
467
|
});
|
|
468
|
+
const nativeBalancePrefetch: Promise<bigint> = useToken===chainInterface.getNativeCurrencyAddress() ?
|
|
469
|
+
balancePrefetch : this.prefetchNativeBalanceIfNeeded(chainIdentifier, abortController);
|
|
470
|
+
|
|
471
|
+
await this.checkNativeBalance(chainIdentifier, nativeBalancePrefetch, abortController.signal);
|
|
457
472
|
|
|
458
473
|
//Check valid amount specified (min/max)
|
|
459
474
|
const {
|
|
@@ -36,6 +36,7 @@ export type FromBtcLnTrustedRequestType = {
|
|
|
36
36
|
*/
|
|
37
37
|
export class FromBtcLnTrusted extends SwapHandler<FromBtcLnTrustedSwap, FromBtcLnTrustedSwapState> {
|
|
38
38
|
readonly type = SwapHandlerType.FROM_BTCLN_TRUSTED;
|
|
39
|
+
readonly inflightSwapStates = new Set([FromBtcLnTrustedSwapState.RECEIVED, FromBtcLnTrustedSwapState.SENT, FromBtcLnTrustedSwapState.CONFIRMED]);
|
|
39
40
|
|
|
40
41
|
activeSubscriptions: Map<string, AbortController> = new Map<string, AbortController>();
|
|
41
42
|
processedTxIds: Map<string, string> = new Map<string, string>();
|
|
@@ -197,6 +198,13 @@ export class FromBtcLnTrusted extends SwapHandler<FromBtcLnTrustedSwap, FromBtcL
|
|
|
197
198
|
|
|
198
199
|
//Important to prevent race condition and issuing 2 signed init messages at the same time
|
|
199
200
|
if(invoiceData.state===FromBtcLnTrustedSwapState.CREATED) {
|
|
201
|
+
try {
|
|
202
|
+
this.checkTooManyInflightSwaps();
|
|
203
|
+
} catch (e) {
|
|
204
|
+
await this.cancelSwapAndInvoice(invoiceData);
|
|
205
|
+
throw e;
|
|
206
|
+
}
|
|
207
|
+
|
|
200
208
|
if(invoiceData.metadata!=null) invoiceData.metadata.times.htlcReceived = Date.now();
|
|
201
209
|
await invoiceData.setState(FromBtcLnTrustedSwapState.RECEIVED);
|
|
202
210
|
await this.storageManager.saveData(invoice.id, null, invoiceData);
|
|
@@ -406,6 +414,8 @@ export class FromBtcLnTrusted extends SwapHandler<FromBtcLnTrustedSwap, FromBtcL
|
|
|
406
414
|
};
|
|
407
415
|
const useToken = parsedBody.token;
|
|
408
416
|
|
|
417
|
+
this.checkTooManyInflightSwaps();
|
|
418
|
+
|
|
409
419
|
//Check request params
|
|
410
420
|
const fees = await this.AmountAssertions.preCheckFromBtcAmounts(this.type, request, requestedAmount);
|
|
411
421
|
metadata.times.requestChecked = Date.now();
|
|
@@ -420,13 +430,17 @@ export class FromBtcLnTrusted extends SwapHandler<FromBtcLnTrustedSwap, FromBtcL
|
|
|
420
430
|
abortController.abort(e);
|
|
421
431
|
return null;
|
|
422
432
|
});
|
|
423
|
-
const balancePrefetch = chainInterface.getBalance(signer.getAddress(), useToken).catch(e => {
|
|
433
|
+
const balancePrefetch: Promise<bigint> = chainInterface.getBalance(signer.getAddress(), useToken).catch(e => {
|
|
424
434
|
this.logger.error("getBalancePrefetch(): balancePrefetch error: ", e);
|
|
425
435
|
abortController.abort(e);
|
|
426
436
|
return null;
|
|
427
437
|
});
|
|
438
|
+
const nativeBalancePrefetch: Promise<bigint> = useToken===chainInterface.getNativeCurrencyAddress() ?
|
|
439
|
+
balancePrefetch : this.prefetchNativeBalanceIfNeeded(chainIdentifier, abortController);
|
|
428
440
|
const channelsPrefetch: Promise<LightningNetworkChannel[]> = this.LightningAssertions.getChannelsPrefetch(abortController);
|
|
429
441
|
|
|
442
|
+
await this.checkNativeBalance(chainIdentifier, nativeBalancePrefetch, abortController.signal);
|
|
443
|
+
|
|
430
444
|
//Check valid amount specified (min/max)
|
|
431
445
|
const {
|
|
432
446
|
amountBD,
|