@atomiqlabs/lp-lib 14.0.0-dev.21 → 14.0.0-dev.23

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 (167) 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 -42
  5. package/dist/index.js +58 -58
  6. package/dist/info/InfoHandler.d.ts +17 -17
  7. package/dist/info/InfoHandler.js +61 -61
  8. package/dist/plugins/IPlugin.d.ts +143 -143
  9. package/dist/plugins/IPlugin.js +34 -34
  10. package/dist/plugins/PluginManager.d.ts +112 -112
  11. package/dist/plugins/PluginManager.js +259 -259
  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 -19
  23. package/dist/storagemanager/IntermediaryStorageManager.js +111 -111
  24. package/dist/storagemanager/StorageManager.d.ts +13 -13
  25. package/dist/storagemanager/StorageManager.js +64 -64
  26. package/dist/swaps/SwapHandler.d.ts +153 -153
  27. package/dist/swaps/SwapHandler.js +160 -160
  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 +72 -72
  32. package/dist/swaps/assertions/FromBtcAmountAssertions.d.ts +76 -76
  33. package/dist/swaps/assertions/FromBtcAmountAssertions.js +180 -180
  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 -102
  45. package/dist/swaps/escrow/FromBtcBaseSwapHandler.js +210 -210
  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 +675 -675
  56. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.d.ts +33 -33
  57. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.js +91 -91
  58. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.d.ts +111 -111
  59. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.js +684 -684
  60. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.d.ts +55 -55
  61. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.js +120 -120
  62. package/dist/swaps/escrow/tobtc_abstract/ToBtcAbs.d.ts +171 -171
  63. package/dist/swaps/escrow/tobtc_abstract/ToBtcAbs.js +706 -706
  64. package/dist/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.d.ts +26 -26
  65. package/dist/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.js +62 -62
  66. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.d.ts +177 -177
  67. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.js +863 -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 +40 -41
  71. package/dist/swaps/spv_vault_swap/SpvVault.js +111 -111
  72. package/dist/swaps/spv_vault_swap/SpvVaultSwap.d.ts +67 -67
  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 +492 -492
  76. package/dist/swaps/spv_vault_swap/SpvVaults.d.ts +52 -52
  77. package/dist/swaps/spv_vault_swap/SpvVaults.js +386 -364
  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 +494 -494
  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 +2 -2
  87. package/dist/utils/BitcoinUtils.js +45 -45
  88. package/dist/utils/Utils.d.ts +29 -29
  89. package/dist/utils/Utils.js +89 -89
  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 -107
  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 +67 -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 -53
  115. package/src/info/InfoHandler.ts +106 -106
  116. package/src/plugins/IPlugin.ts +168 -168
  117. package/src/plugins/PluginManager.ts +336 -336
  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 -118
  124. package/src/storagemanager/StorageManager.ts +78 -78
  125. package/src/swaps/SwapHandler.ts +277 -277
  126. package/src/swaps/SwapHandlerSwap.ts +141 -141
  127. package/src/swaps/assertions/AmountAssertions.ts +76 -76
  128. package/src/swaps/assertions/FromBtcAmountAssertions.ts +246 -246
  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 -286
  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 +856 -856
  140. package/src/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.ts +141 -141
  141. package/src/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.ts +850 -850
  142. package/src/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.ts +196 -196
  143. package/src/swaps/escrow/tobtc_abstract/ToBtcAbs.ts +879 -879
  144. package/src/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.ts +102 -102
  145. package/src/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.ts +1112 -1112
  146. package/src/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.ts +80 -80
  147. package/src/swaps/spv_vault_swap/SpvVault.ts +143 -143
  148. package/src/swaps/spv_vault_swap/SpvVaultSwap.ts +225 -225
  149. package/src/swaps/spv_vault_swap/SpvVaultSwapHandler.ts +628 -628
  150. package/src/swaps/spv_vault_swap/SpvVaults.ts +458 -435
  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 +590 -590
  154. package/src/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.ts +121 -121
  155. package/src/utils/BitcoinUtils.ts +42 -42
  156. package/src/utils/Utils.ts +104 -104
  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 -118
  164. package/src/utils/paramcoders/server/ServerParamEncoder.ts +75 -75
  165. package/src/wallets/IBitcoinWallet.ts +68 -68
  166. package/src/wallets/ILightningWallet.ts +178 -178
  167. package/src/wallets/ISpvVaultSigner.ts +10 -10
@@ -1,153 +1,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
- tokens: string[];
24
- chainTokens: {
25
- [chainId: string]: string[];
26
- };
27
- data?: any;
28
- };
29
- export type SwapBaseConfig = {
30
- initAuthorizationTimeout: number;
31
- initAuthorizationTimeouts?: {
32
- [chainId: string]: number;
33
- };
34
- bitcoinBlocktime: bigint;
35
- baseFee: bigint;
36
- feePPM: bigint;
37
- max: bigint;
38
- min: bigint;
39
- safetyFactor: bigint;
40
- swapCheckInterval: number;
41
- };
42
- export type MultichainData = {
43
- chains: {
44
- [identifier: string]: ChainData;
45
- };
46
- default: string;
47
- };
48
- export type ChainData<T extends ChainType = ChainType> = {
49
- signer: T["Signer"];
50
- swapContract: T["Contract"];
51
- spvVaultContract: T["SpvVaultContract"];
52
- chainInterface: T["ChainInterface"];
53
- chainEvents: T["Events"];
54
- allowedTokens: string[];
55
- tokenMultipliers?: {
56
- [tokenAddress: string]: bigint;
57
- };
58
- allowedDepositTokens?: string[];
59
- btcRelay?: T["BtcRelay"];
60
- };
61
- export type RequestData<T> = {
62
- chainIdentifier: string;
63
- raw: Request & {
64
- paramReader: IParamReader;
65
- };
66
- parsed: T;
67
- metadata: any;
68
- };
69
- /**
70
- * An abstract class defining a singular swap service
71
- */
72
- export declare abstract class SwapHandler<V extends SwapHandlerSwap<S> = SwapHandlerSwap, S = any> {
73
- abstract readonly type: SwapHandlerType;
74
- readonly storageManager: IIntermediaryStorage<V>;
75
- readonly path: string;
76
- readonly chains: MultichainData;
77
- readonly allowedTokens: {
78
- [chainId: string]: Set<string>;
79
- };
80
- readonly swapPricing: ISwapPrice;
81
- abstract config: SwapBaseConfig;
82
- logger: LoggerType;
83
- protected swapLogger: {
84
- debug: (swap: SwapHandlerSwap, msg: string, ...args: any) => void;
85
- info: (swap: SwapHandlerSwap, msg: string, ...args: any) => void;
86
- warn: (swap: SwapHandlerSwap, msg: string, ...args: any) => void;
87
- error: (swap: SwapHandlerSwap, msg: string, ...args: any) => void;
88
- };
89
- protected constructor(storageDirectory: IIntermediaryStorage<V>, path: string, chainsData: MultichainData, swapPricing: ISwapPrice);
90
- protected getDefaultChain(): ChainData;
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
+ tokens: string[];
24
+ chainTokens: {
25
+ [chainId: string]: string[];
26
+ };
27
+ data?: any;
28
+ };
29
+ export type SwapBaseConfig = {
30
+ initAuthorizationTimeout: number;
31
+ initAuthorizationTimeouts?: {
32
+ [chainId: string]: number;
33
+ };
34
+ bitcoinBlocktime: bigint;
35
+ baseFee: bigint;
36
+ feePPM: bigint;
37
+ max: bigint;
38
+ min: bigint;
39
+ safetyFactor: bigint;
40
+ swapCheckInterval: number;
41
+ };
42
+ export type MultichainData = {
43
+ chains: {
44
+ [identifier: string]: ChainData;
45
+ };
46
+ default: string;
47
+ };
48
+ export type ChainData<T extends ChainType = ChainType> = {
49
+ signer: T["Signer"];
50
+ swapContract: T["Contract"];
51
+ spvVaultContract: T["SpvVaultContract"];
52
+ chainInterface: T["ChainInterface"];
53
+ chainEvents: T["Events"];
54
+ allowedTokens: string[];
55
+ tokenMultipliers?: {
56
+ [tokenAddress: string]: bigint;
57
+ };
58
+ allowedDepositTokens?: string[];
59
+ btcRelay?: T["BtcRelay"];
60
+ };
61
+ export type RequestData<T> = {
62
+ chainIdentifier: string;
63
+ raw: Request & {
64
+ paramReader: IParamReader;
65
+ };
66
+ parsed: T;
67
+ metadata: any;
68
+ };
69
+ /**
70
+ * An abstract class defining a singular swap service
71
+ */
72
+ export declare abstract class SwapHandler<V extends SwapHandlerSwap<S> = SwapHandlerSwap, S = any> {
73
+ abstract readonly type: SwapHandlerType;
74
+ readonly storageManager: IIntermediaryStorage<V>;
75
+ readonly path: string;
76
+ readonly chains: MultichainData;
77
+ readonly allowedTokens: {
78
+ [chainId: string]: Set<string>;
79
+ };
80
+ readonly swapPricing: ISwapPrice;
81
+ abstract config: SwapBaseConfig;
82
+ logger: LoggerType;
83
+ protected swapLogger: {
84
+ debug: (swap: SwapHandlerSwap, msg: string, ...args: any) => void;
85
+ info: (swap: SwapHandlerSwap, msg: string, ...args: any) => void;
86
+ warn: (swap: SwapHandlerSwap, msg: string, ...args: any) => void;
87
+ error: (swap: SwapHandlerSwap, msg: string, ...args: any) => void;
88
+ };
89
+ protected constructor(storageDirectory: IIntermediaryStorage<V>, path: string, chainsData: MultichainData, swapPricing: ISwapPrice);
90
+ protected getDefaultChain(): ChainData;
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,160 +1,160 @@
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
- if (this.chains.chains[this.chains.default] == null)
32
- throw new Error("Invalid default chain specified");
33
- this.path = path;
34
- this.swapPricing = swapPricing;
35
- this.allowedTokens = {};
36
- for (let chainId in chainsData.chains) {
37
- this.allowedTokens[chainId] = new Set(chainsData.chains[chainId].allowedTokens);
38
- }
39
- }
40
- getDefaultChain() {
41
- return this.chains.chains[this.chains.default];
42
- }
43
- getChain(identifier) {
44
- if (this.chains.chains[identifier] == null)
45
- throw {
46
- code: 20200,
47
- msg: "Invalid chain specified!"
48
- };
49
- return this.chains.chains[identifier];
50
- }
51
- /**
52
- * Starts the watchdog checking past swaps for expiry or claim eligibility.
53
- */
54
- async startWatchdog() {
55
- let rerun;
56
- rerun = async () => {
57
- await this.processPastSwaps().catch(e => this.logger.error("startWatchdog(): Error when processing past swaps: ", e));
58
- setTimeout(rerun, this.config.swapCheckInterval);
59
- };
60
- await rerun();
61
- }
62
- async loadData(ctor) {
63
- await this.storageManager.loadData(ctor);
64
- //Check if all swaps contain a valid amount
65
- for (let { obj: swap, hash, sequence } of await this.storageManager.query([])) {
66
- if (hash !== swap.getIdentifierHash() || sequence !== (swap.getSequence() ?? 0n)) {
67
- this.swapLogger.info(swap, "loadData(): Swap storage key or sequence mismatch, fixing," +
68
- " old hash: " + hash + " new hash: " + swap.getIdentifierHash() +
69
- " old seq: " + sequence.toString(10) + " new seq: " + (swap.getSequence() ?? 0n).toString(10));
70
- await this.storageManager.removeData(hash, sequence);
71
- await this.storageManager.saveData(swap.getIdentifierHash(), swap.getSequence(), swap);
72
- }
73
- }
74
- }
75
- async removeSwapData(hashOrSwap, sequenceOrUltimateState) {
76
- let swap;
77
- if (typeof (hashOrSwap) === "string") {
78
- if (typeof (sequenceOrUltimateState) !== "bigint")
79
- throw new Error("Sequence must be a BN instance!");
80
- swap = await this.storageManager.getData(hashOrSwap, sequenceOrUltimateState);
81
- }
82
- else {
83
- swap = hashOrSwap;
84
- if (sequenceOrUltimateState != null && typeof (sequenceOrUltimateState) !== "bigint")
85
- await swap.setState(sequenceOrUltimateState);
86
- }
87
- if (swap != null)
88
- await PluginManager_1.PluginManager.swapRemove(swap);
89
- this.swapLogger.debug(swap, "removeSwapData(): removing swap final state: " + swap.state);
90
- await this.storageManager.removeData(swap.getIdentifierHash(), swap.getSequence());
91
- }
92
- async saveSwapData(swap) {
93
- await this.storageManager.saveData(swap.getIdentifierHash(), swap.getSequence(), swap);
94
- }
95
- /**
96
- * Checks if we have enough balance of the token in the swap vault
97
- *
98
- * @param totalInToken
99
- * @param balancePrefetch
100
- * @param signal
101
- * @throws {DefinedRuntimeError} will throw an error if there are not enough funds in the vault
102
- */
103
- async checkBalance(totalInToken, balancePrefetch, signal) {
104
- const balance = await balancePrefetch;
105
- if (signal != null)
106
- signal.throwIfAborted();
107
- if (balance == null || balance < totalInToken) {
108
- throw {
109
- code: 20002,
110
- msg: "Not enough liquidity"
111
- };
112
- }
113
- }
114
- /**
115
- * Checks if the sequence number is between 0-2^64
116
- *
117
- * @param sequence
118
- * @throws {DefinedRuntimeError} will throw an error if sequence number is out of bounds
119
- */
120
- checkSequence(sequence) {
121
- if (sequence < 0n || sequence >= 2n ** 64n) {
122
- throw {
123
- code: 20060,
124
- msg: "Invalid sequence"
125
- };
126
- }
127
- }
128
- /**
129
- * Checks whether a given token is supported on a specified chain
130
- *
131
- * @param chainId
132
- * @param token
133
- * @protected
134
- */
135
- isTokenSupported(chainId, token) {
136
- const chainTokens = this.allowedTokens[chainId];
137
- if (chainTokens == null)
138
- return false;
139
- return chainTokens.has(token);
140
- }
141
- getInfo() {
142
- const chainTokens = {};
143
- for (let chainId in this.allowedTokens) {
144
- chainTokens[chainId] = Array.from(this.allowedTokens[chainId]);
145
- }
146
- return {
147
- swapFeePPM: Number(this.config.feePPM),
148
- swapBaseFee: Number(this.config.baseFee),
149
- min: Number(this.config.min),
150
- max: Number(this.config.max),
151
- data: this.getInfoData(),
152
- tokens: Array.from(this.allowedTokens[this.chains.default]),
153
- chainTokens
154
- };
155
- }
156
- getInitAuthorizationTimeout(chainIdentifier) {
157
- return this.config.initAuthorizationTimeouts?.[chainIdentifier] ?? this.config.initAuthorizationTimeout;
158
- }
159
- }
160
- 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
+ if (this.chains.chains[this.chains.default] == null)
32
+ throw new Error("Invalid default chain specified");
33
+ this.path = path;
34
+ this.swapPricing = swapPricing;
35
+ this.allowedTokens = {};
36
+ for (let chainId in chainsData.chains) {
37
+ this.allowedTokens[chainId] = new Set(chainsData.chains[chainId].allowedTokens);
38
+ }
39
+ }
40
+ getDefaultChain() {
41
+ return this.chains.chains[this.chains.default];
42
+ }
43
+ getChain(identifier) {
44
+ if (this.chains.chains[identifier] == null)
45
+ throw {
46
+ code: 20200,
47
+ msg: "Invalid chain specified!"
48
+ };
49
+ return this.chains.chains[identifier];
50
+ }
51
+ /**
52
+ * Starts the watchdog checking past swaps for expiry or claim eligibility.
53
+ */
54
+ async startWatchdog() {
55
+ let rerun;
56
+ rerun = async () => {
57
+ await this.processPastSwaps().catch(e => this.logger.error("startWatchdog(): Error when processing past swaps: ", e));
58
+ setTimeout(rerun, this.config.swapCheckInterval);
59
+ };
60
+ await rerun();
61
+ }
62
+ async loadData(ctor) {
63
+ await this.storageManager.loadData(ctor);
64
+ //Check if all swaps contain a valid amount
65
+ for (let { obj: swap, hash, sequence } of await this.storageManager.query([])) {
66
+ if (hash !== swap.getIdentifierHash() || sequence !== (swap.getSequence() ?? 0n)) {
67
+ this.swapLogger.info(swap, "loadData(): Swap storage key or sequence mismatch, fixing," +
68
+ " old hash: " + hash + " new hash: " + swap.getIdentifierHash() +
69
+ " old seq: " + sequence.toString(10) + " new seq: " + (swap.getSequence() ?? 0n).toString(10));
70
+ await this.storageManager.removeData(hash, sequence);
71
+ await this.storageManager.saveData(swap.getIdentifierHash(), swap.getSequence(), swap);
72
+ }
73
+ }
74
+ }
75
+ async removeSwapData(hashOrSwap, sequenceOrUltimateState) {
76
+ let swap;
77
+ if (typeof (hashOrSwap) === "string") {
78
+ if (typeof (sequenceOrUltimateState) !== "bigint")
79
+ throw new Error("Sequence must be a BN instance!");
80
+ swap = await this.storageManager.getData(hashOrSwap, sequenceOrUltimateState);
81
+ }
82
+ else {
83
+ swap = hashOrSwap;
84
+ if (sequenceOrUltimateState != null && typeof (sequenceOrUltimateState) !== "bigint")
85
+ await swap.setState(sequenceOrUltimateState);
86
+ }
87
+ if (swap != null)
88
+ await PluginManager_1.PluginManager.swapRemove(swap);
89
+ this.swapLogger.debug(swap, "removeSwapData(): removing swap final state: " + swap.state);
90
+ await this.storageManager.removeData(swap.getIdentifierHash(), swap.getSequence());
91
+ }
92
+ async saveSwapData(swap) {
93
+ await this.storageManager.saveData(swap.getIdentifierHash(), swap.getSequence(), swap);
94
+ }
95
+ /**
96
+ * Checks if we have enough balance of the token in the swap vault
97
+ *
98
+ * @param totalInToken
99
+ * @param balancePrefetch
100
+ * @param signal
101
+ * @throws {DefinedRuntimeError} will throw an error if there are not enough funds in the vault
102
+ */
103
+ async checkBalance(totalInToken, balancePrefetch, signal) {
104
+ const balance = await balancePrefetch;
105
+ if (signal != null)
106
+ signal.throwIfAborted();
107
+ if (balance == null || balance < totalInToken) {
108
+ throw {
109
+ code: 20002,
110
+ msg: "Not enough liquidity"
111
+ };
112
+ }
113
+ }
114
+ /**
115
+ * Checks if the sequence number is between 0-2^64
116
+ *
117
+ * @param sequence
118
+ * @throws {DefinedRuntimeError} will throw an error if sequence number is out of bounds
119
+ */
120
+ checkSequence(sequence) {
121
+ if (sequence < 0n || sequence >= 2n ** 64n) {
122
+ throw {
123
+ code: 20060,
124
+ msg: "Invalid sequence"
125
+ };
126
+ }
127
+ }
128
+ /**
129
+ * Checks whether a given token is supported on a specified chain
130
+ *
131
+ * @param chainId
132
+ * @param token
133
+ * @protected
134
+ */
135
+ isTokenSupported(chainId, token) {
136
+ const chainTokens = this.allowedTokens[chainId];
137
+ if (chainTokens == null)
138
+ return false;
139
+ return chainTokens.has(token);
140
+ }
141
+ getInfo() {
142
+ const chainTokens = {};
143
+ for (let chainId in this.allowedTokens) {
144
+ chainTokens[chainId] = Array.from(this.allowedTokens[chainId]);
145
+ }
146
+ return {
147
+ swapFeePPM: Number(this.config.feePPM),
148
+ swapBaseFee: Number(this.config.baseFee),
149
+ min: Number(this.config.min),
150
+ max: Number(this.config.max),
151
+ data: this.getInfoData(),
152
+ tokens: Array.from(this.allowedTokens[this.chains.default]),
153
+ chainTokens
154
+ };
155
+ }
156
+ getInitAuthorizationTimeout(chainIdentifier) {
157
+ return this.config.initAuthorizationTimeouts?.[chainIdentifier] ?? this.config.initAuthorizationTimeout;
158
+ }
159
+ }
160
+ exports.SwapHandler = SwapHandler;