@atomiqlabs/lp-lib 14.0.0-dev.9 → 15.0.2

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 (166) 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 +40 -42
  5. package/dist/index.js +56 -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 +18 -19
  23. package/dist/storagemanager/IntermediaryStorageManager.js +104 -111
  24. package/dist/storagemanager/StorageManager.d.ts +12 -13
  25. package/dist/storagemanager/StorageManager.js +57 -64
  26. package/dist/swaps/SwapHandler.d.ts +156 -153
  27. package/dist/swaps/SwapHandler.js +163 -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 +172 -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 +101 -102
  45. package/dist/swaps/escrow/FromBtcBaseSwapHandler.js +207 -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 -648
  56. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.d.ts +32 -33
  57. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.js +88 -91
  58. package/dist/swaps/escrow/tobtc_abstract/ToBtcAbs.d.ts +171 -171
  59. package/dist/swaps/escrow/tobtc_abstract/ToBtcAbs.js +706 -706
  60. package/dist/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.d.ts +26 -26
  61. package/dist/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.js +62 -62
  62. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.d.ts +177 -177
  63. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.js +861 -861
  64. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.d.ts +23 -23
  65. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.js +56 -56
  66. package/dist/swaps/spv_vault_swap/SpvVault.d.ts +41 -41
  67. package/dist/swaps/spv_vault_swap/SpvVault.js +111 -111
  68. package/dist/swaps/spv_vault_swap/SpvVaultSwap.d.ts +67 -67
  69. package/dist/swaps/spv_vault_swap/SpvVaultSwap.js +158 -158
  70. package/dist/swaps/spv_vault_swap/SpvVaultSwapHandler.d.ts +68 -68
  71. package/dist/swaps/spv_vault_swap/SpvVaultSwapHandler.js +491 -490
  72. package/dist/swaps/spv_vault_swap/SpvVaults.d.ts +57 -52
  73. package/dist/swaps/spv_vault_swap/SpvVaults.js +369 -364
  74. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrusted.d.ts +51 -51
  75. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrusted.js +650 -650
  76. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrustedSwap.d.ts +52 -52
  77. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrustedSwap.js +118 -118
  78. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.d.ts +76 -76
  79. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.js +495 -494
  80. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.d.ts +34 -34
  81. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.js +81 -81
  82. package/dist/utils/Utils.d.ts +29 -29
  83. package/dist/utils/Utils.js +88 -89
  84. package/dist/utils/paramcoders/IParamReader.d.ts +5 -5
  85. package/dist/utils/paramcoders/IParamReader.js +2 -2
  86. package/dist/utils/paramcoders/IParamWriter.d.ts +4 -4
  87. package/dist/utils/paramcoders/IParamWriter.js +2 -2
  88. package/dist/utils/paramcoders/LegacyParamEncoder.d.ts +10 -10
  89. package/dist/utils/paramcoders/LegacyParamEncoder.js +22 -22
  90. package/dist/utils/paramcoders/ParamDecoder.d.ts +25 -25
  91. package/dist/utils/paramcoders/ParamDecoder.js +222 -222
  92. package/dist/utils/paramcoders/ParamEncoder.d.ts +9 -9
  93. package/dist/utils/paramcoders/ParamEncoder.js +22 -22
  94. package/dist/utils/paramcoders/SchemaVerifier.d.ts +21 -21
  95. package/dist/utils/paramcoders/SchemaVerifier.js +84 -84
  96. package/dist/utils/paramcoders/server/ServerParamDecoder.d.ts +8 -8
  97. package/dist/utils/paramcoders/server/ServerParamDecoder.js +105 -107
  98. package/dist/utils/paramcoders/server/ServerParamEncoder.d.ts +11 -11
  99. package/dist/utils/paramcoders/server/ServerParamEncoder.js +65 -65
  100. package/dist/wallets/IBitcoinWallet.d.ts +67 -67
  101. package/dist/wallets/IBitcoinWallet.js +2 -2
  102. package/dist/wallets/ILightningWallet.d.ts +117 -117
  103. package/dist/wallets/ILightningWallet.js +37 -37
  104. package/dist/wallets/ISpvVaultSigner.d.ts +7 -7
  105. package/dist/wallets/ISpvVaultSigner.js +2 -2
  106. package/dist/wallets/ISpvVaultWallet.d.ts +42 -0
  107. package/dist/wallets/ISpvVaultWallet.js +2 -0
  108. package/package.json +36 -36
  109. package/src/fees/IBtcFeeEstimator.ts +6 -6
  110. package/src/index.ts +51 -53
  111. package/src/info/InfoHandler.ts +106 -106
  112. package/src/plugins/IPlugin.ts +168 -168
  113. package/src/plugins/PluginManager.ts +336 -336
  114. package/src/prices/BinanceSwapPrice.ts +113 -113
  115. package/src/prices/CoinGeckoSwapPrice.ts +87 -87
  116. package/src/prices/ISwapPrice.ts +88 -88
  117. package/src/prices/OKXSwapPrice.ts +113 -113
  118. package/src/storage/IIntermediaryStorage.ts +19 -19
  119. package/src/storagemanager/IntermediaryStorageManager.ts +109 -118
  120. package/src/storagemanager/StorageManager.ts +68 -78
  121. package/src/swaps/SwapHandler.ts +280 -277
  122. package/src/swaps/SwapHandlerSwap.ts +141 -141
  123. package/src/swaps/assertions/AmountAssertions.ts +76 -76
  124. package/src/swaps/assertions/FromBtcAmountAssertions.ts +238 -238
  125. package/src/swaps/assertions/LightningAssertions.ts +103 -103
  126. package/src/swaps/assertions/ToBtcAmountAssertions.ts +203 -203
  127. package/src/swaps/escrow/EscrowHandler.ts +179 -179
  128. package/src/swaps/escrow/EscrowHandlerSwap.ts +86 -86
  129. package/src/swaps/escrow/FromBtcBaseSwap.ts +38 -38
  130. package/src/swaps/escrow/FromBtcBaseSwapHandler.ts +283 -286
  131. package/src/swaps/escrow/ToBtcBaseSwap.ts +85 -85
  132. package/src/swaps/escrow/ToBtcBaseSwapHandler.ts +129 -129
  133. package/src/swaps/escrow/frombtc_abstract/FromBtcAbs.ts +452 -452
  134. package/src/swaps/escrow/frombtc_abstract/FromBtcSwapAbs.ts +61 -61
  135. package/src/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.ts +856 -828
  136. package/src/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.ts +137 -141
  137. package/src/swaps/escrow/tobtc_abstract/ToBtcAbs.ts +879 -879
  138. package/src/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.ts +102 -102
  139. package/src/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.ts +1110 -1110
  140. package/src/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.ts +77 -77
  141. package/src/swaps/spv_vault_swap/SpvVault.ts +143 -143
  142. package/src/swaps/spv_vault_swap/SpvVaultSwap.ts +225 -225
  143. package/src/swaps/spv_vault_swap/SpvVaultSwapHandler.ts +627 -626
  144. package/src/swaps/spv_vault_swap/SpvVaults.ts +440 -435
  145. package/src/swaps/trusted/frombtc_trusted/FromBtcTrusted.ts +747 -747
  146. package/src/swaps/trusted/frombtc_trusted/FromBtcTrustedSwap.ts +185 -185
  147. package/src/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.ts +592 -590
  148. package/src/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.ts +121 -121
  149. package/src/utils/Utils.ts +102 -104
  150. package/src/utils/paramcoders/IParamReader.ts +7 -7
  151. package/src/utils/paramcoders/IParamWriter.ts +8 -8
  152. package/src/utils/paramcoders/LegacyParamEncoder.ts +27 -27
  153. package/src/utils/paramcoders/ParamDecoder.ts +218 -218
  154. package/src/utils/paramcoders/ParamEncoder.ts +29 -29
  155. package/src/utils/paramcoders/SchemaVerifier.ts +96 -96
  156. package/src/utils/paramcoders/server/ServerParamDecoder.ts +115 -118
  157. package/src/utils/paramcoders/server/ServerParamEncoder.ts +75 -75
  158. package/src/wallets/IBitcoinWallet.ts +68 -68
  159. package/src/wallets/ILightningWallet.ts +178 -178
  160. package/src/wallets/ISpvVaultSigner.ts +10 -10
  161. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.d.ts +0 -104
  162. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.js +0 -629
  163. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.d.ts +0 -54
  164. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.js +0 -117
  165. package/src/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.ts +0 -789
  166. package/src/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.ts +0 -192
@@ -1,153 +1,156 @@
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
+ 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
+ tokens: string[];
22
+ chainTokens: {
23
+ [chainId: string]: string[];
24
+ };
25
+ data?: any;
26
+ };
27
+ export type SwapBaseConfig = {
28
+ initAuthorizationTimeout: number;
29
+ initAuthorizationTimeouts?: {
30
+ [chainId: string]: number;
31
+ };
32
+ bitcoinBlocktime: bigint;
33
+ baseFee: bigint;
34
+ feePPM: bigint;
35
+ max: bigint;
36
+ min: bigint;
37
+ safetyFactor: bigint;
38
+ swapCheckInterval: number;
39
+ };
40
+ export type MultichainData = {
41
+ chains: {
42
+ [identifier: string]: ChainData;
43
+ };
44
+ default: string;
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: {
81
+ debug: (msg: string, ...args: any) => void;
82
+ info: (msg: string, ...args: any) => void;
83
+ warn: (msg: string, ...args: any) => void;
84
+ error: (msg: string, ...args: any) => void;
85
+ };
86
+ protected swapLogger: {
87
+ debug: (swap: SwapHandlerSwap, msg: string, ...args: any) => void;
88
+ info: (swap: SwapHandlerSwap, msg: string, ...args: any) => void;
89
+ warn: (swap: SwapHandlerSwap, msg: string, ...args: any) => void;
90
+ error: (swap: SwapHandlerSwap, msg: string, ...args: any) => void;
91
+ };
92
+ protected constructor(storageDirectory: IIntermediaryStorage<V>, path: string, chainsData: MultichainData, swapPricing: ISwapPrice);
93
+ protected getDefaultChain(): ChainData;
94
+ protected getChain(identifier: string): ChainData;
95
+ protected abstract processPastSwaps(): Promise<void>;
96
+ /**
97
+ * Starts the watchdog checking past swaps for expiry or claim eligibility.
98
+ */
99
+ startWatchdog(): Promise<void>;
100
+ /**
101
+ * Initializes swap handler, loads data and subscribes to chain events
102
+ */
103
+ abstract init(): Promise<void>;
104
+ protected loadData(ctor: new (data: any) => V): Promise<void>;
105
+ /**
106
+ * Sets up required listeners for the REST server
107
+ *
108
+ * @param restServer
109
+ */
110
+ abstract startRestServer(restServer: Express): void;
111
+ /**
112
+ * Returns data to be returned in swap handler info
113
+ */
114
+ abstract getInfoData(): any;
115
+ /**
116
+ * Remove swap data
117
+ *
118
+ * @param hash
119
+ * @param sequence
120
+ */
121
+ protected removeSwapData(hash: string, sequence: bigint): Promise<void>;
122
+ /**
123
+ * Remove swap data
124
+ *
125
+ * @param swap
126
+ * @param ultimateState set the ultimate state of the swap before removing
127
+ */
128
+ protected removeSwapData(swap: V, ultimateState?: S): Promise<void>;
129
+ protected saveSwapData(swap: V): Promise<void>;
130
+ /**
131
+ * Checks if we have enough balance of the token in the swap vault
132
+ *
133
+ * @param totalInToken
134
+ * @param balancePrefetch
135
+ * @param signal
136
+ * @throws {DefinedRuntimeError} will throw an error if there are not enough funds in the vault
137
+ */
138
+ protected checkBalance(totalInToken: bigint, balancePrefetch: Promise<bigint>, signal: AbortSignal | null): Promise<void>;
139
+ /**
140
+ * Checks if the sequence number is between 0-2^64
141
+ *
142
+ * @param sequence
143
+ * @throws {DefinedRuntimeError} will throw an error if sequence number is out of bounds
144
+ */
145
+ protected checkSequence(sequence: bigint): void;
146
+ /**
147
+ * Checks whether a given token is supported on a specified chain
148
+ *
149
+ * @param chainId
150
+ * @param token
151
+ * @protected
152
+ */
153
+ protected isTokenSupported(chainId: string, token: string): boolean;
154
+ getInfo(): SwapHandlerInfoType;
155
+ protected getInitAuthorizationTimeout(chainIdentifier: string): number;
156
+ }
@@ -1,160 +1,163 @@
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
+ 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
+ if (this.chains.chains[this.chains.default] == null)
35
+ throw new Error("Invalid default chain specified");
36
+ this.path = path;
37
+ this.swapPricing = swapPricing;
38
+ this.allowedTokens = {};
39
+ for (let chainId in chainsData.chains) {
40
+ this.allowedTokens[chainId] = new Set(chainsData.chains[chainId].allowedTokens);
41
+ }
42
+ }
43
+ getDefaultChain() {
44
+ return this.chains.chains[this.chains.default];
45
+ }
46
+ getChain(identifier) {
47
+ if (this.chains.chains[identifier] == null)
48
+ throw {
49
+ code: 20200,
50
+ msg: "Invalid chain specified!"
51
+ };
52
+ return this.chains.chains[identifier];
53
+ }
54
+ /**
55
+ * Starts the watchdog checking past swaps for expiry or claim eligibility.
56
+ */
57
+ async startWatchdog() {
58
+ let rerun;
59
+ rerun = async () => {
60
+ await this.processPastSwaps().catch(e => console.error(e));
61
+ setTimeout(rerun, this.config.swapCheckInterval);
62
+ };
63
+ await rerun();
64
+ }
65
+ async loadData(ctor) {
66
+ await this.storageManager.loadData(ctor);
67
+ //Check if all swaps contain a valid amount
68
+ for (let { obj: swap, hash, sequence } of await this.storageManager.query([])) {
69
+ if (hash !== swap.getIdentifierHash() || sequence !== (swap.getSequence() ?? 0n)) {
70
+ this.swapLogger.info(swap, "loadData(): Swap storage key or sequence mismatch, fixing," +
71
+ " old hash: " + hash + " new hash: " + swap.getIdentifierHash() +
72
+ " old seq: " + sequence.toString(10) + " new seq: " + (swap.getSequence() ?? 0n).toString(10));
73
+ await this.storageManager.removeData(hash, sequence);
74
+ await this.storageManager.saveData(swap.getIdentifierHash(), swap.getSequence(), swap);
75
+ }
76
+ }
77
+ }
78
+ async removeSwapData(hashOrSwap, sequenceOrUltimateState) {
79
+ let swap;
80
+ if (typeof (hashOrSwap) === "string") {
81
+ if (typeof (sequenceOrUltimateState) !== "bigint")
82
+ throw new Error("Sequence must be a BN instance!");
83
+ swap = await this.storageManager.getData(hashOrSwap, sequenceOrUltimateState);
84
+ }
85
+ else {
86
+ swap = hashOrSwap;
87
+ if (sequenceOrUltimateState != null && typeof (sequenceOrUltimateState) !== "bigint")
88
+ await swap.setState(sequenceOrUltimateState);
89
+ }
90
+ if (swap != null)
91
+ await PluginManager_1.PluginManager.swapRemove(swap);
92
+ this.swapLogger.debug(swap, "removeSwapData(): removing swap final state: " + swap.state);
93
+ await this.storageManager.removeData(swap.getIdentifierHash(), swap.getSequence());
94
+ }
95
+ async saveSwapData(swap) {
96
+ await this.storageManager.saveData(swap.getIdentifierHash(), swap.getSequence(), swap);
97
+ }
98
+ /**
99
+ * Checks if we have enough balance of the token in the swap vault
100
+ *
101
+ * @param totalInToken
102
+ * @param balancePrefetch
103
+ * @param signal
104
+ * @throws {DefinedRuntimeError} will throw an error if there are not enough funds in the vault
105
+ */
106
+ async checkBalance(totalInToken, balancePrefetch, signal) {
107
+ const balance = await balancePrefetch;
108
+ if (signal != null)
109
+ signal.throwIfAborted();
110
+ if (balance == null || balance < totalInToken) {
111
+ throw {
112
+ code: 20002,
113
+ msg: "Not enough liquidity"
114
+ };
115
+ }
116
+ }
117
+ /**
118
+ * Checks if the sequence number is between 0-2^64
119
+ *
120
+ * @param sequence
121
+ * @throws {DefinedRuntimeError} will throw an error if sequence number is out of bounds
122
+ */
123
+ checkSequence(sequence) {
124
+ if (sequence < 0n || sequence >= 2n ** 64n) {
125
+ throw {
126
+ code: 20060,
127
+ msg: "Invalid sequence"
128
+ };
129
+ }
130
+ }
131
+ /**
132
+ * Checks whether a given token is supported on a specified chain
133
+ *
134
+ * @param chainId
135
+ * @param token
136
+ * @protected
137
+ */
138
+ isTokenSupported(chainId, token) {
139
+ const chainTokens = this.allowedTokens[chainId];
140
+ if (chainTokens == null)
141
+ return false;
142
+ return chainTokens.has(token);
143
+ }
144
+ getInfo() {
145
+ const chainTokens = {};
146
+ for (let chainId in this.allowedTokens) {
147
+ chainTokens[chainId] = Array.from(this.allowedTokens[chainId]);
148
+ }
149
+ return {
150
+ swapFeePPM: Number(this.config.feePPM),
151
+ swapBaseFee: Number(this.config.baseFee),
152
+ min: Number(this.config.min),
153
+ max: Number(this.config.max),
154
+ data: this.getInfoData(),
155
+ tokens: Array.from(this.allowedTokens[this.chains.default]),
156
+ chainTokens
157
+ };
158
+ }
159
+ getInitAuthorizationTimeout(chainIdentifier) {
160
+ return this.config.initAuthorizationTimeouts?.[chainIdentifier] ?? this.config.initAuthorizationTimeout;
161
+ }
162
+ }
163
+ exports.SwapHandler = SwapHandler;