@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.
- package/dist/bitcoin/coinselect2/utils.d.ts +6 -0
- package/dist/bitcoin/wallet/BitcoinWallet.d.ts +41 -5
- package/dist/bitcoin/wallet/BitcoinWallet.js +36 -1
- package/dist/bitcoin/wallet/IBitcoinWallet.d.ts +52 -2
- package/dist/bitcoin/wallet/IBitcoinWallet.js +2 -1
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.d.ts +42 -7
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.js +36 -1
- package/dist/enums/FeeType.d.ts +8 -1
- package/dist/enums/FeeType.js +8 -1
- package/dist/enums/SwapAmountType.d.ts +7 -0
- package/dist/enums/SwapAmountType.js +7 -0
- package/dist/enums/SwapDirection.d.ts +7 -0
- package/dist/enums/SwapDirection.js +7 -0
- package/dist/enums/SwapType.d.ts +62 -1
- package/dist/enums/SwapType.js +62 -1
- package/dist/errors/IntermediaryError.d.ts +4 -0
- package/dist/errors/IntermediaryError.js +1 -0
- package/dist/errors/RequestError.d.ts +15 -1
- package/dist/errors/RequestError.js +8 -0
- package/dist/errors/UserError.d.ts +1 -0
- package/dist/errors/UserError.js +1 -0
- package/dist/index.d.ts +5 -5
- package/dist/index.js +7 -6
- package/dist/intermediaries/Intermediary.d.ts +61 -14
- package/dist/intermediaries/Intermediary.js +38 -11
- package/dist/intermediaries/IntermediaryDiscovery.d.ts +62 -29
- package/dist/intermediaries/IntermediaryDiscovery.js +39 -24
- package/dist/prices/RedundantSwapPrice.d.ts +26 -5
- package/dist/prices/RedundantSwapPrice.js +22 -2
- package/dist/prices/SingleSwapPrice.d.ts +10 -7
- package/dist/prices/SingleSwapPrice.js +11 -8
- package/dist/prices/SwapPriceWithChain.d.ts +56 -19
- package/dist/prices/SwapPriceWithChain.js +62 -25
- package/dist/prices/abstract/IPriceProvider.d.ts +4 -4
- package/dist/prices/abstract/IPriceProvider.js +1 -1
- package/dist/prices/abstract/ISwapPrice.d.ts +95 -46
- package/dist/prices/abstract/ISwapPrice.js +104 -56
- package/dist/prices/providers/BinancePriceProvider.d.ts +8 -1
- package/dist/prices/providers/BinancePriceProvider.js +8 -1
- package/dist/prices/providers/CoinGeckoPriceProvider.d.ts +7 -1
- package/dist/prices/providers/CoinGeckoPriceProvider.js +7 -1
- package/dist/prices/providers/CoinPaprikaPriceProvider.d.ts +7 -1
- package/dist/prices/providers/CoinPaprikaPriceProvider.js +7 -1
- package/dist/prices/providers/CustomPriceProvider.d.ts +12 -1
- package/dist/prices/providers/CustomPriceProvider.js +12 -1
- package/dist/prices/providers/KrakenPriceProvider.d.ts +10 -1
- package/dist/prices/providers/KrakenPriceProvider.js +10 -1
- package/dist/prices/providers/OKXPriceProvider.d.ts +7 -1
- package/dist/prices/providers/OKXPriceProvider.js +7 -1
- package/dist/prices/providers/abstract/ExchangePriceProvider.d.ts +3 -0
- package/dist/prices/providers/abstract/ExchangePriceProvider.js +3 -0
- package/dist/storage/IUnifiedStorage.d.ts +19 -7
- package/dist/storage/UnifiedSwapStorage.d.ts +33 -3
- package/dist/storage/UnifiedSwapStorage.js +29 -1
- package/dist/storage-browser/IndexedDBUnifiedStorage.d.ts +31 -7
- package/dist/storage-browser/IndexedDBUnifiedStorage.js +29 -6
- package/dist/storage-browser/LocalStorageManager.d.ts +25 -1
- package/dist/storage-browser/LocalStorageManager.js +25 -1
- package/dist/swapper/Swapper.d.ts +380 -226
- package/dist/swapper/Swapper.js +383 -349
- package/dist/swapper/SwapperFactory.d.ts +66 -18
- package/dist/swapper/SwapperFactory.js +24 -3
- package/dist/swapper/SwapperUtils.d.ts +75 -28
- package/dist/swapper/SwapperUtils.js +107 -60
- package/dist/swapper/SwapperWithChain.d.ts +286 -91
- package/dist/swapper/SwapperWithChain.js +218 -64
- package/dist/swapper/SwapperWithSigner.d.ts +229 -80
- package/dist/swapper/SwapperWithSigner.js +190 -44
- package/dist/swaps/IAddressSwap.d.ts +12 -3
- package/dist/swaps/IAddressSwap.js +3 -2
- package/dist/swaps/IBTCWalletSwap.d.ts +26 -8
- package/dist/swaps/IBTCWalletSwap.js +3 -2
- package/dist/swaps/IClaimableSwap.d.ts +38 -6
- package/dist/swaps/IClaimableSwap.js +3 -2
- package/dist/swaps/IClaimableSwapWrapper.d.ts +11 -1
- package/dist/swaps/IRefundableSwap.d.ts +31 -5
- package/dist/swaps/IRefundableSwap.js +3 -2
- package/dist/swaps/ISwap.d.ts +162 -24
- package/dist/swaps/ISwap.js +92 -35
- package/dist/swaps/ISwapWithGasDrop.d.ts +8 -2
- package/dist/swaps/ISwapWithGasDrop.js +2 -1
- package/dist/swaps/ISwapWrapper.d.ts +161 -52
- package/dist/swaps/ISwapWrapper.js +131 -73
- package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.d.ts +51 -6
- package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.js +22 -12
- package/dist/swaps/escrow_swaps/IEscrowSwap.d.ts +65 -12
- package/dist/swaps/escrow_swaps/IEscrowSwap.js +38 -19
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.d.ts +39 -9
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.js +30 -21
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.d.ts +31 -15
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.js +33 -18
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.d.ts +97 -28
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.js +91 -27
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.d.ts +22 -9
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.js +24 -11
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.d.ts +278 -60
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +519 -241
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +77 -26
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +132 -50
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.d.ts +313 -52
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +544 -194
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +87 -26
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +147 -58
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.d.ts +222 -55
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +462 -244
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.d.ts +77 -23
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.js +116 -46
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.d.ts +195 -58
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +324 -191
- package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.d.ts +30 -5
- package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.js +44 -19
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.d.ts +61 -20
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.js +75 -32
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.d.ts +76 -50
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.js +106 -101
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.d.ts +37 -14
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.js +66 -20
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.d.ts +46 -17
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.js +82 -27
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +350 -88
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +482 -215
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.d.ts +76 -24
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.js +247 -124
- package/dist/swaps/trusted/ln/LnForGasSwap.d.ts +148 -20
- package/dist/swaps/trusted/ln/LnForGasSwap.js +175 -45
- package/dist/swaps/trusted/ln/LnForGasWrapper.d.ts +29 -10
- package/dist/swaps/trusted/ln/LnForGasWrapper.js +30 -11
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.d.ts +202 -49
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.js +232 -80
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.d.ts +34 -12
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.js +33 -14
- package/dist/types/AmountData.d.ts +2 -1
- package/dist/types/CustomPriceFunction.d.ts +8 -2
- package/dist/types/PriceInfoType.d.ts +4 -4
- package/dist/types/PriceInfoType.js +3 -3
- package/dist/types/SwapExecutionAction.d.ts +85 -4
- package/dist/types/SwapWithSigner.d.ts +5 -2
- package/dist/types/SwapWithSigner.js +5 -2
- package/dist/types/Token.d.ts +11 -5
- package/dist/types/Token.js +6 -3
- package/dist/types/TokenAmount.d.ts +3 -0
- package/dist/types/TokenAmount.js +2 -0
- package/dist/types/fees/Fee.d.ts +3 -2
- package/dist/types/fees/FeeBreakdown.d.ts +3 -2
- package/dist/types/fees/PercentagePPM.d.ts +4 -2
- package/dist/types/fees/PercentagePPM.js +2 -1
- package/dist/types/lnurl/LNURLPay.d.ts +20 -12
- package/dist/types/lnurl/LNURLPay.js +8 -4
- package/dist/types/lnurl/LNURLWithdraw.d.ts +17 -10
- package/dist/types/lnurl/LNURLWithdraw.js +8 -4
- package/dist/types/wallets/LightningInvoiceCreateService.d.ts +24 -0
- package/dist/types/wallets/LightningInvoiceCreateService.js +15 -0
- package/dist/types/wallets/MinimalBitcoinWalletInterface.d.ts +3 -1
- package/dist/types/wallets/MinimalLightningNetworkWalletInterface.d.ts +4 -2
- package/dist/utils/BitcoinUtils.d.ts +1 -0
- package/dist/utils/BitcoinUtils.js +5 -1
- package/dist/utils/SwapUtils.d.ts +58 -1
- package/dist/utils/SwapUtils.js +55 -1
- package/dist/utils/TokenUtils.d.ts +10 -2
- package/dist/utils/TokenUtils.js +12 -4
- package/package.json +3 -3
- package/src/bitcoin/coinselect2/utils.ts +6 -0
- package/src/bitcoin/wallet/BitcoinWallet.ts +41 -5
- package/src/bitcoin/wallet/IBitcoinWallet.ts +57 -2
- package/src/bitcoin/wallet/SingleAddressBitcoinWallet.ts +42 -6
- package/src/enums/FeeType.ts +8 -1
- package/src/enums/SwapAmountType.ts +7 -0
- package/src/enums/SwapDirection.ts +7 -0
- package/src/enums/SwapType.ts +62 -2
- package/src/errors/IntermediaryError.ts +4 -0
- package/src/errors/RequestError.ts +15 -1
- package/src/errors/UserError.ts +1 -0
- package/src/index.ts +12 -5
- package/src/intermediaries/Intermediary.ts +61 -14
- package/src/intermediaries/IntermediaryDiscovery.ts +69 -34
- package/src/prices/RedundantSwapPrice.ts +26 -6
- package/src/prices/SingleSwapPrice.ts +11 -8
- package/src/prices/SwapPriceWithChain.ts +63 -26
- package/src/prices/abstract/IPriceProvider.ts +4 -4
- package/src/prices/abstract/ISwapPrice.ts +115 -66
- package/src/prices/providers/BinancePriceProvider.ts +8 -1
- package/src/prices/providers/CoinGeckoPriceProvider.ts +7 -1
- package/src/prices/providers/CoinPaprikaPriceProvider.ts +7 -1
- package/src/prices/providers/CustomPriceProvider.ts +12 -1
- package/src/prices/providers/KrakenPriceProvider.ts +10 -1
- package/src/prices/providers/OKXPriceProvider.ts +7 -1
- package/src/prices/providers/abstract/ExchangePriceProvider.ts +3 -0
- package/src/storage/IUnifiedStorage.ts +19 -7
- package/src/storage/UnifiedSwapStorage.ts +33 -3
- package/src/storage-browser/IndexedDBUnifiedStorage.ts +31 -8
- package/src/storage-browser/LocalStorageManager.ts +25 -1
- package/src/swapper/Swapper.ts +599 -390
- package/src/swapper/SwapperFactory.ts +73 -24
- package/src/swapper/SwapperUtils.ts +107 -60
- package/src/swapper/SwapperWithChain.ts +320 -81
- package/src/swapper/SwapperWithSigner.ts +263 -56
- package/src/swaps/IAddressSwap.ts +13 -3
- package/src/swaps/IBTCWalletSwap.ts +26 -10
- package/src/swaps/IClaimableSwap.ts +41 -6
- package/src/swaps/IClaimableSwapWrapper.ts +11 -2
- package/src/swaps/IRefundableSwap.ts +34 -5
- package/src/swaps/ISwap.ts +224 -85
- package/src/swaps/ISwapWithGasDrop.ts +8 -2
- package/src/swaps/ISwapWrapper.ts +216 -98
- package/src/swaps/escrow_swaps/IEscrowSelfInitSwap.ts +64 -18
- package/src/swaps/escrow_swaps/IEscrowSwap.ts +83 -37
- package/src/swaps/escrow_swaps/IEscrowSwapWrapper.ts +61 -30
- package/src/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.ts +37 -19
- package/src/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.ts +123 -50
- package/src/swaps/escrow_swaps/frombtc/IFromBTCWrapper.ts +24 -11
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.ts +562 -258
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +156 -62
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +592 -227
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +177 -74
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.ts +483 -245
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.ts +141 -59
- package/src/swaps/escrow_swaps/tobtc/IToBTCSwap.ts +350 -195
- package/src/swaps/escrow_swaps/tobtc/IToBTCWrapper.ts +48 -23
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.ts +87 -40
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.ts +110 -110
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.ts +89 -34
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.ts +101 -31
- package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +556 -259
- package/src/swaps/spv_swaps/SpvFromBTCWrapper.ts +292 -148
- package/src/swaps/trusted/ln/LnForGasSwap.ts +186 -47
- package/src/swaps/trusted/ln/LnForGasWrapper.ts +34 -15
- package/src/swaps/trusted/onchain/OnchainForGasSwap.ts +262 -88
- package/src/swaps/trusted/onchain/OnchainForGasWrapper.ts +41 -19
- package/src/types/AmountData.ts +2 -1
- package/src/types/CustomPriceFunction.ts +8 -2
- package/src/types/PriceInfoType.ts +4 -4
- package/src/types/SwapExecutionAction.ts +97 -5
- package/src/types/SwapWithSigner.ts +8 -4
- package/src/types/Token.ts +12 -5
- package/src/types/TokenAmount.ts +3 -0
- package/src/types/fees/Fee.ts +3 -2
- package/src/types/fees/FeeBreakdown.ts +3 -2
- package/src/types/fees/PercentagePPM.ts +4 -2
- package/src/types/lnurl/LNURLPay.ts +20 -12
- package/src/types/lnurl/LNURLWithdraw.ts +17 -10
- package/src/types/wallets/LightningInvoiceCreateService.ts +30 -0
- package/src/types/wallets/MinimalBitcoinWalletInterface.ts +3 -1
- package/src/types/wallets/MinimalLightningNetworkWalletInterface.ts +4 -2
- package/src/utils/BitcoinUtils.ts +5 -0
- package/src/utils/SwapUtils.ts +63 -1
- package/src/utils/TokenUtils.ts +12 -4
- package/dist/bitcoin/BitcoinRpcWithAddressIndex.d.ts +0 -68
- package/dist/bitcoin/BitcoinRpcWithAddressIndex.js +0 -2
- package/dist/bitcoin/LightningNetworkApi.d.ts +0 -12
- package/dist/bitcoin/LightningNetworkApi.js +0 -2
- package/dist/bitcoin/mempool/MempoolApi.d.ts +0 -350
- package/dist/bitcoin/mempool/MempoolApi.js +0 -311
- package/dist/bitcoin/mempool/MempoolBitcoinBlock.d.ts +0 -44
- package/dist/bitcoin/mempool/MempoolBitcoinBlock.js +0 -48
- package/dist/bitcoin/mempool/MempoolBitcoinRpc.d.ts +0 -119
- package/dist/bitcoin/mempool/MempoolBitcoinRpc.js +0 -361
- package/dist/bitcoin/mempool/synchronizer/MempoolBtcRelaySynchronizer.d.ts +0 -22
- package/dist/bitcoin/mempool/synchronizer/MempoolBtcRelaySynchronizer.js +0 -105
- package/dist/errors/PaymentAuthError.d.ts +0 -11
- package/dist/errors/PaymentAuthError.js +0 -23
- 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.
|
|
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.
|
|
80
|
+
this._state = ToBTCSwapState.CREATED;
|
|
29
81
|
this.networkFee = initOrObject.networkFee;
|
|
30
82
|
this.networkFeeBtc = initOrObject.networkFeeBtc;
|
|
31
|
-
this.
|
|
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.
|
|
96
|
+
return this._data;
|
|
41
97
|
}
|
|
98
|
+
/**
|
|
99
|
+
* @inheritDoc
|
|
100
|
+
* @internal
|
|
101
|
+
*/
|
|
42
102
|
upgradeVersion() {
|
|
43
103
|
if (this.version == null) {
|
|
44
|
-
switch (this.
|
|
104
|
+
switch (this._state) {
|
|
45
105
|
case -2:
|
|
46
|
-
this.
|
|
106
|
+
this._state = ToBTCSwapState.REFUNDED;
|
|
47
107
|
break;
|
|
48
108
|
case -1:
|
|
49
|
-
this.
|
|
109
|
+
this._state = ToBTCSwapState.QUOTE_EXPIRED;
|
|
50
110
|
break;
|
|
51
111
|
case 0:
|
|
52
|
-
this.
|
|
112
|
+
this._state = ToBTCSwapState.CREATED;
|
|
53
113
|
break;
|
|
54
114
|
case 1:
|
|
55
|
-
this.
|
|
115
|
+
this._state = ToBTCSwapState.COMMITED;
|
|
56
116
|
break;
|
|
57
117
|
case 2:
|
|
58
|
-
this.
|
|
118
|
+
this._state = ToBTCSwapState.CLAIMED;
|
|
59
119
|
break;
|
|
60
120
|
case 3:
|
|
61
|
-
this.
|
|
121
|
+
this._state = ToBTCSwapState.REFUNDABLE;
|
|
62
122
|
break;
|
|
63
123
|
}
|
|
64
124
|
this.version = 1;
|
|
65
125
|
}
|
|
66
126
|
}
|
|
67
127
|
/**
|
|
68
|
-
*
|
|
69
|
-
* @
|
|
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
|
-
* @
|
|
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.
|
|
162
|
+
return this._commitTxId ?? null;
|
|
97
163
|
}
|
|
164
|
+
/**
|
|
165
|
+
* @inheritDoc
|
|
166
|
+
*/
|
|
98
167
|
requiresAction() {
|
|
99
168
|
return this.isRefundable();
|
|
100
169
|
}
|
|
101
170
|
/**
|
|
102
|
-
*
|
|
171
|
+
* @inheritDoc
|
|
103
172
|
*/
|
|
104
173
|
isFinished() {
|
|
105
|
-
return this.
|
|
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.
|
|
180
|
+
return this._state === ToBTCSwapState.REFUNDABLE;
|
|
109
181
|
}
|
|
182
|
+
/**
|
|
183
|
+
* @inheritDoc
|
|
184
|
+
*/
|
|
110
185
|
isQuoteExpired() {
|
|
111
|
-
return this.
|
|
186
|
+
return this._state === ToBTCSwapState.QUOTE_EXPIRED;
|
|
112
187
|
}
|
|
188
|
+
/**
|
|
189
|
+
* @inheritDoc
|
|
190
|
+
*/
|
|
113
191
|
isQuoteSoftExpired() {
|
|
114
|
-
return this.
|
|
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.
|
|
198
|
+
return this._state === ToBTCSwapState.CLAIMED;
|
|
118
199
|
}
|
|
200
|
+
/**
|
|
201
|
+
* @inheritDoc
|
|
202
|
+
*/
|
|
119
203
|
isFailed() {
|
|
120
|
-
return this.
|
|
204
|
+
return this._state === ToBTCSwapState.REFUNDED;
|
|
121
205
|
}
|
|
206
|
+
/**
|
|
207
|
+
* @inheritDoc
|
|
208
|
+
* @internal
|
|
209
|
+
*/
|
|
122
210
|
_getInitiator() {
|
|
123
|
-
return this.
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
|
148
|
-
*
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
286
|
+
return this.wrapper._tokens[this._data.getToken()];
|
|
184
287
|
}
|
|
288
|
+
/**
|
|
289
|
+
* @inheritDoc
|
|
290
|
+
*/
|
|
185
291
|
getInput() {
|
|
186
|
-
return (0, TokenAmount_1.toTokenAmount)(this.
|
|
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.
|
|
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
|
|
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.
|
|
197
|
-
this.
|
|
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.
|
|
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.
|
|
205
|
-
required: (0, TokenAmount_1.toTokenAmount)(required, this.wrapper.
|
|
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
|
-
*
|
|
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.
|
|
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.
|
|
219
|
-
required: (0, TokenAmount_1.toTokenAmount)(commitFee, this.wrapper.
|
|
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
|
|
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.
|
|
345
|
+
if (this._state === ToBTCSwapState.QUOTE_EXPIRED || this._state === ToBTCSwapState.QUOTE_SOFT_EXPIRED)
|
|
236
346
|
throw new Error("Quote expired");
|
|
237
|
-
if (this.
|
|
347
|
+
if (this._state === ToBTCSwapState.REFUNDED)
|
|
238
348
|
throw new Error("Swap already refunded");
|
|
239
|
-
if (this.
|
|
349
|
+
if (this._state === ToBTCSwapState.REFUNDABLE)
|
|
240
350
|
throw new Error("Swap refundable, refund with swap.refund()");
|
|
241
|
-
if (this.
|
|
351
|
+
if (this._state === ToBTCSwapState.SOFT_CLAIMED || this._state === ToBTCSwapState.CLAIMED)
|
|
242
352
|
throw new Error("Swap already settled!");
|
|
243
|
-
if (this.
|
|
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.
|
|
359
|
+
if (this._state === ToBTCSwapState.CLAIMED || this._state === ToBTCSwapState.SOFT_CLAIMED)
|
|
250
360
|
return true;
|
|
251
|
-
if (this.
|
|
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.
|
|
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
|
-
*
|
|
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
|
|
397
|
+
* @throws {Error} When in invalid state (not {@link ToBTCSwapState.CREATED})
|
|
286
398
|
*/
|
|
287
399
|
async txsCommit(skipChecks) {
|
|
288
|
-
if (this.
|
|
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.
|
|
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
|
-
*
|
|
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.
|
|
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.
|
|
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.
|
|
320
|
-
if (this.
|
|
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.
|
|
430
|
+
return this._commitTxId;
|
|
324
431
|
}
|
|
325
432
|
/**
|
|
326
|
-
*
|
|
433
|
+
* @inheritDoc
|
|
327
434
|
*
|
|
328
|
-
* @
|
|
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.
|
|
438
|
+
if (this._state === ToBTCSwapState.COMMITED || this._state === ToBTCSwapState.CLAIMED)
|
|
333
439
|
return Promise.resolve();
|
|
334
|
-
if (this.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
-
*
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
417
|
-
this.
|
|
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
|
|
432
|
-
* @param abortSignal
|
|
433
|
-
* @returns
|
|
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.
|
|
555
|
+
if (this._state === ToBTCSwapState.CLAIMED)
|
|
441
556
|
return Promise.resolve(true);
|
|
442
|
-
if (this.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
-
|
|
501
|
-
|
|
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
|
-
*
|
|
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.
|
|
515
|
-
return await this.wrapper.
|
|
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.
|
|
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.
|
|
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
|
-
*
|
|
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.
|
|
536
|
-
const result = await this.wrapper.
|
|
537
|
-
this.
|
|
538
|
-
if (this.
|
|
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
|
-
*
|
|
658
|
+
* @inheritDoc
|
|
545
659
|
*
|
|
546
|
-
* @
|
|
547
|
-
*
|
|
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.
|
|
665
|
+
if (this._state === ToBTCSwapState.REFUNDED)
|
|
552
666
|
return Promise.resolve();
|
|
553
|
-
if (this.
|
|
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.
|
|
575
|
-
this.
|
|
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.
|
|
581
|
-
this.
|
|
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.
|
|
605
|
-
this.
|
|
606
|
-
this.
|
|
607
|
-
this.
|
|
608
|
-
this.
|
|
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.
|
|
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.
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
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.
|
|
738
|
+
this._state = ToBTCSwapState.QUOTE_EXPIRED;
|
|
654
739
|
return true;
|
|
655
740
|
}
|
|
656
741
|
}
|
|
657
742
|
}
|
|
658
743
|
return false;
|
|
659
744
|
}
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
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.
|
|
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.
|
|
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.
|
|
831
|
+
switch (this._state) {
|
|
688
832
|
case ToBTCSwapState.CREATED:
|
|
689
833
|
if (this.expiry < Date.now()) {
|
|
690
|
-
this.
|
|
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.
|
|
842
|
+
const expired = await this.wrapper._contract.isExpired(this._getInitiator(), this._data);
|
|
699
843
|
if (expired) {
|
|
700
|
-
this.
|
|
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 = {}));
|