@atomiqlabs/sdk 8.1.8 → 8.3.1

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 (253) hide show
  1. package/dist/bitcoin/wallet/BitcoinWallet.d.ts +41 -5
  2. package/dist/bitcoin/wallet/BitcoinWallet.js +36 -1
  3. package/dist/bitcoin/wallet/IBitcoinWallet.d.ts +52 -2
  4. package/dist/bitcoin/wallet/IBitcoinWallet.js +2 -1
  5. package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.d.ts +42 -7
  6. package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.js +36 -1
  7. package/dist/enums/FeeType.d.ts +7 -0
  8. package/dist/enums/FeeType.js +7 -0
  9. package/dist/enums/SwapAmountType.d.ts +7 -0
  10. package/dist/enums/SwapAmountType.js +7 -0
  11. package/dist/enums/SwapDirection.d.ts +7 -0
  12. package/dist/enums/SwapDirection.js +7 -0
  13. package/dist/enums/SwapType.d.ts +62 -1
  14. package/dist/enums/SwapType.js +62 -1
  15. package/dist/errors/IntermediaryError.d.ts +4 -0
  16. package/dist/errors/IntermediaryError.js +1 -0
  17. package/dist/errors/RequestError.d.ts +15 -1
  18. package/dist/errors/RequestError.js +8 -0
  19. package/dist/errors/UserError.d.ts +1 -0
  20. package/dist/errors/UserError.js +1 -0
  21. package/dist/index.d.ts +4 -5
  22. package/dist/index.js +3 -4
  23. package/dist/intermediaries/Intermediary.d.ts +57 -10
  24. package/dist/intermediaries/Intermediary.js +37 -10
  25. package/dist/intermediaries/IntermediaryDiscovery.d.ts +55 -22
  26. package/dist/intermediaries/IntermediaryDiscovery.js +35 -22
  27. package/dist/prices/RedundantSwapPrice.d.ts +24 -3
  28. package/dist/prices/RedundantSwapPrice.js +21 -1
  29. package/dist/prices/SingleSwapPrice.d.ts +9 -6
  30. package/dist/prices/SingleSwapPrice.js +10 -7
  31. package/dist/prices/SwapPriceWithChain.d.ts +54 -16
  32. package/dist/prices/SwapPriceWithChain.js +58 -20
  33. package/dist/prices/abstract/ISwapPrice.d.ts +94 -45
  34. package/dist/prices/abstract/ISwapPrice.js +103 -55
  35. package/dist/prices/providers/BinancePriceProvider.d.ts +7 -0
  36. package/dist/prices/providers/BinancePriceProvider.js +7 -0
  37. package/dist/prices/providers/CoinGeckoPriceProvider.d.ts +6 -0
  38. package/dist/prices/providers/CoinGeckoPriceProvider.js +6 -0
  39. package/dist/prices/providers/CoinPaprikaPriceProvider.d.ts +6 -0
  40. package/dist/prices/providers/CoinPaprikaPriceProvider.js +6 -0
  41. package/dist/prices/providers/CustomPriceProvider.d.ts +11 -0
  42. package/dist/prices/providers/CustomPriceProvider.js +11 -0
  43. package/dist/prices/providers/KrakenPriceProvider.d.ts +9 -0
  44. package/dist/prices/providers/KrakenPriceProvider.js +9 -0
  45. package/dist/prices/providers/OKXPriceProvider.d.ts +6 -0
  46. package/dist/prices/providers/OKXPriceProvider.js +6 -0
  47. package/dist/prices/providers/abstract/ExchangePriceProvider.d.ts +3 -0
  48. package/dist/prices/providers/abstract/ExchangePriceProvider.js +3 -0
  49. package/dist/storage/IUnifiedStorage.d.ts +19 -7
  50. package/dist/storage/UnifiedSwapStorage.d.ts +33 -3
  51. package/dist/storage/UnifiedSwapStorage.js +29 -1
  52. package/dist/storage-browser/IndexedDBUnifiedStorage.d.ts +31 -7
  53. package/dist/storage-browser/IndexedDBUnifiedStorage.js +29 -6
  54. package/dist/storage-browser/LocalStorageManager.d.ts +25 -1
  55. package/dist/storage-browser/LocalStorageManager.js +25 -1
  56. package/dist/swapper/Swapper.d.ts +302 -221
  57. package/dist/swapper/Swapper.js +376 -344
  58. package/dist/swapper/SwapperFactory.d.ts +41 -17
  59. package/dist/swapper/SwapperFactory.js +23 -2
  60. package/dist/swapper/SwapperUtils.d.ts +75 -28
  61. package/dist/swapper/SwapperUtils.js +107 -60
  62. package/dist/swapper/SwapperWithChain.d.ts +286 -91
  63. package/dist/swapper/SwapperWithChain.js +218 -64
  64. package/dist/swapper/SwapperWithSigner.d.ts +229 -80
  65. package/dist/swapper/SwapperWithSigner.js +190 -44
  66. package/dist/swaps/IAddressSwap.d.ts +10 -1
  67. package/dist/swaps/IAddressSwap.js +2 -1
  68. package/dist/swaps/IBTCWalletSwap.d.ts +24 -6
  69. package/dist/swaps/IBTCWalletSwap.js +2 -1
  70. package/dist/swaps/IClaimableSwap.d.ts +36 -4
  71. package/dist/swaps/IClaimableSwap.js +2 -1
  72. package/dist/swaps/IClaimableSwapWrapper.d.ts +11 -1
  73. package/dist/swaps/IRefundableSwap.d.ts +29 -3
  74. package/dist/swaps/IRefundableSwap.js +2 -1
  75. package/dist/swaps/ISwap.d.ts +159 -21
  76. package/dist/swaps/ISwap.js +90 -33
  77. package/dist/swaps/ISwapWithGasDrop.d.ts +6 -0
  78. package/dist/swaps/ISwapWithGasDrop.js +1 -0
  79. package/dist/swaps/ISwapWrapper.d.ts +157 -48
  80. package/dist/swaps/ISwapWrapper.js +130 -72
  81. package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.d.ts +49 -6
  82. package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.js +22 -12
  83. package/dist/swaps/escrow_swaps/IEscrowSwap.d.ts +65 -12
  84. package/dist/swaps/escrow_swaps/IEscrowSwap.js +38 -19
  85. package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.d.ts +39 -9
  86. package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.js +30 -21
  87. package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.d.ts +31 -15
  88. package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.js +33 -18
  89. package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.d.ts +94 -29
  90. package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.js +90 -27
  91. package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.d.ts +22 -9
  92. package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.js +24 -11
  93. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.d.ts +275 -58
  94. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +516 -239
  95. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +76 -25
  96. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +131 -49
  97. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.d.ts +311 -51
  98. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +542 -193
  99. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +87 -26
  100. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +147 -58
  101. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.d.ts +209 -53
  102. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +449 -242
  103. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.d.ts +77 -23
  104. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.js +116 -46
  105. package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.d.ts +197 -56
  106. package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +326 -189
  107. package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.d.ts +30 -5
  108. package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.js +44 -19
  109. package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.d.ts +60 -19
  110. package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.js +74 -31
  111. package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.d.ts +76 -50
  112. package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.js +106 -101
  113. package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.d.ts +36 -13
  114. package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.js +65 -19
  115. package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.d.ts +46 -17
  116. package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.js +82 -27
  117. package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +328 -92
  118. package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +460 -219
  119. package/dist/swaps/spv_swaps/SpvFromBTCWrapper.d.ts +76 -24
  120. package/dist/swaps/spv_swaps/SpvFromBTCWrapper.js +244 -124
  121. package/dist/swaps/trusted/ln/LnForGasSwap.d.ts +146 -18
  122. package/dist/swaps/trusted/ln/LnForGasSwap.js +173 -43
  123. package/dist/swaps/trusted/ln/LnForGasWrapper.d.ts +29 -10
  124. package/dist/swaps/trusted/ln/LnForGasWrapper.js +30 -11
  125. package/dist/swaps/trusted/onchain/OnchainForGasSwap.d.ts +200 -47
  126. package/dist/swaps/trusted/onchain/OnchainForGasSwap.js +230 -78
  127. package/dist/swaps/trusted/onchain/OnchainForGasWrapper.d.ts +34 -12
  128. package/dist/swaps/trusted/onchain/OnchainForGasWrapper.js +33 -14
  129. package/dist/types/AmountData.d.ts +2 -1
  130. package/dist/types/CustomPriceFunction.d.ts +7 -1
  131. package/dist/types/SwapExecutionAction.d.ts +74 -4
  132. package/dist/types/SwapWithSigner.d.ts +4 -1
  133. package/dist/types/SwapWithSigner.js +5 -2
  134. package/dist/types/Token.d.ts +11 -5
  135. package/dist/types/Token.js +6 -3
  136. package/dist/types/TokenAmount.d.ts +3 -0
  137. package/dist/types/TokenAmount.js +2 -0
  138. package/dist/types/fees/Fee.d.ts +2 -1
  139. package/dist/types/fees/FeeBreakdown.d.ts +2 -1
  140. package/dist/types/fees/PercentagePPM.d.ts +2 -0
  141. package/dist/types/fees/PercentagePPM.js +1 -0
  142. package/dist/types/lnurl/LNURLPay.d.ts +14 -6
  143. package/dist/types/lnurl/LNURLPay.js +6 -2
  144. package/dist/types/lnurl/LNURLWithdraw.d.ts +12 -5
  145. package/dist/types/lnurl/LNURLWithdraw.js +6 -2
  146. package/dist/types/wallets/LightningInvoiceCreateService.d.ts +20 -0
  147. package/dist/types/wallets/LightningInvoiceCreateService.js +15 -0
  148. package/dist/types/wallets/MinimalBitcoinWalletInterface.d.ts +3 -1
  149. package/dist/types/wallets/MinimalLightningNetworkWalletInterface.d.ts +3 -1
  150. package/dist/utils/BitcoinUtils.d.ts +1 -0
  151. package/dist/utils/BitcoinUtils.js +5 -1
  152. package/dist/utils/SwapUtils.d.ts +56 -1
  153. package/dist/utils/SwapUtils.js +53 -1
  154. package/dist/utils/TokenUtils.d.ts +10 -2
  155. package/dist/utils/TokenUtils.js +12 -4
  156. package/package.json +3 -3
  157. package/src/bitcoin/wallet/BitcoinWallet.ts +41 -5
  158. package/src/bitcoin/wallet/IBitcoinWallet.ts +57 -2
  159. package/src/bitcoin/wallet/SingleAddressBitcoinWallet.ts +42 -6
  160. package/src/enums/FeeType.ts +7 -0
  161. package/src/enums/SwapAmountType.ts +7 -0
  162. package/src/enums/SwapDirection.ts +7 -0
  163. package/src/enums/SwapType.ts +62 -2
  164. package/src/errors/IntermediaryError.ts +4 -0
  165. package/src/errors/RequestError.ts +15 -1
  166. package/src/errors/UserError.ts +1 -0
  167. package/src/index.ts +6 -5
  168. package/src/intermediaries/Intermediary.ts +57 -10
  169. package/src/intermediaries/IntermediaryDiscovery.ts +60 -27
  170. package/src/prices/RedundantSwapPrice.ts +24 -4
  171. package/src/prices/SingleSwapPrice.ts +10 -7
  172. package/src/prices/SwapPriceWithChain.ts +59 -21
  173. package/src/prices/abstract/ISwapPrice.ts +114 -65
  174. package/src/prices/providers/BinancePriceProvider.ts +7 -0
  175. package/src/prices/providers/CoinGeckoPriceProvider.ts +6 -0
  176. package/src/prices/providers/CoinPaprikaPriceProvider.ts +6 -0
  177. package/src/prices/providers/CustomPriceProvider.ts +11 -0
  178. package/src/prices/providers/KrakenPriceProvider.ts +9 -0
  179. package/src/prices/providers/OKXPriceProvider.ts +6 -0
  180. package/src/prices/providers/abstract/ExchangePriceProvider.ts +3 -0
  181. package/src/storage/IUnifiedStorage.ts +19 -7
  182. package/src/storage/UnifiedSwapStorage.ts +33 -3
  183. package/src/storage-browser/IndexedDBUnifiedStorage.ts +31 -8
  184. package/src/storage-browser/LocalStorageManager.ts +25 -1
  185. package/src/swapper/Swapper.ts +512 -378
  186. package/src/swapper/SwapperFactory.ts +44 -21
  187. package/src/swapper/SwapperUtils.ts +107 -60
  188. package/src/swapper/SwapperWithChain.ts +320 -81
  189. package/src/swapper/SwapperWithSigner.ts +263 -56
  190. package/src/swaps/IAddressSwap.ts +11 -1
  191. package/src/swaps/IBTCWalletSwap.ts +24 -8
  192. package/src/swaps/IClaimableSwap.ts +39 -4
  193. package/src/swaps/IClaimableSwapWrapper.ts +11 -2
  194. package/src/swaps/IRefundableSwap.ts +32 -3
  195. package/src/swaps/ISwap.ts +221 -82
  196. package/src/swaps/ISwapWithGasDrop.ts +6 -0
  197. package/src/swaps/ISwapWrapper.ts +212 -94
  198. package/src/swaps/escrow_swaps/IEscrowSelfInitSwap.ts +62 -18
  199. package/src/swaps/escrow_swaps/IEscrowSwap.ts +83 -37
  200. package/src/swaps/escrow_swaps/IEscrowSwapWrapper.ts +61 -30
  201. package/src/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.ts +37 -19
  202. package/src/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.ts +120 -51
  203. package/src/swaps/escrow_swaps/frombtc/IFromBTCWrapper.ts +24 -11
  204. package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.ts +559 -256
  205. package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +155 -61
  206. package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +590 -226
  207. package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +177 -74
  208. package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.ts +470 -243
  209. package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.ts +141 -59
  210. package/src/swaps/escrow_swaps/tobtc/IToBTCSwap.ts +352 -193
  211. package/src/swaps/escrow_swaps/tobtc/IToBTCWrapper.ts +48 -23
  212. package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.ts +86 -39
  213. package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.ts +110 -110
  214. package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.ts +88 -33
  215. package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.ts +101 -31
  216. package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +534 -263
  217. package/src/swaps/spv_swaps/SpvFromBTCWrapper.ts +289 -148
  218. package/src/swaps/trusted/ln/LnForGasSwap.ts +184 -45
  219. package/src/swaps/trusted/ln/LnForGasWrapper.ts +34 -15
  220. package/src/swaps/trusted/onchain/OnchainForGasSwap.ts +260 -86
  221. package/src/swaps/trusted/onchain/OnchainForGasWrapper.ts +41 -19
  222. package/src/types/AmountData.ts +2 -1
  223. package/src/types/CustomPriceFunction.ts +7 -1
  224. package/src/types/SwapExecutionAction.ts +84 -5
  225. package/src/types/SwapWithSigner.ts +7 -3
  226. package/src/types/Token.ts +12 -5
  227. package/src/types/TokenAmount.ts +3 -0
  228. package/src/types/fees/Fee.ts +2 -1
  229. package/src/types/fees/FeeBreakdown.ts +2 -1
  230. package/src/types/fees/PercentagePPM.ts +2 -0
  231. package/src/types/lnurl/LNURLPay.ts +14 -6
  232. package/src/types/lnurl/LNURLWithdraw.ts +12 -5
  233. package/src/types/wallets/LightningInvoiceCreateService.ts +26 -0
  234. package/src/types/wallets/MinimalBitcoinWalletInterface.ts +3 -1
  235. package/src/types/wallets/MinimalLightningNetworkWalletInterface.ts +3 -1
  236. package/src/utils/BitcoinUtils.ts +5 -0
  237. package/src/utils/SwapUtils.ts +61 -1
  238. package/src/utils/TokenUtils.ts +12 -4
  239. package/dist/bitcoin/BitcoinRpcWithAddressIndex.d.ts +0 -68
  240. package/dist/bitcoin/BitcoinRpcWithAddressIndex.js +0 -2
  241. package/dist/bitcoin/LightningNetworkApi.d.ts +0 -12
  242. package/dist/bitcoin/LightningNetworkApi.js +0 -2
  243. package/dist/bitcoin/mempool/MempoolApi.d.ts +0 -350
  244. package/dist/bitcoin/mempool/MempoolApi.js +0 -311
  245. package/dist/bitcoin/mempool/MempoolBitcoinBlock.d.ts +0 -44
  246. package/dist/bitcoin/mempool/MempoolBitcoinBlock.js +0 -48
  247. package/dist/bitcoin/mempool/MempoolBitcoinRpc.d.ts +0 -119
  248. package/dist/bitcoin/mempool/MempoolBitcoinRpc.js +0 -361
  249. package/dist/bitcoin/mempool/synchronizer/MempoolBtcRelaySynchronizer.d.ts +0 -22
  250. package/dist/bitcoin/mempool/synchronizer/MempoolBtcRelaySynchronizer.js +0 -105
  251. package/dist/errors/PaymentAuthError.d.ts +0 -11
  252. package/dist/errors/PaymentAuthError.js +0 -23
  253. package/src/errors/PaymentAuthError.ts +0 -26
@@ -3,7 +3,8 @@ import {ISwap} from "./ISwap";
3
3
  import {ISwapWrapper, SwapTypeDefinition} from "./ISwapWrapper";
4
4
 
5
5
  /**
6
- * Type guard to check if an object is an IRefundableSwap
6
+ * Type guard to check if an object is an {@link IRefundableSwap}
7
+ *
7
8
  * @category Swaps
8
9
  */
9
10
  export function isIRefundableSwap(obj: any): obj is IRefundableSwap {
@@ -13,7 +14,8 @@ export function isIRefundableSwap(obj: any): obj is IRefundableSwap {
13
14
  }
14
15
 
15
16
  /**
16
- * Interface for swaps that can be refunded
17
+ * Interface for swaps that can be refunded in case of failure
18
+ *
17
19
  * @category Swaps
18
20
  */
19
21
  export interface IRefundableSwap<
@@ -22,8 +24,35 @@ export interface IRefundableSwap<
22
24
  S extends number = number
23
25
  > extends ISwap<T, D, S> {
24
26
 
27
+ /**
28
+ * Checks whether a swap is currently refundable
29
+ */
25
30
  isRefundable(): boolean;
26
- txsRefund(_signer?: T["Signer"] | T["NativeSigner"]): Promise<T["TX"][]>;
31
+
32
+ /**
33
+ * Returns transactions for refunding the swap if the swap is in refundable state, you can check so with
34
+ * {@link isRefundable}. After sending the transaction manually be sure to call the {@link waitTillRefunded}
35
+ * function to wait till the refund transaction is observed, processed by the SDK and state of the swap
36
+ * properly updated.
37
+ *
38
+ * @param _signer Address of the signer to create the refund transactions for
39
+ */
40
+ txsRefund(_signer?: string): Promise<T["TX"][]>;
41
+
42
+ /**
43
+ * Refunds the swap if the swap is in refundable state, you can check so with {@link isRefundable}
44
+ *
45
+ * @param _signer Signer to sign the transactions with, must be the same as used in the initialization
46
+ * @param abortSignal Abort signal
47
+ */
27
48
  refund(_signer?: T["Signer"] | T["NativeSigner"], abortSignal?: AbortSignal): Promise<string>;
28
49
 
50
+ /**
51
+ * Waits till a swap is refunded, should be called after sending the refund transactions manually to
52
+ * wait till the SDK processes the refund and updates the swap state accordingly
53
+ *
54
+ * @param abortSignal AbortSignal
55
+ */
56
+ waitTillRefunded(abortSignal?: AbortSignal): Promise<void>;
57
+
29
58
  }
@@ -15,6 +15,7 @@ import {isPriceInfoType, PriceInfoType} from "../types/PriceInfoType";
15
15
 
16
16
  /**
17
17
  * Initialization data for creating a swap
18
+ *
18
19
  * @category Swaps
19
20
  */
20
21
  export type ISwapInit = {
@@ -28,6 +29,7 @@ export type ISwapInit = {
28
29
 
29
30
  /**
30
31
  * Type guard to check if an object is an ISwapInit
32
+ *
31
33
  * @category Swaps
32
34
  */
33
35
  export function isISwapInit(obj: any): obj is ISwapInit {
@@ -43,6 +45,7 @@ export function isISwapInit(obj: any): obj is ISwapInit {
43
45
 
44
46
  /**
45
47
  * Base abstract class for all swap types
48
+ *
46
49
  * @category Swaps
47
50
  */
48
51
  export abstract class ISwap<
@@ -50,38 +53,95 @@ export abstract class ISwap<
50
53
  D extends SwapTypeDefinition<T, ISwapWrapper<T, D>, ISwap<T, D, S>> = SwapTypeDefinition<T, ISwapWrapper<T, any>, ISwap<T, any, any>>,
51
54
  S extends number = number
52
55
  > {
56
+ /**
57
+ * Swap type
58
+ */
53
59
  protected readonly abstract TYPE: SwapType;
60
+ /**
61
+ * Swap logger
62
+ * @internal
63
+ */
54
64
  protected readonly abstract logger: LoggerType;
55
-
65
+ /**
66
+ * Current newest defined version of the swap
67
+ * @internal
68
+ */
56
69
  protected readonly currentVersion: number = 1;
70
+ /**
71
+ * Wrapper instance holding this swap
72
+ * @internal
73
+ */
57
74
  protected readonly wrapper: D["Wrapper"];
58
75
 
59
- readonly url?: string;
60
-
61
- readonly chainIdentifier: T["ChainId"];
62
- readonly exactIn: boolean;
63
- createdAt: number;
64
76
 
77
+ /**
78
+ * The current version of the swap
79
+ * @internal
80
+ */
65
81
  protected version: number;
82
+ /**
83
+ * Whether a swap was initialized, a swap is considered initialize on first interaction with it, i.e.
84
+ * calling commit() on a Smart chain -> Bitcoin swaps, calling waitForPayment() or similar on the other
85
+ * direction. Not initiated swaps are not saved to the persistent storage by default (see
86
+ * {@link SwapperOptions.saveUninitializedSwaps})
87
+ * @internal
88
+ */
66
89
  protected initiated: boolean = false;
67
-
68
- state: S = 0 as S;
69
- expiry: number;
70
- pricingInfo?: PriceInfoType;
71
-
90
+ /**
91
+ * Expiration of the swap quote
92
+ * @internal
93
+ */
94
+ protected expiry: number;
95
+ /**
96
+ * Pricing information of the swap
97
+ * @internal
98
+ */
99
+ protected pricingInfo?: PriceInfoType;
100
+ /**
101
+ * Swap fee in the non-bitcoin token
102
+ * @internal
103
+ */
72
104
  protected swapFee: bigint;
105
+ /**
106
+ * Swap fee in bitcoin satoshis
107
+ * @internal
108
+ */
73
109
  protected swapFeeBtc: bigint;
74
110
 
111
+
112
+ /**
113
+ * Swap state
114
+ * @internal
115
+ */
116
+ _state: S = 0 as S;
75
117
  /**
76
118
  * Random nonce to differentiate the swap from others with the same identifier hash (i.e. when quoting the same swap
77
119
  * from multiple LPs)
120
+ * @internal
78
121
  */
79
- randomNonce: string;
122
+ _randomNonce: string;
123
+
80
124
 
81
125
  /**
82
- * Event emitter emitting "swapState" event when swap's state changes
126
+ * Event emitter emitting `"swapState"` event when swap's state changes
83
127
  */
84
- events: EventEmitter<{swapState: [D["Swap"]]}> = new EventEmitter();
128
+ readonly events: EventEmitter<{swapState: [D["Swap"]]}> = new EventEmitter();
129
+ /**
130
+ * URL of the intermediary (LP) used for this swap, already has the swap service specific path appended
131
+ */
132
+ readonly url?: string;
133
+ /**
134
+ * Smart chain identifier string corresponding to this swap
135
+ */
136
+ readonly chainIdentifier: T["ChainId"];
137
+ /**
138
+ * Whether a swap is an exact input swap
139
+ */
140
+ readonly exactIn: boolean;
141
+ /**
142
+ * A UNIX milliseconds timestamps of when this swap was created
143
+ */
144
+ createdAt: number;
85
145
 
86
146
  protected constructor(wrapper: D["Wrapper"], obj: any);
87
147
  protected constructor(wrapper: D["Wrapper"], swapInit: ISwapInit);
@@ -100,12 +160,12 @@ export abstract class ISwap<
100
160
  this.exactIn = swapInitOrObj.exactIn;
101
161
  this.version = this.currentVersion;
102
162
  this.createdAt = Date.now();
103
- this.randomNonce = randomBytes(16).toString("hex");
163
+ this._randomNonce = randomBytes(16).toString("hex");
104
164
  } else {
105
165
  this.expiry = swapInitOrObj.expiry;
106
166
  this.url = swapInitOrObj.url;
107
167
 
108
- this.state = swapInitOrObj.state;
168
+ this._state = swapInitOrObj.state;
109
169
 
110
170
  if(
111
171
  swapInitOrObj._isValid!=null && swapInitOrObj._differencePPM!=null && swapInitOrObj._satsBaseFee!=null &&
@@ -130,7 +190,7 @@ export abstract class ISwap<
130
190
  this.exactIn = swapInitOrObj.exactIn;
131
191
  this.createdAt = swapInitOrObj.createdAt ?? swapInitOrObj.expiry;
132
192
 
133
- this.randomNonce = swapInitOrObj.randomNonce;
193
+ this._randomNonce = swapInitOrObj.randomNonce;
134
194
  }
135
195
  if(this.version!==this.currentVersion) {
136
196
  this.upgradeVersion();
@@ -138,6 +198,11 @@ export abstract class ISwap<
138
198
  if(this.initiated==null) this.initiated = true;
139
199
  }
140
200
 
201
+ /**
202
+ * Called when swap is deserialized to potentially update the version of the data for the swap
203
+ *
204
+ * @internal
205
+ */
141
206
  protected abstract upgradeVersion(): void;
142
207
 
143
208
  /**
@@ -145,14 +210,14 @@ export abstract class ISwap<
145
210
  *
146
211
  * @param targetState The state to wait for
147
212
  * @param type Whether to wait for the state exactly or also to a state with a higher number
148
- * @param abortSignal
149
- * @protected
213
+ * @param abortSignal Abort signal
214
+ * @internal
150
215
  */
151
216
  protected waitTillState(targetState: S, type: "eq" | "gte" | "neq" = "eq", abortSignal?: AbortSignal): Promise<void> {
152
217
  return new Promise((resolve, reject) => {
153
218
  let listener: (swap: D["Swap"]) => void;
154
219
  listener = (swap) => {
155
- if(type==="eq" ? swap.state===targetState : type==="gte" ? swap.state>=targetState : swap.state!=targetState) {
220
+ if(type==="eq" ? swap._state===targetState : type==="gte" ? swap._state>=targetState : swap._state!=targetState) {
156
221
  resolve();
157
222
  this.events.removeListener("swapState", listener);
158
223
  }
@@ -165,11 +230,21 @@ export abstract class ISwap<
165
230
  });
166
231
  }
167
232
 
168
- abstract txsExecute(options?: any): Promise<SwapExecutionAction<T>[]>;
233
+ /**
234
+ * Returns a list of steps or transactions required to finish and settle the swap
235
+ *
236
+ * @param options Additional options for executing the swap
237
+ */
238
+ public abstract txsExecute(options?: any): Promise<SwapExecutionAction<T>[]>;
169
239
 
170
240
  //////////////////////////////
171
241
  //// Pricing
172
242
 
243
+ /**
244
+ * This attempts to populate missing fields in the pricing info based on the swap amounts
245
+ *
246
+ * @internal
247
+ */
173
248
  protected tryRecomputeSwapPrice(): void {
174
249
  if(this.pricingInfo==null) return;
175
250
  if(this.pricingInfo.swapPriceUSatPerToken==null) {
@@ -178,7 +253,7 @@ export abstract class ISwap<
178
253
  const output = this.getOutput();
179
254
  if(input.isUnknown || output.isUnknown) return;
180
255
  if(isSCToken(input.token) && this.getDirection()===SwapDirection.TO_BTC) {
181
- this.pricingInfo = this.wrapper.prices.recomputePriceInfoSend(
256
+ this.pricingInfo = this.wrapper._prices.recomputePriceInfoSend(
182
257
  this.chainIdentifier,
183
258
  output.rawAmount!,
184
259
  this.pricingInfo.satsBaseFee,
@@ -188,7 +263,7 @@ export abstract class ISwap<
188
263
  );
189
264
  this.pricingInfo.realPriceUsdPerBitcoin = priceUsdPerBtc;
190
265
  } else if(isSCToken(output.token) && this.getDirection()===SwapDirection.FROM_BTC) {
191
- this.pricingInfo = this.wrapper.prices.recomputePriceInfoReceive(
266
+ this.pricingInfo = this.wrapper._prices.recomputePriceInfoReceive(
192
267
  this.chainIdentifier,
193
268
  input.rawAmount!,
194
269
  this.pricingInfo.satsBaseFee,
@@ -202,9 +277,9 @@ export abstract class ISwap<
202
277
  }
203
278
 
204
279
  /**
205
- * Re-fetches & revalidates the price data
280
+ * Re-fetches & revalidates the price data based on the current market prices
206
281
  */
207
- async refreshPriceData(): Promise<void> {
282
+ public async refreshPriceData(): Promise<void> {
208
283
  if(this.pricingInfo==null) return;
209
284
  const priceUsdPerBtc = this.pricingInfo.realPriceUsdPerBitcoin;
210
285
  const input = this.getInput();
@@ -212,7 +287,7 @@ export abstract class ISwap<
212
287
  if(input.isUnknown || output.isUnknown) return;
213
288
 
214
289
  if(isSCToken(input.token) && this.getDirection()===SwapDirection.TO_BTC) {
215
- this.pricingInfo = await this.wrapper.prices.isValidAmountSend(
290
+ this.pricingInfo = await this.wrapper._prices.isValidAmountSend(
216
291
  this.chainIdentifier,
217
292
  output.rawAmount!,
218
293
  this.pricingInfo.satsBaseFee,
@@ -222,7 +297,7 @@ export abstract class ISwap<
222
297
  );
223
298
  this.pricingInfo.realPriceUsdPerBitcoin = priceUsdPerBtc;
224
299
  } else if(isSCToken(output.token) && this.getDirection()===SwapDirection.FROM_BTC) {
225
- this.pricingInfo = await this.wrapper.prices.isValidAmountReceive(
300
+ this.pricingInfo = await this.wrapper._prices.isValidAmountReceive(
226
301
  this.chainIdentifier,
227
302
  input.rawAmount!,
228
303
  this.pricingInfo.satsBaseFee,
@@ -237,7 +312,7 @@ export abstract class ISwap<
237
312
  /**
238
313
  * Checks if the pricing for the swap is valid, according to max allowed price difference set in the ISwapPrice
239
314
  */
240
- hasValidPrice(): boolean {
315
+ public hasValidPrice(): boolean {
241
316
  if(this.pricingInfo==null) throw new Error("Pricing info not found, cannot check price validity!");
242
317
  return this.pricingInfo.isValid;
243
318
  }
@@ -245,7 +320,7 @@ export abstract class ISwap<
245
320
  /**
246
321
  * Returns pricing info about the swap
247
322
  */
248
- getPriceInfo(): {
323
+ public getPriceInfo(): {
249
324
  marketPrice?: number,
250
325
  swapPrice: number,
251
326
  difference: PercentagePPM
@@ -273,102 +348,142 @@ export abstract class ISwap<
273
348
  //////////////////////////////
274
349
  //// Getters & utils
275
350
 
276
- abstract _getEscrowHash(): string | null;
277
-
278
351
  /**
352
+ * Asserts a given signer is the initiator of this swap
353
+ *
279
354
  * @param signer Signer to check with this swap's initiator
280
355
  * @throws {Error} When signer's address doesn't match with the swap's initiator one
356
+ * @internal
281
357
  */
282
358
  protected checkSigner(signer: T["Signer"] | string): void {
283
359
  if((typeof(signer)==="string" ? signer : signer.getAddress())!==this._getInitiator()) throw new Error("Invalid signer provided!");
284
360
  }
285
361
 
362
+ /**
363
+ * Returns an escrow hash of the swap
364
+ *
365
+ * @internal
366
+ */
367
+ abstract _getEscrowHash(): string | null;
368
+
369
+ /**
370
+ * Checks if the swap's quote is expired for good (i.e. the swap strictly cannot be initiated anymore)
371
+ * @internal
372
+ */
373
+ abstract _verifyQuoteDefinitelyExpired(): Promise<boolean>;
374
+
286
375
  /**
287
376
  * Checks if the swap's quote is still valid
377
+ * @internal
288
378
  */
289
- abstract verifyQuoteValid(): Promise<boolean>;
379
+ abstract _verifyQuoteValid(): Promise<boolean>;
290
380
 
291
- abstract getInputAddress(): string | null;
292
- abstract getOutputAddress(): string | null;
381
+ /**
382
+ * Returns the intiator address of the swap - address that created this swap
383
+ * @internal
384
+ */
385
+ abstract _getInitiator(): string;
386
+
387
+ /**
388
+ * Sets this swap as initiated
389
+ * @internal
390
+ */
391
+ _setInitiated(): void {
392
+ this.initiated = true;
393
+ }
394
+
395
+ /**
396
+ * Returns source address of the swap
397
+ */
398
+ public abstract getInputAddress(): string | null;
399
+
400
+ /**
401
+ * Returns destination address of the swap
402
+ */
403
+ public abstract getOutputAddress(): string | null;
293
404
 
294
- abstract getInputTxId(): string | null;
295
- abstract getOutputTxId(): string | null;
405
+ /**
406
+ * Returns swap input transaction ID on the source chain
407
+ */
408
+ public abstract getInputTxId(): string | null;
409
+
410
+ /**
411
+ * Returns swap output transaction ID on the destination chain
412
+ */
413
+ public abstract getOutputTxId(): string | null;
296
414
 
297
415
  /**
298
- * Returns the ID of the swap, as used in the storage and getSwapById function
416
+ * Returns the ID of the swap, as used in the storage
299
417
  */
300
- abstract getId(): string;
418
+ public abstract getId(): string;
301
419
 
302
420
  /**
303
421
  * Checks whether there is some action required from the user for this swap - can mean either refundable or claimable
304
422
  */
305
- abstract requiresAction(): boolean;
423
+ public abstract requiresAction(): boolean;
306
424
 
307
425
  /**
308
426
  * Returns whether the swap is finished and in its terminal state (this can mean successful, refunded or failed)
309
427
  */
310
- abstract isFinished(): boolean;
428
+ public abstract isFinished(): boolean;
311
429
 
312
430
  /**
313
431
  * Checks whether the swap's quote has definitely expired and cannot be committed anymore, we can remove such swap
314
432
  */
315
- abstract isQuoteExpired(): boolean;
433
+ public abstract isQuoteExpired(): boolean;
316
434
 
317
435
  /**
318
436
  * Checks whether the swap's quote is soft expired (this means there is not enough time buffer for it to commit,
319
437
  * but it still can happen)
320
438
  */
321
- abstract isQuoteSoftExpired(): boolean;
439
+ public abstract isQuoteSoftExpired(): boolean;
322
440
 
323
441
  /**
324
442
  * Returns whether the swap finished successful
325
443
  */
326
- abstract isSuccessful(): boolean;
444
+ public abstract isSuccessful(): boolean;
327
445
 
328
446
  /**
329
447
  * Returns whether the swap failed (e.g. was refunded)
330
448
  */
331
- abstract isFailed(): boolean;
449
+ public abstract isFailed(): boolean;
332
450
 
333
451
  /**
334
- * Returns the intiator address of the swap - address that created this swap
452
+ * Whether a swap was initialized, a swap is considered initialized on first interaction with it, i.e.
453
+ * calling commit() on a Smart chain -> Bitcoin swaps, calling waitForPayment() or similar on the other
454
+ * direction. Not initiated swaps are not saved to the persistent storage by default (see
455
+ * {@link SwapperOptions.saveUninitializedSwaps})
335
456
  */
336
- abstract _getInitiator(): string;
337
-
338
- isInitiated(): boolean {
457
+ public isInitiated(): boolean {
339
458
  return this.initiated;
340
459
  }
341
460
 
342
- _setInitiated(): void {
343
- this.initiated = true;
344
- }
345
-
346
461
  /**
347
462
  * Returns quote expiry in UNIX millis
348
463
  */
349
- getQuoteExpiry(): number {
464
+ public getQuoteExpiry(): number {
350
465
  return this.expiry;
351
466
  }
352
467
 
353
468
  /**
354
469
  * Returns the type of the swap
355
470
  */
356
- getType(): SwapType {
471
+ public getType(): SwapType {
357
472
  return this.TYPE;
358
473
  }
359
474
 
360
475
  /**
361
476
  * Returns the direction of the swap
362
477
  */
363
- getDirection(): SwapDirection {
478
+ public getDirection(): SwapDirection {
364
479
  return this.TYPE===SwapType.TO_BTC || this.TYPE===SwapType.TO_BTCLN ? SwapDirection.TO_BTC : SwapDirection.FROM_BTC;
365
480
  }
366
481
 
367
482
  /**
368
483
  * Returns the current state of the swap
369
484
  */
370
- getState(): S {
371
- return this.state;
485
+ public getState(): S {
486
+ return this._state;
372
487
  }
373
488
 
374
489
  //////////////////////////////
@@ -377,44 +492,73 @@ export abstract class ISwap<
377
492
  /**
378
493
  * Returns output amount of the swap, user receives this much
379
494
  */
380
- abstract getOutput(): TokenAmount;
495
+ public abstract getOutput(): TokenAmount;
381
496
 
382
497
  /**
383
498
  * Returns the output token of the swap
384
499
  */
385
- abstract getOutputToken(): Token<T["ChainId"]>;
500
+ public abstract getOutputToken(): Token<T["ChainId"]>;
386
501
 
387
502
  /**
388
503
  * Returns input amount of the swap, user needs to pay this much
389
504
  */
390
- abstract getInput(): TokenAmount;
505
+ public abstract getInput(): TokenAmount;
391
506
 
392
507
  /**
393
508
  * Returns the input token of the swap
394
509
  */
395
- abstract getInputToken(): Token<T["ChainId"]>;
510
+ public abstract getInputToken(): Token<T["ChainId"]>;
396
511
 
397
512
  /**
398
- * Returns input amount if the swap without the fees (swap fee, network fee)
513
+ * Returns input amount of the swap without the fees (swap fee, network fee)
399
514
  */
400
- abstract getInputWithoutFee(): TokenAmount;
515
+ public abstract getInputWithoutFee(): TokenAmount;
401
516
 
402
517
  /**
403
518
  * Returns total fee for the swap, the fee is represented in source currency & destination currency, but is
404
519
  * paid only once
405
520
  */
406
- abstract getFee(): Fee;
521
+ public abstract getFee(): Fee;
407
522
 
408
523
  /**
409
524
  * Returns the breakdown of all the fees paid
410
525
  */
411
- abstract getFeeBreakdown(): FeeBreakdown<T["ChainId"]>;
526
+ public abstract getFeeBreakdown(): FeeBreakdown<T["ChainId"]>;
412
527
 
413
528
 
414
529
  //////////////////////////////
415
530
  //// Storage
416
531
 
417
- serialize(): any {
532
+ /**
533
+ * Saves the swap data to the underlying storage, or removes it if it is in a quote expired state
534
+ *
535
+ * @internal
536
+ */
537
+ _save(): Promise<void> {
538
+ if(this.isQuoteExpired()) {
539
+ return this.wrapper._removeSwapData(this);
540
+ } else {
541
+ return this.wrapper._saveSwapData(this);
542
+ }
543
+ }
544
+
545
+ /**
546
+ * Saves the swap data and also emits a swap state change
547
+ *
548
+ * @param state Optional state to set before the swap is saved an event emitted
549
+ *
550
+ * @internal
551
+ */
552
+ async _saveAndEmit(state?: S): Promise<void> {
553
+ if(state!=null) this._state = state;
554
+ await this._save();
555
+ this._emitEvent();
556
+ }
557
+
558
+ /**
559
+ * Serializes the swap to a JSON stringifiable representation (i.e. no bigints, buffers etc.)
560
+ */
561
+ public serialize(): any {
418
562
  if(this.pricingInfo==null) return {};
419
563
  return {
420
564
  id: this.getId(),
@@ -429,7 +573,7 @@ export abstract class ISwap<
429
573
  _realPriceUSatPerToken: this.pricingInfo.realPriceUSatPerToken==null ? null :this.pricingInfo.realPriceUSatPerToken.toString(10),
430
574
  _realPriceUsdPerBitcoin: this.pricingInfo.realPriceUsdPerBitcoin,
431
575
  _swapPriceUSatPerToken: this.pricingInfo.swapPriceUSatPerToken==null ? null :this.pricingInfo.swapPriceUSatPerToken.toString(10),
432
- state: this.state,
576
+ state: this._state,
433
577
  url: this.url,
434
578
  swapFee: this.swapFee==null ? null : this.swapFee.toString(10),
435
579
  swapFeeBtc: this.swapFeeBtc==null ? null : this.swapFeeBtc.toString(10),
@@ -438,28 +582,19 @@ export abstract class ISwap<
438
582
  initiated: this.initiated,
439
583
  exactIn: this.exactIn,
440
584
  createdAt: this.createdAt,
441
- randomNonce: this.randomNonce
585
+ randomNonce: this._randomNonce
442
586
  }
443
587
  }
444
588
 
445
- _save(): Promise<void> {
446
- if(this.isQuoteExpired()) {
447
- return this.wrapper.removeSwapData(this);
448
- } else {
449
- return this.wrapper.saveSwapData(this);
450
- }
451
- }
452
-
453
- async _saveAndEmit(state?: S): Promise<void> {
454
- if(state!=null) this.state = state;
455
- await this._save();
456
- this._emitEvent();
457
- }
458
-
459
589
 
460
590
  //////////////////////////////
461
591
  //// Events
462
592
 
593
+ /**
594
+ * Emits a `swapState` event with the current swap
595
+ *
596
+ * @internal
597
+ */
463
598
  _emitEvent() {
464
599
  this.wrapper.events.emit("swapState", this);
465
600
  this.events.emit("swapState", this);
@@ -475,6 +610,8 @@ export abstract class ISwap<
475
610
  * @param save whether to save the new swap state or not
476
611
  *
477
612
  * @returns {boolean} true if the swap changed, false if the swap hasn't changed
613
+ *
614
+ * @internal
478
615
  */
479
616
  abstract _sync(save?: boolean): Promise<boolean>;
480
617
 
@@ -484,6 +621,8 @@ export abstract class ISwap<
484
621
  * @param save whether to save the new swap state or not
485
622
  *
486
623
  * @returns {boolean} true if the swap changed, false if the swap hasn't changed
624
+ *
625
+ * @internal
487
626
  */
488
627
  abstract _tick(save?: boolean): Promise<boolean>;
489
628
 
@@ -4,6 +4,7 @@ import {SCToken} from "../types/Token";
4
4
 
5
5
  /**
6
6
  * Type guard to check if a swap has gas drop functionality
7
+ *
7
8
  * @category Swaps
8
9
  */
9
10
  export function isSwapWithGasDrop(swap: any): swap is ISwapWithGasDrop<any> {
@@ -12,8 +13,13 @@ export function isSwapWithGasDrop(swap: any): swap is ISwapWithGasDrop<any> {
12
13
 
13
14
  /**
14
15
  * Interface for swaps with gas drop functionality
16
+ *
15
17
  * @category Swaps
16
18
  */
17
19
  export interface ISwapWithGasDrop<T extends ChainType> {
20
+ /**
21
+ * Returns the output of the "gas drop", additional native token received by the user on
22
+ * the destination chain
23
+ */
18
24
  getGasDropOutput(): TokenAmount<T["ChainId"], SCToken<T["ChainId"]>>;
19
25
  }