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

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,104 @@
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
+ gasTokenMax: {
15
+ [chainId: string]: bigint;
16
+ };
17
+ };
18
+ export type FromBtcLnAutoRequestType = {
19
+ address: string;
20
+ paymentHash: string;
21
+ amount: bigint;
22
+ token: string;
23
+ gasToken: string;
24
+ gasAmount: bigint;
25
+ claimerBounty: bigint;
26
+ descriptionHash?: string;
27
+ exactOut?: boolean;
28
+ };
29
+ /**
30
+ * Swap handler handling from BTCLN swaps using submarine swaps
31
+ */
32
+ export declare class FromBtcLnAuto extends FromBtcBaseSwapHandler<FromBtcLnAutoSwap, FromBtcLnAutoSwapState> {
33
+ readonly type = SwapHandlerType.FROM_BTCLN_AUTO;
34
+ readonly swapType = ChainSwapType.HTLC;
35
+ readonly config: FromBtcLnAutoConfig;
36
+ readonly lightning: ILightningWallet;
37
+ readonly LightningAssertions: LightningAssertions;
38
+ constructor(storageDirectory: IIntermediaryStorage<FromBtcLnAutoSwap>, path: string, chains: MultichainData, lightning: ILightningWallet, swapPricing: ISwapPrice, config: FromBtcLnAutoConfig);
39
+ protected processPastSwap(swap: FromBtcLnAutoSwap): Promise<"REFUND" | "SETTLE" | null>;
40
+ protected refundSwaps(refundSwaps: FromBtcLnAutoSwap[]): Promise<void>;
41
+ protected settleInvoices(swaps: FromBtcLnAutoSwap[]): Promise<void>;
42
+ /**
43
+ * Checks past swaps, refunds and deletes ones that are already expired.
44
+ */
45
+ protected processPastSwaps(): Promise<void>;
46
+ protected processInitializeEvent(chainIdentifier: string, savedSwap: FromBtcLnAutoSwap, event: InitializeEvent<SwapData>): Promise<void>;
47
+ protected processClaimEvent(chainIdentifier: string, savedSwap: FromBtcLnAutoSwap, event: ClaimEvent<SwapData>): Promise<void>;
48
+ protected processRefundEvent(chainIdentifier: string, savedSwap: FromBtcLnAutoSwap, event: RefundEvent<SwapData>): Promise<void>;
49
+ /**
50
+ * Called when lightning HTLC is received, also signs an init transaction on the smart chain side, expiry of the
51
+ * smart chain authorization starts ticking as soon as this HTLC is received
52
+ *
53
+ * @param invoiceData
54
+ * @param invoice
55
+ */
56
+ private htlcReceived;
57
+ private offerHtlc;
58
+ /**
59
+ * Checks invoice description hash
60
+ *
61
+ * @param descriptionHash
62
+ * @throws {DefinedRuntimeError} will throw an error if the description hash is invalid
63
+ */
64
+ private checkDescriptionHash;
65
+ /**
66
+ * Asynchronously sends the LN node's public key to the client, so he can pre-fetch the node's channels from 1ml api
67
+ *
68
+ * @param responseStream
69
+ */
70
+ private sendPublicKeyAsync;
71
+ /**
72
+ * Returns the CLTV timeout (blockheight) of the received HTLC corresponding to the invoice. If multiple HTLCs are
73
+ * received (MPP) it returns the lowest of the timeouts
74
+ *
75
+ * @param invoice
76
+ */
77
+ private getInvoicePaymentsTimeout;
78
+ /**
79
+ * Checks if the received HTLC's CLTV timeout is large enough to still process the swap
80
+ *
81
+ * @param invoice
82
+ * @throws {DefinedRuntimeError} Will throw if HTLC expires too soon and therefore cannot be processed
83
+ * @returns expiry timeout in seconds
84
+ */
85
+ private checkHtlcExpiry;
86
+ /**
87
+ * Cancels the swap (CANCELED state) & also cancels the LN invoice (including all pending HTLCs)
88
+ *
89
+ * @param invoiceData
90
+ */
91
+ private cancelSwapAndInvoice;
92
+ /**
93
+ *
94
+ * Checks if the lightning invoice is in HELD state (htlcs received but yet unclaimed)
95
+ *
96
+ * @param paymentHash
97
+ * @throws {DefinedRuntimeError} Will throw if the lightning invoice is not found, or if it isn't in the HELD state
98
+ * @returns the fetched lightning invoice
99
+ */
100
+ private checkInvoiceStatus;
101
+ startRestServer(restServer: Express): void;
102
+ init(): Promise<void>;
103
+ getInfoData(): any;
104
+ }