@atomiqlabs/sdk 8.1.8 → 8.3.5

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 (261) hide show
  1. package/dist/bitcoin/coinselect2/utils.d.ts +6 -0
  2. package/dist/bitcoin/wallet/BitcoinWallet.d.ts +41 -5
  3. package/dist/bitcoin/wallet/BitcoinWallet.js +36 -1
  4. package/dist/bitcoin/wallet/IBitcoinWallet.d.ts +52 -2
  5. package/dist/bitcoin/wallet/IBitcoinWallet.js +2 -1
  6. package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.d.ts +42 -7
  7. package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.js +36 -1
  8. package/dist/enums/FeeType.d.ts +8 -1
  9. package/dist/enums/FeeType.js +8 -1
  10. package/dist/enums/SwapAmountType.d.ts +7 -0
  11. package/dist/enums/SwapAmountType.js +7 -0
  12. package/dist/enums/SwapDirection.d.ts +7 -0
  13. package/dist/enums/SwapDirection.js +7 -0
  14. package/dist/enums/SwapType.d.ts +62 -1
  15. package/dist/enums/SwapType.js +62 -1
  16. package/dist/errors/IntermediaryError.d.ts +4 -0
  17. package/dist/errors/IntermediaryError.js +1 -0
  18. package/dist/errors/RequestError.d.ts +15 -1
  19. package/dist/errors/RequestError.js +8 -0
  20. package/dist/errors/UserError.d.ts +1 -0
  21. package/dist/errors/UserError.js +1 -0
  22. package/dist/index.d.ts +5 -5
  23. package/dist/index.js +7 -6
  24. package/dist/intermediaries/Intermediary.d.ts +61 -14
  25. package/dist/intermediaries/Intermediary.js +38 -11
  26. package/dist/intermediaries/IntermediaryDiscovery.d.ts +62 -29
  27. package/dist/intermediaries/IntermediaryDiscovery.js +39 -24
  28. package/dist/prices/RedundantSwapPrice.d.ts +26 -5
  29. package/dist/prices/RedundantSwapPrice.js +22 -2
  30. package/dist/prices/SingleSwapPrice.d.ts +10 -7
  31. package/dist/prices/SingleSwapPrice.js +11 -8
  32. package/dist/prices/SwapPriceWithChain.d.ts +56 -19
  33. package/dist/prices/SwapPriceWithChain.js +62 -25
  34. package/dist/prices/abstract/IPriceProvider.d.ts +4 -4
  35. package/dist/prices/abstract/IPriceProvider.js +1 -1
  36. package/dist/prices/abstract/ISwapPrice.d.ts +95 -46
  37. package/dist/prices/abstract/ISwapPrice.js +104 -56
  38. package/dist/prices/providers/BinancePriceProvider.d.ts +8 -1
  39. package/dist/prices/providers/BinancePriceProvider.js +8 -1
  40. package/dist/prices/providers/CoinGeckoPriceProvider.d.ts +7 -1
  41. package/dist/prices/providers/CoinGeckoPriceProvider.js +7 -1
  42. package/dist/prices/providers/CoinPaprikaPriceProvider.d.ts +7 -1
  43. package/dist/prices/providers/CoinPaprikaPriceProvider.js +7 -1
  44. package/dist/prices/providers/CustomPriceProvider.d.ts +12 -1
  45. package/dist/prices/providers/CustomPriceProvider.js +12 -1
  46. package/dist/prices/providers/KrakenPriceProvider.d.ts +10 -1
  47. package/dist/prices/providers/KrakenPriceProvider.js +10 -1
  48. package/dist/prices/providers/OKXPriceProvider.d.ts +7 -1
  49. package/dist/prices/providers/OKXPriceProvider.js +7 -1
  50. package/dist/prices/providers/abstract/ExchangePriceProvider.d.ts +3 -0
  51. package/dist/prices/providers/abstract/ExchangePriceProvider.js +3 -0
  52. package/dist/storage/IUnifiedStorage.d.ts +19 -7
  53. package/dist/storage/UnifiedSwapStorage.d.ts +33 -3
  54. package/dist/storage/UnifiedSwapStorage.js +29 -1
  55. package/dist/storage-browser/IndexedDBUnifiedStorage.d.ts +31 -7
  56. package/dist/storage-browser/IndexedDBUnifiedStorage.js +29 -6
  57. package/dist/storage-browser/LocalStorageManager.d.ts +25 -1
  58. package/dist/storage-browser/LocalStorageManager.js +25 -1
  59. package/dist/swapper/Swapper.d.ts +380 -226
  60. package/dist/swapper/Swapper.js +383 -349
  61. package/dist/swapper/SwapperFactory.d.ts +66 -18
  62. package/dist/swapper/SwapperFactory.js +24 -3
  63. package/dist/swapper/SwapperUtils.d.ts +75 -28
  64. package/dist/swapper/SwapperUtils.js +107 -60
  65. package/dist/swapper/SwapperWithChain.d.ts +286 -91
  66. package/dist/swapper/SwapperWithChain.js +218 -64
  67. package/dist/swapper/SwapperWithSigner.d.ts +229 -80
  68. package/dist/swapper/SwapperWithSigner.js +190 -44
  69. package/dist/swaps/IAddressSwap.d.ts +12 -3
  70. package/dist/swaps/IAddressSwap.js +3 -2
  71. package/dist/swaps/IBTCWalletSwap.d.ts +26 -8
  72. package/dist/swaps/IBTCWalletSwap.js +3 -2
  73. package/dist/swaps/IClaimableSwap.d.ts +38 -6
  74. package/dist/swaps/IClaimableSwap.js +3 -2
  75. package/dist/swaps/IClaimableSwapWrapper.d.ts +11 -1
  76. package/dist/swaps/IRefundableSwap.d.ts +31 -5
  77. package/dist/swaps/IRefundableSwap.js +3 -2
  78. package/dist/swaps/ISwap.d.ts +162 -24
  79. package/dist/swaps/ISwap.js +92 -35
  80. package/dist/swaps/ISwapWithGasDrop.d.ts +8 -2
  81. package/dist/swaps/ISwapWithGasDrop.js +2 -1
  82. package/dist/swaps/ISwapWrapper.d.ts +161 -52
  83. package/dist/swaps/ISwapWrapper.js +131 -73
  84. package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.d.ts +51 -6
  85. package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.js +22 -12
  86. package/dist/swaps/escrow_swaps/IEscrowSwap.d.ts +65 -12
  87. package/dist/swaps/escrow_swaps/IEscrowSwap.js +38 -19
  88. package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.d.ts +39 -9
  89. package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.js +30 -21
  90. package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.d.ts +31 -15
  91. package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.js +33 -18
  92. package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.d.ts +97 -28
  93. package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.js +91 -27
  94. package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.d.ts +22 -9
  95. package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.js +24 -11
  96. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.d.ts +278 -60
  97. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +519 -241
  98. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +77 -26
  99. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +132 -50
  100. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.d.ts +313 -52
  101. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +544 -194
  102. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +87 -26
  103. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +147 -58
  104. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.d.ts +222 -55
  105. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +462 -244
  106. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.d.ts +77 -23
  107. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.js +116 -46
  108. package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.d.ts +195 -58
  109. package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +324 -191
  110. package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.d.ts +30 -5
  111. package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.js +44 -19
  112. package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.d.ts +61 -20
  113. package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.js +75 -32
  114. package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.d.ts +76 -50
  115. package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.js +106 -101
  116. package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.d.ts +37 -14
  117. package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.js +66 -20
  118. package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.d.ts +46 -17
  119. package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.js +82 -27
  120. package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +350 -88
  121. package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +482 -215
  122. package/dist/swaps/spv_swaps/SpvFromBTCWrapper.d.ts +76 -24
  123. package/dist/swaps/spv_swaps/SpvFromBTCWrapper.js +247 -124
  124. package/dist/swaps/trusted/ln/LnForGasSwap.d.ts +148 -20
  125. package/dist/swaps/trusted/ln/LnForGasSwap.js +175 -45
  126. package/dist/swaps/trusted/ln/LnForGasWrapper.d.ts +29 -10
  127. package/dist/swaps/trusted/ln/LnForGasWrapper.js +30 -11
  128. package/dist/swaps/trusted/onchain/OnchainForGasSwap.d.ts +202 -49
  129. package/dist/swaps/trusted/onchain/OnchainForGasSwap.js +232 -80
  130. package/dist/swaps/trusted/onchain/OnchainForGasWrapper.d.ts +34 -12
  131. package/dist/swaps/trusted/onchain/OnchainForGasWrapper.js +33 -14
  132. package/dist/types/AmountData.d.ts +2 -1
  133. package/dist/types/CustomPriceFunction.d.ts +8 -2
  134. package/dist/types/PriceInfoType.d.ts +4 -4
  135. package/dist/types/PriceInfoType.js +3 -3
  136. package/dist/types/SwapExecutionAction.d.ts +85 -4
  137. package/dist/types/SwapWithSigner.d.ts +5 -2
  138. package/dist/types/SwapWithSigner.js +5 -2
  139. package/dist/types/Token.d.ts +11 -5
  140. package/dist/types/Token.js +6 -3
  141. package/dist/types/TokenAmount.d.ts +3 -0
  142. package/dist/types/TokenAmount.js +2 -0
  143. package/dist/types/fees/Fee.d.ts +3 -2
  144. package/dist/types/fees/FeeBreakdown.d.ts +3 -2
  145. package/dist/types/fees/PercentagePPM.d.ts +4 -2
  146. package/dist/types/fees/PercentagePPM.js +2 -1
  147. package/dist/types/lnurl/LNURLPay.d.ts +20 -12
  148. package/dist/types/lnurl/LNURLPay.js +8 -4
  149. package/dist/types/lnurl/LNURLWithdraw.d.ts +17 -10
  150. package/dist/types/lnurl/LNURLWithdraw.js +8 -4
  151. package/dist/types/wallets/LightningInvoiceCreateService.d.ts +24 -0
  152. package/dist/types/wallets/LightningInvoiceCreateService.js +15 -0
  153. package/dist/types/wallets/MinimalBitcoinWalletInterface.d.ts +3 -1
  154. package/dist/types/wallets/MinimalLightningNetworkWalletInterface.d.ts +4 -2
  155. package/dist/utils/BitcoinUtils.d.ts +1 -0
  156. package/dist/utils/BitcoinUtils.js +5 -1
  157. package/dist/utils/SwapUtils.d.ts +58 -1
  158. package/dist/utils/SwapUtils.js +55 -1
  159. package/dist/utils/TokenUtils.d.ts +10 -2
  160. package/dist/utils/TokenUtils.js +12 -4
  161. package/package.json +3 -3
  162. package/src/bitcoin/coinselect2/utils.ts +6 -0
  163. package/src/bitcoin/wallet/BitcoinWallet.ts +41 -5
  164. package/src/bitcoin/wallet/IBitcoinWallet.ts +57 -2
  165. package/src/bitcoin/wallet/SingleAddressBitcoinWallet.ts +42 -6
  166. package/src/enums/FeeType.ts +8 -1
  167. package/src/enums/SwapAmountType.ts +7 -0
  168. package/src/enums/SwapDirection.ts +7 -0
  169. package/src/enums/SwapType.ts +62 -2
  170. package/src/errors/IntermediaryError.ts +4 -0
  171. package/src/errors/RequestError.ts +15 -1
  172. package/src/errors/UserError.ts +1 -0
  173. package/src/index.ts +12 -5
  174. package/src/intermediaries/Intermediary.ts +61 -14
  175. package/src/intermediaries/IntermediaryDiscovery.ts +69 -34
  176. package/src/prices/RedundantSwapPrice.ts +26 -6
  177. package/src/prices/SingleSwapPrice.ts +11 -8
  178. package/src/prices/SwapPriceWithChain.ts +63 -26
  179. package/src/prices/abstract/IPriceProvider.ts +4 -4
  180. package/src/prices/abstract/ISwapPrice.ts +115 -66
  181. package/src/prices/providers/BinancePriceProvider.ts +8 -1
  182. package/src/prices/providers/CoinGeckoPriceProvider.ts +7 -1
  183. package/src/prices/providers/CoinPaprikaPriceProvider.ts +7 -1
  184. package/src/prices/providers/CustomPriceProvider.ts +12 -1
  185. package/src/prices/providers/KrakenPriceProvider.ts +10 -1
  186. package/src/prices/providers/OKXPriceProvider.ts +7 -1
  187. package/src/prices/providers/abstract/ExchangePriceProvider.ts +3 -0
  188. package/src/storage/IUnifiedStorage.ts +19 -7
  189. package/src/storage/UnifiedSwapStorage.ts +33 -3
  190. package/src/storage-browser/IndexedDBUnifiedStorage.ts +31 -8
  191. package/src/storage-browser/LocalStorageManager.ts +25 -1
  192. package/src/swapper/Swapper.ts +599 -390
  193. package/src/swapper/SwapperFactory.ts +73 -24
  194. package/src/swapper/SwapperUtils.ts +107 -60
  195. package/src/swapper/SwapperWithChain.ts +320 -81
  196. package/src/swapper/SwapperWithSigner.ts +263 -56
  197. package/src/swaps/IAddressSwap.ts +13 -3
  198. package/src/swaps/IBTCWalletSwap.ts +26 -10
  199. package/src/swaps/IClaimableSwap.ts +41 -6
  200. package/src/swaps/IClaimableSwapWrapper.ts +11 -2
  201. package/src/swaps/IRefundableSwap.ts +34 -5
  202. package/src/swaps/ISwap.ts +224 -85
  203. package/src/swaps/ISwapWithGasDrop.ts +8 -2
  204. package/src/swaps/ISwapWrapper.ts +216 -98
  205. package/src/swaps/escrow_swaps/IEscrowSelfInitSwap.ts +64 -18
  206. package/src/swaps/escrow_swaps/IEscrowSwap.ts +83 -37
  207. package/src/swaps/escrow_swaps/IEscrowSwapWrapper.ts +61 -30
  208. package/src/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.ts +37 -19
  209. package/src/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.ts +123 -50
  210. package/src/swaps/escrow_swaps/frombtc/IFromBTCWrapper.ts +24 -11
  211. package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.ts +562 -258
  212. package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +156 -62
  213. package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +592 -227
  214. package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +177 -74
  215. package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.ts +483 -245
  216. package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.ts +141 -59
  217. package/src/swaps/escrow_swaps/tobtc/IToBTCSwap.ts +350 -195
  218. package/src/swaps/escrow_swaps/tobtc/IToBTCWrapper.ts +48 -23
  219. package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.ts +87 -40
  220. package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.ts +110 -110
  221. package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.ts +89 -34
  222. package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.ts +101 -31
  223. package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +556 -259
  224. package/src/swaps/spv_swaps/SpvFromBTCWrapper.ts +292 -148
  225. package/src/swaps/trusted/ln/LnForGasSwap.ts +186 -47
  226. package/src/swaps/trusted/ln/LnForGasWrapper.ts +34 -15
  227. package/src/swaps/trusted/onchain/OnchainForGasSwap.ts +262 -88
  228. package/src/swaps/trusted/onchain/OnchainForGasWrapper.ts +41 -19
  229. package/src/types/AmountData.ts +2 -1
  230. package/src/types/CustomPriceFunction.ts +8 -2
  231. package/src/types/PriceInfoType.ts +4 -4
  232. package/src/types/SwapExecutionAction.ts +97 -5
  233. package/src/types/SwapWithSigner.ts +8 -4
  234. package/src/types/Token.ts +12 -5
  235. package/src/types/TokenAmount.ts +3 -0
  236. package/src/types/fees/Fee.ts +3 -2
  237. package/src/types/fees/FeeBreakdown.ts +3 -2
  238. package/src/types/fees/PercentagePPM.ts +4 -2
  239. package/src/types/lnurl/LNURLPay.ts +20 -12
  240. package/src/types/lnurl/LNURLWithdraw.ts +17 -10
  241. package/src/types/wallets/LightningInvoiceCreateService.ts +30 -0
  242. package/src/types/wallets/MinimalBitcoinWalletInterface.ts +3 -1
  243. package/src/types/wallets/MinimalLightningNetworkWalletInterface.ts +4 -2
  244. package/src/utils/BitcoinUtils.ts +5 -0
  245. package/src/utils/SwapUtils.ts +63 -1
  246. package/src/utils/TokenUtils.ts +12 -4
  247. package/dist/bitcoin/BitcoinRpcWithAddressIndex.d.ts +0 -68
  248. package/dist/bitcoin/BitcoinRpcWithAddressIndex.js +0 -2
  249. package/dist/bitcoin/LightningNetworkApi.d.ts +0 -12
  250. package/dist/bitcoin/LightningNetworkApi.js +0 -2
  251. package/dist/bitcoin/mempool/MempoolApi.d.ts +0 -350
  252. package/dist/bitcoin/mempool/MempoolApi.js +0 -311
  253. package/dist/bitcoin/mempool/MempoolBitcoinBlock.d.ts +0 -44
  254. package/dist/bitcoin/mempool/MempoolBitcoinBlock.js +0 -48
  255. package/dist/bitcoin/mempool/MempoolBitcoinRpc.d.ts +0 -119
  256. package/dist/bitcoin/mempool/MempoolBitcoinRpc.js +0 -361
  257. package/dist/bitcoin/mempool/synchronizer/MempoolBtcRelaySynchronizer.d.ts +0 -22
  258. package/dist/bitcoin/mempool/synchronizer/MempoolBtcRelaySynchronizer.js +0 -105
  259. package/dist/errors/PaymentAuthError.d.ts +0 -11
  260. package/dist/errors/PaymentAuthError.js +0 -23
  261. package/src/errors/PaymentAuthError.ts +0 -26
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ToBTCSwapState = exports.IToBTCSwap = exports.isIToBTCSwapInit = void 0;
3
+ exports.IToBTCSwap = exports.ToBTCSwapState = exports.isIToBTCSwapInit = void 0;
4
4
  const base_1 = require("@atomiqlabs/base");
5
5
  const IntermediaryAPI_1 = require("../../../intermediaries/apis/IntermediaryAPI");
6
6
  const IntermediaryError_1 = require("../../../errors/IntermediaryError");
@@ -21,14 +21,66 @@ function isIToBTCSwapInit(obj) {
21
21
  (0, IEscrowSelfInitSwap_1.isIEscrowSelfInitSwapInit)(obj);
22
22
  }
23
23
  exports.isIToBTCSwapInit = isIToBTCSwapInit;
24
+ /**
25
+ * State enum for escrow-based Smart chain -> Bitcoin (on-chain & lightning) swaps
26
+ *
27
+ * @category Swaps/Smart chain → Bitcoin
28
+ */
29
+ var ToBTCSwapState;
30
+ (function (ToBTCSwapState) {
31
+ /**
32
+ * Intermediary (LP) was unable to process the swap and the funds were refunded on the
33
+ * source chain
34
+ */
35
+ ToBTCSwapState[ToBTCSwapState["REFUNDED"] = -3] = "REFUNDED";
36
+ /**
37
+ * Swap has expired for good and there is no way how it can be executed anymore
38
+ */
39
+ ToBTCSwapState[ToBTCSwapState["QUOTE_EXPIRED"] = -2] = "QUOTE_EXPIRED";
40
+ /**
41
+ * A swap is almost expired, and it should be presented to the user as expired, though
42
+ * there is still a chance that it will be processed
43
+ */
44
+ ToBTCSwapState[ToBTCSwapState["QUOTE_SOFT_EXPIRED"] = -1] = "QUOTE_SOFT_EXPIRED";
45
+ /**
46
+ * Swap was created, use the {@link IToBTCSwap.commit} or {@link IToBTCSwap.txsCommit} to
47
+ * initiate it by creating the swap escrow on the source chain
48
+ */
49
+ ToBTCSwapState[ToBTCSwapState["CREATED"] = 0] = "CREATED";
50
+ /**
51
+ * Swap escrow was initiated (committed) on the source chain, the intermediary (LP) will
52
+ * now process the swap. You can wait till that happens with the {@link IToBTCSwap.waitForPayment}
53
+ * function.
54
+ */
55
+ ToBTCSwapState[ToBTCSwapState["COMMITED"] = 1] = "COMMITED";
56
+ /**
57
+ * The intermediary (LP) has processed the transaction and sent out the funds on the destination chain,
58
+ * but hasn't yet settled the escrow on the source chain.
59
+ */
60
+ ToBTCSwapState[ToBTCSwapState["SOFT_CLAIMED"] = 2] = "SOFT_CLAIMED";
61
+ /**
62
+ * Swap was successfully settled by the intermediary (LP) on the source chain
63
+ */
64
+ ToBTCSwapState[ToBTCSwapState["CLAIMED"] = 3] = "CLAIMED";
65
+ /**
66
+ * Intermediary (LP) was unable to process the swap and the swap escrow on the source chain
67
+ * is refundable, call {@link IToBTCSwap.refund} or {@link IToBTCSwap.txsRefund} to refund
68
+ */
69
+ ToBTCSwapState[ToBTCSwapState["REFUNDABLE"] = 4] = "REFUNDABLE";
70
+ })(ToBTCSwapState = exports.ToBTCSwapState || (exports.ToBTCSwapState = {}));
71
+ /**
72
+ * Base class for escrow-based Smart chain -> Bitcoin (on-chain & lightning) swaps
73
+ *
74
+ * @category Swaps/Smart chain → Bitcoin
75
+ */
24
76
  class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
25
77
  constructor(wrapper, initOrObject) {
26
78
  super(wrapper, initOrObject);
27
79
  if (isIToBTCSwapInit(initOrObject)) {
28
- this.state = ToBTCSwapState.CREATED;
80
+ this._state = ToBTCSwapState.CREATED;
29
81
  this.networkFee = initOrObject.networkFee;
30
82
  this.networkFeeBtc = initOrObject.networkFeeBtc;
31
- this.data = initOrObject.data;
83
+ this._data = initOrObject.data;
32
84
  this.signatureData = initOrObject.signatureData;
33
85
  }
34
86
  else {
@@ -36,37 +88,45 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
36
88
  this.networkFeeBtc = (0, Utils_1.toBigInt)(initOrObject.networkFeeBtc);
37
89
  }
38
90
  }
91
+ /**
92
+ * @inheritDoc
93
+ * @internal
94
+ */
39
95
  getSwapData() {
40
- return this.data;
96
+ return this._data;
41
97
  }
98
+ /**
99
+ * @inheritDoc
100
+ * @internal
101
+ */
42
102
  upgradeVersion() {
43
103
  if (this.version == null) {
44
- switch (this.state) {
104
+ switch (this._state) {
45
105
  case -2:
46
- this.state = ToBTCSwapState.REFUNDED;
106
+ this._state = ToBTCSwapState.REFUNDED;
47
107
  break;
48
108
  case -1:
49
- this.state = ToBTCSwapState.QUOTE_EXPIRED;
109
+ this._state = ToBTCSwapState.QUOTE_EXPIRED;
50
110
  break;
51
111
  case 0:
52
- this.state = ToBTCSwapState.CREATED;
112
+ this._state = ToBTCSwapState.CREATED;
53
113
  break;
54
114
  case 1:
55
- this.state = ToBTCSwapState.COMMITED;
115
+ this._state = ToBTCSwapState.COMMITED;
56
116
  break;
57
117
  case 2:
58
- this.state = ToBTCSwapState.CLAIMED;
118
+ this._state = ToBTCSwapState.CLAIMED;
59
119
  break;
60
120
  case 3:
61
- this.state = ToBTCSwapState.REFUNDABLE;
121
+ this._state = ToBTCSwapState.REFUNDABLE;
62
122
  break;
63
123
  }
64
124
  this.version = 1;
65
125
  }
66
126
  }
67
127
  /**
68
- * In case swapFee in BTC is not supplied it recalculates it based on swap price
69
- * @protected
128
+ * @inheritDoc
129
+ * @internal
70
130
  */
71
131
  tryRecomputeSwapPrice() {
72
132
  const output = this.getOutput();
@@ -82,91 +142,131 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
82
142
  }
83
143
  /**
84
144
  * Returns the payment hash identifier to be sent to the LP for getStatus and getRefund
85
- * @protected
145
+ * @internal
86
146
  */
87
147
  getLpIdentifier() {
88
148
  return this.getClaimHash();
89
149
  }
90
150
  //////////////////////////////
91
151
  //// Getters & utils
152
+ /**
153
+ * @inheritDoc
154
+ */
92
155
  getInputAddress() {
93
156
  return this._getInitiator();
94
157
  }
158
+ /**
159
+ * @inheritDoc
160
+ */
95
161
  getInputTxId() {
96
- return this.commitTxId ?? null;
162
+ return this._commitTxId ?? null;
97
163
  }
164
+ /**
165
+ * @inheritDoc
166
+ */
98
167
  requiresAction() {
99
168
  return this.isRefundable();
100
169
  }
101
170
  /**
102
- * Returns whether the swap is finished and in its terminal state (this can mean successful, refunded or failed)
171
+ * @inheritDoc
103
172
  */
104
173
  isFinished() {
105
- return this.state === ToBTCSwapState.CLAIMED || this.state === ToBTCSwapState.REFUNDED || this.state === ToBTCSwapState.QUOTE_EXPIRED;
174
+ return this._state === ToBTCSwapState.CLAIMED || this._state === ToBTCSwapState.REFUNDED || this._state === ToBTCSwapState.QUOTE_EXPIRED;
106
175
  }
176
+ /**
177
+ * @inheritDoc
178
+ */
107
179
  isRefundable() {
108
- return this.state === ToBTCSwapState.REFUNDABLE;
180
+ return this._state === ToBTCSwapState.REFUNDABLE;
109
181
  }
182
+ /**
183
+ * @inheritDoc
184
+ */
110
185
  isQuoteExpired() {
111
- return this.state === ToBTCSwapState.QUOTE_EXPIRED;
186
+ return this._state === ToBTCSwapState.QUOTE_EXPIRED;
112
187
  }
188
+ /**
189
+ * @inheritDoc
190
+ */
113
191
  isQuoteSoftExpired() {
114
- return this.state === ToBTCSwapState.QUOTE_EXPIRED || this.state === ToBTCSwapState.QUOTE_SOFT_EXPIRED;
192
+ return this._state === ToBTCSwapState.QUOTE_EXPIRED || this._state === ToBTCSwapState.QUOTE_SOFT_EXPIRED;
115
193
  }
194
+ /**
195
+ * @inheritDoc
196
+ */
116
197
  isSuccessful() {
117
- return this.state === ToBTCSwapState.CLAIMED;
198
+ return this._state === ToBTCSwapState.CLAIMED;
118
199
  }
200
+ /**
201
+ * @inheritDoc
202
+ */
119
203
  isFailed() {
120
- return this.state === ToBTCSwapState.REFUNDED;
204
+ return this._state === ToBTCSwapState.REFUNDED;
121
205
  }
206
+ /**
207
+ * @inheritDoc
208
+ * @internal
209
+ */
122
210
  _getInitiator() {
123
- return this.data.getOfferer();
211
+ return this._data.getOfferer();
124
212
  }
125
213
  //////////////////////////////
126
214
  //// Amounts & fees
215
+ /**
216
+ * Returns the swap fee charged by the intermediary (LP) on this swap
217
+ *
218
+ * @internal
219
+ */
127
220
  getSwapFee() {
128
221
  if (this.pricingInfo == null)
129
222
  throw new Error("No pricing info known, cannot estimate fee!");
130
223
  const feeWithoutBaseFee = this.swapFeeBtc - this.pricingInfo.satsBaseFee;
131
224
  const output = this.getOutput();
132
225
  const swapFeePPM = output.rawAmount == null ? 0n : feeWithoutBaseFee * 1000000n / output.rawAmount;
133
- const amountInDstToken = (0, TokenAmount_1.toTokenAmount)(this.swapFeeBtc, this.outputToken, this.wrapper.prices, this.pricingInfo);
226
+ const amountInDstToken = (0, TokenAmount_1.toTokenAmount)(this.swapFeeBtc, this.outputToken, this.wrapper._prices, this.pricingInfo);
134
227
  return {
135
- amountInSrcToken: (0, TokenAmount_1.toTokenAmount)(this.swapFee, this.wrapper.tokens[this.data.getToken()], this.wrapper.prices, this.pricingInfo),
228
+ amountInSrcToken: (0, TokenAmount_1.toTokenAmount)(this.swapFee, this.wrapper._tokens[this._data.getToken()], this.wrapper._prices, this.pricingInfo),
136
229
  amountInDstToken,
137
230
  currentUsdValue: amountInDstToken.currentUsdValue,
138
231
  usdValue: amountInDstToken.usdValue,
139
232
  pastUsdValue: amountInDstToken.pastUsdValue,
140
233
  composition: {
141
- base: (0, TokenAmount_1.toTokenAmount)(this.pricingInfo.satsBaseFee, this.outputToken, this.wrapper.prices, this.pricingInfo),
234
+ base: (0, TokenAmount_1.toTokenAmount)(this.pricingInfo.satsBaseFee, this.outputToken, this.wrapper._prices, this.pricingInfo),
142
235
  percentage: (0, PercentagePPM_1.ppmToPercentage)(swapFeePPM)
143
236
  }
144
237
  };
145
238
  }
146
239
  /**
147
- * Returns network fee for the swap, the fee is represented in source currency & destination currency, but is
148
- * paid only once
240
+ * Returns network fee for on the destination chain for the swap
241
+ *
242
+ * @internal
149
243
  */
150
244
  getNetworkFee() {
151
- const amountInDstToken = (0, TokenAmount_1.toTokenAmount)(this.networkFeeBtc, this.outputToken, this.wrapper.prices, this.pricingInfo);
245
+ const amountInDstToken = (0, TokenAmount_1.toTokenAmount)(this.networkFeeBtc, this.outputToken, this.wrapper._prices, this.pricingInfo);
152
246
  return {
153
- amountInSrcToken: (0, TokenAmount_1.toTokenAmount)(this.networkFee, this.wrapper.tokens[this.data.getToken()], this.wrapper.prices, this.pricingInfo),
247
+ amountInSrcToken: (0, TokenAmount_1.toTokenAmount)(this.networkFee, this.wrapper._tokens[this._data.getToken()], this.wrapper._prices, this.pricingInfo),
154
248
  amountInDstToken,
155
249
  currentUsdValue: amountInDstToken.currentUsdValue,
156
250
  usdValue: amountInDstToken.usdValue,
157
251
  pastUsdValue: amountInDstToken.pastUsdValue
158
252
  };
159
253
  }
254
+ /**
255
+ * @inheritDoc
256
+ */
160
257
  getFee() {
161
- const amountInDstToken = (0, TokenAmount_1.toTokenAmount)(this.swapFeeBtc + this.networkFeeBtc, this.outputToken, this.wrapper.prices, this.pricingInfo);
258
+ const amountInDstToken = (0, TokenAmount_1.toTokenAmount)(this.swapFeeBtc + this.networkFeeBtc, this.outputToken, this.wrapper._prices, this.pricingInfo);
162
259
  return {
163
- amountInSrcToken: (0, TokenAmount_1.toTokenAmount)(this.swapFee + this.networkFee, this.wrapper.tokens[this.data.getToken()], this.wrapper.prices, this.pricingInfo),
260
+ amountInSrcToken: (0, TokenAmount_1.toTokenAmount)(this.swapFee + this.networkFee, this.wrapper._tokens[this._data.getToken()], this.wrapper._prices, this.pricingInfo),
164
261
  amountInDstToken,
165
262
  currentUsdValue: amountInDstToken.currentUsdValue,
166
263
  usdValue: amountInDstToken.usdValue,
167
264
  pastUsdValue: amountInDstToken.pastUsdValue
168
265
  };
169
266
  }
267
+ /**
268
+ * @inheritDoc
269
+ */
170
270
  getFeeBreakdown() {
171
271
  return [
172
272
  {
@@ -179,44 +279,54 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
179
279
  }
180
280
  ];
181
281
  }
282
+ /**
283
+ * @inheritDoc
284
+ */
182
285
  getInputToken() {
183
- return this.wrapper.tokens[this.data.getToken()];
286
+ return this.wrapper._tokens[this._data.getToken()];
184
287
  }
288
+ /**
289
+ * @inheritDoc
290
+ */
185
291
  getInput() {
186
- return (0, TokenAmount_1.toTokenAmount)(this.data.getAmount(), this.wrapper.tokens[this.data.getToken()], this.wrapper.prices, this.pricingInfo);
292
+ return (0, TokenAmount_1.toTokenAmount)(this._data.getAmount(), this.wrapper._tokens[this._data.getToken()], this.wrapper._prices, this.pricingInfo);
187
293
  }
294
+ /**
295
+ * @inheritDoc
296
+ */
188
297
  getInputWithoutFee() {
189
- return (0, TokenAmount_1.toTokenAmount)(this.data.getAmount() - (this.swapFee + this.networkFee), this.wrapper.tokens[this.data.getToken()], this.wrapper.prices, this.pricingInfo);
298
+ return (0, TokenAmount_1.toTokenAmount)(this._data.getAmount() - (this.swapFee + this.networkFee), this.wrapper._tokens[this._data.getToken()], this.wrapper._prices, this.pricingInfo);
190
299
  }
191
300
  /**
192
- * Checks if the intiator/sender has enough balance to go through with the swap
301
+ * Checks if the initiator/sender on the source chain has enough balance to go through with the swap
193
302
  */
194
303
  async hasEnoughBalance() {
195
304
  const [balance, commitFee] = await Promise.all([
196
- this.wrapper.contract.getBalance(this._getInitiator(), this.data.getToken(), false),
197
- this.data.getToken() === this.wrapper.chain.getNativeCurrencyAddress() ? this.getCommitFee() : Promise.resolve(null)
305
+ this.wrapper._contract.getBalance(this._getInitiator(), this._data.getToken(), false),
306
+ this._data.getToken() === this.wrapper._chain.getNativeCurrencyAddress() ? this.getCommitFee() : Promise.resolve(null)
198
307
  ]);
199
- let required = this.data.getAmount();
308
+ let required = this._data.getAmount();
200
309
  if (commitFee != null)
201
310
  required = required + commitFee;
202
311
  return {
203
312
  enoughBalance: balance >= required,
204
- balance: (0, TokenAmount_1.toTokenAmount)(balance, this.wrapper.tokens[this.data.getToken()], this.wrapper.prices, this.pricingInfo),
205
- required: (0, TokenAmount_1.toTokenAmount)(required, this.wrapper.tokens[this.data.getToken()], this.wrapper.prices, this.pricingInfo)
313
+ balance: (0, TokenAmount_1.toTokenAmount)(balance, this.wrapper._tokens[this._data.getToken()], this.wrapper._prices, this.pricingInfo),
314
+ required: (0, TokenAmount_1.toTokenAmount)(required, this.wrapper._tokens[this._data.getToken()], this.wrapper._prices, this.pricingInfo)
206
315
  };
207
316
  }
208
317
  /**
209
- * Check if the initiator/sender has enough balance to cover the transaction fee for processing the swap
318
+ * Checks if the initiator/sender on the source chain has enough native token balance
319
+ * to cover the transaction fee of initiating the swap
210
320
  */
211
321
  async hasEnoughForTxFees() {
212
322
  const [balance, commitFee] = await Promise.all([
213
- this.wrapper.contract.getBalance(this._getInitiator(), this.wrapper.chain.getNativeCurrencyAddress(), false),
323
+ this.wrapper._contract.getBalance(this._getInitiator(), this.wrapper._chain.getNativeCurrencyAddress(), false),
214
324
  this.getCommitFee()
215
325
  ]);
216
326
  return {
217
327
  enoughBalance: balance >= commitFee,
218
- balance: (0, TokenAmount_1.toTokenAmount)(balance, this.wrapper.getNativeToken(), this.wrapper.prices),
219
- required: (0, TokenAmount_1.toTokenAmount)(commitFee, this.wrapper.getNativeToken(), this.wrapper.prices)
328
+ balance: (0, TokenAmount_1.toTokenAmount)(balance, this.wrapper._getNativeToken(), this.wrapper._prices),
329
+ required: (0, TokenAmount_1.toTokenAmount)(commitFee, this.wrapper._getNativeToken(), this.wrapper._prices)
220
330
  };
221
331
  }
222
332
  //////////////////////////////
@@ -229,26 +339,26 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
229
339
  * @param options Optional options for the swap like feeRate, AbortSignal, and timeouts/intervals
230
340
  *
231
341
  * @returns {boolean} Whether the swap was successfully processed by the LP, in case `false` is returned
232
- * the user can refund their funds back on the source chain by calling `swap.refund()`
342
+ * the user can refund their funds back on the source chain by calling {@link refund}
233
343
  */
234
344
  async execute(signer, callbacks, options) {
235
- if (this.state === ToBTCSwapState.QUOTE_EXPIRED || this.state === ToBTCSwapState.QUOTE_SOFT_EXPIRED)
345
+ if (this._state === ToBTCSwapState.QUOTE_EXPIRED || this._state === ToBTCSwapState.QUOTE_SOFT_EXPIRED)
236
346
  throw new Error("Quote expired");
237
- if (this.state === ToBTCSwapState.REFUNDED)
347
+ if (this._state === ToBTCSwapState.REFUNDED)
238
348
  throw new Error("Swap already refunded");
239
- if (this.state === ToBTCSwapState.REFUNDABLE)
349
+ if (this._state === ToBTCSwapState.REFUNDABLE)
240
350
  throw new Error("Swap refundable, refund with swap.refund()");
241
- if (this.state === ToBTCSwapState.SOFT_CLAIMED || this.state === ToBTCSwapState.CLAIMED)
351
+ if (this._state === ToBTCSwapState.SOFT_CLAIMED || this._state === ToBTCSwapState.CLAIMED)
242
352
  throw new Error("Swap already settled!");
243
- if (this.state === ToBTCSwapState.CREATED) {
353
+ if (this._state === ToBTCSwapState.CREATED) {
244
354
  const txId = await this.commit(signer, options?.abortSignal, false, callbacks?.onSourceTransactionSent);
245
355
  if (callbacks?.onSourceTransactionConfirmed != null)
246
356
  callbacks.onSourceTransactionConfirmed(txId);
247
357
  }
248
358
  // @ts-ignore
249
- if (this.state === ToBTCSwapState.CLAIMED || this.state === ToBTCSwapState.SOFT_CLAIMED)
359
+ if (this._state === ToBTCSwapState.CLAIMED || this._state === ToBTCSwapState.SOFT_CLAIMED)
250
360
  return true;
251
- if (this.state === ToBTCSwapState.COMMITED) {
361
+ if (this._state === ToBTCSwapState.COMMITED) {
252
362
  const success = await this.waitForPayment(options?.maxWaitTillSwapProcessedSeconds ?? 120, options?.paymentCheckIntervalSeconds, options?.abortSignal);
253
363
  if (success) {
254
364
  if (callbacks?.onSwapSettled != null)
@@ -261,8 +371,13 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
261
371
  }
262
372
  throw new Error("Unexpected state reached!");
263
373
  }
374
+ /**
375
+ * @inheritDoc
376
+ * @param options.skipChecks Skip checks like making sure init signature is still valid and swap wasn't commited yet
377
+ * (this is handled on swap creation, if you commit right after quoting, you can use `skipChecks=true`)
378
+ */
264
379
  async txsExecute(options) {
265
- if (this.state !== ToBTCSwapState.CREATED)
380
+ if (this._state !== ToBTCSwapState.CREATED)
266
381
  throw new Error("Invalid swap state, needs to be CREATED!");
267
382
  const txsCommit = await this.txsCommit(options?.skipChecks);
268
383
  return [
@@ -277,15 +392,12 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
277
392
  //////////////////////////////
278
393
  //// Commit
279
394
  /**
280
- * Returns transactions for committing the swap on-chain, initiating the swap
281
- *
282
- * @param skipChecks Skip checks like making sure init signature is still valid and swap wasn't commited yet
283
- * (this is handled on swap creation, if you commit right after quoting, you can use skipChecks=true)
395
+ * @inheritDoc
284
396
  *
285
- * @throws {Error} When in invalid state (not PR_CREATED)
397
+ * @throws {Error} When in invalid state (not {@link ToBTCSwapState.CREATED})
286
398
  */
287
399
  async txsCommit(skipChecks) {
288
- if (this.state !== ToBTCSwapState.CREATED)
400
+ if (this._state !== ToBTCSwapState.CREATED)
289
401
  throw new Error("Must be in CREATED state!");
290
402
  if (this.signatureData == null)
291
403
  throw new Error("Init signature data not known, cannot commit!");
@@ -293,45 +405,39 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
293
405
  this.initiated = true;
294
406
  await this._saveAndEmit();
295
407
  }
296
- return await this.wrapper.contract.txsInit(this._getInitiator(), this.data, this.signatureData, skipChecks, this.feeRate).catch(e => Promise.reject(e instanceof base_1.SignatureVerificationError ? new Error("Request timed out") : e));
408
+ return await this.wrapper._contract.txsInit(this._getInitiator(), this._data, this.signatureData, skipChecks, this.feeRate).catch(e => Promise.reject(e instanceof base_1.SignatureVerificationError ? new Error("Request timed out") : e));
297
409
  }
298
410
  /**
299
- * Commits the swap on-chain, initiating the swap
411
+ * @inheritDoc
300
412
  *
301
- * @param _signer Signer to sign the transactions with, must be the same as used in the initialization
302
- * @param abortSignal Abort signal
303
- * @param skipChecks Skip checks like making sure init signature is still valid and swap wasn't commited yet
304
- * (this is handled on swap creation, if you commit right after quoting, you can skipChecks)`
305
- * @param onBeforeTxSent
306
413
  * @throws {Error} If invalid signer is provided that doesn't match the swap data
307
414
  */
308
415
  async commit(_signer, abortSignal, skipChecks, onBeforeTxSent) {
309
- const signer = (0, base_1.isAbstractSigner)(_signer) ? _signer : await this.wrapper.chain.wrapSigner(_signer);
416
+ const signer = (0, base_1.isAbstractSigner)(_signer) ? _signer : await this.wrapper._chain.wrapSigner(_signer);
310
417
  this.checkSigner(signer);
311
418
  const txs = await this.txsCommit(skipChecks);
312
419
  let txCount = 0;
313
- const result = await this.wrapper.chain.sendAndConfirm(signer, txs, true, abortSignal, false, (txId, rawTx) => {
420
+ const result = await this.wrapper._chain.sendAndConfirm(signer, txs, true, abortSignal, false, (txId, rawTx) => {
314
421
  txCount++;
315
422
  if (onBeforeTxSent != null && txCount === txs.length)
316
423
  onBeforeTxSent(txId);
317
424
  return Promise.resolve();
318
425
  });
319
- this.commitTxId = result[result.length - 1];
320
- if (this.state === ToBTCSwapState.CREATED || this.state === ToBTCSwapState.QUOTE_SOFT_EXPIRED || this.state === ToBTCSwapState.QUOTE_EXPIRED) {
426
+ this._commitTxId = result[result.length - 1];
427
+ if (this._state === ToBTCSwapState.CREATED || this._state === ToBTCSwapState.QUOTE_SOFT_EXPIRED || this._state === ToBTCSwapState.QUOTE_EXPIRED) {
321
428
  await this._saveAndEmit(ToBTCSwapState.COMMITED);
322
429
  }
323
- return this.commitTxId;
430
+ return this._commitTxId;
324
431
  }
325
432
  /**
326
- * Waits till a swap is committed, should be called after sending the commit transactions manually
433
+ * @inheritDoc
327
434
  *
328
- * @param abortSignal AbortSignal
329
- * @throws {Error} If swap is not in the correct state (must be CREATED)
435
+ * @throws {Error} If swap is not in the correct state (must be {@link ToBTCSwapState.CREATED})
330
436
  */
331
437
  async waitTillCommited(abortSignal) {
332
- if (this.state === ToBTCSwapState.COMMITED || this.state === ToBTCSwapState.CLAIMED)
438
+ if (this._state === ToBTCSwapState.COMMITED || this._state === ToBTCSwapState.CLAIMED)
333
439
  return Promise.resolve();
334
- if (this.state !== ToBTCSwapState.CREATED && this.state !== ToBTCSwapState.QUOTE_SOFT_EXPIRED)
440
+ if (this._state !== ToBTCSwapState.CREATED && this._state !== ToBTCSwapState.QUOTE_SOFT_EXPIRED)
335
441
  throw new Error("Invalid state (not CREATED)");
336
442
  const abortController = (0, Utils_1.extendAbortController)(abortSignal);
337
443
  let result;
@@ -352,28 +458,35 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
352
458
  this.logger.debug("waitTillCommited(): Resolved from watchdog - commited");
353
459
  if (result === false) {
354
460
  this.logger.debug("waitTillCommited(): Resolved from watchdog - signature expiry");
355
- if (this.state === ToBTCSwapState.QUOTE_SOFT_EXPIRED || this.state === ToBTCSwapState.CREATED) {
461
+ if (this._state === ToBTCSwapState.QUOTE_SOFT_EXPIRED || this._state === ToBTCSwapState.CREATED) {
356
462
  await this._saveAndEmit(ToBTCSwapState.QUOTE_EXPIRED);
357
463
  }
358
464
  throw new Error("Quote expired while waiting for transaction confirmation!");
359
465
  }
360
- if (this.state === ToBTCSwapState.QUOTE_SOFT_EXPIRED || this.state === ToBTCSwapState.CREATED || this.state === ToBTCSwapState.QUOTE_EXPIRED) {
466
+ if (this._state === ToBTCSwapState.QUOTE_SOFT_EXPIRED || this._state === ToBTCSwapState.CREATED || this._state === ToBTCSwapState.QUOTE_EXPIRED) {
361
467
  await this._saveAndEmit(ToBTCSwapState.COMMITED);
362
468
  }
363
469
  }
364
470
  //////////////////////////////
365
471
  //// Payment
472
+ /**
473
+ * Waits till the swap is processed by the intermediary (LP)
474
+ *
475
+ * @param checkIntervalSeconds How often to poll the intermediary for status (5 seconds default)
476
+ * @param abortSignal Abort signal
477
+ * @internal
478
+ */
366
479
  async waitTillIntermediarySwapProcessed(checkIntervalSeconds, abortSignal) {
367
480
  if (this.url == null)
368
481
  throw new Error("LP URL not specified!");
369
482
  checkIntervalSeconds ??= 5;
370
483
  let resp = { code: IntermediaryAPI_1.RefundAuthorizationResponseCodes.PENDING, msg: "" };
371
484
  while (!abortSignal?.aborted && (resp.code === IntermediaryAPI_1.RefundAuthorizationResponseCodes.PENDING || resp.code === IntermediaryAPI_1.RefundAuthorizationResponseCodes.NOT_FOUND)) {
372
- resp = await IntermediaryAPI_1.IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this.data.getSequence());
485
+ resp = await IntermediaryAPI_1.IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this._data.getSequence());
373
486
  if (resp.code === IntermediaryAPI_1.RefundAuthorizationResponseCodes.PAID) {
374
487
  const validResponse = await this._setPaymentResult(resp.data, true);
375
488
  if (validResponse) {
376
- if (this.state === ToBTCSwapState.COMMITED || this.state === ToBTCSwapState.REFUNDABLE) {
489
+ if (this._state === ToBTCSwapState.COMMITED || this._state === ToBTCSwapState.REFUNDABLE) {
377
490
  await this._saveAndEmit(ToBTCSwapState.SOFT_CLAIMED);
378
491
  }
379
492
  }
@@ -393,28 +506,29 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
393
506
  * refund.
394
507
  *
395
508
  * @param save whether to save the data
396
- * @returns true if swap is processed, false if the swap is still ongoing
397
- * @private
509
+ * @returns `true` if swap is processed, `false` if the swap is still ongoing
510
+ *
511
+ * @internal
398
512
  */
399
513
  async checkIntermediarySwapProcessed(save = true) {
400
- if (this.state === ToBTCSwapState.CREATED || this.state == ToBTCSwapState.QUOTE_EXPIRED || this.url == null)
514
+ if (this._state === ToBTCSwapState.CREATED || this._state == ToBTCSwapState.QUOTE_EXPIRED || this.url == null)
401
515
  return false;
402
516
  if (this.isFinished() || this.isRefundable())
403
517
  return true;
404
518
  //Check if that maybe already concluded according to the LP
405
- const resp = await IntermediaryAPI_1.IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this.data.getSequence());
519
+ const resp = await IntermediaryAPI_1.IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this._data.getSequence());
406
520
  switch (resp.code) {
407
521
  case IntermediaryAPI_1.RefundAuthorizationResponseCodes.PAID:
408
522
  const processed = await this._setPaymentResult(resp.data, true);
409
523
  if (processed) {
410
- this.state = ToBTCSwapState.SOFT_CLAIMED;
524
+ this._state = ToBTCSwapState.SOFT_CLAIMED;
411
525
  if (save)
412
526
  await this._saveAndEmit();
413
527
  }
414
528
  return processed;
415
529
  case IntermediaryAPI_1.RefundAuthorizationResponseCodes.REFUND_DATA:
416
- await this.wrapper.contract.isValidRefundAuthorization(this.data, resp.data);
417
- this.state = ToBTCSwapState.REFUNDABLE;
530
+ await this.wrapper._contract.isValidRefundAuthorization(this._data, resp.data);
531
+ this._state = ToBTCSwapState.REFUNDABLE;
418
532
  if (save)
419
533
  await this._saveAndEmit();
420
534
  return true;
@@ -423,23 +537,24 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
423
537
  }
424
538
  }
425
539
  /**
426
- * A blocking promise resolving when swap was concluded by the intermediary,
540
+ * A blocking promise resolving when swap was concluded by the intermediary (LP),
427
541
  * rejecting in case of failure
428
542
  *
429
543
  * @param maxWaitTimeSeconds Maximum time in seconds to wait for the swap to be settled, an error is thrown if the
430
544
  * swap is taking too long to claim
431
- * @param checkIntervalSeconds How often to poll the intermediary for answer
432
- * @param abortSignal Abort signal
433
- * @returns {Promise<boolean>} Was the payment successful? If not we can refund.
545
+ * @param checkIntervalSeconds How often to poll the intermediary for answer
546
+ * @param abortSignal Abort signal
547
+ * @returns `true` if swap was successful, `false` if swap failed and we can refund
548
+ *
434
549
  * @throws {IntermediaryError} If a swap is determined expired by the intermediary, but it is actually still valid
435
550
  * @throws {SignatureVerificationError} If the swap should be cooperatively refundable but the intermediary returned
436
551
  * invalid refund signature
437
- * @throws {Error} When swap expires or if the swap has invalid state (must be COMMITED)
552
+ * @throws {Error} When swap expires or if the swap has invalid state (must be {@link ToBTCSwapState.COMMITED})
438
553
  */
439
554
  async waitForPayment(maxWaitTimeSeconds, checkIntervalSeconds, abortSignal) {
440
- if (this.state === ToBTCSwapState.CLAIMED)
555
+ if (this._state === ToBTCSwapState.CLAIMED)
441
556
  return Promise.resolve(true);
442
- if (this.state !== ToBTCSwapState.COMMITED && this.state !== ToBTCSwapState.SOFT_CLAIMED)
557
+ if (this._state !== ToBTCSwapState.COMMITED && this._state !== ToBTCSwapState.SOFT_CLAIMED)
443
558
  throw new Error("Invalid state (not COMMITED)");
444
559
  const abortController = (0, Utils_1.extendAbortController)(abortSignal);
445
560
  let timedOut = false;
@@ -462,12 +577,12 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
462
577
  abortController.abort();
463
578
  if (timedOut) {
464
579
  throw new Error("Timed out while waiting for LP to process the swap, the LP might be unresponsive or offline!" +
465
- ` Please check later or wait till ${new Date(Number(this.data.getExpiry()) * 1000).toLocaleString()} to refund unilaterally!`);
580
+ ` Please check later or wait till ${new Date(Number(this._data.getExpiry()) * 1000).toLocaleString()} to refund unilaterally!`);
466
581
  }
467
582
  throw e;
468
583
  }
469
584
  if (typeof result !== "object") {
470
- if (this.state === ToBTCSwapState.REFUNDABLE)
585
+ if (this._state === ToBTCSwapState.REFUNDABLE)
471
586
  throw new Error("Swap expired");
472
587
  this.logger.debug("waitTillRefunded(): Resolved from state change");
473
588
  return true;
@@ -478,15 +593,15 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
478
593
  return true;
479
594
  case IntermediaryAPI_1.RefundAuthorizationResponseCodes.REFUND_DATA:
480
595
  const resultData = result.data;
481
- await this.wrapper.contract.isValidRefundAuthorization(this.data, resultData);
596
+ await this.wrapper._contract.isValidRefundAuthorization(this._data, resultData);
482
597
  await this._saveAndEmit(ToBTCSwapState.REFUNDABLE);
483
598
  return false;
484
599
  case IntermediaryAPI_1.RefundAuthorizationResponseCodes.EXPIRED:
485
- if (await this.wrapper.contract.isExpired(this._getInitiator(), this.data))
600
+ if (await this.wrapper._contract.isExpired(this._getInitiator(), this._data))
486
601
  throw new Error("Swap expired");
487
602
  throw new IntermediaryError_1.IntermediaryError("Swap expired");
488
603
  case IntermediaryAPI_1.RefundAuthorizationResponseCodes.NOT_FOUND:
489
- if (this.state === ToBTCSwapState.CLAIMED)
604
+ if (this._state === ToBTCSwapState.CLAIMED)
490
605
  return true;
491
606
  throw new Error("LP swap not found");
492
607
  }
@@ -497,11 +612,12 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
497
612
  /**
498
613
  * Get the estimated smart chain transaction fee of the refund transaction
499
614
  */
500
- getRefundFee() {
501
- return this.wrapper.contract.getRefundFee(this._getInitiator(), this.data);
615
+ async getRefundNetworkFee() {
616
+ const swapContract = this.wrapper._contract;
617
+ return (0, TokenAmount_1.toTokenAmount)(await swapContract.getRefundFee(this._getInitiator(), this._data), this.wrapper._getNativeToken(), this.wrapper._prices);
502
618
  }
503
619
  /**
504
- * Returns transactions for refunding the swap if the swap is in refundable state, you can check so with isRefundable()
620
+ * @inheritDoc
505
621
  *
506
622
  * @throws {IntermediaryError} If intermediary returns invalid response in case cooperative refund should be used
507
623
  * @throws {SignatureVerificationError} If intermediary returned invalid cooperative refund signature
@@ -511,46 +627,46 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
511
627
  if (!this.isRefundable())
512
628
  throw new Error("Must be in REFUNDABLE state or expired!");
513
629
  signer ??= this._getInitiator();
514
- if (await this.wrapper.contract.isExpired(this._getInitiator(), this.data)) {
515
- return await this.wrapper.contract.txsRefund(signer, this.data, true, true);
630
+ if (await this.wrapper._contract.isExpired(this._getInitiator(), this._data)) {
631
+ return await this.wrapper._contract.txsRefund(signer, this._data, true, true);
516
632
  }
517
633
  else {
518
634
  if (this.url == null)
519
635
  throw new Error("LP URL not known, cannot get cooperative refund message, wait till expiry to refund!");
520
- const res = await IntermediaryAPI_1.IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this.data.getSequence());
636
+ const res = await IntermediaryAPI_1.IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this._data.getSequence());
521
637
  if (res.code === IntermediaryAPI_1.RefundAuthorizationResponseCodes.REFUND_DATA) {
522
- return await this.wrapper.contract.txsRefundWithAuthorization(signer, this.data, res.data, true, true);
638
+ return await this.wrapper._contract.txsRefundWithAuthorization(signer, this._data, res.data, true, true);
523
639
  }
524
640
  throw new IntermediaryError_1.IntermediaryError("Invalid intermediary cooperative message returned");
525
641
  }
526
642
  }
527
643
  /**
528
- * Refunds the swap if the swap is in refundable state, you can check so with isRefundable()
644
+ * @inheritDoc
529
645
  *
530
- * @param _signer Signer to sign the transactions with, must be the same as used in the initialization
531
- * @param abortSignal Abort signal
532
646
  * @throws {Error} If invalid signer is provided that doesn't match the swap data
533
647
  */
534
648
  async refund(_signer, abortSignal) {
535
- const signer = (0, base_1.isAbstractSigner)(_signer) ? _signer : await this.wrapper.chain.wrapSigner(_signer);
536
- const result = await this.wrapper.chain.sendAndConfirm(signer, await this.txsRefund(signer.getAddress()), true, abortSignal);
537
- this.refundTxId = result[0];
538
- if (this.state === ToBTCSwapState.COMMITED || this.state === ToBTCSwapState.REFUNDABLE || this.state === ToBTCSwapState.SOFT_CLAIMED) {
649
+ const signer = (0, base_1.isAbstractSigner)(_signer) ? _signer : await this.wrapper._chain.wrapSigner(_signer);
650
+ const result = await this.wrapper._chain.sendAndConfirm(signer, await this.txsRefund(signer.getAddress()), true, abortSignal);
651
+ this._refundTxId = result[0];
652
+ if (this._state === ToBTCSwapState.COMMITED || this._state === ToBTCSwapState.REFUNDABLE || this._state === ToBTCSwapState.SOFT_CLAIMED) {
539
653
  await this._saveAndEmit(ToBTCSwapState.REFUNDED);
540
654
  }
541
655
  return result[0];
542
656
  }
543
657
  /**
544
- * Waits till a swap is refunded, should be called after sending the refund transactions manually
658
+ * @inheritDoc
545
659
  *
546
- * @param abortSignal AbortSignal
547
- * @throws {Error} When swap is not in a valid state (must be COMMITED)
660
+ * @throws {Error} When swap is not in a valid state (must be {@link ToBTCSwapState.COMMITED} or
661
+ * {@link ToBTCSwapState.REFUNDABLE})
548
662
  * @throws {Error} If we tried to refund but claimer was able to claim first
549
663
  */
550
664
  async waitTillRefunded(abortSignal) {
551
- if (this.state === ToBTCSwapState.REFUNDED)
665
+ if (this._state === ToBTCSwapState.REFUNDED)
552
666
  return Promise.resolve();
553
- if (this.state !== ToBTCSwapState.COMMITED && this.state !== ToBTCSwapState.SOFT_CLAIMED)
667
+ if (this._state !== ToBTCSwapState.COMMITED &&
668
+ this._state !== ToBTCSwapState.SOFT_CLAIMED &&
669
+ this._state !== ToBTCSwapState.REFUNDABLE)
554
670
  throw new Error("Invalid state (not COMMITED)");
555
671
  const abortController = new AbortController();
556
672
  if (abortSignal != null)
@@ -571,19 +687,22 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
571
687
  }
572
688
  this.logger.debug("waitTillRefunded(): Resolved from watchdog");
573
689
  if (res?.type === base_1.SwapCommitStateType.PAID) {
574
- if (this.claimTxId == null)
575
- this.claimTxId = await res.getClaimTxId();
690
+ if (this._claimTxId == null)
691
+ this._claimTxId = await res.getClaimTxId();
576
692
  await this._saveAndEmit(ToBTCSwapState.CLAIMED);
577
693
  throw new Error("Tried to refund swap, but claimer claimed it in the meantime!");
578
694
  }
579
695
  if (res?.type === base_1.SwapCommitStateType.NOT_COMMITED) {
580
- if (this.refundTxId == null && res.getRefundTxId != null)
581
- this.refundTxId = await res.getRefundTxId();
696
+ if (this._refundTxId == null && res.getRefundTxId != null)
697
+ this._refundTxId = await res.getRefundTxId();
582
698
  await this._saveAndEmit(ToBTCSwapState.REFUNDED);
583
699
  }
584
700
  }
585
701
  //////////////////////////////
586
702
  //// Storage
703
+ /**
704
+ * @inheritDoc
705
+ */
587
706
  serialize() {
588
707
  const obj = super.serialize();
589
708
  return {
@@ -601,75 +720,53 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
601
720
  * @private
602
721
  */
603
722
  async syncStateFromChain(quoteDefinitelyExpired, commitStatus) {
604
- if (this.state === ToBTCSwapState.CREATED ||
605
- this.state === ToBTCSwapState.QUOTE_SOFT_EXPIRED ||
606
- this.state === ToBTCSwapState.COMMITED ||
607
- this.state === ToBTCSwapState.SOFT_CLAIMED ||
608
- this.state === ToBTCSwapState.REFUNDABLE) {
723
+ if (this._state === ToBTCSwapState.CREATED ||
724
+ this._state === ToBTCSwapState.QUOTE_SOFT_EXPIRED ||
725
+ this._state === ToBTCSwapState.COMMITED ||
726
+ this._state === ToBTCSwapState.SOFT_CLAIMED ||
727
+ this._state === ToBTCSwapState.REFUNDABLE) {
609
728
  let quoteExpired = false;
610
- if (this.state === ToBTCSwapState.CREATED || this.state === ToBTCSwapState.QUOTE_SOFT_EXPIRED) {
729
+ if (this._state === ToBTCSwapState.CREATED || this._state === ToBTCSwapState.QUOTE_SOFT_EXPIRED) {
611
730
  //Check if quote is still valid
612
731
  quoteExpired = quoteDefinitelyExpired ?? await this._verifyQuoteDefinitelyExpired();
613
732
  }
614
- commitStatus ??= await this.wrapper.contract.getCommitStatus(this._getInitiator(), this.data);
615
- switch (commitStatus?.type) {
616
- case base_1.SwapCommitStateType.PAID:
617
- if (this.claimTxId == null && commitStatus.getClaimTxId)
618
- this.claimTxId = await commitStatus.getClaimTxId();
619
- const eventResult = await commitStatus.getClaimResult();
620
- try {
621
- await this._setPaymentResult({ secret: eventResult, txId: Buffer.from(eventResult, "hex").reverse().toString("hex") });
622
- }
623
- catch (e) {
624
- this.logger.error(`Failed to set payment result ${eventResult} on the swap!`);
625
- }
626
- this.state = ToBTCSwapState.CLAIMED;
627
- return true;
628
- case base_1.SwapCommitStateType.REFUNDABLE:
629
- this.state = ToBTCSwapState.REFUNDABLE;
630
- return true;
631
- case base_1.SwapCommitStateType.EXPIRED:
632
- if (this.refundTxId == null && commitStatus.getRefundTxId)
633
- this.refundTxId = await commitStatus.getRefundTxId();
634
- this.state = ToBTCSwapState.QUOTE_EXPIRED;
635
- return true;
636
- case base_1.SwapCommitStateType.NOT_COMMITED:
637
- if (this.refundTxId == null && commitStatus.getRefundTxId)
638
- this.refundTxId = await commitStatus.getRefundTxId();
639
- if (this.state === ToBTCSwapState.COMMITED || this.state === ToBTCSwapState.REFUNDABLE) {
640
- this.state = ToBTCSwapState.REFUNDED;
641
- return true;
642
- }
643
- break;
644
- case base_1.SwapCommitStateType.COMMITED:
645
- if (this.state !== ToBTCSwapState.COMMITED && this.state !== ToBTCSwapState.REFUNDABLE) {
646
- this.state = ToBTCSwapState.COMMITED;
647
- return true;
648
- }
649
- break;
650
- }
651
- if ((this.state === ToBTCSwapState.CREATED || this.state === ToBTCSwapState.QUOTE_SOFT_EXPIRED)) {
733
+ commitStatus ??= await this.wrapper._contract.getCommitStatus(this._getInitiator(), this._data);
734
+ if (commitStatus != null && await this._forciblySetOnchainState(commitStatus))
735
+ return true;
736
+ if ((this._state === ToBTCSwapState.CREATED || this._state === ToBTCSwapState.QUOTE_SOFT_EXPIRED)) {
652
737
  if (quoteExpired) {
653
- this.state = ToBTCSwapState.QUOTE_EXPIRED;
738
+ this._state = ToBTCSwapState.QUOTE_EXPIRED;
654
739
  return true;
655
740
  }
656
741
  }
657
742
  }
658
743
  return false;
659
744
  }
660
- _shouldFetchCommitStatus() {
661
- return this.state === ToBTCSwapState.CREATED ||
662
- this.state === ToBTCSwapState.QUOTE_SOFT_EXPIRED ||
663
- this.state === ToBTCSwapState.COMMITED ||
664
- this.state === ToBTCSwapState.SOFT_CLAIMED ||
665
- this.state === ToBTCSwapState.REFUNDABLE;
745
+ /**
746
+ * @inheritDoc
747
+ * @internal
748
+ */
749
+ _shouldFetchOnchainState() {
750
+ return this._state === ToBTCSwapState.CREATED ||
751
+ this._state === ToBTCSwapState.QUOTE_SOFT_EXPIRED ||
752
+ this._state === ToBTCSwapState.COMMITED ||
753
+ this._state === ToBTCSwapState.SOFT_CLAIMED ||
754
+ this._state === ToBTCSwapState.REFUNDABLE;
666
755
  }
756
+ /**
757
+ * @inheritDoc
758
+ * @internal
759
+ */
667
760
  _shouldFetchExpiryStatus() {
668
- return this.state === ToBTCSwapState.CREATED || this.state === ToBTCSwapState.QUOTE_SOFT_EXPIRED;
761
+ return this._state === ToBTCSwapState.CREATED || this._state === ToBTCSwapState.QUOTE_SOFT_EXPIRED;
669
762
  }
763
+ /**
764
+ * @inheritDoc
765
+ * @internal
766
+ */
670
767
  async _sync(save, quoteDefinitelyExpired, commitStatus) {
671
768
  let changed = await this.syncStateFromChain(quoteDefinitelyExpired, commitStatus);
672
- if (this.state === ToBTCSwapState.COMMITED || this.state === ToBTCSwapState.SOFT_CLAIMED) {
769
+ if (this._state === ToBTCSwapState.COMMITED || this._state === ToBTCSwapState.SOFT_CLAIMED) {
673
770
  //Check if that maybe already concluded
674
771
  try {
675
772
  if (await this.checkIntermediarySwapProcessed(false))
@@ -683,11 +780,58 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
683
780
  await this._saveAndEmit();
684
781
  return changed;
685
782
  }
783
+ /**
784
+ * @inheritDoc
785
+ * @internal
786
+ */
787
+ async _forciblySetOnchainState(commitStatus) {
788
+ switch (commitStatus.type) {
789
+ case base_1.SwapCommitStateType.PAID:
790
+ if (this._claimTxId == null && commitStatus.getClaimTxId)
791
+ this._claimTxId = await commitStatus.getClaimTxId();
792
+ const eventResult = await commitStatus.getClaimResult();
793
+ try {
794
+ await this._setPaymentResult({ secret: eventResult, txId: Buffer.from(eventResult, "hex").reverse().toString("hex") });
795
+ }
796
+ catch (e) {
797
+ this.logger.error(`Failed to set payment result ${eventResult} on the swap!`);
798
+ }
799
+ this._state = ToBTCSwapState.CLAIMED;
800
+ return true;
801
+ case base_1.SwapCommitStateType.REFUNDABLE:
802
+ this._state = ToBTCSwapState.REFUNDABLE;
803
+ return true;
804
+ case base_1.SwapCommitStateType.EXPIRED:
805
+ if (this._refundTxId == null && commitStatus.getRefundTxId)
806
+ this._refundTxId = await commitStatus.getRefundTxId();
807
+ this._state = this._refundTxId == null ? ToBTCSwapState.QUOTE_EXPIRED : ToBTCSwapState.REFUNDED;
808
+ return true;
809
+ case base_1.SwapCommitStateType.NOT_COMMITED:
810
+ if (this._refundTxId == null && commitStatus.getRefundTxId)
811
+ this._refundTxId = await commitStatus.getRefundTxId();
812
+ if (this._refundTxId != null) {
813
+ this._state = ToBTCSwapState.REFUNDED;
814
+ return true;
815
+ }
816
+ break;
817
+ case base_1.SwapCommitStateType.COMMITED:
818
+ if (this._state !== ToBTCSwapState.COMMITED && this._state !== ToBTCSwapState.REFUNDABLE && this._state !== ToBTCSwapState.SOFT_CLAIMED) {
819
+ this._state = ToBTCSwapState.COMMITED;
820
+ return true;
821
+ }
822
+ break;
823
+ }
824
+ return false;
825
+ }
826
+ /**
827
+ * @inheritDoc
828
+ * @internal
829
+ */
686
830
  async _tick(save) {
687
- switch (this.state) {
831
+ switch (this._state) {
688
832
  case ToBTCSwapState.CREATED:
689
833
  if (this.expiry < Date.now()) {
690
- this.state = ToBTCSwapState.QUOTE_SOFT_EXPIRED;
834
+ this._state = ToBTCSwapState.QUOTE_SOFT_EXPIRED;
691
835
  if (save)
692
836
  await this._saveAndEmit();
693
837
  return true;
@@ -695,9 +839,9 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
695
839
  break;
696
840
  case ToBTCSwapState.COMMITED:
697
841
  case ToBTCSwapState.SOFT_CLAIMED:
698
- const expired = await this.wrapper.contract.isExpired(this._getInitiator(), this.data);
842
+ const expired = await this.wrapper._contract.isExpired(this._getInitiator(), this._data);
699
843
  if (expired) {
700
- this.state = ToBTCSwapState.REFUNDABLE;
844
+ this._state = ToBTCSwapState.REFUNDABLE;
701
845
  if (save)
702
846
  await this._saveAndEmit();
703
847
  return true;
@@ -708,14 +852,3 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
708
852
  }
709
853
  }
710
854
  exports.IToBTCSwap = IToBTCSwap;
711
- var ToBTCSwapState;
712
- (function (ToBTCSwapState) {
713
- ToBTCSwapState[ToBTCSwapState["REFUNDED"] = -3] = "REFUNDED";
714
- ToBTCSwapState[ToBTCSwapState["QUOTE_EXPIRED"] = -2] = "QUOTE_EXPIRED";
715
- ToBTCSwapState[ToBTCSwapState["QUOTE_SOFT_EXPIRED"] = -1] = "QUOTE_SOFT_EXPIRED";
716
- ToBTCSwapState[ToBTCSwapState["CREATED"] = 0] = "CREATED";
717
- ToBTCSwapState[ToBTCSwapState["COMMITED"] = 1] = "COMMITED";
718
- ToBTCSwapState[ToBTCSwapState["SOFT_CLAIMED"] = 2] = "SOFT_CLAIMED";
719
- ToBTCSwapState[ToBTCSwapState["CLAIMED"] = 3] = "CLAIMED";
720
- ToBTCSwapState[ToBTCSwapState["REFUNDABLE"] = 4] = "REFUNDABLE";
721
- })(ToBTCSwapState = exports.ToBTCSwapState || (exports.ToBTCSwapState = {}));