@atomiqlabs/lp-lib 14.0.0-dev.6 → 14.0.0-dev.7

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/index.d.ts CHANGED
@@ -9,6 +9,8 @@ export * from "./swaps/escrow/frombtc_abstract/FromBtcAbs";
9
9
  export * from "./swaps/escrow/frombtc_abstract/FromBtcSwapAbs";
10
10
  export * from "./swaps/escrow/frombtcln_abstract/FromBtcLnAbs";
11
11
  export * from "./swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs";
12
+ export * from "./swaps/escrow/frombtcln_autoinit/FromBtcLnAuto";
13
+ export * from "./swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap";
12
14
  export * from "./swaps/escrow/tobtc_abstract/ToBtcAbs";
13
15
  export * from "./swaps/escrow/tobtc_abstract/ToBtcSwapAbs";
14
16
  export * from "./swaps/escrow/tobtcln_abstract/ToBtcLnAbs";
package/dist/index.js CHANGED
@@ -25,6 +25,8 @@ __exportStar(require("./swaps/escrow/frombtc_abstract/FromBtcAbs"), exports);
25
25
  __exportStar(require("./swaps/escrow/frombtc_abstract/FromBtcSwapAbs"), exports);
26
26
  __exportStar(require("./swaps/escrow/frombtcln_abstract/FromBtcLnAbs"), exports);
27
27
  __exportStar(require("./swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs"), exports);
28
+ __exportStar(require("./swaps/escrow/frombtcln_autoinit/FromBtcLnAuto"), exports);
29
+ __exportStar(require("./swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap"), exports);
28
30
  __exportStar(require("./swaps/escrow/tobtc_abstract/ToBtcAbs"), exports);
29
31
  __exportStar(require("./swaps/escrow/tobtc_abstract/ToBtcSwapAbs"), exports);
30
32
  __exportStar(require("./swaps/escrow/tobtcln_abstract/ToBtcLnAbs"), exports);
@@ -72,7 +72,7 @@ export interface IPlugin {
72
72
  onSwapStateChange?(swap: SwapHandlerSwap): Promise<void>;
73
73
  onSwapCreate?(swap: SwapHandlerSwap): Promise<void>;
74
74
  onSwapRemove?(swap: SwapHandlerSwap): Promise<void>;
75
- onHandlePreFromBtcQuote?(swapType: SwapHandlerType.FROM_BTCLN | SwapHandlerType.FROM_BTC | SwapHandlerType.FROM_BTCLN_TRUSTED | SwapHandlerType.FROM_BTC_TRUSTED | SwapHandlerType.FROM_BTC_SPV, request: RequestData<FromBtcLnRequestType | FromBtcRequestType | FromBtcLnTrustedRequestType | FromBtcTrustedRequestType | SpvVaultSwapRequestType>, requestedAmount: {
75
+ onHandlePreFromBtcQuote?(swapType: SwapHandlerType.FROM_BTCLN | SwapHandlerType.FROM_BTC | SwapHandlerType.FROM_BTCLN_TRUSTED | SwapHandlerType.FROM_BTC_TRUSTED | SwapHandlerType.FROM_BTC_SPV | SwapHandlerType.FROM_BTCLN_AUTO, request: RequestData<FromBtcLnRequestType | FromBtcRequestType | FromBtcLnTrustedRequestType | FromBtcTrustedRequestType | SpvVaultSwapRequestType>, requestedAmount: {
76
76
  input: boolean;
77
77
  amount: bigint;
78
78
  token: string;
@@ -87,7 +87,7 @@ export interface IPlugin {
87
87
  amount: bigint;
88
88
  token: string;
89
89
  }): Promise<QuoteThrow | QuoteSetFees | QuoteAmountTooLow | QuoteAmountTooHigh>;
90
- onHandlePostFromBtcQuote?(swapType: SwapHandlerType.FROM_BTCLN | SwapHandlerType.FROM_BTC | SwapHandlerType.FROM_BTCLN_TRUSTED | SwapHandlerType.FROM_BTC_TRUSTED | SwapHandlerType.FROM_BTC_SPV, request: RequestData<FromBtcLnRequestType | FromBtcRequestType | FromBtcLnTrustedRequestType | FromBtcTrustedRequestType | SpvVaultSwapRequestType>, requestedAmount: {
90
+ onHandlePostFromBtcQuote?(swapType: SwapHandlerType.FROM_BTCLN | SwapHandlerType.FROM_BTC | SwapHandlerType.FROM_BTCLN_TRUSTED | SwapHandlerType.FROM_BTC_TRUSTED | SwapHandlerType.FROM_BTC_SPV | SwapHandlerType.FROM_BTCLN_AUTO, request: RequestData<FromBtcLnRequestType | FromBtcRequestType | FromBtcLnTrustedRequestType | FromBtcTrustedRequestType | SpvVaultSwapRequestType>, requestedAmount: {
91
91
  input: boolean;
92
92
  amount: bigint;
93
93
  token: string;
@@ -41,7 +41,7 @@ export declare class PluginManager {
41
41
  static swapStateChange(swap: SwapHandlerSwap, oldState?: any): Promise<void>;
42
42
  static swapCreate(swap: SwapHandlerSwap): Promise<void>;
43
43
  static swapRemove(swap: SwapHandlerSwap): Promise<void>;
44
- static onHandlePostFromBtcQuote(swapType: SwapHandlerType.FROM_BTCLN | SwapHandlerType.FROM_BTC | SwapHandlerType.FROM_BTCLN_TRUSTED | SwapHandlerType.FROM_BTC_TRUSTED | SwapHandlerType.FROM_BTC_SPV, request: RequestData<FromBtcLnRequestType | FromBtcRequestType | FromBtcLnTrustedRequestType | FromBtcTrustedRequestType | SpvVaultSwapRequestType>, requestedAmount: {
44
+ static onHandlePostFromBtcQuote(swapType: SwapHandlerType.FROM_BTCLN | SwapHandlerType.FROM_BTC | SwapHandlerType.FROM_BTCLN_TRUSTED | SwapHandlerType.FROM_BTC_TRUSTED | SwapHandlerType.FROM_BTC_SPV | SwapHandlerType.FROM_BTCLN_AUTO, request: RequestData<FromBtcLnRequestType | FromBtcRequestType | FromBtcLnTrustedRequestType | FromBtcTrustedRequestType | SpvVaultSwapRequestType>, requestedAmount: {
45
45
  input: boolean;
46
46
  amount: bigint;
47
47
  token: string;
@@ -58,7 +58,7 @@ export declare class PluginManager {
58
58
  token: string;
59
59
  pricePrefetch?: Promise<bigint>;
60
60
  }): Promise<QuoteThrow | QuoteSetFees | QuoteAmountTooLow | QuoteAmountTooHigh | PluginQuote>;
61
- static onHandlePreFromBtcQuote(swapType: SwapHandlerType.FROM_BTCLN | SwapHandlerType.FROM_BTC | SwapHandlerType.FROM_BTCLN_TRUSTED | SwapHandlerType.FROM_BTC_TRUSTED | SwapHandlerType.FROM_BTC_SPV, request: RequestData<FromBtcLnRequestType | FromBtcRequestType | FromBtcLnTrustedRequestType | FromBtcTrustedRequestType | SpvVaultSwapRequestType>, requestedAmount: {
61
+ static onHandlePreFromBtcQuote(swapType: SwapHandlerType.FROM_BTCLN | SwapHandlerType.FROM_BTC | SwapHandlerType.FROM_BTCLN_TRUSTED | SwapHandlerType.FROM_BTC_TRUSTED | SwapHandlerType.FROM_BTC_SPV | SwapHandlerType.FROM_BTCLN_AUTO, request: RequestData<FromBtcLnRequestType | FromBtcRequestType | FromBtcLnTrustedRequestType | FromBtcTrustedRequestType | SpvVaultSwapRequestType>, requestedAmount: {
62
62
  input: boolean;
63
63
  amount: bigint;
64
64
  token: string;
@@ -12,7 +12,8 @@ export declare enum SwapHandlerType {
12
12
  FROM_BTCLN = "FROM_BTCLN",
13
13
  FROM_BTCLN_TRUSTED = "FROM_BTCLN_TRUSTED",
14
14
  FROM_BTC_TRUSTED = "FROM_BTC_TRUSTED",
15
- FROM_BTC_SPV = "FROM_BTC_SPV"
15
+ FROM_BTC_SPV = "FROM_BTC_SPV",
16
+ FROM_BTCLN_AUTO = "FROM_BTCLN_AUTO"
16
17
  }
17
18
  export type SwapHandlerInfoType = {
18
19
  swapFeePPM: number;
@@ -12,6 +12,7 @@ var SwapHandlerType;
12
12
  SwapHandlerType["FROM_BTCLN_TRUSTED"] = "FROM_BTCLN_TRUSTED";
13
13
  SwapHandlerType["FROM_BTC_TRUSTED"] = "FROM_BTC_TRUSTED";
14
14
  SwapHandlerType["FROM_BTC_SPV"] = "FROM_BTC_SPV";
15
+ SwapHandlerType["FROM_BTCLN_AUTO"] = "FROM_BTCLN_AUTO";
15
16
  })(SwapHandlerType = exports.SwapHandlerType || (exports.SwapHandlerType = {}));
16
17
  /**
17
18
  * An abstract class defining a singular swap service
@@ -23,7 +23,7 @@ export declare class FromBtcAmountAssertions extends AmountAssertions {
23
23
  * @param gasAmount
24
24
  * @throws {DefinedRuntimeError} will throw an error if the amount is outside minimum/maximum bounds
25
25
  */
26
- preCheckFromBtcAmounts(swapType: SwapHandlerType.FROM_BTCLN | SwapHandlerType.FROM_BTC | SwapHandlerType.FROM_BTCLN_TRUSTED | SwapHandlerType.FROM_BTC_TRUSTED | SwapHandlerType.FROM_BTC_SPV, request: RequestData<FromBtcLnRequestType | FromBtcRequestType | FromBtcLnTrustedRequestType | FromBtcTrustedRequestType | SpvVaultSwapRequestType>, requestedAmount: {
26
+ preCheckFromBtcAmounts(swapType: SwapHandlerType.FROM_BTCLN | SwapHandlerType.FROM_BTC | SwapHandlerType.FROM_BTCLN_TRUSTED | SwapHandlerType.FROM_BTC_TRUSTED | SwapHandlerType.FROM_BTC_SPV | SwapHandlerType.FROM_BTCLN_AUTO, request: RequestData<FromBtcLnRequestType | FromBtcRequestType | FromBtcLnTrustedRequestType | FromBtcTrustedRequestType | SpvVaultSwapRequestType>, requestedAmount: {
27
27
  input: boolean;
28
28
  amount: bigint;
29
29
  token: string;
@@ -48,7 +48,7 @@ export declare class FromBtcAmountAssertions extends AmountAssertions {
48
48
  * @param gasTokenAmount
49
49
  * @throws {DefinedRuntimeError} will throw an error if the amount is outside minimum/maximum bounds
50
50
  */
51
- checkFromBtcAmount(swapType: SwapHandlerType.FROM_BTCLN | SwapHandlerType.FROM_BTC | SwapHandlerType.FROM_BTCLN_TRUSTED | SwapHandlerType.FROM_BTC_TRUSTED | SwapHandlerType.FROM_BTC_SPV, request: RequestData<FromBtcLnRequestType | FromBtcRequestType | FromBtcLnTrustedRequestType | FromBtcTrustedRequestType | SpvVaultSwapRequestType>, requestedAmount: {
51
+ checkFromBtcAmount(swapType: SwapHandlerType.FROM_BTCLN | SwapHandlerType.FROM_BTC | SwapHandlerType.FROM_BTCLN_TRUSTED | SwapHandlerType.FROM_BTC_TRUSTED | SwapHandlerType.FROM_BTC_SPV | SwapHandlerType.FROM_BTCLN_AUTO, request: RequestData<FromBtcLnRequestType | FromBtcRequestType | FromBtcLnTrustedRequestType | FromBtcTrustedRequestType | SpvVaultSwapRequestType>, requestedAmount: {
52
52
  input: boolean;
53
53
  amount: bigint;
54
54
  token: string;
@@ -44,8 +44,9 @@ export declare abstract class FromBtcBaseSwapHandler<V extends FromBtcBaseSwap<S
44
44
  * @param chainIdentifier
45
45
  * @param useToken
46
46
  * @param abortController
47
+ * @param inContract
47
48
  */
48
- protected getBalancePrefetch(chainIdentifier: string, useToken: string, abortController: AbortController): Promise<bigint>;
49
+ protected getBalancePrefetch(chainIdentifier: string, useToken: string, abortController: AbortController, inContract?: boolean): Promise<bigint>;
49
50
  /**
50
51
  * Checks if we have enough balance of the token in the swap vault
51
52
  *
@@ -88,11 +88,12 @@ class FromBtcBaseSwapHandler extends EscrowHandler_1.EscrowHandler {
88
88
  * @param chainIdentifier
89
89
  * @param useToken
90
90
  * @param abortController
91
+ * @param inContract
91
92
  */
92
- async getBalancePrefetch(chainIdentifier, useToken, abortController) {
93
+ async getBalancePrefetch(chainIdentifier, useToken, abortController, inContract = true) {
93
94
  const { swapContract, signer } = this.getChain(chainIdentifier);
94
95
  try {
95
- return await swapContract.getBalance(signer.getAddress(), useToken, true);
96
+ return await swapContract.getBalance(signer.getAddress(), useToken, inContract);
96
97
  }
97
98
  catch (e) {
98
99
  this.logger.error("getBalancePrefetch(): balancePrefetch error: ", e);
@@ -109,6 +110,8 @@ class FromBtcBaseSwapHandler extends EscrowHandler_1.EscrowHandler {
109
110
  * @throws {DefinedRuntimeError} will throw an error if there are not enough funds in the vault
110
111
  */
111
112
  async checkBalance(totalInToken, balancePrefetch, signal) {
113
+ if (totalInToken === 0n)
114
+ return;
112
115
  const balance = await balancePrefetch;
113
116
  if (signal != null)
114
117
  signal.throwIfAborted();
@@ -0,0 +1,101 @@
1
+ import { Express } from "express";
2
+ import { FromBtcLnAutoSwap, FromBtcLnAutoSwapState } from "./FromBtcLnAutoSwap";
3
+ import { MultichainData, SwapHandlerType } from "../../SwapHandler";
4
+ import { ISwapPrice } from "../../../prices/ISwapPrice";
5
+ import { ChainSwapType, ClaimEvent, InitializeEvent, RefundEvent, SwapData } from "@atomiqlabs/base";
6
+ import { IIntermediaryStorage } from "../../../storage/IIntermediaryStorage";
7
+ import { FromBtcBaseConfig, FromBtcBaseSwapHandler } from "../FromBtcBaseSwapHandler";
8
+ import { ILightningWallet } from "../../../wallets/ILightningWallet";
9
+ import { LightningAssertions } from "../../assertions/LightningAssertions";
10
+ export type FromBtcLnAutoConfig = FromBtcBaseConfig & {
11
+ invoiceTimeoutSeconds?: number;
12
+ minCltv: bigint;
13
+ gracePeriod: bigint;
14
+ };
15
+ export type FromBtcLnAutoRequestType = {
16
+ address: string;
17
+ paymentHash: string;
18
+ amount: bigint;
19
+ token: string;
20
+ gasToken: string;
21
+ gasAmount: bigint;
22
+ claimerBounty: bigint;
23
+ descriptionHash?: string;
24
+ exactOut?: boolean;
25
+ };
26
+ /**
27
+ * Swap handler handling from BTCLN swaps using submarine swaps
28
+ */
29
+ export declare class FromBtcLnAuto extends FromBtcBaseSwapHandler<FromBtcLnAutoSwap, FromBtcLnAutoSwapState> {
30
+ readonly type = SwapHandlerType.FROM_BTCLN_AUTO;
31
+ readonly swapType = ChainSwapType.HTLC;
32
+ readonly config: FromBtcLnAutoConfig;
33
+ readonly lightning: ILightningWallet;
34
+ readonly LightningAssertions: LightningAssertions;
35
+ constructor(storageDirectory: IIntermediaryStorage<FromBtcLnAutoSwap>, path: string, chains: MultichainData, lightning: ILightningWallet, swapPricing: ISwapPrice, config: FromBtcLnAutoConfig);
36
+ protected processPastSwap(swap: FromBtcLnAutoSwap): Promise<"REFUND" | "SETTLE" | null>;
37
+ protected refundSwaps(refundSwaps: FromBtcLnAutoSwap[]): Promise<void>;
38
+ protected settleInvoices(swaps: FromBtcLnAutoSwap[]): Promise<void>;
39
+ /**
40
+ * Checks past swaps, refunds and deletes ones that are already expired.
41
+ */
42
+ protected processPastSwaps(): Promise<void>;
43
+ protected processInitializeEvent(chainIdentifier: string, savedSwap: FromBtcLnAutoSwap, event: InitializeEvent<SwapData>): Promise<void>;
44
+ protected processClaimEvent(chainIdentifier: string, savedSwap: FromBtcLnAutoSwap, event: ClaimEvent<SwapData>): Promise<void>;
45
+ protected processRefundEvent(chainIdentifier: string, savedSwap: FromBtcLnAutoSwap, event: RefundEvent<SwapData>): Promise<void>;
46
+ /**
47
+ * Called when lightning HTLC is received, also signs an init transaction on the smart chain side, expiry of the
48
+ * smart chain authorization starts ticking as soon as this HTLC is received
49
+ *
50
+ * @param invoiceData
51
+ * @param invoice
52
+ */
53
+ private htlcReceived;
54
+ private offerHtlc;
55
+ /**
56
+ * Checks invoice description hash
57
+ *
58
+ * @param descriptionHash
59
+ * @throws {DefinedRuntimeError} will throw an error if the description hash is invalid
60
+ */
61
+ private checkDescriptionHash;
62
+ /**
63
+ * Asynchronously sends the LN node's public key to the client, so he can pre-fetch the node's channels from 1ml api
64
+ *
65
+ * @param responseStream
66
+ */
67
+ private sendPublicKeyAsync;
68
+ /**
69
+ * Returns the CLTV timeout (blockheight) of the received HTLC corresponding to the invoice. If multiple HTLCs are
70
+ * received (MPP) it returns the lowest of the timeouts
71
+ *
72
+ * @param invoice
73
+ */
74
+ private getInvoicePaymentsTimeout;
75
+ /**
76
+ * Checks if the received HTLC's CLTV timeout is large enough to still process the swap
77
+ *
78
+ * @param invoice
79
+ * @throws {DefinedRuntimeError} Will throw if HTLC expires too soon and therefore cannot be processed
80
+ * @returns expiry timeout in seconds
81
+ */
82
+ private checkHtlcExpiry;
83
+ /**
84
+ * Cancels the swap (CANCELED state) & also cancels the LN invoice (including all pending HTLCs)
85
+ *
86
+ * @param invoiceData
87
+ */
88
+ private cancelSwapAndInvoice;
89
+ /**
90
+ *
91
+ * Checks if the lightning invoice is in HELD state (htlcs received but yet unclaimed)
92
+ *
93
+ * @param paymentHash
94
+ * @throws {DefinedRuntimeError} Will throw if the lightning invoice is not found, or if it isn't in the HELD state
95
+ * @returns the fetched lightning invoice
96
+ */
97
+ private checkInvoiceStatus;
98
+ startRestServer(restServer: Express): void;
99
+ init(): Promise<void>;
100
+ getInfoData(): any;
101
+ }