@atomiqlabs/sdk 8.1.7 → 8.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +7 -0
- package/dist/enums/FeeType.js +7 -0
- 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 +4 -5
- package/dist/index.js +3 -4
- package/dist/intermediaries/Intermediary.d.ts +57 -10
- package/dist/intermediaries/Intermediary.js +37 -10
- package/dist/intermediaries/IntermediaryDiscovery.d.ts +55 -22
- package/dist/intermediaries/IntermediaryDiscovery.js +35 -22
- package/dist/prices/RedundantSwapPrice.d.ts +24 -3
- package/dist/prices/RedundantSwapPrice.js +21 -1
- package/dist/prices/SingleSwapPrice.d.ts +9 -6
- package/dist/prices/SingleSwapPrice.js +10 -7
- package/dist/prices/SwapPriceWithChain.d.ts +54 -16
- package/dist/prices/SwapPriceWithChain.js +58 -20
- package/dist/prices/abstract/ISwapPrice.d.ts +94 -45
- package/dist/prices/abstract/ISwapPrice.js +103 -55
- package/dist/prices/providers/BinancePriceProvider.d.ts +7 -0
- package/dist/prices/providers/BinancePriceProvider.js +7 -0
- package/dist/prices/providers/CoinGeckoPriceProvider.d.ts +6 -0
- package/dist/prices/providers/CoinGeckoPriceProvider.js +6 -0
- package/dist/prices/providers/CoinPaprikaPriceProvider.d.ts +6 -0
- package/dist/prices/providers/CoinPaprikaPriceProvider.js +6 -0
- package/dist/prices/providers/CustomPriceProvider.d.ts +11 -0
- package/dist/prices/providers/CustomPriceProvider.js +11 -0
- package/dist/prices/providers/KrakenPriceProvider.d.ts +9 -0
- package/dist/prices/providers/KrakenPriceProvider.js +9 -0
- package/dist/prices/providers/OKXPriceProvider.d.ts +6 -0
- package/dist/prices/providers/OKXPriceProvider.js +6 -0
- 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 +303 -222
- package/dist/swapper/Swapper.js +376 -344
- package/dist/swapper/SwapperFactory.d.ts +41 -17
- package/dist/swapper/SwapperFactory.js +23 -2
- 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 +10 -1
- package/dist/swaps/IAddressSwap.js +2 -1
- package/dist/swaps/IBTCWalletSwap.d.ts +24 -6
- package/dist/swaps/IBTCWalletSwap.js +2 -1
- package/dist/swaps/IClaimableSwap.d.ts +36 -4
- package/dist/swaps/IClaimableSwap.js +2 -1
- package/dist/swaps/IClaimableSwapWrapper.d.ts +11 -1
- package/dist/swaps/IRefundableSwap.d.ts +29 -3
- package/dist/swaps/IRefundableSwap.js +2 -1
- package/dist/swaps/ISwap.d.ts +159 -21
- package/dist/swaps/ISwap.js +90 -33
- package/dist/swaps/ISwapWithGasDrop.d.ts +6 -0
- package/dist/swaps/ISwapWithGasDrop.js +1 -0
- package/dist/swaps/ISwapWrapper.d.ts +157 -48
- package/dist/swaps/ISwapWrapper.js +130 -72
- package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.d.ts +49 -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 +94 -29
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.js +90 -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 +275 -58
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +516 -239
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +76 -25
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +131 -49
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.d.ts +311 -51
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +542 -193
- 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 +209 -53
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +449 -242
- 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 +197 -56
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +326 -189
- 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 +60 -19
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.js +74 -31
- 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 +36 -13
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.js +65 -19
- 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 +328 -92
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +460 -219
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.d.ts +76 -24
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.js +244 -124
- package/dist/swaps/trusted/ln/LnForGasSwap.d.ts +146 -18
- package/dist/swaps/trusted/ln/LnForGasSwap.js +173 -43
- 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 +200 -47
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.js +230 -78
- 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 +7 -1
- package/dist/types/SwapExecutionAction.d.ts +74 -4
- package/dist/types/SwapWithSigner.d.ts +4 -1
- 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 +2 -1
- package/dist/types/fees/FeeBreakdown.d.ts +2 -1
- package/dist/types/fees/PercentagePPM.d.ts +2 -0
- package/dist/types/fees/PercentagePPM.js +1 -0
- package/dist/types/lnurl/LNURLPay.d.ts +14 -6
- package/dist/types/lnurl/LNURLPay.js +6 -2
- package/dist/types/lnurl/LNURLWithdraw.d.ts +12 -5
- package/dist/types/lnurl/LNURLWithdraw.js +6 -2
- package/dist/types/wallets/LightningInvoiceCreateService.d.ts +20 -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 +3 -1
- package/dist/utils/BitcoinUtils.d.ts +1 -0
- package/dist/utils/BitcoinUtils.js +5 -1
- package/dist/utils/SwapUtils.d.ts +56 -1
- package/dist/utils/SwapUtils.js +53 -1
- package/dist/utils/TokenUtils.d.ts +10 -2
- package/dist/utils/TokenUtils.js +12 -4
- package/package.json +3 -3
- 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 +7 -0
- 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 +6 -5
- package/src/intermediaries/Intermediary.ts +57 -10
- package/src/intermediaries/IntermediaryDiscovery.ts +60 -27
- package/src/prices/RedundantSwapPrice.ts +24 -4
- package/src/prices/SingleSwapPrice.ts +10 -7
- package/src/prices/SwapPriceWithChain.ts +59 -21
- package/src/prices/abstract/ISwapPrice.ts +114 -65
- package/src/prices/providers/BinancePriceProvider.ts +7 -0
- package/src/prices/providers/CoinGeckoPriceProvider.ts +6 -0
- package/src/prices/providers/CoinPaprikaPriceProvider.ts +6 -0
- package/src/prices/providers/CustomPriceProvider.ts +11 -0
- package/src/prices/providers/KrakenPriceProvider.ts +9 -0
- package/src/prices/providers/OKXPriceProvider.ts +6 -0
- 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 +513 -379
- package/src/swapper/SwapperFactory.ts +44 -21
- 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 +11 -1
- package/src/swaps/IBTCWalletSwap.ts +24 -8
- package/src/swaps/IClaimableSwap.ts +39 -4
- package/src/swaps/IClaimableSwapWrapper.ts +11 -2
- package/src/swaps/IRefundableSwap.ts +32 -3
- package/src/swaps/ISwap.ts +221 -82
- package/src/swaps/ISwapWithGasDrop.ts +6 -0
- package/src/swaps/ISwapWrapper.ts +212 -94
- package/src/swaps/escrow_swaps/IEscrowSelfInitSwap.ts +62 -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 +120 -51
- package/src/swaps/escrow_swaps/frombtc/IFromBTCWrapper.ts +24 -11
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.ts +559 -256
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +155 -61
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +590 -226
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +177 -74
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.ts +470 -243
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.ts +141 -59
- package/src/swaps/escrow_swaps/tobtc/IToBTCSwap.ts +352 -193
- package/src/swaps/escrow_swaps/tobtc/IToBTCWrapper.ts +48 -23
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.ts +86 -39
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.ts +110 -110
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.ts +88 -33
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.ts +101 -31
- package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +534 -263
- package/src/swaps/spv_swaps/SpvFromBTCWrapper.ts +289 -148
- package/src/swaps/trusted/ln/LnForGasSwap.ts +184 -45
- package/src/swaps/trusted/ln/LnForGasWrapper.ts +34 -15
- package/src/swaps/trusted/onchain/OnchainForGasSwap.ts +260 -86
- package/src/swaps/trusted/onchain/OnchainForGasWrapper.ts +41 -19
- package/src/types/AmountData.ts +2 -1
- package/src/types/CustomPriceFunction.ts +7 -1
- package/src/types/SwapExecutionAction.ts +84 -5
- package/src/types/SwapWithSigner.ts +7 -3
- package/src/types/Token.ts +12 -5
- package/src/types/TokenAmount.ts +3 -0
- package/src/types/fees/Fee.ts +2 -1
- package/src/types/fees/FeeBreakdown.ts +2 -1
- package/src/types/fees/PercentagePPM.ts +2 -0
- package/src/types/lnurl/LNURLPay.ts +14 -6
- package/src/types/lnurl/LNURLWithdraw.ts +12 -5
- package/src/types/wallets/LightningInvoiceCreateService.ts +26 -0
- package/src/types/wallets/MinimalBitcoinWalletInterface.ts +3 -1
- package/src/types/wallets/MinimalLightningNetworkWalletInterface.ts +3 -1
- package/src/utils/BitcoinUtils.ts +5 -0
- package/src/utils/SwapUtils.ts +61 -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
|
|
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
|
|
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,16 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
|
|
|
277
392
|
//////////////////////////////
|
|
278
393
|
//// Commit
|
|
279
394
|
/**
|
|
280
|
-
*
|
|
395
|
+
* After sending the transaction manually be sure to call the {@link waitTillCommited} function
|
|
396
|
+
* to wait till the initiation transaction is observed, processed by the SDK and state of the swap
|
|
397
|
+
* properly updated.
|
|
281
398
|
*
|
|
282
|
-
* @
|
|
283
|
-
* (this is handled on swap creation, if you commit right after quoting, you can use skipChecks=true)
|
|
399
|
+
* @inheritDoc
|
|
284
400
|
*
|
|
285
401
|
* @throws {Error} When in invalid state (not PR_CREATED)
|
|
286
402
|
*/
|
|
287
403
|
async txsCommit(skipChecks) {
|
|
288
|
-
if (this.
|
|
404
|
+
if (this._state !== ToBTCSwapState.CREATED)
|
|
289
405
|
throw new Error("Must be in CREATED state!");
|
|
290
406
|
if (this.signatureData == null)
|
|
291
407
|
throw new Error("Init signature data not known, cannot commit!");
|
|
@@ -293,45 +409,39 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
|
|
|
293
409
|
this.initiated = true;
|
|
294
410
|
await this._saveAndEmit();
|
|
295
411
|
}
|
|
296
|
-
return await this.wrapper.
|
|
412
|
+
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
413
|
}
|
|
298
414
|
/**
|
|
299
|
-
*
|
|
415
|
+
* @inheritDoc
|
|
300
416
|
*
|
|
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
417
|
* @throws {Error} If invalid signer is provided that doesn't match the swap data
|
|
307
418
|
*/
|
|
308
419
|
async commit(_signer, abortSignal, skipChecks, onBeforeTxSent) {
|
|
309
|
-
const signer = (0, base_1.isAbstractSigner)(_signer) ? _signer : await this.wrapper.
|
|
420
|
+
const signer = (0, base_1.isAbstractSigner)(_signer) ? _signer : await this.wrapper._chain.wrapSigner(_signer);
|
|
310
421
|
this.checkSigner(signer);
|
|
311
422
|
const txs = await this.txsCommit(skipChecks);
|
|
312
423
|
let txCount = 0;
|
|
313
|
-
const result = await this.wrapper.
|
|
424
|
+
const result = await this.wrapper._chain.sendAndConfirm(signer, txs, true, abortSignal, false, (txId, rawTx) => {
|
|
314
425
|
txCount++;
|
|
315
426
|
if (onBeforeTxSent != null && txCount === txs.length)
|
|
316
427
|
onBeforeTxSent(txId);
|
|
317
428
|
return Promise.resolve();
|
|
318
429
|
});
|
|
319
|
-
this.
|
|
320
|
-
if (this.
|
|
430
|
+
this._commitTxId = result[result.length - 1];
|
|
431
|
+
if (this._state === ToBTCSwapState.CREATED || this._state === ToBTCSwapState.QUOTE_SOFT_EXPIRED || this._state === ToBTCSwapState.QUOTE_EXPIRED) {
|
|
321
432
|
await this._saveAndEmit(ToBTCSwapState.COMMITED);
|
|
322
433
|
}
|
|
323
|
-
return this.
|
|
434
|
+
return this._commitTxId;
|
|
324
435
|
}
|
|
325
436
|
/**
|
|
326
|
-
*
|
|
437
|
+
* @inheritDoc
|
|
327
438
|
*
|
|
328
|
-
* @
|
|
329
|
-
* @throws {Error} If swap is not in the correct state (must be CREATED)
|
|
439
|
+
* @throws {Error} If swap is not in the correct state (must be {@link ToBTCSwapState.CREATED})
|
|
330
440
|
*/
|
|
331
441
|
async waitTillCommited(abortSignal) {
|
|
332
|
-
if (this.
|
|
442
|
+
if (this._state === ToBTCSwapState.COMMITED || this._state === ToBTCSwapState.CLAIMED)
|
|
333
443
|
return Promise.resolve();
|
|
334
|
-
if (this.
|
|
444
|
+
if (this._state !== ToBTCSwapState.CREATED && this._state !== ToBTCSwapState.QUOTE_SOFT_EXPIRED)
|
|
335
445
|
throw new Error("Invalid state (not CREATED)");
|
|
336
446
|
const abortController = (0, Utils_1.extendAbortController)(abortSignal);
|
|
337
447
|
let result;
|
|
@@ -352,28 +462,35 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
|
|
|
352
462
|
this.logger.debug("waitTillCommited(): Resolved from watchdog - commited");
|
|
353
463
|
if (result === false) {
|
|
354
464
|
this.logger.debug("waitTillCommited(): Resolved from watchdog - signature expiry");
|
|
355
|
-
if (this.
|
|
465
|
+
if (this._state === ToBTCSwapState.QUOTE_SOFT_EXPIRED || this._state === ToBTCSwapState.CREATED) {
|
|
356
466
|
await this._saveAndEmit(ToBTCSwapState.QUOTE_EXPIRED);
|
|
357
467
|
}
|
|
358
468
|
throw new Error("Quote expired while waiting for transaction confirmation!");
|
|
359
469
|
}
|
|
360
|
-
if (this.
|
|
470
|
+
if (this._state === ToBTCSwapState.QUOTE_SOFT_EXPIRED || this._state === ToBTCSwapState.CREATED || this._state === ToBTCSwapState.QUOTE_EXPIRED) {
|
|
361
471
|
await this._saveAndEmit(ToBTCSwapState.COMMITED);
|
|
362
472
|
}
|
|
363
473
|
}
|
|
364
474
|
//////////////////////////////
|
|
365
475
|
//// Payment
|
|
476
|
+
/**
|
|
477
|
+
* Waits till the swap is processed by the intermediary (LP)
|
|
478
|
+
*
|
|
479
|
+
* @param checkIntervalSeconds How often to poll the intermediary for status (5 seconds default)
|
|
480
|
+
* @param abortSignal Abort signal
|
|
481
|
+
* @internal
|
|
482
|
+
*/
|
|
366
483
|
async waitTillIntermediarySwapProcessed(checkIntervalSeconds, abortSignal) {
|
|
367
484
|
if (this.url == null)
|
|
368
485
|
throw new Error("LP URL not specified!");
|
|
369
486
|
checkIntervalSeconds ??= 5;
|
|
370
487
|
let resp = { code: IntermediaryAPI_1.RefundAuthorizationResponseCodes.PENDING, msg: "" };
|
|
371
488
|
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.
|
|
489
|
+
resp = await IntermediaryAPI_1.IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this._data.getSequence());
|
|
373
490
|
if (resp.code === IntermediaryAPI_1.RefundAuthorizationResponseCodes.PAID) {
|
|
374
491
|
const validResponse = await this._setPaymentResult(resp.data, true);
|
|
375
492
|
if (validResponse) {
|
|
376
|
-
if (this.
|
|
493
|
+
if (this._state === ToBTCSwapState.COMMITED || this._state === ToBTCSwapState.REFUNDABLE) {
|
|
377
494
|
await this._saveAndEmit(ToBTCSwapState.SOFT_CLAIMED);
|
|
378
495
|
}
|
|
379
496
|
}
|
|
@@ -393,28 +510,29 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
|
|
|
393
510
|
* refund.
|
|
394
511
|
*
|
|
395
512
|
* @param save whether to save the data
|
|
396
|
-
* @returns true if swap is processed, false if the swap is still ongoing
|
|
397
|
-
*
|
|
513
|
+
* @returns `true` if swap is processed, `false` if the swap is still ongoing
|
|
514
|
+
*
|
|
515
|
+
* @internal
|
|
398
516
|
*/
|
|
399
517
|
async checkIntermediarySwapProcessed(save = true) {
|
|
400
|
-
if (this.
|
|
518
|
+
if (this._state === ToBTCSwapState.CREATED || this._state == ToBTCSwapState.QUOTE_EXPIRED || this.url == null)
|
|
401
519
|
return false;
|
|
402
520
|
if (this.isFinished() || this.isRefundable())
|
|
403
521
|
return true;
|
|
404
522
|
//Check if that maybe already concluded according to the LP
|
|
405
|
-
const resp = await IntermediaryAPI_1.IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this.
|
|
523
|
+
const resp = await IntermediaryAPI_1.IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this._data.getSequence());
|
|
406
524
|
switch (resp.code) {
|
|
407
525
|
case IntermediaryAPI_1.RefundAuthorizationResponseCodes.PAID:
|
|
408
526
|
const processed = await this._setPaymentResult(resp.data, true);
|
|
409
527
|
if (processed) {
|
|
410
|
-
this.
|
|
528
|
+
this._state = ToBTCSwapState.SOFT_CLAIMED;
|
|
411
529
|
if (save)
|
|
412
530
|
await this._saveAndEmit();
|
|
413
531
|
}
|
|
414
532
|
return processed;
|
|
415
533
|
case IntermediaryAPI_1.RefundAuthorizationResponseCodes.REFUND_DATA:
|
|
416
|
-
await this.wrapper.
|
|
417
|
-
this.
|
|
534
|
+
await this.wrapper._contract.isValidRefundAuthorization(this._data, resp.data);
|
|
535
|
+
this._state = ToBTCSwapState.REFUNDABLE;
|
|
418
536
|
if (save)
|
|
419
537
|
await this._saveAndEmit();
|
|
420
538
|
return true;
|
|
@@ -423,23 +541,24 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
|
|
|
423
541
|
}
|
|
424
542
|
}
|
|
425
543
|
/**
|
|
426
|
-
* A blocking promise resolving when swap was concluded by the intermediary,
|
|
544
|
+
* A blocking promise resolving when swap was concluded by the intermediary (LP),
|
|
427
545
|
* rejecting in case of failure
|
|
428
546
|
*
|
|
429
547
|
* @param maxWaitTimeSeconds Maximum time in seconds to wait for the swap to be settled, an error is thrown if the
|
|
430
548
|
* swap is taking too long to claim
|
|
431
|
-
* @param checkIntervalSeconds
|
|
432
|
-
* @param abortSignal
|
|
433
|
-
* @returns
|
|
549
|
+
* @param checkIntervalSeconds How often to poll the intermediary for answer
|
|
550
|
+
* @param abortSignal Abort signal
|
|
551
|
+
* @returns `true` if swap was successful, `false` if swap failed and we can refund
|
|
552
|
+
*
|
|
434
553
|
* @throws {IntermediaryError} If a swap is determined expired by the intermediary, but it is actually still valid
|
|
435
554
|
* @throws {SignatureVerificationError} If the swap should be cooperatively refundable but the intermediary returned
|
|
436
555
|
* invalid refund signature
|
|
437
|
-
* @throws {Error} When swap expires or if the swap has invalid state (must be COMMITED)
|
|
556
|
+
* @throws {Error} When swap expires or if the swap has invalid state (must be {@link ToBTCSwapState.COMMITED})
|
|
438
557
|
*/
|
|
439
558
|
async waitForPayment(maxWaitTimeSeconds, checkIntervalSeconds, abortSignal) {
|
|
440
|
-
if (this.
|
|
559
|
+
if (this._state === ToBTCSwapState.CLAIMED)
|
|
441
560
|
return Promise.resolve(true);
|
|
442
|
-
if (this.
|
|
561
|
+
if (this._state !== ToBTCSwapState.COMMITED && this._state !== ToBTCSwapState.SOFT_CLAIMED)
|
|
443
562
|
throw new Error("Invalid state (not COMMITED)");
|
|
444
563
|
const abortController = (0, Utils_1.extendAbortController)(abortSignal);
|
|
445
564
|
let timedOut = false;
|
|
@@ -462,12 +581,12 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
|
|
|
462
581
|
abortController.abort();
|
|
463
582
|
if (timedOut) {
|
|
464
583
|
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.
|
|
584
|
+
` Please check later or wait till ${new Date(Number(this._data.getExpiry()) * 1000).toLocaleString()} to refund unilaterally!`);
|
|
466
585
|
}
|
|
467
586
|
throw e;
|
|
468
587
|
}
|
|
469
588
|
if (typeof result !== "object") {
|
|
470
|
-
if (this.
|
|
589
|
+
if (this._state === ToBTCSwapState.REFUNDABLE)
|
|
471
590
|
throw new Error("Swap expired");
|
|
472
591
|
this.logger.debug("waitTillRefunded(): Resolved from state change");
|
|
473
592
|
return true;
|
|
@@ -478,15 +597,15 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
|
|
|
478
597
|
return true;
|
|
479
598
|
case IntermediaryAPI_1.RefundAuthorizationResponseCodes.REFUND_DATA:
|
|
480
599
|
const resultData = result.data;
|
|
481
|
-
await this.wrapper.
|
|
600
|
+
await this.wrapper._contract.isValidRefundAuthorization(this._data, resultData);
|
|
482
601
|
await this._saveAndEmit(ToBTCSwapState.REFUNDABLE);
|
|
483
602
|
return false;
|
|
484
603
|
case IntermediaryAPI_1.RefundAuthorizationResponseCodes.EXPIRED:
|
|
485
|
-
if (await this.wrapper.
|
|
604
|
+
if (await this.wrapper._contract.isExpired(this._getInitiator(), this._data))
|
|
486
605
|
throw new Error("Swap expired");
|
|
487
606
|
throw new IntermediaryError_1.IntermediaryError("Swap expired");
|
|
488
607
|
case IntermediaryAPI_1.RefundAuthorizationResponseCodes.NOT_FOUND:
|
|
489
|
-
if (this.
|
|
608
|
+
if (this._state === ToBTCSwapState.CLAIMED)
|
|
490
609
|
return true;
|
|
491
610
|
throw new Error("LP swap not found");
|
|
492
611
|
}
|
|
@@ -497,11 +616,12 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
|
|
|
497
616
|
/**
|
|
498
617
|
* Get the estimated smart chain transaction fee of the refund transaction
|
|
499
618
|
*/
|
|
500
|
-
|
|
501
|
-
|
|
619
|
+
async getRefundNetworkFee() {
|
|
620
|
+
const swapContract = this.wrapper._contract;
|
|
621
|
+
return (0, TokenAmount_1.toTokenAmount)(await swapContract.getRefundFee(this._getInitiator(), this._data), this.wrapper._getNativeToken(), this.wrapper._prices);
|
|
502
622
|
}
|
|
503
623
|
/**
|
|
504
|
-
*
|
|
624
|
+
* @inheritDoc
|
|
505
625
|
*
|
|
506
626
|
* @throws {IntermediaryError} If intermediary returns invalid response in case cooperative refund should be used
|
|
507
627
|
* @throws {SignatureVerificationError} If intermediary returned invalid cooperative refund signature
|
|
@@ -511,46 +631,46 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
|
|
|
511
631
|
if (!this.isRefundable())
|
|
512
632
|
throw new Error("Must be in REFUNDABLE state or expired!");
|
|
513
633
|
signer ??= this._getInitiator();
|
|
514
|
-
if (await this.wrapper.
|
|
515
|
-
return await this.wrapper.
|
|
634
|
+
if (await this.wrapper._contract.isExpired(this._getInitiator(), this._data)) {
|
|
635
|
+
return await this.wrapper._contract.txsRefund(signer, this._data, true, true);
|
|
516
636
|
}
|
|
517
637
|
else {
|
|
518
638
|
if (this.url == null)
|
|
519
639
|
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.
|
|
640
|
+
const res = await IntermediaryAPI_1.IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this._data.getSequence());
|
|
521
641
|
if (res.code === IntermediaryAPI_1.RefundAuthorizationResponseCodes.REFUND_DATA) {
|
|
522
|
-
return await this.wrapper.
|
|
642
|
+
return await this.wrapper._contract.txsRefundWithAuthorization(signer, this._data, res.data, true, true);
|
|
523
643
|
}
|
|
524
644
|
throw new IntermediaryError_1.IntermediaryError("Invalid intermediary cooperative message returned");
|
|
525
645
|
}
|
|
526
646
|
}
|
|
527
647
|
/**
|
|
528
|
-
*
|
|
648
|
+
* @inheritDoc
|
|
529
649
|
*
|
|
530
|
-
* @param _signer Signer to sign the transactions with, must be the same as used in the initialization
|
|
531
|
-
* @param abortSignal Abort signal
|
|
532
650
|
* @throws {Error} If invalid signer is provided that doesn't match the swap data
|
|
533
651
|
*/
|
|
534
652
|
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.
|
|
653
|
+
const signer = (0, base_1.isAbstractSigner)(_signer) ? _signer : await this.wrapper._chain.wrapSigner(_signer);
|
|
654
|
+
const result = await this.wrapper._chain.sendAndConfirm(signer, await this.txsRefund(signer.getAddress()), true, abortSignal);
|
|
655
|
+
this._refundTxId = result[0];
|
|
656
|
+
if (this._state === ToBTCSwapState.COMMITED || this._state === ToBTCSwapState.REFUNDABLE || this._state === ToBTCSwapState.SOFT_CLAIMED) {
|
|
539
657
|
await this._saveAndEmit(ToBTCSwapState.REFUNDED);
|
|
540
658
|
}
|
|
541
659
|
return result[0];
|
|
542
660
|
}
|
|
543
661
|
/**
|
|
544
|
-
*
|
|
662
|
+
* @inheritDoc
|
|
545
663
|
*
|
|
546
|
-
* @
|
|
547
|
-
*
|
|
664
|
+
* @throws {Error} When swap is not in a valid state (must be {@link ToBTCSwapState.COMMITED} or
|
|
665
|
+
* {@link ToBTCSwapState.REFUNDABLE})
|
|
548
666
|
* @throws {Error} If we tried to refund but claimer was able to claim first
|
|
549
667
|
*/
|
|
550
668
|
async waitTillRefunded(abortSignal) {
|
|
551
|
-
if (this.
|
|
669
|
+
if (this._state === ToBTCSwapState.REFUNDED)
|
|
552
670
|
return Promise.resolve();
|
|
553
|
-
if (this.
|
|
671
|
+
if (this._state !== ToBTCSwapState.COMMITED &&
|
|
672
|
+
this._state !== ToBTCSwapState.SOFT_CLAIMED &&
|
|
673
|
+
this._state !== ToBTCSwapState.REFUNDABLE)
|
|
554
674
|
throw new Error("Invalid state (not COMMITED)");
|
|
555
675
|
const abortController = new AbortController();
|
|
556
676
|
if (abortSignal != null)
|
|
@@ -571,19 +691,22 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
|
|
|
571
691
|
}
|
|
572
692
|
this.logger.debug("waitTillRefunded(): Resolved from watchdog");
|
|
573
693
|
if (res?.type === base_1.SwapCommitStateType.PAID) {
|
|
574
|
-
if (this.
|
|
575
|
-
this.
|
|
694
|
+
if (this._claimTxId == null)
|
|
695
|
+
this._claimTxId = await res.getClaimTxId();
|
|
576
696
|
await this._saveAndEmit(ToBTCSwapState.CLAIMED);
|
|
577
697
|
throw new Error("Tried to refund swap, but claimer claimed it in the meantime!");
|
|
578
698
|
}
|
|
579
699
|
if (res?.type === base_1.SwapCommitStateType.NOT_COMMITED) {
|
|
580
|
-
if (this.
|
|
581
|
-
this.
|
|
700
|
+
if (this._refundTxId == null && res.getRefundTxId != null)
|
|
701
|
+
this._refundTxId = await res.getRefundTxId();
|
|
582
702
|
await this._saveAndEmit(ToBTCSwapState.REFUNDED);
|
|
583
703
|
}
|
|
584
704
|
}
|
|
585
705
|
//////////////////////////////
|
|
586
706
|
//// Storage
|
|
707
|
+
/**
|
|
708
|
+
* @inheritDoc
|
|
709
|
+
*/
|
|
587
710
|
serialize() {
|
|
588
711
|
const obj = super.serialize();
|
|
589
712
|
return {
|
|
@@ -601,75 +724,53 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
|
|
|
601
724
|
* @private
|
|
602
725
|
*/
|
|
603
726
|
async syncStateFromChain(quoteDefinitelyExpired, commitStatus) {
|
|
604
|
-
if (this.
|
|
605
|
-
this.
|
|
606
|
-
this.
|
|
607
|
-
this.
|
|
608
|
-
this.
|
|
727
|
+
if (this._state === ToBTCSwapState.CREATED ||
|
|
728
|
+
this._state === ToBTCSwapState.QUOTE_SOFT_EXPIRED ||
|
|
729
|
+
this._state === ToBTCSwapState.COMMITED ||
|
|
730
|
+
this._state === ToBTCSwapState.SOFT_CLAIMED ||
|
|
731
|
+
this._state === ToBTCSwapState.REFUNDABLE) {
|
|
609
732
|
let quoteExpired = false;
|
|
610
|
-
if (this.
|
|
733
|
+
if (this._state === ToBTCSwapState.CREATED || this._state === ToBTCSwapState.QUOTE_SOFT_EXPIRED) {
|
|
611
734
|
//Check if quote is still valid
|
|
612
735
|
quoteExpired = quoteDefinitelyExpired ?? await this._verifyQuoteDefinitelyExpired();
|
|
613
736
|
}
|
|
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)) {
|
|
737
|
+
commitStatus ??= await this.wrapper._contract.getCommitStatus(this._getInitiator(), this._data);
|
|
738
|
+
if (commitStatus != null && await this._forciblySetOnchainState(commitStatus))
|
|
739
|
+
return true;
|
|
740
|
+
if ((this._state === ToBTCSwapState.CREATED || this._state === ToBTCSwapState.QUOTE_SOFT_EXPIRED)) {
|
|
652
741
|
if (quoteExpired) {
|
|
653
|
-
this.
|
|
742
|
+
this._state = ToBTCSwapState.QUOTE_EXPIRED;
|
|
654
743
|
return true;
|
|
655
744
|
}
|
|
656
745
|
}
|
|
657
746
|
}
|
|
658
747
|
return false;
|
|
659
748
|
}
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
749
|
+
/**
|
|
750
|
+
* @inheritDoc
|
|
751
|
+
* @internal
|
|
752
|
+
*/
|
|
753
|
+
_shouldFetchOnchainState() {
|
|
754
|
+
return this._state === ToBTCSwapState.CREATED ||
|
|
755
|
+
this._state === ToBTCSwapState.QUOTE_SOFT_EXPIRED ||
|
|
756
|
+
this._state === ToBTCSwapState.COMMITED ||
|
|
757
|
+
this._state === ToBTCSwapState.SOFT_CLAIMED ||
|
|
758
|
+
this._state === ToBTCSwapState.REFUNDABLE;
|
|
666
759
|
}
|
|
760
|
+
/**
|
|
761
|
+
* @inheritDoc
|
|
762
|
+
* @internal
|
|
763
|
+
*/
|
|
667
764
|
_shouldFetchExpiryStatus() {
|
|
668
|
-
return this.
|
|
765
|
+
return this._state === ToBTCSwapState.CREATED || this._state === ToBTCSwapState.QUOTE_SOFT_EXPIRED;
|
|
669
766
|
}
|
|
767
|
+
/**
|
|
768
|
+
* @inheritDoc
|
|
769
|
+
* @internal
|
|
770
|
+
*/
|
|
670
771
|
async _sync(save, quoteDefinitelyExpired, commitStatus) {
|
|
671
772
|
let changed = await this.syncStateFromChain(quoteDefinitelyExpired, commitStatus);
|
|
672
|
-
if (this.
|
|
773
|
+
if (this._state === ToBTCSwapState.COMMITED || this._state === ToBTCSwapState.SOFT_CLAIMED) {
|
|
673
774
|
//Check if that maybe already concluded
|
|
674
775
|
try {
|
|
675
776
|
if (await this.checkIntermediarySwapProcessed(false))
|
|
@@ -683,11 +784,58 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
|
|
|
683
784
|
await this._saveAndEmit();
|
|
684
785
|
return changed;
|
|
685
786
|
}
|
|
787
|
+
/**
|
|
788
|
+
* @inheritDoc
|
|
789
|
+
* @internal
|
|
790
|
+
*/
|
|
791
|
+
async _forciblySetOnchainState(commitStatus) {
|
|
792
|
+
switch (commitStatus.type) {
|
|
793
|
+
case base_1.SwapCommitStateType.PAID:
|
|
794
|
+
if (this._claimTxId == null && commitStatus.getClaimTxId)
|
|
795
|
+
this._claimTxId = await commitStatus.getClaimTxId();
|
|
796
|
+
const eventResult = await commitStatus.getClaimResult();
|
|
797
|
+
try {
|
|
798
|
+
await this._setPaymentResult({ secret: eventResult, txId: Buffer.from(eventResult, "hex").reverse().toString("hex") });
|
|
799
|
+
}
|
|
800
|
+
catch (e) {
|
|
801
|
+
this.logger.error(`Failed to set payment result ${eventResult} on the swap!`);
|
|
802
|
+
}
|
|
803
|
+
this._state = ToBTCSwapState.CLAIMED;
|
|
804
|
+
return true;
|
|
805
|
+
case base_1.SwapCommitStateType.REFUNDABLE:
|
|
806
|
+
this._state = ToBTCSwapState.REFUNDABLE;
|
|
807
|
+
return true;
|
|
808
|
+
case base_1.SwapCommitStateType.EXPIRED:
|
|
809
|
+
if (this._refundTxId == null && commitStatus.getRefundTxId)
|
|
810
|
+
this._refundTxId = await commitStatus.getRefundTxId();
|
|
811
|
+
this._state = this._refundTxId == null ? ToBTCSwapState.QUOTE_EXPIRED : ToBTCSwapState.REFUNDED;
|
|
812
|
+
return true;
|
|
813
|
+
case base_1.SwapCommitStateType.NOT_COMMITED:
|
|
814
|
+
if (this._refundTxId == null && commitStatus.getRefundTxId)
|
|
815
|
+
this._refundTxId = await commitStatus.getRefundTxId();
|
|
816
|
+
if (this._refundTxId != null) {
|
|
817
|
+
this._state = ToBTCSwapState.REFUNDED;
|
|
818
|
+
return true;
|
|
819
|
+
}
|
|
820
|
+
break;
|
|
821
|
+
case base_1.SwapCommitStateType.COMMITED:
|
|
822
|
+
if (this._state !== ToBTCSwapState.COMMITED && this._state !== ToBTCSwapState.REFUNDABLE && this._state !== ToBTCSwapState.SOFT_CLAIMED) {
|
|
823
|
+
this._state = ToBTCSwapState.COMMITED;
|
|
824
|
+
return true;
|
|
825
|
+
}
|
|
826
|
+
break;
|
|
827
|
+
}
|
|
828
|
+
return false;
|
|
829
|
+
}
|
|
830
|
+
/**
|
|
831
|
+
* @inheritDoc
|
|
832
|
+
* @internal
|
|
833
|
+
*/
|
|
686
834
|
async _tick(save) {
|
|
687
|
-
switch (this.
|
|
835
|
+
switch (this._state) {
|
|
688
836
|
case ToBTCSwapState.CREATED:
|
|
689
837
|
if (this.expiry < Date.now()) {
|
|
690
|
-
this.
|
|
838
|
+
this._state = ToBTCSwapState.QUOTE_SOFT_EXPIRED;
|
|
691
839
|
if (save)
|
|
692
840
|
await this._saveAndEmit();
|
|
693
841
|
return true;
|
|
@@ -695,9 +843,9 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
|
|
|
695
843
|
break;
|
|
696
844
|
case ToBTCSwapState.COMMITED:
|
|
697
845
|
case ToBTCSwapState.SOFT_CLAIMED:
|
|
698
|
-
const expired = await this.wrapper.
|
|
846
|
+
const expired = await this.wrapper._contract.isExpired(this._getInitiator(), this._data);
|
|
699
847
|
if (expired) {
|
|
700
|
-
this.
|
|
848
|
+
this._state = ToBTCSwapState.REFUNDABLE;
|
|
701
849
|
if (save)
|
|
702
850
|
await this._saveAndEmit();
|
|
703
851
|
return true;
|
|
@@ -708,14 +856,3 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
|
|
|
708
856
|
}
|
|
709
857
|
}
|
|
710
858
|
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 = {}));
|