@atomiqlabs/sdk 8.7.7 → 8.8.4

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 (339) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +1760 -1760
  3. package/dist/SmartChainAssets.d.ts +181 -181
  4. package/dist/SmartChainAssets.js +181 -181
  5. package/dist/bitcoin/coinselect2/accumulative.d.ts +7 -6
  6. package/dist/bitcoin/coinselect2/accumulative.js +52 -52
  7. package/dist/bitcoin/coinselect2/blackjack.d.ts +7 -6
  8. package/dist/bitcoin/coinselect2/blackjack.js +38 -38
  9. package/dist/bitcoin/coinselect2/index.d.ts +20 -19
  10. package/dist/bitcoin/coinselect2/index.js +69 -69
  11. package/dist/bitcoin/coinselect2/utils.d.ts +82 -77
  12. package/dist/bitcoin/coinselect2/utils.js +158 -123
  13. package/dist/bitcoin/wallet/BitcoinWallet.d.ts +113 -130
  14. package/dist/bitcoin/wallet/BitcoinWallet.js +335 -322
  15. package/dist/bitcoin/wallet/IBitcoinWallet.d.ts +116 -78
  16. package/dist/bitcoin/wallet/IBitcoinWallet.js +21 -21
  17. package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.d.ts +106 -101
  18. package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.js +196 -190
  19. package/dist/enums/FeeType.d.ts +15 -15
  20. package/dist/enums/FeeType.js +19 -19
  21. package/dist/enums/SwapAmountType.d.ts +15 -15
  22. package/dist/enums/SwapAmountType.js +19 -19
  23. package/dist/enums/SwapDirection.d.ts +15 -15
  24. package/dist/enums/SwapDirection.js +19 -19
  25. package/dist/enums/SwapSide.d.ts +15 -15
  26. package/dist/enums/SwapSide.js +19 -19
  27. package/dist/enums/SwapType.d.ts +75 -75
  28. package/dist/enums/SwapType.js +79 -79
  29. package/dist/errors/IntermediaryError.d.ts +13 -13
  30. package/dist/errors/IntermediaryError.js +27 -27
  31. package/dist/errors/RequestError.d.ts +32 -32
  32. package/dist/errors/RequestError.js +54 -54
  33. package/dist/errors/UserError.d.ts +8 -8
  34. package/dist/errors/UserError.js +16 -16
  35. package/dist/events/UnifiedSwapEventListener.d.ts +23 -23
  36. package/dist/events/UnifiedSwapEventListener.js +132 -132
  37. package/dist/http/HttpUtils.d.ts +27 -27
  38. package/dist/http/HttpUtils.js +91 -91
  39. package/dist/http/paramcoders/IParamReader.d.ts +8 -8
  40. package/dist/http/paramcoders/IParamReader.js +2 -2
  41. package/dist/http/paramcoders/ParamDecoder.d.ts +44 -44
  42. package/dist/http/paramcoders/ParamDecoder.js +137 -137
  43. package/dist/http/paramcoders/ParamEncoder.d.ts +20 -20
  44. package/dist/http/paramcoders/ParamEncoder.js +36 -36
  45. package/dist/http/paramcoders/SchemaVerifier.d.ts +26 -26
  46. package/dist/http/paramcoders/SchemaVerifier.js +145 -145
  47. package/dist/http/paramcoders/client/ResponseParamDecoder.d.ts +11 -11
  48. package/dist/http/paramcoders/client/ResponseParamDecoder.js +57 -57
  49. package/dist/http/paramcoders/client/StreamParamEncoder.d.ts +13 -13
  50. package/dist/http/paramcoders/client/StreamParamEncoder.js +26 -26
  51. package/dist/http/paramcoders/client/StreamingFetchPromise.d.ts +16 -16
  52. package/dist/http/paramcoders/client/StreamingFetchPromise.js +174 -174
  53. package/dist/index.d.ts +85 -85
  54. package/dist/index.js +158 -158
  55. package/dist/intermediaries/Intermediary.d.ts +178 -178
  56. package/dist/intermediaries/Intermediary.js +166 -166
  57. package/dist/intermediaries/IntermediaryDiscovery.d.ts +211 -211
  58. package/dist/intermediaries/IntermediaryDiscovery.js +424 -424
  59. package/dist/intermediaries/apis/IntermediaryAPI.d.ts +450 -440
  60. package/dist/intermediaries/apis/IntermediaryAPI.js +618 -603
  61. package/dist/intermediaries/apis/TrustedIntermediaryAPI.d.ts +155 -155
  62. package/dist/intermediaries/apis/TrustedIntermediaryAPI.js +137 -137
  63. package/dist/lnurl/LNURL.d.ts +102 -102
  64. package/dist/lnurl/LNURL.js +321 -321
  65. package/dist/prices/RedundantSwapPrice.d.ts +110 -110
  66. package/dist/prices/RedundantSwapPrice.js +222 -222
  67. package/dist/prices/SingleSwapPrice.d.ts +34 -34
  68. package/dist/prices/SingleSwapPrice.js +44 -44
  69. package/dist/prices/SwapPriceWithChain.d.ts +107 -107
  70. package/dist/prices/SwapPriceWithChain.js +128 -128
  71. package/dist/prices/abstract/ICachedSwapPrice.d.ts +28 -28
  72. package/dist/prices/abstract/ICachedSwapPrice.js +62 -62
  73. package/dist/prices/abstract/IPriceProvider.d.ts +81 -81
  74. package/dist/prices/abstract/IPriceProvider.js +74 -74
  75. package/dist/prices/abstract/ISwapPrice.d.ts +168 -168
  76. package/dist/prices/abstract/ISwapPrice.js +279 -279
  77. package/dist/prices/providers/BinancePriceProvider.d.ts +23 -23
  78. package/dist/prices/providers/BinancePriceProvider.js +30 -30
  79. package/dist/prices/providers/CoinGeckoPriceProvider.d.ts +23 -23
  80. package/dist/prices/providers/CoinGeckoPriceProvider.js +29 -29
  81. package/dist/prices/providers/CoinPaprikaPriceProvider.d.ts +25 -25
  82. package/dist/prices/providers/CoinPaprikaPriceProvider.js +29 -29
  83. package/dist/prices/providers/CustomPriceProvider.d.ts +24 -24
  84. package/dist/prices/providers/CustomPriceProvider.js +35 -35
  85. package/dist/prices/providers/KrakenPriceProvider.d.ts +38 -38
  86. package/dist/prices/providers/KrakenPriceProvider.js +45 -45
  87. package/dist/prices/providers/OKXPriceProvider.d.ts +34 -34
  88. package/dist/prices/providers/OKXPriceProvider.js +29 -29
  89. package/dist/prices/providers/abstract/ExchangePriceProvider.d.ts +17 -17
  90. package/dist/prices/providers/abstract/ExchangePriceProvider.js +21 -21
  91. package/dist/prices/providers/abstract/HttpPriceProvider.d.ts +7 -7
  92. package/dist/prices/providers/abstract/HttpPriceProvider.js +12 -12
  93. package/dist/storage/IUnifiedStorage.d.ts +85 -85
  94. package/dist/storage/IUnifiedStorage.js +2 -2
  95. package/dist/storage/UnifiedSwapStorage.d.ts +114 -114
  96. package/dist/storage/UnifiedSwapStorage.js +116 -116
  97. package/dist/storage-browser/IndexedDBUnifiedStorage.d.ts +63 -63
  98. package/dist/storage-browser/IndexedDBUnifiedStorage.js +298 -298
  99. package/dist/storage-browser/LocalStorageManager.d.ts +49 -49
  100. package/dist/storage-browser/LocalStorageManager.js +93 -93
  101. package/dist/swapper/Swapper.d.ts +732 -692
  102. package/dist/swapper/Swapper.js +1713 -1657
  103. package/dist/swapper/SwapperFactory.d.ts +135 -135
  104. package/dist/swapper/SwapperFactory.js +162 -162
  105. package/dist/swapper/SwapperUtils.d.ts +206 -206
  106. package/dist/swapper/SwapperUtils.js +481 -481
  107. package/dist/swapper/SwapperWithChain.d.ts +404 -404
  108. package/dist/swapper/SwapperWithChain.js +469 -469
  109. package/dist/swapper/SwapperWithSigner.d.ts +322 -322
  110. package/dist/swapper/SwapperWithSigner.js +318 -318
  111. package/dist/swaps/IAddressSwap.d.ts +22 -22
  112. package/dist/swaps/IAddressSwap.js +14 -14
  113. package/dist/swaps/IBTCWalletSwap.d.ts +73 -73
  114. package/dist/swaps/IBTCWalletSwap.js +18 -18
  115. package/dist/swaps/IClaimableSwap.d.ts +49 -49
  116. package/dist/swaps/IClaimableSwap.js +15 -15
  117. package/dist/swaps/IClaimableSwapWrapper.d.ts +15 -15
  118. package/dist/swaps/IClaimableSwapWrapper.js +2 -2
  119. package/dist/swaps/IRefundableSwap.d.ts +43 -43
  120. package/dist/swaps/IRefundableSwap.js +14 -14
  121. package/dist/swaps/ISwap.d.ts +392 -392
  122. package/dist/swaps/ISwap.js +349 -349
  123. package/dist/swaps/ISwapWithGasDrop.d.ts +21 -21
  124. package/dist/swaps/ISwapWithGasDrop.js +12 -12
  125. package/dist/swaps/ISwapWrapper.d.ts +285 -285
  126. package/dist/swaps/ISwapWrapper.js +353 -353
  127. package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.d.ts +98 -98
  128. package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.js +126 -126
  129. package/dist/swaps/escrow_swaps/IEscrowSwap.d.ts +139 -139
  130. package/dist/swaps/escrow_swaps/IEscrowSwap.js +170 -170
  131. package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.d.ts +128 -128
  132. package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.js +167 -167
  133. package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.d.ts +105 -105
  134. package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.js +129 -129
  135. package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.d.ts +162 -162
  136. package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.js +190 -190
  137. package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.d.ts +64 -64
  138. package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.js +82 -82
  139. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.d.ts +531 -531
  140. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +1285 -1285
  141. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +190 -190
  142. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +432 -432
  143. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.d.ts +583 -583
  144. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +1371 -1371
  145. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +235 -235
  146. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +525 -525
  147. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.d.ts +458 -458
  148. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +1126 -1126
  149. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.d.ts +202 -202
  150. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.js +406 -406
  151. package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.d.ts +403 -403
  152. package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +924 -924
  153. package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.d.ts +68 -68
  154. package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.js +117 -117
  155. package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.d.ts +127 -127
  156. package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.js +256 -256
  157. package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.d.ts +251 -251
  158. package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.js +536 -536
  159. package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.d.ts +73 -73
  160. package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.js +155 -155
  161. package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.d.ts +132 -132
  162. package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.js +286 -286
  163. package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +637 -631
  164. package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +1448 -1444
  165. package/dist/swaps/spv_swaps/SpvFromBTCWrapper.d.ts +257 -225
  166. package/dist/swaps/spv_swaps/SpvFromBTCWrapper.js +947 -822
  167. package/dist/swaps/trusted/ln/LnForGasSwap.d.ts +261 -261
  168. package/dist/swaps/trusted/ln/LnForGasSwap.js +511 -511
  169. package/dist/swaps/trusted/ln/LnForGasWrapper.d.ts +40 -40
  170. package/dist/swaps/trusted/ln/LnForGasWrapper.js +83 -83
  171. package/dist/swaps/trusted/onchain/OnchainForGasSwap.d.ts +342 -342
  172. package/dist/swaps/trusted/onchain/OnchainForGasSwap.js +715 -715
  173. package/dist/swaps/trusted/onchain/OnchainForGasWrapper.d.ts +69 -69
  174. package/dist/swaps/trusted/onchain/OnchainForGasWrapper.js +93 -93
  175. package/dist/types/AmountData.d.ts +10 -10
  176. package/dist/types/AmountData.js +2 -2
  177. package/dist/types/CustomPriceFunction.d.ts +11 -11
  178. package/dist/types/CustomPriceFunction.js +2 -2
  179. package/dist/types/PriceInfoType.d.ts +28 -28
  180. package/dist/types/PriceInfoType.js +57 -57
  181. package/dist/types/SwapExecutionAction.d.ts +88 -88
  182. package/dist/types/SwapExecutionAction.js +2 -2
  183. package/dist/types/SwapStateInfo.d.ts +5 -5
  184. package/dist/types/SwapStateInfo.js +2 -2
  185. package/dist/types/SwapWithSigner.d.ts +17 -17
  186. package/dist/types/SwapWithSigner.js +43 -43
  187. package/dist/types/Token.d.ts +99 -99
  188. package/dist/types/Token.js +76 -76
  189. package/dist/types/TokenAmount.d.ts +69 -69
  190. package/dist/types/TokenAmount.js +60 -60
  191. package/dist/types/fees/Fee.d.ts +50 -50
  192. package/dist/types/fees/Fee.js +2 -2
  193. package/dist/types/fees/FeeBreakdown.d.ts +11 -11
  194. package/dist/types/fees/FeeBreakdown.js +2 -2
  195. package/dist/types/fees/PercentagePPM.d.ts +17 -17
  196. package/dist/types/fees/PercentagePPM.js +18 -18
  197. package/dist/types/lnurl/LNURLPay.d.ts +61 -61
  198. package/dist/types/lnurl/LNURLPay.js +31 -31
  199. package/dist/types/lnurl/LNURLWithdraw.d.ts +48 -48
  200. package/dist/types/lnurl/LNURLWithdraw.js +27 -27
  201. package/dist/types/wallets/LightningInvoiceCreateService.d.ts +24 -24
  202. package/dist/types/wallets/LightningInvoiceCreateService.js +15 -15
  203. package/dist/types/wallets/MinimalBitcoinWalletInterface.d.ts +23 -23
  204. package/dist/types/wallets/MinimalBitcoinWalletInterface.js +2 -2
  205. package/dist/types/wallets/MinimalLightningNetworkWalletInterface.d.ts +9 -9
  206. package/dist/types/wallets/MinimalLightningNetworkWalletInterface.js +2 -2
  207. package/dist/utils/AutomaticClockDriftCorrection.d.ts +1 -1
  208. package/dist/utils/AutomaticClockDriftCorrection.js +70 -70
  209. package/dist/utils/BitcoinUtils.d.ts +16 -14
  210. package/dist/utils/BitcoinUtils.js +141 -102
  211. package/dist/utils/BitcoinWalletUtils.d.ts +7 -7
  212. package/dist/utils/BitcoinWalletUtils.js +14 -14
  213. package/dist/utils/Logger.d.ts +7 -7
  214. package/dist/utils/Logger.js +12 -12
  215. package/dist/utils/RetryUtils.d.ts +22 -22
  216. package/dist/utils/RetryUtils.js +67 -67
  217. package/dist/utils/SwapUtils.d.ts +88 -88
  218. package/dist/utils/SwapUtils.js +72 -72
  219. package/dist/utils/TimeoutUtils.d.ts +17 -17
  220. package/dist/utils/TimeoutUtils.js +55 -55
  221. package/dist/utils/TokenUtils.d.ts +19 -19
  222. package/dist/utils/TokenUtils.js +37 -37
  223. package/dist/utils/TypeUtils.d.ts +7 -7
  224. package/dist/utils/TypeUtils.js +2 -2
  225. package/dist/utils/Utils.d.ts +67 -67
  226. package/dist/utils/Utils.js +208 -208
  227. package/package.json +43 -43
  228. package/src/SmartChainAssets.ts +186 -186
  229. package/src/bitcoin/coinselect2/accumulative.ts +69 -68
  230. package/src/bitcoin/coinselect2/blackjack.ts +50 -49
  231. package/src/bitcoin/coinselect2/index.ts +93 -92
  232. package/src/bitcoin/coinselect2/utils.ts +236 -195
  233. package/src/bitcoin/wallet/BitcoinWallet.ts +439 -427
  234. package/src/bitcoin/wallet/IBitcoinWallet.ts +140 -99
  235. package/src/bitcoin/wallet/SingleAddressBitcoinWallet.ts +225 -217
  236. package/src/enums/FeeType.ts +15 -15
  237. package/src/enums/SwapAmountType.ts +16 -16
  238. package/src/enums/SwapDirection.ts +15 -15
  239. package/src/enums/SwapSide.ts +16 -16
  240. package/src/enums/SwapType.ts +75 -75
  241. package/src/errors/IntermediaryError.ts +28 -28
  242. package/src/errors/RequestError.ts +64 -64
  243. package/src/errors/UserError.ts +15 -15
  244. package/src/events/UnifiedSwapEventListener.ts +173 -173
  245. package/src/http/HttpUtils.ts +91 -91
  246. package/src/http/paramcoders/IParamReader.ts +9 -9
  247. package/src/http/paramcoders/ParamDecoder.ts +145 -145
  248. package/src/http/paramcoders/ParamEncoder.ts +40 -40
  249. package/src/http/paramcoders/SchemaVerifier.ts +153 -153
  250. package/src/http/paramcoders/client/ResponseParamDecoder.ts +57 -57
  251. package/src/http/paramcoders/client/StreamParamEncoder.ts +28 -28
  252. package/src/http/paramcoders/client/StreamingFetchPromise.ts +192 -192
  253. package/src/index.ts +140 -140
  254. package/src/intermediaries/Intermediary.ts +280 -280
  255. package/src/intermediaries/IntermediaryDiscovery.ts +541 -541
  256. package/src/intermediaries/apis/IntermediaryAPI.ts +963 -947
  257. package/src/intermediaries/apis/TrustedIntermediaryAPI.ts +257 -257
  258. package/src/lnurl/LNURL.ts +402 -402
  259. package/src/prices/RedundantSwapPrice.ts +264 -264
  260. package/src/prices/SingleSwapPrice.ts +50 -50
  261. package/src/prices/SwapPriceWithChain.ts +194 -194
  262. package/src/prices/abstract/ICachedSwapPrice.ts +85 -85
  263. package/src/prices/abstract/IPriceProvider.ts +127 -127
  264. package/src/prices/abstract/ISwapPrice.ts +390 -390
  265. package/src/prices/providers/BinancePriceProvider.ts +48 -48
  266. package/src/prices/providers/CoinGeckoPriceProvider.ts +46 -46
  267. package/src/prices/providers/CoinPaprikaPriceProvider.ts +49 -49
  268. package/src/prices/providers/CustomPriceProvider.ts +40 -40
  269. package/src/prices/providers/KrakenPriceProvider.ts +83 -83
  270. package/src/prices/providers/OKXPriceProvider.ts +59 -59
  271. package/src/prices/providers/abstract/ExchangePriceProvider.ts +31 -31
  272. package/src/prices/providers/abstract/HttpPriceProvider.ts +14 -14
  273. package/src/storage/IUnifiedStorage.ts +95 -95
  274. package/src/storage/UnifiedSwapStorage.ts +141 -141
  275. package/src/storage-browser/IndexedDBUnifiedStorage.ts +350 -350
  276. package/src/storage-browser/LocalStorageManager.ts +106 -106
  277. package/src/swapper/Swapper.ts +2488 -2416
  278. package/src/swapper/SwapperFactory.ts +307 -307
  279. package/src/swapper/SwapperUtils.ts +570 -570
  280. package/src/swapper/SwapperWithChain.ts +707 -707
  281. package/src/swapper/SwapperWithSigner.ts +511 -511
  282. package/src/swaps/IAddressSwap.ts +30 -30
  283. package/src/swaps/IBTCWalletSwap.ts +92 -92
  284. package/src/swaps/IClaimableSwap.ts +65 -65
  285. package/src/swaps/IClaimableSwapWrapper.ts +17 -17
  286. package/src/swaps/IRefundableSwap.ts +58 -58
  287. package/src/swaps/ISwap.ts +703 -703
  288. package/src/swaps/ISwapWithGasDrop.ts +25 -25
  289. package/src/swaps/ISwapWrapper.ts +539 -539
  290. package/src/swaps/escrow_swaps/IEscrowSelfInitSwap.ts +217 -217
  291. package/src/swaps/escrow_swaps/IEscrowSwap.ts +269 -269
  292. package/src/swaps/escrow_swaps/IEscrowSwapWrapper.ts +282 -282
  293. package/src/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.ts +169 -169
  294. package/src/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.ts +300 -300
  295. package/src/swaps/escrow_swaps/frombtc/IFromBTCWrapper.ts +107 -107
  296. package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.ts +1473 -1474
  297. package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +601 -601
  298. package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +1582 -1582
  299. package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +750 -750
  300. package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.ts +1299 -1299
  301. package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.ts +610 -610
  302. package/src/swaps/escrow_swaps/tobtc/IToBTCSwap.ts +1096 -1096
  303. package/src/swaps/escrow_swaps/tobtc/IToBTCWrapper.ts +138 -138
  304. package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.ts +304 -304
  305. package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.ts +786 -786
  306. package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.ts +206 -206
  307. package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.ts +401 -401
  308. package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +1812 -1799
  309. package/src/swaps/spv_swaps/SpvFromBTCWrapper.ts +1236 -1060
  310. package/src/swaps/trusted/ln/LnForGasSwap.ts +589 -589
  311. package/src/swaps/trusted/ln/LnForGasWrapper.ts +91 -91
  312. package/src/swaps/trusted/onchain/OnchainForGasSwap.ts +862 -862
  313. package/src/swaps/trusted/onchain/OnchainForGasWrapper.ts +131 -131
  314. package/src/types/AmountData.ts +9 -9
  315. package/src/types/CustomPriceFunction.ts +11 -11
  316. package/src/types/PriceInfoType.ts +66 -66
  317. package/src/types/SwapExecutionAction.ts +99 -99
  318. package/src/types/SwapStateInfo.ts +6 -6
  319. package/src/types/SwapWithSigner.ts +61 -61
  320. package/src/types/Token.ts +163 -163
  321. package/src/types/TokenAmount.ts +132 -132
  322. package/src/types/fees/Fee.ts +56 -56
  323. package/src/types/fees/FeeBreakdown.ts +11 -11
  324. package/src/types/fees/PercentagePPM.ts +26 -26
  325. package/src/types/lnurl/LNURLPay.ts +79 -79
  326. package/src/types/lnurl/LNURLWithdraw.ts +61 -61
  327. package/src/types/wallets/LightningInvoiceCreateService.ts +30 -30
  328. package/src/types/wallets/MinimalBitcoinWalletInterface.ts +21 -21
  329. package/src/types/wallets/MinimalLightningNetworkWalletInterface.ts +9 -9
  330. package/src/utils/AutomaticClockDriftCorrection.ts +71 -71
  331. package/src/utils/BitcoinUtils.ts +132 -91
  332. package/src/utils/BitcoinWalletUtils.ts +15 -15
  333. package/src/utils/Logger.ts +14 -14
  334. package/src/utils/RetryUtils.ts +78 -78
  335. package/src/utils/SwapUtils.ts +99 -99
  336. package/src/utils/TimeoutUtils.ts +49 -49
  337. package/src/utils/TokenUtils.ts +33 -33
  338. package/src/utils/TypeUtils.ts +8 -8
  339. package/src/utils/Utils.ts +212 -212
@@ -1,540 +1,540 @@
1
- import {ChainEvent, ChainType} from "@atomiqlabs/base";
2
- import {EventEmitter} from "events";
3
- import {ISwap} from "./ISwap";
4
- import {ISwapPrice} from "../prices/abstract/ISwapPrice";
5
- import {IntermediaryError} from "../errors/IntermediaryError";
6
- import {ChainIds, MultiChain} from "../swapper/Swapper";
7
- import {UnifiedSwapEventListener} from "../events/UnifiedSwapEventListener";
8
- import {SwapType} from "../enums/SwapType";
9
- import {UnifiedSwapStorage} from "../storage/UnifiedSwapStorage";
10
- import {SCToken} from "../types/Token";
11
- import {getLogger} from "../utils/Logger";
12
- import {PriceInfoType} from "../types/PriceInfoType";
13
- import {fromHumanReadableString} from "../utils/TokenUtils";
14
- import {UserError} from "../errors/UserError";
15
-
16
- export const DEFAULT_MAX_PARALLEL_SWAP_TICKS = 50;
17
- export const DEFAULT_MAX_PARALLEL_SWAP_SYNCS = 50;
18
-
19
- /**
20
- * Options for swap wrapper configuration
21
- *
22
- * @category Swaps/Base
23
- */
24
- export type ISwapWrapperOptions = {
25
- getRequestTimeout?: number,
26
- postRequestTimeout?: number,
27
- /**
28
- * How many swaps to call `_tick()` for in parallel
29
- */
30
- maxParallelSwapTicks?: number,
31
- /**
32
- * How many swaps to call `_sync()` for in parallel
33
- */
34
- maxParallelSwapSyncs?: number,
35
- /**
36
- * Whether to save swaps that are not initialized into the persistent storage
37
- */
38
- saveUninitializedSwaps?: boolean
39
- };
40
-
41
- /**
42
- * Token configuration for wrapper constructors
43
- *
44
- * @category Swaps/Base
45
- */
46
- export type WrapperCtorTokens<T extends ChainType = ChainType> = {
47
- [tokenAddress: string]: SCToken<T["ChainId"]>
48
- };
49
-
50
- /**
51
- * Type definition linking wrapper and swap types
52
- *
53
- * @category Swaps/Base
54
- */
55
- export type SwapTypeDefinition<T extends ChainType, W extends ISwapWrapper<T, any>, S extends ISwap<T>> = {
56
- Wrapper: W;
57
- Swap: S;
58
- };
59
-
60
- /**
61
- * Base abstract class for swap handler implementations
62
- *
63
- * @category Swaps/Base
64
- */
65
- export abstract class ISwapWrapper<
66
- T extends ChainType,
67
- D extends SwapTypeDefinition<T, ISwapWrapper<T, D>, ISwap<T, D>>,
68
- O extends ISwapWrapperOptions = ISwapWrapperOptions
69
- > {
70
- /**
71
- * Swap type
72
- */
73
- abstract readonly TYPE: SwapType;
74
- /**
75
- * Function for deserializing swaps
76
- * @internal
77
- */
78
- abstract readonly _swapDeserializer: new (wrapper: D["Wrapper"], data: any) => D["Swap"];
79
-
80
-
81
- /**
82
- * Logger instance
83
- * @internal
84
- */
85
- protected readonly logger = getLogger(this.constructor.name+": ");
86
- /**
87
- * Persistent storage backend for the swaps
88
- * @internal
89
- */
90
- protected readonly unifiedStorage: UnifiedSwapStorage<T>;
91
- /**
92
- * Smart chain events listener for listening to and parsing of on-chain events
93
- * @internal
94
- */
95
- protected readonly unifiedChainEvents: UnifiedSwapEventListener<T>;
96
- /**
97
- * States of the swaps where {@link ISwap._tick} should be called every second
98
- * @internal
99
- */
100
- protected readonly tickSwapState?: Array<D["Swap"]["_state"]>;
101
- /**
102
- * In-memory mapping of pending (not initiated) swaps, utilizing weak references to automatically
103
- * free memory when swaps are dereferenced in not initiated state
104
- * @internal
105
- */
106
- protected readonly pendingSwaps: Map<string, WeakRef<D["Swap"]>> = new Map();
107
-
108
-
109
- /**
110
- * Whether this wrapper is initialized (have to call {@link init} to initialize a wrapper)
111
- * @internal
112
- */
113
- protected isInitialized: boolean = false;
114
- /**
115
- * An interval for calling tick functions on the underlying swaps
116
- * @internal
117
- */
118
- protected tickInterval?: NodeJS.Timeout;
119
- /**
120
- * An internal abort controller for the running tick handler
121
- * @internal
122
- */
123
- protected tickAbortController?: AbortController;
124
-
125
-
126
- /**
127
- * States of the swaps in pending (non-final state), these are checked automatically on initial swap synchronization
128
- * @internal
129
- */
130
- abstract readonly _pendingSwapStates: Array<D["Swap"]["_state"]>;
131
- /**
132
- * Chain interface of the underlying smart chain
133
- * @internal
134
- */
135
- readonly _chain: T["ChainInterface"];
136
- /**
137
- * Pricing API
138
- * @internal
139
- */
140
- readonly _prices: ISwapPrice;
141
- /**
142
- * Wrapper options
143
- * @internal
144
- */
145
- readonly _options: O;
146
- /**
147
- * Tokens indexed by their token address
148
- * @internal
149
- */
150
- readonly _tokens: {
151
- [tokenAddress: string]: SCToken<T["ChainId"]>
152
- };
153
-
154
-
155
- /**
156
- * Chain identifier string of this wrapper
157
- */
158
- readonly chainIdentifier: T["ChainId"];
159
- /**
160
- * Event emitter emitting `"swapState"` event when swap's state changes
161
- */
162
- readonly events: EventEmitter<{swapState: [D["Swap"]]}>;
163
-
164
-
165
- constructor(
166
- chainIdentifier: T["ChainId"],
167
- unifiedStorage: UnifiedSwapStorage<T>,
168
- unifiedChainEvents: UnifiedSwapEventListener<T>,
169
- chain: T["ChainInterface"],
170
- prices: ISwapPrice,
171
- tokens: WrapperCtorTokens,
172
- options: O,
173
- events?: EventEmitter<{swapState: [ISwap]}>
174
- ) {
175
- if(options?.maxParallelSwapTicks!=null && options.maxParallelSwapTicks < 1)
176
- throw new Error("maxParallelSwapTicks must be at least 1!");
177
- if(options?.maxParallelSwapSyncs!=null && options.maxParallelSwapSyncs < 1)
178
- throw new Error("maxParallelSwapSyncs must be at least 1!");
179
-
180
- this.unifiedStorage = unifiedStorage;
181
- this.unifiedChainEvents = unifiedChainEvents;
182
-
183
- this.chainIdentifier = chainIdentifier;
184
- this._chain = chain;
185
- this._prices = prices;
186
- this.events = events || new EventEmitter();
187
- this._options = options;
188
- this._tokens = tokens;
189
- }
190
-
191
- /**
192
- * Parses the provided gas amount from its `string` or `bigint` representation to `bigint` base units.
193
- *
194
- * Defaults to `0n` if no gasAmount is provided
195
- *
196
- * @param gasAmount
197
- * @internal
198
- */
199
- protected parseGasAmount(gasAmount?: string | bigint): bigint {
200
- let result: bigint | undefined | null;
201
- if(typeof(gasAmount)==="string") {
202
- result = fromHumanReadableString(gasAmount, this._getNativeToken());
203
- if(result==null) throw new UserError("Invalid `gasAmount` option provided, not a numerical string!");
204
- } else {
205
- result = gasAmount;
206
- }
207
- return result ?? 0n;
208
- }
209
-
210
- /**
211
- * Pre-fetches swap price for a given swap
212
- *
213
- * @param amountData Amount data
214
- * @param abortSignal Abort signal
215
- * @returns Price of the token in uSats (micro sats)
216
- * @internal
217
- */
218
- protected preFetchPrice(amountData: { token: string }, abortSignal?: AbortSignal): Promise<bigint | undefined> {
219
- return this._prices.preFetchPrice(this.chainIdentifier, amountData.token, abortSignal).catch(e => {
220
- this.logger.error("preFetchPrice.token(): Error: ", e);
221
- return undefined;
222
- });
223
- }
224
-
225
- /**
226
- * Pre-fetches bitcoin's USD price
227
- *
228
- * @param abortSignal Abort signal
229
- * @internal
230
- */
231
- protected preFetchUsdPrice(abortSignal?: AbortSignal): Promise<number | undefined> {
232
- return this._prices.preFetchUsdPrice(abortSignal).catch(e => {
233
- this.logger.error("preFetchPrice.usd(): Error: ", e);
234
- return undefined;
235
- })
236
- }
237
-
238
- /**
239
- * Verifies returned price for swaps
240
- *
241
- * @param lpServiceData Service data for the service in question (TO_BTCLN, TO_BTC, etc.) of the given intermediary
242
- * @param send Whether this is a send (Smart chain -> Bitcoin) or receive (Bitcoin -> Smart chain) swap
243
- * @param amountSats Amount in BTC
244
- * @param amountToken Amount in token
245
- * @param token Token used in the swap
246
- * @param feeData Fee data as returned by the intermediary
247
- * @param pricePrefetchPromise Optional price pre-fetch promise
248
- * @param usdPricePrefetchPromise Optiona USD price pre-fetch promise
249
- * @param abortSignal Abort signal
250
- * @returns Price info object
251
- * @throws {IntermediaryError} if the calculated fee is too high
252
- *
253
- * @internal
254
- */
255
- protected async verifyReturnedPrice(
256
- lpServiceData: {swapBaseFee: number, swapFeePPM: number},
257
- send: boolean,
258
- amountSats: bigint,
259
- amountToken: bigint,
260
- token: string,
261
- feeData: {
262
- networkFee?: bigint,
263
- swapFeeBtc?: bigint
264
- },
265
- pricePrefetchPromise: Promise<bigint | undefined> = Promise.resolve(undefined),
266
- usdPricePrefetchPromise: Promise<number | undefined> = Promise.resolve(undefined),
267
- abortSignal?: AbortSignal
268
- ): Promise<PriceInfoType> {
269
- const swapBaseFee = BigInt(lpServiceData.swapBaseFee);
270
- const swapFeePPM = BigInt(lpServiceData.swapFeePPM);
271
- if(send && feeData.networkFee!=null) amountToken = amountToken - feeData.networkFee;
272
-
273
- const [isValidAmount, usdPrice] = await Promise.all([
274
- send ?
275
- this._prices.isValidAmountSend(this.chainIdentifier, amountSats, swapBaseFee, swapFeePPM, amountToken, token, abortSignal, await pricePrefetchPromise, feeData.swapFeeBtc) :
276
- this._prices.isValidAmountReceive(this.chainIdentifier, amountSats, swapBaseFee, swapFeePPM, amountToken, token, abortSignal, await pricePrefetchPromise, feeData.swapFeeBtc),
277
- usdPricePrefetchPromise.then(value => {
278
- if(value!=null) return value;
279
- return this._prices.preFetchUsdPrice(abortSignal);
280
- })
281
- ]);
282
- if(!isValidAmount.isValid) throw new IntermediaryError("Fee too high");
283
- isValidAmount.realPriceUsdPerBitcoin = usdPrice;
284
-
285
- return isValidAmount;
286
- }
287
-
288
- /**
289
- * Processes a single smart chain on-chain event
290
- *
291
- * @param event Smart chain event to process
292
- * @param swap A swap related to the event
293
- * @internal
294
- */
295
- protected abstract processEvent?(event: ChainEvent<T["Data"]>, swap: D["Swap"]): Promise<void>;
296
-
297
- /**
298
- * Starts the interval calling the {@link ISwap._tick} on all the known swaps in tick-enabled states
299
- * @internal
300
- */
301
- protected startTickInterval(): void {
302
- if(this.tickSwapState==null || this.tickSwapState.length===0) return;
303
- if(this.tickAbortController!=null) this.tickAbortController.abort("New tick interval has been started!");
304
- const abortController = this.tickAbortController = new AbortController();
305
- let run: () => Promise<void>;
306
- run = async () => {
307
- if(!this.isInitialized) return;
308
- await this.tick(undefined, abortController.signal).catch(e => {
309
- if(abortController.signal.aborted) return;
310
- this.logger.warn("startTickInterval(): Tick on swaps failed, error: ", e);
311
- });
312
- if(abortController.signal.aborted) return;
313
- if(!this.isInitialized) return;
314
- this.tickInterval = setTimeout(run, 1000);
315
- }
316
- run();
317
- }
318
-
319
- /**
320
- * Runs checks on passed swaps, syncing their state from on-chain data
321
- *
322
- * @param pastSwaps Swaps to check
323
- * @internal
324
- */
325
- protected async _checkPastSwaps(pastSwaps: D["Swap"][]): Promise<{changedSwaps: D["Swap"][], removeSwaps: D["Swap"][]}> {
326
- const changedSwaps: D["Swap"][] = [];
327
- const removeSwaps: D["Swap"][] = [];
328
-
329
- await Promise.all(pastSwaps.map((swap: D["Swap"]) =>
330
- swap._sync(false).then(changed => {
331
- if(swap.isQuoteExpired()) {
332
- removeSwaps.push(swap);
333
- this.logger.debug("_checkPastSwaps(): Removing expired swap: "+swap.getId());
334
- } else {
335
- if(changed) changedSwaps.push(swap);
336
- }
337
- }).catch(e => this.logger.error("_checkPastSwaps(): Error when checking swap "+swap.getId()+": ", e))
338
- ));
339
-
340
- return {changedSwaps, removeSwaps};
341
- }
342
-
343
-
344
- /**
345
- * Initializes the swap wrapper, needs to be called before any other action can be taken
346
- *
347
- * @param noTimers Whether to skip scheduling a tick timer for the swaps, if the tick timer is not initiated
348
- * the swap states depending on e.g. expiry can be out of sync with the actual expiration of the swap
349
- * @param noCheckPastSwaps Whether to skip checking past swaps on initialization (by default all pending swaps
350
- * are re-checked on init, and their state is synchronized from the on-chain data)
351
- */
352
- public async init(noTimers: boolean = false, noCheckPastSwaps: boolean = false): Promise<void> {
353
- if(this.isInitialized) return;
354
-
355
- if(!noCheckPastSwaps) {
356
- //Save events received in the meantime into the event queue and process them only after we've checked and
357
- // processed all the past swaps
358
- let eventQueue: {
359
- event: ChainEvent<T["Data"]>,
360
- swap: D["Swap"]
361
- }[] = [];
362
- const initListener = (event: ChainEvent<T["Data"]>, swap: D["Swap"]) => {
363
- eventQueue.push({event, swap});
364
- return Promise.resolve();
365
- }
366
- if(this.processEvent!=null) this.unifiedChainEvents.registerListener(this.TYPE, initListener, this._swapDeserializer.bind(null, this));
367
-
368
- await this.checkPastSwaps();
369
-
370
- if(this.processEvent!=null) {
371
- //Process accumulated event queue
372
- for(let event of eventQueue) {
373
- await this.processEvent(event.event, event.swap);
374
- }
375
-
376
- //Unregister the temporary event handler
377
- this.unifiedChainEvents.unregisterListener(this.TYPE);
378
- }
379
- }
380
-
381
- if(this.processEvent!=null) this.unifiedChainEvents.registerListener(this.TYPE, this.processEvent.bind(this), this._swapDeserializer.bind(null, this));
382
-
383
- this.isInitialized = true;
384
-
385
- if(!noTimers) this.startTickInterval();
386
-
387
- // this.logger.info("init(): Swap wrapper initialized");
388
- }
389
-
390
- /**
391
- * Un-subscribes from event listeners on the smart chain, terminates the tick interval and stops this wrapper
392
- */
393
- public async stop() {
394
- this.isInitialized = false;
395
- this.unifiedChainEvents.unregisterListener(this.TYPE);
396
- this.logger.info("stop(): Swap wrapper stopped");
397
- if(this.tickInterval!=null) {
398
- clearTimeout(this.tickInterval);
399
- delete this.tickInterval;
400
- }
401
- if(this.tickAbortController!=null) {
402
- this.tickAbortController.abort("Wrapper instance stopped!");
403
- delete this.tickAbortController;
404
- }
405
- }
406
-
407
- /**
408
- * Runs checks on all the known pending swaps, syncing their state from on-chain data
409
- *
410
- * @remarks Doesn't work properly if you pass non-persisted swaps
411
- *
412
- * @param pastSwaps Optional array of past swaps to check, otherwise all relevant swaps will be fetched
413
- * from the persistent storage
414
- * @param noSave Whether to skip saving the swap changes in the persistent storage
415
- */
416
- public async checkPastSwaps(pastSwaps?: D["Swap"][], noSave?: boolean): Promise<{ removeSwaps: D["Swap"][], changedSwaps: D["Swap"][] }> {
417
- if (pastSwaps == null) pastSwaps = await this.unifiedStorage.query<D["Swap"]>(
418
- [[{key: "type", value: this.TYPE}, {key: "state", value: this._pendingSwapStates}]],
419
- (val: any) => new this._swapDeserializer(this, val)
420
- );
421
-
422
- const maxParallelSyncs = this._options.maxParallelSwapSyncs ?? DEFAULT_MAX_PARALLEL_SWAP_SYNCS;
423
-
424
- const totalRemoveSwaps: D["Swap"][] = [];
425
- const totalChangedSwaps: D["Swap"][] = [];
426
- for(let i=0; i<pastSwaps.length; i+=maxParallelSyncs) {
427
- const {removeSwaps, changedSwaps} = await this._checkPastSwaps(pastSwaps.slice(i, i+maxParallelSyncs));
428
- if (!noSave) {
429
- await this.unifiedStorage.removeAll(removeSwaps);
430
- await this.unifiedStorage.saveAll(changedSwaps);
431
- changedSwaps.forEach(swap => swap._emitEvent());
432
- removeSwaps.forEach(swap => swap._emitEvent());
433
- }
434
- totalRemoveSwaps.push(...removeSwaps);
435
- totalChangedSwaps.push(...changedSwaps);
436
- }
437
-
438
- return {
439
- removeSwaps: totalRemoveSwaps,
440
- changedSwaps: totalChangedSwaps
441
- }
442
- }
443
-
444
- /**
445
- * Invokes {@link ISwap._tick} on all the known swaps
446
- *
447
- * @param swaps Optional array of swaps to invoke `_tick()` on, otherwise all relevant swaps will be fetched
448
- * from the persistent storage
449
- * @param abortSignal Abort signal
450
- */
451
- public async tick(swaps?: D["Swap"][], abortSignal?: AbortSignal): Promise<void> {
452
- if(swaps==null) swaps = await this.unifiedStorage.query<D["Swap"]>(
453
- [[{key: "type", value: this.TYPE}, {key: "state", value: this.tickSwapState}]],
454
- (val: any) => new this._swapDeserializer(this, val)
455
- );
456
- abortSignal?.throwIfAborted();
457
-
458
- const parallelTicks = this._options.maxParallelSwapTicks ?? DEFAULT_MAX_PARALLEL_SWAP_TICKS;
459
-
460
- let promises: Promise<any>[] = [];
461
- for(let pendingSwap of this.pendingSwaps.values()) {
462
- const value = pendingSwap.deref();
463
- if(value != null) promises.push(value._tick(true).catch(e => {
464
- this.logger.warn(`tick(): Error ticking swap ${value.getId()}: `, e);
465
- }));
466
- if(promises.length >= parallelTicks) {
467
- await Promise.all(promises);
468
- abortSignal?.throwIfAborted();
469
- promises = [];
470
- }
471
- }
472
-
473
- for(let value of swaps) {
474
- promises.push(value._tick(true).catch(e => {
475
- this.logger.warn(`tick(): Error ticking swap ${value.getId()}: `, e);
476
- }));
477
- if(promises.length >= parallelTicks) {
478
- await Promise.all(promises);
479
- abortSignal?.throwIfAborted();
480
- promises = [];
481
- }
482
- }
483
-
484
- if(promises.length>0) await Promise.all(promises);
485
- abortSignal?.throwIfAborted();
486
- }
487
-
488
- /**
489
- * Returns the smart chain's native token used to pay for fees
490
- * @internal
491
- */
492
- _getNativeToken(): SCToken<T["ChainId"]> {
493
- return this._tokens[this._chain.getNativeCurrencyAddress()];
494
- }
495
-
496
- /**
497
- * Saves the swap, if it is not initiated it is only saved to pending swaps
498
- *
499
- * @param swap Swap to save
500
- *
501
- * @internal
502
- */
503
- _saveSwapData(swap: D["Swap"]): Promise<void> {
504
- if(!this._options.saveUninitializedSwaps) {
505
- if(!swap.isInitiated()) {
506
- this.logger.debug("saveSwapData(): Swap "+swap.getId()+" not initiated, saving to pending swaps");
507
- this.pendingSwaps.set(swap.getId(), new WeakRef<D["Swap"]>(swap));
508
- return Promise.resolve();
509
- } else {
510
- this.pendingSwaps.delete(swap.getId());
511
- }
512
- }
513
- return this.unifiedStorage.save(swap);
514
- }
515
-
516
- /**
517
- * Removes the swap from the persistent storage and pending swaps
518
- *
519
- * @param swap Swap to remove
520
- *
521
- * @internal
522
- */
523
- _removeSwapData(swap: D["Swap"]): Promise<void> {
524
- this.pendingSwaps.delete(swap.getId());
525
- if(!swap._persisted) return Promise.resolve();
526
- return this.unifiedStorage.remove(swap);
527
- }
528
-
529
- /**
530
- * Retrieves a swap by its ID from the pending swap mapping
531
- *
532
- * @param id
533
- *
534
- * @internal
535
- */
536
- _getPendingSwap(id: string): D["Swap"] | null {
537
- return this.pendingSwaps.get(id)?.deref() ?? null;
538
- }
539
-
1
+ import {ChainEvent, ChainType} from "@atomiqlabs/base";
2
+ import {EventEmitter} from "events";
3
+ import {ISwap} from "./ISwap";
4
+ import {ISwapPrice} from "../prices/abstract/ISwapPrice";
5
+ import {IntermediaryError} from "../errors/IntermediaryError";
6
+ import {ChainIds, MultiChain} from "../swapper/Swapper";
7
+ import {UnifiedSwapEventListener} from "../events/UnifiedSwapEventListener";
8
+ import {SwapType} from "../enums/SwapType";
9
+ import {UnifiedSwapStorage} from "../storage/UnifiedSwapStorage";
10
+ import {SCToken} from "../types/Token";
11
+ import {getLogger} from "../utils/Logger";
12
+ import {PriceInfoType} from "../types/PriceInfoType";
13
+ import {fromHumanReadableString} from "../utils/TokenUtils";
14
+ import {UserError} from "../errors/UserError";
15
+
16
+ export const DEFAULT_MAX_PARALLEL_SWAP_TICKS = 50;
17
+ export const DEFAULT_MAX_PARALLEL_SWAP_SYNCS = 50;
18
+
19
+ /**
20
+ * Options for swap wrapper configuration
21
+ *
22
+ * @category Swaps/Base
23
+ */
24
+ export type ISwapWrapperOptions = {
25
+ getRequestTimeout?: number,
26
+ postRequestTimeout?: number,
27
+ /**
28
+ * How many swaps to call `_tick()` for in parallel
29
+ */
30
+ maxParallelSwapTicks?: number,
31
+ /**
32
+ * How many swaps to call `_sync()` for in parallel
33
+ */
34
+ maxParallelSwapSyncs?: number,
35
+ /**
36
+ * Whether to save swaps that are not initialized into the persistent storage
37
+ */
38
+ saveUninitializedSwaps?: boolean
39
+ };
40
+
41
+ /**
42
+ * Token configuration for wrapper constructors
43
+ *
44
+ * @category Swaps/Base
45
+ */
46
+ export type WrapperCtorTokens<T extends ChainType = ChainType> = {
47
+ [tokenAddress: string]: SCToken<T["ChainId"]>
48
+ };
49
+
50
+ /**
51
+ * Type definition linking wrapper and swap types
52
+ *
53
+ * @category Swaps/Base
54
+ */
55
+ export type SwapTypeDefinition<T extends ChainType, W extends ISwapWrapper<T, any>, S extends ISwap<T>> = {
56
+ Wrapper: W;
57
+ Swap: S;
58
+ };
59
+
60
+ /**
61
+ * Base abstract class for swap handler implementations
62
+ *
63
+ * @category Swaps/Base
64
+ */
65
+ export abstract class ISwapWrapper<
66
+ T extends ChainType,
67
+ D extends SwapTypeDefinition<T, ISwapWrapper<T, D>, ISwap<T, D>>,
68
+ O extends ISwapWrapperOptions = ISwapWrapperOptions
69
+ > {
70
+ /**
71
+ * Swap type
72
+ */
73
+ abstract readonly TYPE: SwapType;
74
+ /**
75
+ * Function for deserializing swaps
76
+ * @internal
77
+ */
78
+ abstract readonly _swapDeserializer: new (wrapper: D["Wrapper"], data: any) => D["Swap"];
79
+
80
+
81
+ /**
82
+ * Logger instance
83
+ * @internal
84
+ */
85
+ protected readonly logger = getLogger(this.constructor.name+": ");
86
+ /**
87
+ * Persistent storage backend for the swaps
88
+ * @internal
89
+ */
90
+ protected readonly unifiedStorage: UnifiedSwapStorage<T>;
91
+ /**
92
+ * Smart chain events listener for listening to and parsing of on-chain events
93
+ * @internal
94
+ */
95
+ protected readonly unifiedChainEvents: UnifiedSwapEventListener<T>;
96
+ /**
97
+ * States of the swaps where {@link ISwap._tick} should be called every second
98
+ * @internal
99
+ */
100
+ protected readonly tickSwapState?: Array<D["Swap"]["_state"]>;
101
+ /**
102
+ * In-memory mapping of pending (not initiated) swaps, utilizing weak references to automatically
103
+ * free memory when swaps are dereferenced in not initiated state
104
+ * @internal
105
+ */
106
+ protected readonly pendingSwaps: Map<string, WeakRef<D["Swap"]>> = new Map();
107
+
108
+
109
+ /**
110
+ * Whether this wrapper is initialized (have to call {@link init} to initialize a wrapper)
111
+ * @internal
112
+ */
113
+ protected isInitialized: boolean = false;
114
+ /**
115
+ * An interval for calling tick functions on the underlying swaps
116
+ * @internal
117
+ */
118
+ protected tickInterval?: NodeJS.Timeout;
119
+ /**
120
+ * An internal abort controller for the running tick handler
121
+ * @internal
122
+ */
123
+ protected tickAbortController?: AbortController;
124
+
125
+
126
+ /**
127
+ * States of the swaps in pending (non-final state), these are checked automatically on initial swap synchronization
128
+ * @internal
129
+ */
130
+ abstract readonly _pendingSwapStates: Array<D["Swap"]["_state"]>;
131
+ /**
132
+ * Chain interface of the underlying smart chain
133
+ * @internal
134
+ */
135
+ readonly _chain: T["ChainInterface"];
136
+ /**
137
+ * Pricing API
138
+ * @internal
139
+ */
140
+ readonly _prices: ISwapPrice;
141
+ /**
142
+ * Wrapper options
143
+ * @internal
144
+ */
145
+ readonly _options: O;
146
+ /**
147
+ * Tokens indexed by their token address
148
+ * @internal
149
+ */
150
+ readonly _tokens: {
151
+ [tokenAddress: string]: SCToken<T["ChainId"]>
152
+ };
153
+
154
+
155
+ /**
156
+ * Chain identifier string of this wrapper
157
+ */
158
+ readonly chainIdentifier: T["ChainId"];
159
+ /**
160
+ * Event emitter emitting `"swapState"` event when swap's state changes
161
+ */
162
+ readonly events: EventEmitter<{swapState: [D["Swap"]]}>;
163
+
164
+
165
+ constructor(
166
+ chainIdentifier: T["ChainId"],
167
+ unifiedStorage: UnifiedSwapStorage<T>,
168
+ unifiedChainEvents: UnifiedSwapEventListener<T>,
169
+ chain: T["ChainInterface"],
170
+ prices: ISwapPrice,
171
+ tokens: WrapperCtorTokens,
172
+ options: O,
173
+ events?: EventEmitter<{swapState: [ISwap]}>
174
+ ) {
175
+ if(options?.maxParallelSwapTicks!=null && options.maxParallelSwapTicks < 1)
176
+ throw new Error("maxParallelSwapTicks must be at least 1!");
177
+ if(options?.maxParallelSwapSyncs!=null && options.maxParallelSwapSyncs < 1)
178
+ throw new Error("maxParallelSwapSyncs must be at least 1!");
179
+
180
+ this.unifiedStorage = unifiedStorage;
181
+ this.unifiedChainEvents = unifiedChainEvents;
182
+
183
+ this.chainIdentifier = chainIdentifier;
184
+ this._chain = chain;
185
+ this._prices = prices;
186
+ this.events = events || new EventEmitter();
187
+ this._options = options;
188
+ this._tokens = tokens;
189
+ }
190
+
191
+ /**
192
+ * Parses the provided gas amount from its `string` or `bigint` representation to `bigint` base units.
193
+ *
194
+ * Defaults to `0n` if no gasAmount is provided
195
+ *
196
+ * @param gasAmount
197
+ * @internal
198
+ */
199
+ protected parseGasAmount(gasAmount?: string | bigint): bigint {
200
+ let result: bigint | undefined | null;
201
+ if(typeof(gasAmount)==="string") {
202
+ result = fromHumanReadableString(gasAmount, this._getNativeToken());
203
+ if(result==null) throw new UserError("Invalid `gasAmount` option provided, not a numerical string!");
204
+ } else {
205
+ result = gasAmount;
206
+ }
207
+ return result ?? 0n;
208
+ }
209
+
210
+ /**
211
+ * Pre-fetches swap price for a given swap
212
+ *
213
+ * @param amountData Amount data
214
+ * @param abortSignal Abort signal
215
+ * @returns Price of the token in uSats (micro sats)
216
+ * @internal
217
+ */
218
+ protected preFetchPrice(amountData: { token: string }, abortSignal?: AbortSignal): Promise<bigint | undefined> {
219
+ return this._prices.preFetchPrice(this.chainIdentifier, amountData.token, abortSignal).catch(e => {
220
+ this.logger.error("preFetchPrice.token(): Error: ", e);
221
+ return undefined;
222
+ });
223
+ }
224
+
225
+ /**
226
+ * Pre-fetches bitcoin's USD price
227
+ *
228
+ * @param abortSignal Abort signal
229
+ * @internal
230
+ */
231
+ protected preFetchUsdPrice(abortSignal?: AbortSignal): Promise<number | undefined> {
232
+ return this._prices.preFetchUsdPrice(abortSignal).catch(e => {
233
+ this.logger.error("preFetchPrice.usd(): Error: ", e);
234
+ return undefined;
235
+ })
236
+ }
237
+
238
+ /**
239
+ * Verifies returned price for swaps
240
+ *
241
+ * @param lpServiceData Service data for the service in question (TO_BTCLN, TO_BTC, etc.) of the given intermediary
242
+ * @param send Whether this is a send (Smart chain -> Bitcoin) or receive (Bitcoin -> Smart chain) swap
243
+ * @param amountSats Amount in BTC
244
+ * @param amountToken Amount in token
245
+ * @param token Token used in the swap
246
+ * @param feeData Fee data as returned by the intermediary
247
+ * @param pricePrefetchPromise Optional price pre-fetch promise
248
+ * @param usdPricePrefetchPromise Optiona USD price pre-fetch promise
249
+ * @param abortSignal Abort signal
250
+ * @returns Price info object
251
+ * @throws {IntermediaryError} if the calculated fee is too high
252
+ *
253
+ * @internal
254
+ */
255
+ protected async verifyReturnedPrice(
256
+ lpServiceData: {swapBaseFee: number, swapFeePPM: number},
257
+ send: boolean,
258
+ amountSats: bigint,
259
+ amountToken: bigint,
260
+ token: string,
261
+ feeData: {
262
+ networkFee?: bigint,
263
+ swapFeeBtc?: bigint
264
+ },
265
+ pricePrefetchPromise: Promise<bigint | undefined> = Promise.resolve(undefined),
266
+ usdPricePrefetchPromise: Promise<number | undefined> = Promise.resolve(undefined),
267
+ abortSignal?: AbortSignal
268
+ ): Promise<PriceInfoType> {
269
+ const swapBaseFee = BigInt(lpServiceData.swapBaseFee);
270
+ const swapFeePPM = BigInt(lpServiceData.swapFeePPM);
271
+ if(send && feeData.networkFee!=null) amountToken = amountToken - feeData.networkFee;
272
+
273
+ const [isValidAmount, usdPrice] = await Promise.all([
274
+ send ?
275
+ this._prices.isValidAmountSend(this.chainIdentifier, amountSats, swapBaseFee, swapFeePPM, amountToken, token, abortSignal, await pricePrefetchPromise, feeData.swapFeeBtc) :
276
+ this._prices.isValidAmountReceive(this.chainIdentifier, amountSats, swapBaseFee, swapFeePPM, amountToken, token, abortSignal, await pricePrefetchPromise, feeData.swapFeeBtc),
277
+ usdPricePrefetchPromise.then(value => {
278
+ if(value!=null) return value;
279
+ return this._prices.preFetchUsdPrice(abortSignal);
280
+ })
281
+ ]);
282
+ if(!isValidAmount.isValid) throw new IntermediaryError("Fee too high");
283
+ isValidAmount.realPriceUsdPerBitcoin = usdPrice;
284
+
285
+ return isValidAmount;
286
+ }
287
+
288
+ /**
289
+ * Processes a single smart chain on-chain event
290
+ *
291
+ * @param event Smart chain event to process
292
+ * @param swap A swap related to the event
293
+ * @internal
294
+ */
295
+ protected abstract processEvent?(event: ChainEvent<T["Data"]>, swap: D["Swap"]): Promise<void>;
296
+
297
+ /**
298
+ * Starts the interval calling the {@link ISwap._tick} on all the known swaps in tick-enabled states
299
+ * @internal
300
+ */
301
+ protected startTickInterval(): void {
302
+ if(this.tickSwapState==null || this.tickSwapState.length===0) return;
303
+ if(this.tickAbortController!=null) this.tickAbortController.abort("New tick interval has been started!");
304
+ const abortController = this.tickAbortController = new AbortController();
305
+ let run: () => Promise<void>;
306
+ run = async () => {
307
+ if(!this.isInitialized) return;
308
+ await this.tick(undefined, abortController.signal).catch(e => {
309
+ if(abortController.signal.aborted) return;
310
+ this.logger.warn("startTickInterval(): Tick on swaps failed, error: ", e);
311
+ });
312
+ if(abortController.signal.aborted) return;
313
+ if(!this.isInitialized) return;
314
+ this.tickInterval = setTimeout(run, 1000);
315
+ }
316
+ run();
317
+ }
318
+
319
+ /**
320
+ * Runs checks on passed swaps, syncing their state from on-chain data
321
+ *
322
+ * @param pastSwaps Swaps to check
323
+ * @internal
324
+ */
325
+ protected async _checkPastSwaps(pastSwaps: D["Swap"][]): Promise<{changedSwaps: D["Swap"][], removeSwaps: D["Swap"][]}> {
326
+ const changedSwaps: D["Swap"][] = [];
327
+ const removeSwaps: D["Swap"][] = [];
328
+
329
+ await Promise.all(pastSwaps.map((swap: D["Swap"]) =>
330
+ swap._sync(false).then(changed => {
331
+ if(swap.isQuoteExpired()) {
332
+ removeSwaps.push(swap);
333
+ this.logger.debug("_checkPastSwaps(): Removing expired swap: "+swap.getId());
334
+ } else {
335
+ if(changed) changedSwaps.push(swap);
336
+ }
337
+ }).catch(e => this.logger.error("_checkPastSwaps(): Error when checking swap "+swap.getId()+": ", e))
338
+ ));
339
+
340
+ return {changedSwaps, removeSwaps};
341
+ }
342
+
343
+
344
+ /**
345
+ * Initializes the swap wrapper, needs to be called before any other action can be taken
346
+ *
347
+ * @param noTimers Whether to skip scheduling a tick timer for the swaps, if the tick timer is not initiated
348
+ * the swap states depending on e.g. expiry can be out of sync with the actual expiration of the swap
349
+ * @param noCheckPastSwaps Whether to skip checking past swaps on initialization (by default all pending swaps
350
+ * are re-checked on init, and their state is synchronized from the on-chain data)
351
+ */
352
+ public async init(noTimers: boolean = false, noCheckPastSwaps: boolean = false): Promise<void> {
353
+ if(this.isInitialized) return;
354
+
355
+ if(!noCheckPastSwaps) {
356
+ //Save events received in the meantime into the event queue and process them only after we've checked and
357
+ // processed all the past swaps
358
+ let eventQueue: {
359
+ event: ChainEvent<T["Data"]>,
360
+ swap: D["Swap"]
361
+ }[] = [];
362
+ const initListener = (event: ChainEvent<T["Data"]>, swap: D["Swap"]) => {
363
+ eventQueue.push({event, swap});
364
+ return Promise.resolve();
365
+ }
366
+ if(this.processEvent!=null) this.unifiedChainEvents.registerListener(this.TYPE, initListener, this._swapDeserializer.bind(null, this));
367
+
368
+ await this.checkPastSwaps();
369
+
370
+ if(this.processEvent!=null) {
371
+ //Process accumulated event queue
372
+ for(let event of eventQueue) {
373
+ await this.processEvent(event.event, event.swap);
374
+ }
375
+
376
+ //Unregister the temporary event handler
377
+ this.unifiedChainEvents.unregisterListener(this.TYPE);
378
+ }
379
+ }
380
+
381
+ if(this.processEvent!=null) this.unifiedChainEvents.registerListener(this.TYPE, this.processEvent.bind(this), this._swapDeserializer.bind(null, this));
382
+
383
+ this.isInitialized = true;
384
+
385
+ if(!noTimers) this.startTickInterval();
386
+
387
+ // this.logger.info("init(): Swap wrapper initialized");
388
+ }
389
+
390
+ /**
391
+ * Un-subscribes from event listeners on the smart chain, terminates the tick interval and stops this wrapper
392
+ */
393
+ public async stop() {
394
+ this.isInitialized = false;
395
+ this.unifiedChainEvents.unregisterListener(this.TYPE);
396
+ this.logger.info("stop(): Swap wrapper stopped");
397
+ if(this.tickInterval!=null) {
398
+ clearTimeout(this.tickInterval);
399
+ delete this.tickInterval;
400
+ }
401
+ if(this.tickAbortController!=null) {
402
+ this.tickAbortController.abort("Wrapper instance stopped!");
403
+ delete this.tickAbortController;
404
+ }
405
+ }
406
+
407
+ /**
408
+ * Runs checks on all the known pending swaps, syncing their state from on-chain data
409
+ *
410
+ * @remarks Doesn't work properly if you pass non-persisted swaps
411
+ *
412
+ * @param pastSwaps Optional array of past swaps to check, otherwise all relevant swaps will be fetched
413
+ * from the persistent storage
414
+ * @param noSave Whether to skip saving the swap changes in the persistent storage
415
+ */
416
+ public async checkPastSwaps(pastSwaps?: D["Swap"][], noSave?: boolean): Promise<{ removeSwaps: D["Swap"][], changedSwaps: D["Swap"][] }> {
417
+ if (pastSwaps == null) pastSwaps = await this.unifiedStorage.query<D["Swap"]>(
418
+ [[{key: "type", value: this.TYPE}, {key: "state", value: this._pendingSwapStates}]],
419
+ (val: any) => new this._swapDeserializer(this, val)
420
+ );
421
+
422
+ const maxParallelSyncs = this._options.maxParallelSwapSyncs ?? DEFAULT_MAX_PARALLEL_SWAP_SYNCS;
423
+
424
+ const totalRemoveSwaps: D["Swap"][] = [];
425
+ const totalChangedSwaps: D["Swap"][] = [];
426
+ for(let i=0; i<pastSwaps.length; i+=maxParallelSyncs) {
427
+ const {removeSwaps, changedSwaps} = await this._checkPastSwaps(pastSwaps.slice(i, i+maxParallelSyncs));
428
+ if (!noSave) {
429
+ await this.unifiedStorage.removeAll(removeSwaps);
430
+ await this.unifiedStorage.saveAll(changedSwaps);
431
+ changedSwaps.forEach(swap => swap._emitEvent());
432
+ removeSwaps.forEach(swap => swap._emitEvent());
433
+ }
434
+ totalRemoveSwaps.push(...removeSwaps);
435
+ totalChangedSwaps.push(...changedSwaps);
436
+ }
437
+
438
+ return {
439
+ removeSwaps: totalRemoveSwaps,
440
+ changedSwaps: totalChangedSwaps
441
+ }
442
+ }
443
+
444
+ /**
445
+ * Invokes {@link ISwap._tick} on all the known swaps
446
+ *
447
+ * @param swaps Optional array of swaps to invoke `_tick()` on, otherwise all relevant swaps will be fetched
448
+ * from the persistent storage
449
+ * @param abortSignal Abort signal
450
+ */
451
+ public async tick(swaps?: D["Swap"][], abortSignal?: AbortSignal): Promise<void> {
452
+ if(swaps==null) swaps = await this.unifiedStorage.query<D["Swap"]>(
453
+ [[{key: "type", value: this.TYPE}, {key: "state", value: this.tickSwapState}]],
454
+ (val: any) => new this._swapDeserializer(this, val)
455
+ );
456
+ abortSignal?.throwIfAborted();
457
+
458
+ const parallelTicks = this._options.maxParallelSwapTicks ?? DEFAULT_MAX_PARALLEL_SWAP_TICKS;
459
+
460
+ let promises: Promise<any>[] = [];
461
+ for(let pendingSwap of this.pendingSwaps.values()) {
462
+ const value = pendingSwap.deref();
463
+ if(value != null) promises.push(value._tick(true).catch(e => {
464
+ this.logger.warn(`tick(): Error ticking swap ${value.getId()}: `, e);
465
+ }));
466
+ if(promises.length >= parallelTicks) {
467
+ await Promise.all(promises);
468
+ abortSignal?.throwIfAborted();
469
+ promises = [];
470
+ }
471
+ }
472
+
473
+ for(let value of swaps) {
474
+ promises.push(value._tick(true).catch(e => {
475
+ this.logger.warn(`tick(): Error ticking swap ${value.getId()}: `, e);
476
+ }));
477
+ if(promises.length >= parallelTicks) {
478
+ await Promise.all(promises);
479
+ abortSignal?.throwIfAborted();
480
+ promises = [];
481
+ }
482
+ }
483
+
484
+ if(promises.length>0) await Promise.all(promises);
485
+ abortSignal?.throwIfAborted();
486
+ }
487
+
488
+ /**
489
+ * Returns the smart chain's native token used to pay for fees
490
+ * @internal
491
+ */
492
+ _getNativeToken(): SCToken<T["ChainId"]> {
493
+ return this._tokens[this._chain.getNativeCurrencyAddress()];
494
+ }
495
+
496
+ /**
497
+ * Saves the swap, if it is not initiated it is only saved to pending swaps
498
+ *
499
+ * @param swap Swap to save
500
+ *
501
+ * @internal
502
+ */
503
+ _saveSwapData(swap: D["Swap"]): Promise<void> {
504
+ if(!this._options.saveUninitializedSwaps) {
505
+ if(!swap.isInitiated()) {
506
+ this.logger.debug("saveSwapData(): Swap "+swap.getId()+" not initiated, saving to pending swaps");
507
+ this.pendingSwaps.set(swap.getId(), new WeakRef<D["Swap"]>(swap));
508
+ return Promise.resolve();
509
+ } else {
510
+ this.pendingSwaps.delete(swap.getId());
511
+ }
512
+ }
513
+ return this.unifiedStorage.save(swap);
514
+ }
515
+
516
+ /**
517
+ * Removes the swap from the persistent storage and pending swaps
518
+ *
519
+ * @param swap Swap to remove
520
+ *
521
+ * @internal
522
+ */
523
+ _removeSwapData(swap: D["Swap"]): Promise<void> {
524
+ this.pendingSwaps.delete(swap.getId());
525
+ if(!swap._persisted) return Promise.resolve();
526
+ return this.unifiedStorage.remove(swap);
527
+ }
528
+
529
+ /**
530
+ * Retrieves a swap by its ID from the pending swap mapping
531
+ *
532
+ * @param id
533
+ *
534
+ * @internal
535
+ */
536
+ _getPendingSwap(id: string): D["Swap"] | null {
537
+ return this.pendingSwaps.get(id)?.deref() ?? null;
538
+ }
539
+
540
540
  }