@atomiqlabs/lp-lib 15.0.14 → 16.0.0

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.
Files changed (169) hide show
  1. package/LICENSE +201 -201
  2. package/dist/fees/IBtcFeeEstimator.d.ts +3 -3
  3. package/dist/fees/IBtcFeeEstimator.js +2 -2
  4. package/dist/index.d.ts +42 -40
  5. package/dist/index.js +58 -56
  6. package/dist/info/InfoHandler.d.ts +17 -17
  7. package/dist/info/InfoHandler.js +58 -58
  8. package/dist/plugins/IPlugin.d.ts +144 -144
  9. package/dist/plugins/IPlugin.js +34 -34
  10. package/dist/plugins/PluginManager.d.ts +113 -113
  11. package/dist/plugins/PluginManager.js +274 -274
  12. package/dist/prices/BinanceSwapPrice.d.ts +26 -26
  13. package/dist/prices/BinanceSwapPrice.js +92 -92
  14. package/dist/prices/CoinGeckoSwapPrice.d.ts +30 -30
  15. package/dist/prices/CoinGeckoSwapPrice.js +64 -64
  16. package/dist/prices/ISwapPrice.d.ts +43 -43
  17. package/dist/prices/ISwapPrice.js +55 -55
  18. package/dist/prices/OKXSwapPrice.d.ts +26 -26
  19. package/dist/prices/OKXSwapPrice.js +92 -92
  20. package/dist/storage/IIntermediaryStorage.d.ts +18 -18
  21. package/dist/storage/IIntermediaryStorage.js +2 -2
  22. package/dist/storagemanager/IntermediaryStorageManager.d.ts +19 -18
  23. package/dist/storagemanager/IntermediaryStorageManager.js +111 -104
  24. package/dist/storagemanager/StorageManager.d.ts +13 -12
  25. package/dist/storagemanager/StorageManager.js +64 -57
  26. package/dist/swaps/SwapHandler.d.ts +150 -153
  27. package/dist/swaps/SwapHandler.js +154 -157
  28. package/dist/swaps/SwapHandlerSwap.d.ts +79 -79
  29. package/dist/swaps/SwapHandlerSwap.js +78 -78
  30. package/dist/swaps/assertions/AmountAssertions.d.ts +28 -28
  31. package/dist/swaps/assertions/AmountAssertions.js +74 -74
  32. package/dist/swaps/assertions/FromBtcAmountAssertions.d.ts +76 -76
  33. package/dist/swaps/assertions/FromBtcAmountAssertions.js +180 -172
  34. package/dist/swaps/assertions/LightningAssertions.d.ts +44 -44
  35. package/dist/swaps/assertions/LightningAssertions.js +86 -86
  36. package/dist/swaps/assertions/ToBtcAmountAssertions.d.ts +53 -53
  37. package/dist/swaps/assertions/ToBtcAmountAssertions.js +150 -150
  38. package/dist/swaps/escrow/EscrowHandler.d.ts +51 -51
  39. package/dist/swaps/escrow/EscrowHandler.js +158 -158
  40. package/dist/swaps/escrow/EscrowHandlerSwap.d.ts +35 -35
  41. package/dist/swaps/escrow/EscrowHandlerSwap.js +69 -69
  42. package/dist/swaps/escrow/FromBtcBaseSwap.d.ts +14 -14
  43. package/dist/swaps/escrow/FromBtcBaseSwap.js +32 -32
  44. package/dist/swaps/escrow/FromBtcBaseSwapHandler.d.ts +102 -101
  45. package/dist/swaps/escrow/FromBtcBaseSwapHandler.js +210 -207
  46. package/dist/swaps/escrow/ToBtcBaseSwap.d.ts +36 -36
  47. package/dist/swaps/escrow/ToBtcBaseSwap.js +67 -67
  48. package/dist/swaps/escrow/ToBtcBaseSwapHandler.d.ts +53 -53
  49. package/dist/swaps/escrow/ToBtcBaseSwapHandler.js +81 -81
  50. package/dist/swaps/escrow/frombtc_abstract/FromBtcAbs.d.ts +83 -83
  51. package/dist/swaps/escrow/frombtc_abstract/FromBtcAbs.js +318 -318
  52. package/dist/swaps/escrow/frombtc_abstract/FromBtcSwapAbs.d.ts +21 -21
  53. package/dist/swaps/escrow/frombtc_abstract/FromBtcSwapAbs.js +50 -50
  54. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.d.ts +107 -107
  55. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.js +673 -673
  56. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.d.ts +33 -32
  57. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.js +91 -88
  58. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.d.ts +111 -0
  59. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.js +682 -0
  60. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.d.ts +55 -0
  61. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.js +120 -0
  62. package/dist/swaps/escrow/tobtc_abstract/ToBtcAbs.d.ts +169 -171
  63. package/dist/swaps/escrow/tobtc_abstract/ToBtcAbs.js +735 -718
  64. package/dist/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.d.ts +28 -28
  65. package/dist/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.js +64 -64
  66. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.d.ts +177 -177
  67. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.js +865 -863
  68. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.d.ts +24 -24
  69. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.js +58 -58
  70. package/dist/swaps/spv_vault_swap/SpvVault.d.ts +44 -45
  71. package/dist/swaps/spv_vault_swap/SpvVault.js +145 -145
  72. package/dist/swaps/spv_vault_swap/SpvVaultSwap.d.ts +68 -68
  73. package/dist/swaps/spv_vault_swap/SpvVaultSwap.js +158 -158
  74. package/dist/swaps/spv_vault_swap/SpvVaultSwapHandler.d.ts +68 -68
  75. package/dist/swaps/spv_vault_swap/SpvVaultSwapHandler.js +530 -528
  76. package/dist/swaps/spv_vault_swap/SpvVaults.d.ts +63 -68
  77. package/dist/swaps/spv_vault_swap/SpvVaults.js +488 -454
  78. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrusted.d.ts +51 -51
  79. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrusted.js +650 -650
  80. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrustedSwap.d.ts +52 -52
  81. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrustedSwap.js +118 -118
  82. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.d.ts +76 -76
  83. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.js +492 -493
  84. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.d.ts +34 -34
  85. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.js +81 -81
  86. package/dist/utils/BitcoinUtils.d.ts +4 -4
  87. package/dist/utils/BitcoinUtils.js +61 -61
  88. package/dist/utils/Utils.d.ts +29 -29
  89. package/dist/utils/Utils.js +89 -88
  90. package/dist/utils/paramcoders/IParamReader.d.ts +5 -5
  91. package/dist/utils/paramcoders/IParamReader.js +2 -2
  92. package/dist/utils/paramcoders/IParamWriter.d.ts +4 -4
  93. package/dist/utils/paramcoders/IParamWriter.js +2 -2
  94. package/dist/utils/paramcoders/LegacyParamEncoder.d.ts +10 -10
  95. package/dist/utils/paramcoders/LegacyParamEncoder.js +22 -22
  96. package/dist/utils/paramcoders/ParamDecoder.d.ts +25 -25
  97. package/dist/utils/paramcoders/ParamDecoder.js +222 -222
  98. package/dist/utils/paramcoders/ParamEncoder.d.ts +9 -9
  99. package/dist/utils/paramcoders/ParamEncoder.js +22 -22
  100. package/dist/utils/paramcoders/SchemaVerifier.d.ts +21 -21
  101. package/dist/utils/paramcoders/SchemaVerifier.js +84 -84
  102. package/dist/utils/paramcoders/server/ServerParamDecoder.d.ts +8 -8
  103. package/dist/utils/paramcoders/server/ServerParamDecoder.js +107 -105
  104. package/dist/utils/paramcoders/server/ServerParamEncoder.d.ts +11 -11
  105. package/dist/utils/paramcoders/server/ServerParamEncoder.js +65 -65
  106. package/dist/wallets/IBitcoinWallet.d.ts +74 -67
  107. package/dist/wallets/IBitcoinWallet.js +2 -2
  108. package/dist/wallets/ILightningWallet.d.ts +117 -117
  109. package/dist/wallets/ILightningWallet.js +37 -37
  110. package/dist/wallets/ISpvVaultSigner.d.ts +7 -7
  111. package/dist/wallets/ISpvVaultSigner.js +2 -2
  112. package/package.json +36 -36
  113. package/src/fees/IBtcFeeEstimator.ts +6 -6
  114. package/src/index.ts +53 -51
  115. package/src/info/InfoHandler.ts +100 -100
  116. package/src/plugins/IPlugin.ts +174 -174
  117. package/src/plugins/PluginManager.ts +354 -354
  118. package/src/prices/BinanceSwapPrice.ts +113 -113
  119. package/src/prices/CoinGeckoSwapPrice.ts +87 -87
  120. package/src/prices/ISwapPrice.ts +88 -88
  121. package/src/prices/OKXSwapPrice.ts +113 -113
  122. package/src/storage/IIntermediaryStorage.ts +19 -19
  123. package/src/storagemanager/IntermediaryStorageManager.ts +118 -109
  124. package/src/storagemanager/StorageManager.ts +78 -68
  125. package/src/swaps/SwapHandler.ts +269 -272
  126. package/src/swaps/SwapHandlerSwap.ts +141 -141
  127. package/src/swaps/assertions/AmountAssertions.ts +77 -77
  128. package/src/swaps/assertions/FromBtcAmountAssertions.ts +246 -238
  129. package/src/swaps/assertions/LightningAssertions.ts +103 -103
  130. package/src/swaps/assertions/ToBtcAmountAssertions.ts +203 -203
  131. package/src/swaps/escrow/EscrowHandler.ts +179 -179
  132. package/src/swaps/escrow/EscrowHandlerSwap.ts +86 -86
  133. package/src/swaps/escrow/FromBtcBaseSwap.ts +38 -38
  134. package/src/swaps/escrow/FromBtcBaseSwapHandler.ts +286 -283
  135. package/src/swaps/escrow/ToBtcBaseSwap.ts +85 -85
  136. package/src/swaps/escrow/ToBtcBaseSwapHandler.ts +129 -129
  137. package/src/swaps/escrow/frombtc_abstract/FromBtcAbs.ts +452 -452
  138. package/src/swaps/escrow/frombtc_abstract/FromBtcSwapAbs.ts +61 -61
  139. package/src/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.ts +855 -855
  140. package/src/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.ts +141 -137
  141. package/src/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.ts +847 -0
  142. package/src/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.ts +196 -0
  143. package/src/swaps/escrow/tobtc_abstract/ToBtcAbs.ts +909 -890
  144. package/src/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.ts +108 -108
  145. package/src/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.ts +1116 -1112
  146. package/src/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.ts +80 -80
  147. package/src/swaps/spv_vault_swap/SpvVault.ts +178 -178
  148. package/src/swaps/spv_vault_swap/SpvVaultSwap.ts +228 -228
  149. package/src/swaps/spv_vault_swap/SpvVaultSwapHandler.ts +673 -671
  150. package/src/swaps/spv_vault_swap/SpvVaults.ts +565 -526
  151. package/src/swaps/trusted/frombtc_trusted/FromBtcTrusted.ts +747 -747
  152. package/src/swaps/trusted/frombtc_trusted/FromBtcTrustedSwap.ts +185 -185
  153. package/src/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.ts +589 -591
  154. package/src/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.ts +121 -121
  155. package/src/utils/BitcoinUtils.ts +59 -59
  156. package/src/utils/Utils.ts +104 -102
  157. package/src/utils/paramcoders/IParamReader.ts +7 -7
  158. package/src/utils/paramcoders/IParamWriter.ts +8 -8
  159. package/src/utils/paramcoders/LegacyParamEncoder.ts +27 -27
  160. package/src/utils/paramcoders/ParamDecoder.ts +218 -218
  161. package/src/utils/paramcoders/ParamEncoder.ts +29 -29
  162. package/src/utils/paramcoders/SchemaVerifier.ts +96 -96
  163. package/src/utils/paramcoders/server/ServerParamDecoder.ts +118 -115
  164. package/src/utils/paramcoders/server/ServerParamEncoder.ts +75 -75
  165. package/src/wallets/IBitcoinWallet.ts +76 -68
  166. package/src/wallets/ILightningWallet.ts +178 -178
  167. package/src/wallets/ISpvVaultSigner.ts +10 -10
  168. package/dist/wallets/ISpvVaultWallet.d.ts +0 -42
  169. package/dist/wallets/ISpvVaultWallet.js +0 -2
@@ -1,153 +1,150 @@
1
- import { Express, Request } from "express";
2
- import { ISwapPrice } from "../prices/ISwapPrice";
3
- import { ChainType } from "@atomiqlabs/base";
4
- import { SwapHandlerSwap } from "./SwapHandlerSwap";
5
- import { IIntermediaryStorage } from "../storage/IIntermediaryStorage";
6
- import { IParamReader } from "../utils/paramcoders/IParamReader";
7
- export declare enum SwapHandlerType {
8
- TO_BTC = "TO_BTC",
9
- FROM_BTC = "FROM_BTC",
10
- TO_BTCLN = "TO_BTCLN",
11
- FROM_BTCLN = "FROM_BTCLN",
12
- FROM_BTCLN_TRUSTED = "FROM_BTCLN_TRUSTED",
13
- FROM_BTC_TRUSTED = "FROM_BTC_TRUSTED",
14
- FROM_BTC_SPV = "FROM_BTC_SPV"
15
- }
16
- export type SwapHandlerInfoType = {
17
- swapFeePPM: number;
18
- swapBaseFee: number;
19
- min: number;
20
- max: number;
21
- chainTokens: {
22
- [chainId: string]: string[];
23
- };
24
- data?: any;
25
- };
26
- export type SwapBaseConfig = {
27
- initAuthorizationTimeout: number;
28
- initAuthorizationTimeouts?: {
29
- [chainId: string]: number;
30
- };
31
- bitcoinBlocktime: bigint;
32
- baseFee: bigint;
33
- feePPM: bigint;
34
- max: bigint;
35
- min: bigint;
36
- safetyFactor: bigint;
37
- swapCheckInterval: number;
38
- };
39
- export type MultichainData = {
40
- chains: {
41
- [identifier: string]: ChainData;
42
- };
43
- };
44
- export type ChainData<T extends ChainType = ChainType> = {
45
- signer: T["Signer"];
46
- swapContract: T["Contract"];
47
- spvVaultContract: T["SpvVaultContract"];
48
- chainInterface: T["ChainInterface"];
49
- chainEvents: T["Events"];
50
- allowedTokens: string[];
51
- tokenMultipliers?: {
52
- [tokenAddress: string]: bigint;
53
- };
54
- allowedDepositTokens?: string[];
55
- btcRelay?: T["BtcRelay"];
56
- };
57
- export type RequestData<T> = {
58
- chainIdentifier: string;
59
- raw: Request & {
60
- paramReader: IParamReader;
61
- };
62
- parsed: T;
63
- metadata: any;
64
- };
65
- /**
66
- * An abstract class defining a singular swap service
67
- */
68
- export declare abstract class SwapHandler<V extends SwapHandlerSwap<S> = SwapHandlerSwap, S = any> {
69
- abstract readonly type: SwapHandlerType;
70
- readonly storageManager: IIntermediaryStorage<V>;
71
- readonly path: string;
72
- readonly chains: MultichainData;
73
- readonly allowedTokens: {
74
- [chainId: string]: Set<string>;
75
- };
76
- readonly swapPricing: ISwapPrice;
77
- abstract config: SwapBaseConfig;
78
- logger: {
79
- debug: (msg: string, ...args: any) => void;
80
- info: (msg: string, ...args: any) => void;
81
- warn: (msg: string, ...args: any) => void;
82
- error: (msg: string, ...args: any) => void;
83
- };
84
- protected swapLogger: {
85
- debug: (swap: SwapHandlerSwap, msg: string, ...args: any) => void;
86
- info: (swap: SwapHandlerSwap, msg: string, ...args: any) => void;
87
- warn: (swap: SwapHandlerSwap, msg: string, ...args: any) => void;
88
- error: (swap: SwapHandlerSwap, msg: string, ...args: any) => void;
89
- };
90
- protected constructor(storageDirectory: IIntermediaryStorage<V>, path: string, chainsData: MultichainData, swapPricing: ISwapPrice);
91
- protected getChain(identifier: string): ChainData;
92
- protected abstract processPastSwaps(): Promise<void>;
93
- /**
94
- * Starts the watchdog checking past swaps for expiry or claim eligibility.
95
- */
96
- startWatchdog(): Promise<void>;
97
- /**
98
- * Initializes swap handler, loads data and subscribes to chain events
99
- */
100
- abstract init(): Promise<void>;
101
- protected loadData(ctor: new (data: any) => V): Promise<void>;
102
- /**
103
- * Sets up required listeners for the REST server
104
- *
105
- * @param restServer
106
- */
107
- abstract startRestServer(restServer: Express): void;
108
- /**
109
- * Returns data to be returned in swap handler info
110
- */
111
- abstract getInfoData(): any;
112
- /**
113
- * Remove swap data
114
- *
115
- * @param hash
116
- * @param sequence
117
- */
118
- protected removeSwapData(hash: string, sequence: bigint): Promise<void>;
119
- /**
120
- * Remove swap data
121
- *
122
- * @param swap
123
- * @param ultimateState set the ultimate state of the swap before removing
124
- */
125
- protected removeSwapData(swap: V, ultimateState?: S): Promise<void>;
126
- protected saveSwapData(swap: V): Promise<void>;
127
- /**
128
- * Checks if we have enough balance of the token in the swap vault
129
- *
130
- * @param totalInToken
131
- * @param balancePrefetch
132
- * @param signal
133
- * @throws {DefinedRuntimeError} will throw an error if there are not enough funds in the vault
134
- */
135
- protected checkBalance(totalInToken: bigint, balancePrefetch: Promise<bigint>, signal: AbortSignal | null): Promise<void>;
136
- /**
137
- * Checks if the sequence number is between 0-2^64
138
- *
139
- * @param sequence
140
- * @throws {DefinedRuntimeError} will throw an error if sequence number is out of bounds
141
- */
142
- protected checkSequence(sequence: bigint): void;
143
- /**
144
- * Checks whether a given token is supported on a specified chain
145
- *
146
- * @param chainId
147
- * @param token
148
- * @protected
149
- */
150
- protected isTokenSupported(chainId: string, token: string): boolean;
151
- getInfo(): SwapHandlerInfoType;
152
- protected getInitAuthorizationTimeout(chainIdentifier: string): number;
153
- }
1
+ import { Express, Request } from "express";
2
+ import { ISwapPrice } from "../prices/ISwapPrice";
3
+ import { ChainType } from "@atomiqlabs/base";
4
+ import { SwapHandlerSwap } from "./SwapHandlerSwap";
5
+ import { IIntermediaryStorage } from "../storage/IIntermediaryStorage";
6
+ import { IParamReader } from "../utils/paramcoders/IParamReader";
7
+ import { LoggerType } from "../utils/Utils";
8
+ export declare enum SwapHandlerType {
9
+ TO_BTC = "TO_BTC",
10
+ FROM_BTC = "FROM_BTC",
11
+ TO_BTCLN = "TO_BTCLN",
12
+ FROM_BTCLN = "FROM_BTCLN",
13
+ FROM_BTCLN_TRUSTED = "FROM_BTCLN_TRUSTED",
14
+ FROM_BTC_TRUSTED = "FROM_BTC_TRUSTED",
15
+ FROM_BTC_SPV = "FROM_BTC_SPV",
16
+ FROM_BTCLN_AUTO = "FROM_BTCLN_AUTO"
17
+ }
18
+ export type SwapHandlerInfoType = {
19
+ swapFeePPM: number;
20
+ swapBaseFee: number;
21
+ min: number;
22
+ max: number;
23
+ chainTokens: {
24
+ [chainId: string]: string[];
25
+ };
26
+ data?: any;
27
+ };
28
+ export type SwapBaseConfig = {
29
+ initAuthorizationTimeout: number;
30
+ initAuthorizationTimeouts?: {
31
+ [chainId: string]: number;
32
+ };
33
+ bitcoinBlocktime: bigint;
34
+ baseFee: bigint;
35
+ feePPM: bigint;
36
+ max: bigint;
37
+ min: bigint;
38
+ safetyFactor: bigint;
39
+ swapCheckInterval: number;
40
+ };
41
+ export type MultichainData = {
42
+ chains: {
43
+ [identifier: string]: ChainData;
44
+ };
45
+ };
46
+ export type ChainData<T extends ChainType = ChainType> = {
47
+ signer: T["Signer"];
48
+ swapContract: T["Contract"];
49
+ spvVaultContract: T["SpvVaultContract"];
50
+ chainInterface: T["ChainInterface"];
51
+ chainEvents: T["Events"];
52
+ allowedTokens: string[];
53
+ tokenMultipliers?: {
54
+ [tokenAddress: string]: bigint;
55
+ };
56
+ allowedDepositTokens?: string[];
57
+ btcRelay?: T["BtcRelay"];
58
+ };
59
+ export type RequestData<T> = {
60
+ chainIdentifier: string;
61
+ raw: Request & {
62
+ paramReader: IParamReader;
63
+ };
64
+ parsed: T;
65
+ metadata: any;
66
+ };
67
+ /**
68
+ * An abstract class defining a singular swap service
69
+ */
70
+ export declare abstract class SwapHandler<V extends SwapHandlerSwap<S> = SwapHandlerSwap, S = any> {
71
+ abstract readonly type: SwapHandlerType;
72
+ readonly storageManager: IIntermediaryStorage<V>;
73
+ readonly path: string;
74
+ readonly chains: MultichainData;
75
+ readonly allowedTokens: {
76
+ [chainId: string]: Set<string>;
77
+ };
78
+ readonly swapPricing: ISwapPrice;
79
+ abstract config: SwapBaseConfig;
80
+ logger: LoggerType;
81
+ protected swapLogger: {
82
+ debug: (swap: SwapHandlerSwap, msg: string, ...args: any) => void;
83
+ info: (swap: SwapHandlerSwap, msg: string, ...args: any) => void;
84
+ warn: (swap: SwapHandlerSwap, msg: string, ...args: any) => void;
85
+ error: (swap: SwapHandlerSwap, msg: string, ...args: any) => void;
86
+ };
87
+ protected constructor(storageDirectory: IIntermediaryStorage<V>, path: string, chainsData: MultichainData, swapPricing: ISwapPrice);
88
+ protected getChain(identifier: string): ChainData;
89
+ protected abstract processPastSwaps(): Promise<void>;
90
+ /**
91
+ * Starts the watchdog checking past swaps for expiry or claim eligibility.
92
+ */
93
+ startWatchdog(): Promise<void>;
94
+ /**
95
+ * Initializes swap handler, loads data and subscribes to chain events
96
+ */
97
+ abstract init(): Promise<void>;
98
+ protected loadData(ctor: new (data: any) => V): Promise<void>;
99
+ /**
100
+ * Sets up required listeners for the REST server
101
+ *
102
+ * @param restServer
103
+ */
104
+ abstract startRestServer(restServer: Express): void;
105
+ /**
106
+ * Returns data to be returned in swap handler info
107
+ */
108
+ abstract getInfoData(): any;
109
+ /**
110
+ * Remove swap data
111
+ *
112
+ * @param hash
113
+ * @param sequence
114
+ */
115
+ protected removeSwapData(hash: string, sequence: bigint): Promise<void>;
116
+ /**
117
+ * Remove swap data
118
+ *
119
+ * @param swap
120
+ * @param ultimateState set the ultimate state of the swap before removing
121
+ */
122
+ protected removeSwapData(swap: V, ultimateState?: S): Promise<void>;
123
+ protected saveSwapData(swap: V): Promise<void>;
124
+ /**
125
+ * Checks if we have enough balance of the token in the swap vault
126
+ *
127
+ * @param totalInToken
128
+ * @param balancePrefetch
129
+ * @param signal
130
+ * @throws {DefinedRuntimeError} will throw an error if there are not enough funds in the vault
131
+ */
132
+ protected checkBalance(totalInToken: bigint, balancePrefetch: Promise<bigint>, signal: AbortSignal | null): Promise<void>;
133
+ /**
134
+ * Checks if the sequence number is between 0-2^64
135
+ *
136
+ * @param sequence
137
+ * @throws {DefinedRuntimeError} will throw an error if sequence number is out of bounds
138
+ */
139
+ protected checkSequence(sequence: bigint): void;
140
+ /**
141
+ * Checks whether a given token is supported on a specified chain
142
+ *
143
+ * @param chainId
144
+ * @param token
145
+ * @protected
146
+ */
147
+ protected isTokenSupported(chainId: string, token: string): boolean;
148
+ getInfo(): SwapHandlerInfoType;
149
+ protected getInitAuthorizationTimeout(chainIdentifier: string): number;
150
+ }
@@ -1,157 +1,154 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SwapHandler = exports.SwapHandlerType = void 0;
4
- const PluginManager_1 = require("../plugins/PluginManager");
5
- var SwapHandlerType;
6
- (function (SwapHandlerType) {
7
- SwapHandlerType["TO_BTC"] = "TO_BTC";
8
- SwapHandlerType["FROM_BTC"] = "FROM_BTC";
9
- SwapHandlerType["TO_BTCLN"] = "TO_BTCLN";
10
- SwapHandlerType["FROM_BTCLN"] = "FROM_BTCLN";
11
- SwapHandlerType["FROM_BTCLN_TRUSTED"] = "FROM_BTCLN_TRUSTED";
12
- SwapHandlerType["FROM_BTC_TRUSTED"] = "FROM_BTC_TRUSTED";
13
- SwapHandlerType["FROM_BTC_SPV"] = "FROM_BTC_SPV";
14
- })(SwapHandlerType = exports.SwapHandlerType || (exports.SwapHandlerType = {}));
15
- /**
16
- * An abstract class defining a singular swap service
17
- */
18
- class SwapHandler {
19
- constructor(storageDirectory, path, chainsData, swapPricing) {
20
- this.logger = {
21
- debug: (msg, ...args) => console.debug("SwapHandler(" + this.type + "): " + msg, ...args),
22
- info: (msg, ...args) => console.info("SwapHandler(" + this.type + "): " + msg, ...args),
23
- warn: (msg, ...args) => console.warn("SwapHandler(" + this.type + "): " + msg, ...args),
24
- error: (msg, ...args) => console.error("SwapHandler(" + this.type + "): " + msg, ...args)
25
- };
26
- this.swapLogger = {
27
- debug: (swap, msg, ...args) => this.logger.debug(swap.getIdentifier() + ": " + msg, ...args),
28
- info: (swap, msg, ...args) => this.logger.info(swap.getIdentifier() + ": " + msg, ...args),
29
- warn: (swap, msg, ...args) => this.logger.warn(swap.getIdentifier() + ": " + msg, ...args),
30
- error: (swap, msg, ...args) => this.logger.error(swap.getIdentifier() + ": " + msg, ...args)
31
- };
32
- this.storageManager = storageDirectory;
33
- this.chains = chainsData;
34
- this.path = path;
35
- this.swapPricing = swapPricing;
36
- this.allowedTokens = {};
37
- for (let chainId in chainsData.chains) {
38
- this.allowedTokens[chainId] = new Set(chainsData.chains[chainId].allowedTokens);
39
- }
40
- }
41
- getChain(identifier) {
42
- if (this.chains.chains[identifier] == null)
43
- throw {
44
- code: 20200,
45
- msg: "Invalid chain specified!"
46
- };
47
- return this.chains.chains[identifier];
48
- }
49
- /**
50
- * Starts the watchdog checking past swaps for expiry or claim eligibility.
51
- */
52
- async startWatchdog() {
53
- let rerun;
54
- rerun = async () => {
55
- await this.processPastSwaps().catch(e => console.error(e));
56
- setTimeout(rerun, this.config.swapCheckInterval);
57
- };
58
- await rerun();
59
- }
60
- async loadData(ctor) {
61
- await this.storageManager.loadData(ctor);
62
- //Check if all swaps contain a valid amount
63
- for (let { obj: swap, hash, sequence } of await this.storageManager.query([])) {
64
- if (hash !== swap.getIdentifierHash() || sequence !== (swap.getSequence() ?? 0n)) {
65
- this.swapLogger.info(swap, "loadData(): Swap storage key or sequence mismatch, fixing," +
66
- " old hash: " + hash + " new hash: " + swap.getIdentifierHash() +
67
- " old seq: " + sequence.toString(10) + " new seq: " + (swap.getSequence() ?? 0n).toString(10));
68
- await this.storageManager.removeData(hash, sequence);
69
- await this.storageManager.saveData(swap.getIdentifierHash(), swap.getSequence(), swap);
70
- }
71
- }
72
- }
73
- async removeSwapData(hashOrSwap, sequenceOrUltimateState) {
74
- let swap;
75
- if (typeof (hashOrSwap) === "string") {
76
- if (typeof (sequenceOrUltimateState) !== "bigint")
77
- throw new Error("Sequence must be a BN instance!");
78
- swap = await this.storageManager.getData(hashOrSwap, sequenceOrUltimateState);
79
- }
80
- else {
81
- swap = hashOrSwap;
82
- if (sequenceOrUltimateState != null && typeof (sequenceOrUltimateState) !== "bigint")
83
- await swap.setState(sequenceOrUltimateState);
84
- }
85
- if (swap != null)
86
- await PluginManager_1.PluginManager.swapRemove(swap);
87
- this.swapLogger.debug(swap, "removeSwapData(): removing swap final state: " + swap.state);
88
- await this.storageManager.removeData(swap.getIdentifierHash(), swap.getSequence());
89
- }
90
- async saveSwapData(swap) {
91
- await this.storageManager.saveData(swap.getIdentifierHash(), swap.getSequence(), swap);
92
- }
93
- /**
94
- * Checks if we have enough balance of the token in the swap vault
95
- *
96
- * @param totalInToken
97
- * @param balancePrefetch
98
- * @param signal
99
- * @throws {DefinedRuntimeError} will throw an error if there are not enough funds in the vault
100
- */
101
- async checkBalance(totalInToken, balancePrefetch, signal) {
102
- const balance = await balancePrefetch;
103
- if (signal != null)
104
- signal.throwIfAborted();
105
- if (balance == null || balance < totalInToken) {
106
- throw {
107
- code: 20002,
108
- msg: "Not enough liquidity"
109
- };
110
- }
111
- }
112
- /**
113
- * Checks if the sequence number is between 0-2^64
114
- *
115
- * @param sequence
116
- * @throws {DefinedRuntimeError} will throw an error if sequence number is out of bounds
117
- */
118
- checkSequence(sequence) {
119
- if (sequence < 0n || sequence >= 2n ** 64n) {
120
- throw {
121
- code: 20060,
122
- msg: "Invalid sequence"
123
- };
124
- }
125
- }
126
- /**
127
- * Checks whether a given token is supported on a specified chain
128
- *
129
- * @param chainId
130
- * @param token
131
- * @protected
132
- */
133
- isTokenSupported(chainId, token) {
134
- const chainTokens = this.allowedTokens[chainId];
135
- if (chainTokens == null)
136
- return false;
137
- return chainTokens.has(token);
138
- }
139
- getInfo() {
140
- const chainTokens = {};
141
- for (let chainId in this.allowedTokens) {
142
- chainTokens[chainId] = Array.from(this.allowedTokens[chainId]);
143
- }
144
- return {
145
- swapFeePPM: Number(this.config.feePPM),
146
- swapBaseFee: Number(this.config.baseFee),
147
- min: Number(this.config.min),
148
- max: Number(this.config.max),
149
- data: this.getInfoData(),
150
- chainTokens
151
- };
152
- }
153
- getInitAuthorizationTimeout(chainIdentifier) {
154
- return this.config.initAuthorizationTimeouts?.[chainIdentifier] ?? this.config.initAuthorizationTimeout;
155
- }
156
- }
157
- exports.SwapHandler = SwapHandler;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SwapHandler = exports.SwapHandlerType = void 0;
4
+ const PluginManager_1 = require("../plugins/PluginManager");
5
+ const Utils_1 = require("../utils/Utils");
6
+ var SwapHandlerType;
7
+ (function (SwapHandlerType) {
8
+ SwapHandlerType["TO_BTC"] = "TO_BTC";
9
+ SwapHandlerType["FROM_BTC"] = "FROM_BTC";
10
+ SwapHandlerType["TO_BTCLN"] = "TO_BTCLN";
11
+ SwapHandlerType["FROM_BTCLN"] = "FROM_BTCLN";
12
+ SwapHandlerType["FROM_BTCLN_TRUSTED"] = "FROM_BTCLN_TRUSTED";
13
+ SwapHandlerType["FROM_BTC_TRUSTED"] = "FROM_BTC_TRUSTED";
14
+ SwapHandlerType["FROM_BTC_SPV"] = "FROM_BTC_SPV";
15
+ SwapHandlerType["FROM_BTCLN_AUTO"] = "FROM_BTCLN_AUTO";
16
+ })(SwapHandlerType = exports.SwapHandlerType || (exports.SwapHandlerType = {}));
17
+ /**
18
+ * An abstract class defining a singular swap service
19
+ */
20
+ class SwapHandler {
21
+ constructor(storageDirectory, path, chainsData, swapPricing) {
22
+ this.logger = (0, Utils_1.getLogger)(() => "SwapHandler(" + this.type + "): ");
23
+ this.swapLogger = {
24
+ debug: (swap, msg, ...args) => this.logger.debug(swap.getIdentifier() + ": " + msg, ...args),
25
+ info: (swap, msg, ...args) => this.logger.info(swap.getIdentifier() + ": " + msg, ...args),
26
+ warn: (swap, msg, ...args) => this.logger.warn(swap.getIdentifier() + ": " + msg, ...args),
27
+ error: (swap, msg, ...args) => this.logger.error(swap.getIdentifier() + ": " + msg, ...args)
28
+ };
29
+ this.storageManager = storageDirectory;
30
+ this.chains = chainsData;
31
+ this.path = path;
32
+ this.swapPricing = swapPricing;
33
+ this.allowedTokens = {};
34
+ for (let chainId in chainsData.chains) {
35
+ this.allowedTokens[chainId] = new Set(chainsData.chains[chainId].allowedTokens);
36
+ }
37
+ }
38
+ getChain(identifier) {
39
+ if (this.chains.chains[identifier] == null)
40
+ throw {
41
+ code: 20200,
42
+ msg: "Invalid chain specified!"
43
+ };
44
+ return this.chains.chains[identifier];
45
+ }
46
+ /**
47
+ * Starts the watchdog checking past swaps for expiry or claim eligibility.
48
+ */
49
+ async startWatchdog() {
50
+ let rerun;
51
+ rerun = async () => {
52
+ await this.processPastSwaps().catch(e => this.logger.error("startWatchdog(): Error when processing past swaps: ", e));
53
+ setTimeout(rerun, this.config.swapCheckInterval);
54
+ };
55
+ await rerun();
56
+ }
57
+ async loadData(ctor) {
58
+ await this.storageManager.loadData(ctor);
59
+ //Check if all swaps contain a valid amount
60
+ for (let { obj: swap, hash, sequence } of await this.storageManager.query([])) {
61
+ if (hash !== swap.getIdentifierHash() || sequence !== (swap.getSequence() ?? 0n)) {
62
+ this.swapLogger.info(swap, "loadData(): Swap storage key or sequence mismatch, fixing," +
63
+ " old hash: " + hash + " new hash: " + swap.getIdentifierHash() +
64
+ " old seq: " + sequence.toString(10) + " new seq: " + (swap.getSequence() ?? 0n).toString(10));
65
+ await this.storageManager.removeData(hash, sequence);
66
+ await this.storageManager.saveData(swap.getIdentifierHash(), swap.getSequence(), swap);
67
+ }
68
+ }
69
+ }
70
+ async removeSwapData(hashOrSwap, sequenceOrUltimateState) {
71
+ let swap;
72
+ if (typeof (hashOrSwap) === "string") {
73
+ if (typeof (sequenceOrUltimateState) !== "bigint")
74
+ throw new Error("Sequence must be a BN instance!");
75
+ swap = await this.storageManager.getData(hashOrSwap, sequenceOrUltimateState);
76
+ }
77
+ else {
78
+ swap = hashOrSwap;
79
+ if (sequenceOrUltimateState != null && typeof (sequenceOrUltimateState) !== "bigint")
80
+ await swap.setState(sequenceOrUltimateState);
81
+ }
82
+ if (swap != null)
83
+ await PluginManager_1.PluginManager.swapRemove(swap);
84
+ this.swapLogger.debug(swap, "removeSwapData(): removing swap final state: " + swap.state);
85
+ await this.storageManager.removeData(swap.getIdentifierHash(), swap.getSequence());
86
+ }
87
+ async saveSwapData(swap) {
88
+ await this.storageManager.saveData(swap.getIdentifierHash(), swap.getSequence(), swap);
89
+ }
90
+ /**
91
+ * Checks if we have enough balance of the token in the swap vault
92
+ *
93
+ * @param totalInToken
94
+ * @param balancePrefetch
95
+ * @param signal
96
+ * @throws {DefinedRuntimeError} will throw an error if there are not enough funds in the vault
97
+ */
98
+ async checkBalance(totalInToken, balancePrefetch, signal) {
99
+ const balance = await balancePrefetch;
100
+ if (signal != null)
101
+ signal.throwIfAborted();
102
+ if (balance == null || balance < totalInToken) {
103
+ throw {
104
+ code: 20002,
105
+ msg: "Not enough liquidity"
106
+ };
107
+ }
108
+ }
109
+ /**
110
+ * Checks if the sequence number is between 0-2^64
111
+ *
112
+ * @param sequence
113
+ * @throws {DefinedRuntimeError} will throw an error if sequence number is out of bounds
114
+ */
115
+ checkSequence(sequence) {
116
+ if (sequence < 0n || sequence >= 2n ** 64n) {
117
+ throw {
118
+ code: 20060,
119
+ msg: "Invalid sequence"
120
+ };
121
+ }
122
+ }
123
+ /**
124
+ * Checks whether a given token is supported on a specified chain
125
+ *
126
+ * @param chainId
127
+ * @param token
128
+ * @protected
129
+ */
130
+ isTokenSupported(chainId, token) {
131
+ const chainTokens = this.allowedTokens[chainId];
132
+ if (chainTokens == null)
133
+ return false;
134
+ return chainTokens.has(token);
135
+ }
136
+ getInfo() {
137
+ const chainTokens = {};
138
+ for (let chainId in this.allowedTokens) {
139
+ chainTokens[chainId] = Array.from(this.allowedTokens[chainId]);
140
+ }
141
+ return {
142
+ swapFeePPM: Number(this.config.feePPM),
143
+ swapBaseFee: Number(this.config.baseFee),
144
+ min: Number(this.config.min),
145
+ max: Number(this.config.max),
146
+ data: this.getInfoData(),
147
+ chainTokens
148
+ };
149
+ }
150
+ getInitAuthorizationTimeout(chainIdentifier) {
151
+ return this.config.initAuthorizationTimeouts?.[chainIdentifier] ?? this.config.initAuthorizationTimeout;
152
+ }
153
+ }
154
+ exports.SwapHandler = SwapHandler;