@atomiqlabs/sdk 8.1.8 → 8.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +302 -221
- 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 +512 -378
- 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
|
@@ -23,6 +23,7 @@ import {ppmToPercentage} from "../../../types/fees/PercentagePPM";
|
|
|
23
23
|
import {TokenAmount, toTokenAmount} from "../../../types/TokenAmount";
|
|
24
24
|
import {BtcToken, SCToken} from "../../../types/Token";
|
|
25
25
|
import {timeoutPromise} from "../../../utils/TimeoutUtils";
|
|
26
|
+
import {SwapExecutionActionCommit} from "../../../types/SwapExecutionAction";
|
|
26
27
|
|
|
27
28
|
export type IToBTCSwapInit<T extends SwapData> = IEscrowSelfInitSwapInit<T> & {
|
|
28
29
|
signatureData?: SignatureData,
|
|
@@ -44,16 +45,79 @@ export function isIToBTCSwapInit<T extends SwapData>(obj: any): obj is IToBTCSwa
|
|
|
44
45
|
isIEscrowSelfInitSwapInit<T>(obj);
|
|
45
46
|
}
|
|
46
47
|
|
|
48
|
+
/**
|
|
49
|
+
* State enum for escrow-based Smart chain -> Bitcoin (on-chain & lightning) swaps
|
|
50
|
+
*
|
|
51
|
+
* @category Swaps
|
|
52
|
+
*/
|
|
53
|
+
export enum ToBTCSwapState {
|
|
54
|
+
/**
|
|
55
|
+
* Intermediary (LP) was unable to process the swap and the funds were refunded on the
|
|
56
|
+
* source chain
|
|
57
|
+
*/
|
|
58
|
+
REFUNDED = -3,
|
|
59
|
+
/**
|
|
60
|
+
* Swap has expired for good and there is no way how it can be executed anymore
|
|
61
|
+
*/
|
|
62
|
+
QUOTE_EXPIRED = -2,
|
|
63
|
+
/**
|
|
64
|
+
* A swap is almost expired, and it should be presented to the user as expired, though
|
|
65
|
+
* there is still a chance that it will be processed
|
|
66
|
+
*/
|
|
67
|
+
QUOTE_SOFT_EXPIRED = -1,
|
|
68
|
+
/**
|
|
69
|
+
* Swap was created, use the {@link IToBTCSwap.commit} or {@link IToBTCSwap.txsCommit} to
|
|
70
|
+
* initiate it by creating the swap escrow on the source chain
|
|
71
|
+
*/
|
|
72
|
+
CREATED = 0,
|
|
73
|
+
/**
|
|
74
|
+
* Swap escrow was initiated (committed) on the source chain, the intermediary (LP) will
|
|
75
|
+
* now process the swap. You can wait till that happens with the {@link IToBTCSwap.waitForPayment}
|
|
76
|
+
* function.
|
|
77
|
+
*/
|
|
78
|
+
COMMITED = 1,
|
|
79
|
+
/**
|
|
80
|
+
* The intermediary (LP) has processed the transaction and sent out the funds on the destination chain,
|
|
81
|
+
* but hasn't yet settled the escrow on the source chain.
|
|
82
|
+
*/
|
|
83
|
+
SOFT_CLAIMED = 2,
|
|
84
|
+
/**
|
|
85
|
+
* Swap was successfully settled by the intermediary (LP) on the source chain
|
|
86
|
+
*/
|
|
87
|
+
CLAIMED = 3,
|
|
88
|
+
/**
|
|
89
|
+
* Intermediary (LP) was unable to process the swap and the swap escrow on the source chain
|
|
90
|
+
* is refundable, call {@link IToBTCSwap.refund} or {@link IToBTCSwap.txsRefund} to refund
|
|
91
|
+
*/
|
|
92
|
+
REFUNDABLE = 4
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Base class for escrow-based Smart chain -> Bitcoin (on-chain & lightning) swaps
|
|
97
|
+
*
|
|
98
|
+
* @category Swaps
|
|
99
|
+
*/
|
|
47
100
|
export abstract class IToBTCSwap<
|
|
48
101
|
T extends ChainType = ChainType,
|
|
49
102
|
D extends IToBTCDefinition<T, IToBTCWrapper<T, D>, IToBTCSwap<T, D>> = IToBTCDefinition<T, IToBTCWrapper<T, any>, IToBTCSwap<T, any>>,
|
|
50
103
|
> extends IEscrowSelfInitSwap<T, D, ToBTCSwapState> implements IRefundableSwap<T, D, ToBTCSwapState> {
|
|
104
|
+
/**
|
|
105
|
+
* @internal
|
|
106
|
+
*/
|
|
107
|
+
protected readonly abstract outputToken: BtcToken;
|
|
108
|
+
/**
|
|
109
|
+
* @internal
|
|
110
|
+
*/
|
|
51
111
|
protected readonly networkFee: bigint;
|
|
112
|
+
/**
|
|
113
|
+
* @internal
|
|
114
|
+
*/
|
|
52
115
|
protected networkFeeBtc: bigint;
|
|
53
|
-
protected readonly abstract outputToken: BtcToken;
|
|
54
116
|
|
|
55
|
-
|
|
56
|
-
|
|
117
|
+
/**
|
|
118
|
+
* @internal
|
|
119
|
+
*/
|
|
120
|
+
readonly _data!: T["Data"];
|
|
57
121
|
|
|
58
122
|
protected constructor(wrapper: D["Wrapper"], serializedObject: any);
|
|
59
123
|
protected constructor(wrapper: D["Wrapper"], init: IToBTCSwapInit<T["Data"]>);
|
|
@@ -63,10 +127,10 @@ export abstract class IToBTCSwap<
|
|
|
63
127
|
) {
|
|
64
128
|
super(wrapper, initOrObject);
|
|
65
129
|
if(isIToBTCSwapInit<T["Data"]>(initOrObject)) {
|
|
66
|
-
this.
|
|
130
|
+
this._state = ToBTCSwapState.CREATED;
|
|
67
131
|
this.networkFee = initOrObject.networkFee;
|
|
68
132
|
this.networkFeeBtc = initOrObject.networkFeeBtc;
|
|
69
|
-
this.
|
|
133
|
+
this._data = initOrObject.data;
|
|
70
134
|
this.signatureData = initOrObject.signatureData;
|
|
71
135
|
} else {
|
|
72
136
|
this.networkFee = toBigInt(initOrObject.networkFee);
|
|
@@ -74,30 +138,38 @@ export abstract class IToBTCSwap<
|
|
|
74
138
|
}
|
|
75
139
|
}
|
|
76
140
|
|
|
141
|
+
/**
|
|
142
|
+
* @inheritDoc
|
|
143
|
+
* @internal
|
|
144
|
+
*/
|
|
77
145
|
protected getSwapData(): T["Data"] {
|
|
78
|
-
return this.
|
|
146
|
+
return this._data;
|
|
79
147
|
}
|
|
80
148
|
|
|
149
|
+
/**
|
|
150
|
+
* @inheritDoc
|
|
151
|
+
* @internal
|
|
152
|
+
*/
|
|
81
153
|
protected upgradeVersion() {
|
|
82
154
|
if(this.version == null) {
|
|
83
|
-
switch(this.
|
|
155
|
+
switch(this._state) {
|
|
84
156
|
case -2:
|
|
85
|
-
this.
|
|
157
|
+
this._state = ToBTCSwapState.REFUNDED
|
|
86
158
|
break;
|
|
87
159
|
case -1:
|
|
88
|
-
this.
|
|
160
|
+
this._state = ToBTCSwapState.QUOTE_EXPIRED
|
|
89
161
|
break;
|
|
90
162
|
case 0:
|
|
91
|
-
this.
|
|
163
|
+
this._state = ToBTCSwapState.CREATED
|
|
92
164
|
break;
|
|
93
165
|
case 1:
|
|
94
|
-
this.
|
|
166
|
+
this._state = ToBTCSwapState.COMMITED
|
|
95
167
|
break;
|
|
96
168
|
case 2:
|
|
97
|
-
this.
|
|
169
|
+
this._state = ToBTCSwapState.CLAIMED
|
|
98
170
|
break;
|
|
99
171
|
case 3:
|
|
100
|
-
this.
|
|
172
|
+
this._state = ToBTCSwapState.REFUNDABLE
|
|
101
173
|
break;
|
|
102
174
|
}
|
|
103
175
|
this.version = 1;
|
|
@@ -105,8 +177,8 @@ export abstract class IToBTCSwap<
|
|
|
105
177
|
}
|
|
106
178
|
|
|
107
179
|
/**
|
|
108
|
-
*
|
|
109
|
-
* @
|
|
180
|
+
* @inheritDoc
|
|
181
|
+
* @internal
|
|
110
182
|
*/
|
|
111
183
|
protected tryRecomputeSwapPrice() {
|
|
112
184
|
const output = this.getOutput();
|
|
@@ -123,7 +195,7 @@ export abstract class IToBTCSwap<
|
|
|
123
195
|
|
|
124
196
|
/**
|
|
125
197
|
* Returns the payment hash identifier to be sent to the LP for getStatus and getRefund
|
|
126
|
-
* @
|
|
198
|
+
* @internal
|
|
127
199
|
*/
|
|
128
200
|
protected getLpIdentifier(): string {
|
|
129
201
|
return this.getClaimHash();
|
|
@@ -136,6 +208,8 @@ export abstract class IToBTCSwap<
|
|
|
136
208
|
* @param check Whether to check the passed result
|
|
137
209
|
* @returns true if check passed, false if check failed with a soft error (e.g. tx not yet found in the mempool)
|
|
138
210
|
* @throws {IntermediaryError} When the data returned by the intermediary isn't valid
|
|
211
|
+
*
|
|
212
|
+
* @internal
|
|
139
213
|
*/
|
|
140
214
|
abstract _setPaymentResult(result: {secret?: string, txId?: string}, check?: boolean): Promise<boolean>;
|
|
141
215
|
|
|
@@ -143,53 +217,86 @@ export abstract class IToBTCSwap<
|
|
|
143
217
|
//////////////////////////////
|
|
144
218
|
//// Getters & utils
|
|
145
219
|
|
|
220
|
+
/**
|
|
221
|
+
* @inheritDoc
|
|
222
|
+
*/
|
|
146
223
|
getInputAddress(): string | null {
|
|
147
224
|
return this._getInitiator();
|
|
148
225
|
}
|
|
149
226
|
|
|
227
|
+
/**
|
|
228
|
+
* @inheritDoc
|
|
229
|
+
*/
|
|
150
230
|
getInputTxId(): string | null {
|
|
151
|
-
return this.
|
|
231
|
+
return this._commitTxId ?? null;
|
|
152
232
|
}
|
|
153
233
|
|
|
234
|
+
/**
|
|
235
|
+
* @inheritDoc
|
|
236
|
+
*/
|
|
154
237
|
requiresAction(): boolean {
|
|
155
238
|
return this.isRefundable();
|
|
156
239
|
}
|
|
157
240
|
|
|
158
241
|
/**
|
|
159
|
-
*
|
|
242
|
+
* @inheritDoc
|
|
160
243
|
*/
|
|
161
244
|
isFinished(): boolean {
|
|
162
|
-
return this.
|
|
245
|
+
return this._state===ToBTCSwapState.CLAIMED || this._state===ToBTCSwapState.REFUNDED || this._state===ToBTCSwapState.QUOTE_EXPIRED;
|
|
163
246
|
}
|
|
164
247
|
|
|
248
|
+
/**
|
|
249
|
+
* @inheritDoc
|
|
250
|
+
*/
|
|
165
251
|
isRefundable(): boolean {
|
|
166
|
-
return this.
|
|
252
|
+
return this._state===ToBTCSwapState.REFUNDABLE;
|
|
167
253
|
}
|
|
168
254
|
|
|
255
|
+
/**
|
|
256
|
+
* @inheritDoc
|
|
257
|
+
*/
|
|
169
258
|
isQuoteExpired(): boolean {
|
|
170
|
-
return this.
|
|
259
|
+
return this._state===ToBTCSwapState.QUOTE_EXPIRED;
|
|
171
260
|
}
|
|
172
261
|
|
|
262
|
+
/**
|
|
263
|
+
* @inheritDoc
|
|
264
|
+
*/
|
|
173
265
|
isQuoteSoftExpired(): boolean {
|
|
174
|
-
return this.
|
|
266
|
+
return this._state===ToBTCSwapState.QUOTE_EXPIRED || this._state===ToBTCSwapState.QUOTE_SOFT_EXPIRED;
|
|
175
267
|
}
|
|
176
268
|
|
|
269
|
+
/**
|
|
270
|
+
* @inheritDoc
|
|
271
|
+
*/
|
|
177
272
|
isSuccessful(): boolean {
|
|
178
|
-
return this.
|
|
273
|
+
return this._state===ToBTCSwapState.CLAIMED;
|
|
179
274
|
}
|
|
180
275
|
|
|
276
|
+
/**
|
|
277
|
+
* @inheritDoc
|
|
278
|
+
*/
|
|
181
279
|
isFailed(): boolean {
|
|
182
|
-
return this.
|
|
280
|
+
return this._state===ToBTCSwapState.REFUNDED;
|
|
183
281
|
}
|
|
184
282
|
|
|
283
|
+
/**
|
|
284
|
+
* @inheritDoc
|
|
285
|
+
* @internal
|
|
286
|
+
*/
|
|
185
287
|
_getInitiator(): string {
|
|
186
|
-
return this.
|
|
288
|
+
return this._data.getOfferer();
|
|
187
289
|
}
|
|
188
290
|
|
|
189
291
|
|
|
190
292
|
//////////////////////////////
|
|
191
293
|
//// Amounts & fees
|
|
192
294
|
|
|
295
|
+
/**
|
|
296
|
+
* Returns the swap fee charged by the intermediary (LP) on this swap
|
|
297
|
+
*
|
|
298
|
+
* @internal
|
|
299
|
+
*/
|
|
193
300
|
protected getSwapFee(): Fee<T["ChainId"], SCToken<T["ChainId"]>, BtcToken> {
|
|
194
301
|
if(this.pricingInfo==null) throw new Error("No pricing info known, cannot estimate fee!");
|
|
195
302
|
|
|
@@ -198,32 +305,33 @@ export abstract class IToBTCSwap<
|
|
|
198
305
|
const swapFeePPM = output.rawAmount==null ? 0n : feeWithoutBaseFee * 1000000n / output.rawAmount;
|
|
199
306
|
|
|
200
307
|
const amountInDstToken = toTokenAmount(
|
|
201
|
-
this.swapFeeBtc, this.outputToken, this.wrapper.
|
|
308
|
+
this.swapFeeBtc, this.outputToken, this.wrapper._prices, this.pricingInfo
|
|
202
309
|
);
|
|
203
310
|
return {
|
|
204
|
-
amountInSrcToken: toTokenAmount(this.swapFee, this.wrapper.
|
|
311
|
+
amountInSrcToken: toTokenAmount(this.swapFee, this.wrapper._tokens[this._data.getToken()], this.wrapper._prices, this.pricingInfo),
|
|
205
312
|
amountInDstToken,
|
|
206
313
|
currentUsdValue: amountInDstToken.currentUsdValue,
|
|
207
314
|
usdValue: amountInDstToken.usdValue,
|
|
208
315
|
pastUsdValue: amountInDstToken.pastUsdValue,
|
|
209
316
|
composition: {
|
|
210
|
-
base: toTokenAmount(this.pricingInfo.satsBaseFee, this.outputToken, this.wrapper.
|
|
317
|
+
base: toTokenAmount(this.pricingInfo.satsBaseFee, this.outputToken, this.wrapper._prices, this.pricingInfo),
|
|
211
318
|
percentage: ppmToPercentage(swapFeePPM)
|
|
212
319
|
}
|
|
213
320
|
};
|
|
214
321
|
}
|
|
215
322
|
|
|
216
323
|
/**
|
|
217
|
-
* Returns network fee for
|
|
218
|
-
*
|
|
324
|
+
* Returns network fee for on the destination chain for the swap
|
|
325
|
+
*
|
|
326
|
+
* @internal
|
|
219
327
|
*/
|
|
220
328
|
protected getNetworkFee(): Fee<T["ChainId"], SCToken<T["ChainId"]>, BtcToken> {
|
|
221
329
|
const amountInDstToken = toTokenAmount(
|
|
222
|
-
this.networkFeeBtc, this.outputToken, this.wrapper.
|
|
330
|
+
this.networkFeeBtc, this.outputToken, this.wrapper._prices, this.pricingInfo
|
|
223
331
|
);
|
|
224
332
|
return {
|
|
225
333
|
amountInSrcToken: toTokenAmount(
|
|
226
|
-
this.networkFee, this.wrapper.
|
|
334
|
+
this.networkFee, this.wrapper._tokens[this._data.getToken()], this.wrapper._prices, this.pricingInfo
|
|
227
335
|
),
|
|
228
336
|
amountInDstToken,
|
|
229
337
|
currentUsdValue: amountInDstToken.currentUsdValue,
|
|
@@ -232,14 +340,17 @@ export abstract class IToBTCSwap<
|
|
|
232
340
|
};
|
|
233
341
|
}
|
|
234
342
|
|
|
343
|
+
/**
|
|
344
|
+
* @inheritDoc
|
|
345
|
+
*/
|
|
235
346
|
getFee(): Fee<T["ChainId"], SCToken<T["ChainId"]>, BtcToken> {
|
|
236
347
|
const amountInDstToken = toTokenAmount(
|
|
237
|
-
this.swapFeeBtc + this.networkFeeBtc, this.outputToken, this.wrapper.
|
|
348
|
+
this.swapFeeBtc + this.networkFeeBtc, this.outputToken, this.wrapper._prices, this.pricingInfo
|
|
238
349
|
);
|
|
239
350
|
return {
|
|
240
351
|
amountInSrcToken: toTokenAmount(
|
|
241
|
-
this.swapFee + this.networkFee, this.wrapper.
|
|
242
|
-
this.wrapper.
|
|
352
|
+
this.swapFee + this.networkFee, this.wrapper._tokens[this._data.getToken()],
|
|
353
|
+
this.wrapper._prices, this.pricingInfo
|
|
243
354
|
),
|
|
244
355
|
amountInDstToken,
|
|
245
356
|
currentUsdValue: amountInDstToken.currentUsdValue,
|
|
@@ -248,6 +359,9 @@ export abstract class IToBTCSwap<
|
|
|
248
359
|
}
|
|
249
360
|
}
|
|
250
361
|
|
|
362
|
+
/**
|
|
363
|
+
* @inheritDoc
|
|
364
|
+
*/
|
|
251
365
|
getFeeBreakdown(): [
|
|
252
366
|
{type: FeeType.SWAP, fee: Fee<T["ChainId"], SCToken<T["ChainId"]>, BtcToken>},
|
|
253
367
|
{type: FeeType.NETWORK_OUTPUT, fee: Fee<T["ChainId"], SCToken<T["ChainId"]>, BtcToken>},
|
|
@@ -264,26 +378,35 @@ export abstract class IToBTCSwap<
|
|
|
264
378
|
];
|
|
265
379
|
}
|
|
266
380
|
|
|
381
|
+
/**
|
|
382
|
+
* @inheritDoc
|
|
383
|
+
*/
|
|
267
384
|
getInputToken(): SCToken<T["ChainId"]> {
|
|
268
|
-
return this.wrapper.
|
|
385
|
+
return this.wrapper._tokens[this._data.getToken()];
|
|
269
386
|
}
|
|
270
387
|
|
|
388
|
+
/**
|
|
389
|
+
* @inheritDoc
|
|
390
|
+
*/
|
|
271
391
|
getInput(): TokenAmount<T["ChainId"], SCToken<T["ChainId"]>, true> {
|
|
272
392
|
return toTokenAmount(
|
|
273
|
-
this.
|
|
274
|
-
this.wrapper.
|
|
393
|
+
this._data.getAmount(), this.wrapper._tokens[this._data.getToken()],
|
|
394
|
+
this.wrapper._prices, this.pricingInfo
|
|
275
395
|
);
|
|
276
396
|
}
|
|
277
397
|
|
|
398
|
+
/**
|
|
399
|
+
* @inheritDoc
|
|
400
|
+
*/
|
|
278
401
|
getInputWithoutFee(): TokenAmount<T["ChainId"], SCToken<T["ChainId"]>, true> {
|
|
279
402
|
return toTokenAmount(
|
|
280
|
-
this.
|
|
281
|
-
this.wrapper.
|
|
403
|
+
this._data.getAmount() - (this.swapFee + this.networkFee),
|
|
404
|
+
this.wrapper._tokens[this._data.getToken()], this.wrapper._prices, this.pricingInfo
|
|
282
405
|
);
|
|
283
406
|
}
|
|
284
407
|
|
|
285
408
|
/**
|
|
286
|
-
* Checks if the
|
|
409
|
+
* Checks if the initiator/sender on the source chain has enough balance to go through with the swap
|
|
287
410
|
*/
|
|
288
411
|
async hasEnoughBalance(): Promise<{
|
|
289
412
|
enoughBalance: boolean,
|
|
@@ -291,20 +414,21 @@ export abstract class IToBTCSwap<
|
|
|
291
414
|
required: TokenAmount<T["ChainId"], SCToken<T["ChainId"]>, true>
|
|
292
415
|
}> {
|
|
293
416
|
const [balance, commitFee] = await Promise.all([
|
|
294
|
-
this.wrapper.
|
|
295
|
-
this.
|
|
417
|
+
this.wrapper._contract.getBalance(this._getInitiator(), this._data.getToken(), false),
|
|
418
|
+
this._data.getToken()===this.wrapper._chain.getNativeCurrencyAddress() ? this.getCommitFee() : Promise.resolve(null)
|
|
296
419
|
]);
|
|
297
|
-
let required = this.
|
|
420
|
+
let required = this._data.getAmount();
|
|
298
421
|
if(commitFee!=null) required = required + commitFee;
|
|
299
422
|
return {
|
|
300
423
|
enoughBalance: balance >= required,
|
|
301
|
-
balance: toTokenAmount(balance, this.wrapper.
|
|
302
|
-
required: toTokenAmount(required, this.wrapper.
|
|
424
|
+
balance: toTokenAmount(balance, this.wrapper._tokens[this._data.getToken()], this.wrapper._prices, this.pricingInfo),
|
|
425
|
+
required: toTokenAmount(required, this.wrapper._tokens[this._data.getToken()], this.wrapper._prices, this.pricingInfo)
|
|
303
426
|
};
|
|
304
427
|
}
|
|
305
428
|
|
|
306
429
|
/**
|
|
307
|
-
*
|
|
430
|
+
* Checks if the initiator/sender on the source chain has enough native token balance
|
|
431
|
+
* to cover the transaction fee of initiating the swap
|
|
308
432
|
*/
|
|
309
433
|
async hasEnoughForTxFees(): Promise<{
|
|
310
434
|
enoughBalance: boolean,
|
|
@@ -312,13 +436,13 @@ export abstract class IToBTCSwap<
|
|
|
312
436
|
required: TokenAmount<T["ChainId"], SCToken<T["ChainId"]>, true>
|
|
313
437
|
}> {
|
|
314
438
|
const [balance, commitFee] = await Promise.all([
|
|
315
|
-
this.wrapper.
|
|
439
|
+
this.wrapper._contract.getBalance(this._getInitiator(), this.wrapper._chain.getNativeCurrencyAddress(), false),
|
|
316
440
|
this.getCommitFee()
|
|
317
441
|
]);
|
|
318
442
|
return {
|
|
319
443
|
enoughBalance: balance >= commitFee,
|
|
320
|
-
balance: toTokenAmount(balance, this.wrapper.
|
|
321
|
-
required: toTokenAmount(commitFee, this.wrapper.
|
|
444
|
+
balance: toTokenAmount(balance, this.wrapper._getNativeToken(), this.wrapper._prices),
|
|
445
|
+
required: toTokenAmount(commitFee, this.wrapper._getNativeToken(), this.wrapper._prices)
|
|
322
446
|
};
|
|
323
447
|
}
|
|
324
448
|
|
|
@@ -334,7 +458,7 @@ export abstract class IToBTCSwap<
|
|
|
334
458
|
* @param options Optional options for the swap like feeRate, AbortSignal, and timeouts/intervals
|
|
335
459
|
*
|
|
336
460
|
* @returns {boolean} Whether the swap was successfully processed by the LP, in case `false` is returned
|
|
337
|
-
* the user can refund their funds back on the source chain by calling
|
|
461
|
+
* the user can refund their funds back on the source chain by calling {@link refund}
|
|
338
462
|
*/
|
|
339
463
|
async execute(
|
|
340
464
|
signer: T["Signer"] | T["NativeSigner"],
|
|
@@ -349,20 +473,20 @@ export abstract class IToBTCSwap<
|
|
|
349
473
|
maxWaitTillSwapProcessedSeconds?: number
|
|
350
474
|
}
|
|
351
475
|
): Promise<boolean> {
|
|
352
|
-
if(this.
|
|
353
|
-
if(this.
|
|
354
|
-
if(this.
|
|
355
|
-
if(this.
|
|
476
|
+
if(this._state===ToBTCSwapState.QUOTE_EXPIRED || this._state===ToBTCSwapState.QUOTE_SOFT_EXPIRED) throw new Error("Quote expired");
|
|
477
|
+
if(this._state===ToBTCSwapState.REFUNDED) throw new Error("Swap already refunded");
|
|
478
|
+
if(this._state===ToBTCSwapState.REFUNDABLE) throw new Error("Swap refundable, refund with swap.refund()");
|
|
479
|
+
if(this._state===ToBTCSwapState.SOFT_CLAIMED || this._state===ToBTCSwapState.CLAIMED) throw new Error("Swap already settled!");
|
|
356
480
|
|
|
357
|
-
if(this.
|
|
481
|
+
if(this._state===ToBTCSwapState.CREATED) {
|
|
358
482
|
const txId = await this.commit(signer, options?.abortSignal, false, callbacks?.onSourceTransactionSent);
|
|
359
483
|
if(callbacks?.onSourceTransactionConfirmed!=null) callbacks.onSourceTransactionConfirmed(txId);
|
|
360
484
|
}
|
|
361
485
|
|
|
362
486
|
// @ts-ignore
|
|
363
|
-
if(this.
|
|
487
|
+
if(this._state===ToBTCSwapState.CLAIMED || this._state===ToBTCSwapState.SOFT_CLAIMED) return true;
|
|
364
488
|
|
|
365
|
-
if(this.
|
|
489
|
+
if(this._state===ToBTCSwapState.COMMITED) {
|
|
366
490
|
const success = await this.waitForPayment(options?.maxWaitTillSwapProcessedSeconds ?? 120, options?.paymentCheckIntervalSeconds, options?.abortSignal);
|
|
367
491
|
if(success) {
|
|
368
492
|
if(callbacks?.onSwapSettled!=null) callbacks.onSwapSettled(this.getOutputTxId()!);
|
|
@@ -375,10 +499,17 @@ export abstract class IToBTCSwap<
|
|
|
375
499
|
throw new Error("Unexpected state reached!");
|
|
376
500
|
}
|
|
377
501
|
|
|
502
|
+
/**
|
|
503
|
+
* @inheritDoc
|
|
504
|
+
* @param options.skipChecks Skip checks like making sure init signature is still valid and swap wasn't commited yet
|
|
505
|
+
* (this is handled on swap creation, if you commit right after quoting, you can use `skipChecks=true`)
|
|
506
|
+
*/
|
|
378
507
|
async txsExecute(options?: {
|
|
379
508
|
skipChecks?: boolean
|
|
380
|
-
})
|
|
381
|
-
|
|
509
|
+
}): Promise<[
|
|
510
|
+
SwapExecutionActionCommit<T>
|
|
511
|
+
]> {
|
|
512
|
+
if(this._state!==ToBTCSwapState.CREATED) throw new Error("Invalid swap state, needs to be CREATED!");
|
|
382
513
|
const txsCommit = await this.txsCommit(options?.skipChecks);
|
|
383
514
|
return [
|
|
384
515
|
{
|
|
@@ -395,15 +526,16 @@ export abstract class IToBTCSwap<
|
|
|
395
526
|
//// Commit
|
|
396
527
|
|
|
397
528
|
/**
|
|
398
|
-
*
|
|
529
|
+
* After sending the transaction manually be sure to call the {@link waitTillCommited} function
|
|
530
|
+
* to wait till the initiation transaction is observed, processed by the SDK and state of the swap
|
|
531
|
+
* properly updated.
|
|
399
532
|
*
|
|
400
|
-
* @
|
|
401
|
-
* (this is handled on swap creation, if you commit right after quoting, you can use skipChecks=true)
|
|
533
|
+
* @inheritDoc
|
|
402
534
|
*
|
|
403
535
|
* @throws {Error} When in invalid state (not PR_CREATED)
|
|
404
536
|
*/
|
|
405
537
|
async txsCommit(skipChecks?: boolean): Promise<T["TX"][]> {
|
|
406
|
-
if(this.
|
|
538
|
+
if(this._state!==ToBTCSwapState.CREATED) throw new Error("Must be in CREATED state!");
|
|
407
539
|
if(this.signatureData==null) throw new Error("Init signature data not known, cannot commit!");
|
|
408
540
|
|
|
409
541
|
if(!this.initiated) {
|
|
@@ -411,27 +543,22 @@ export abstract class IToBTCSwap<
|
|
|
411
543
|
await this._saveAndEmit();
|
|
412
544
|
}
|
|
413
545
|
|
|
414
|
-
return await this.wrapper.
|
|
415
|
-
this._getInitiator(), this.
|
|
546
|
+
return await this.wrapper._contract.txsInit(
|
|
547
|
+
this._getInitiator(), this._data, this.signatureData, skipChecks, this.feeRate
|
|
416
548
|
).catch(e => Promise.reject(e instanceof SignatureVerificationError ? new Error("Request timed out") : e));
|
|
417
549
|
}
|
|
418
550
|
|
|
419
551
|
/**
|
|
420
|
-
*
|
|
552
|
+
* @inheritDoc
|
|
421
553
|
*
|
|
422
|
-
* @param _signer Signer to sign the transactions with, must be the same as used in the initialization
|
|
423
|
-
* @param abortSignal Abort signal
|
|
424
|
-
* @param skipChecks Skip checks like making sure init signature is still valid and swap wasn't commited yet
|
|
425
|
-
* (this is handled on swap creation, if you commit right after quoting, you can skipChecks)`
|
|
426
|
-
* @param onBeforeTxSent
|
|
427
554
|
* @throws {Error} If invalid signer is provided that doesn't match the swap data
|
|
428
555
|
*/
|
|
429
556
|
async commit(_signer: T["Signer"] | T["NativeSigner"], abortSignal?: AbortSignal, skipChecks?: boolean, onBeforeTxSent?: (txId: string) => void): Promise<string> {
|
|
430
|
-
const signer = isAbstractSigner(_signer) ? _signer : await this.wrapper.
|
|
557
|
+
const signer = isAbstractSigner(_signer) ? _signer : await this.wrapper._chain.wrapSigner(_signer);
|
|
431
558
|
this.checkSigner(signer);
|
|
432
559
|
const txs = await this.txsCommit(skipChecks);
|
|
433
560
|
let txCount = 0;
|
|
434
|
-
const result = await this.wrapper.
|
|
561
|
+
const result = await this.wrapper._chain.sendAndConfirm(
|
|
435
562
|
signer, txs, true, abortSignal, false, (txId, rawTx) => {
|
|
436
563
|
txCount++;
|
|
437
564
|
if(onBeforeTxSent!=null && txCount===txs.length) onBeforeTxSent(txId);
|
|
@@ -439,22 +566,21 @@ export abstract class IToBTCSwap<
|
|
|
439
566
|
}
|
|
440
567
|
);
|
|
441
568
|
|
|
442
|
-
this.
|
|
443
|
-
if(this.
|
|
569
|
+
this._commitTxId = result[result.length-1];
|
|
570
|
+
if(this._state===ToBTCSwapState.CREATED || this._state===ToBTCSwapState.QUOTE_SOFT_EXPIRED || this._state===ToBTCSwapState.QUOTE_EXPIRED) {
|
|
444
571
|
await this._saveAndEmit(ToBTCSwapState.COMMITED);
|
|
445
572
|
}
|
|
446
|
-
return this.
|
|
573
|
+
return this._commitTxId;
|
|
447
574
|
}
|
|
448
575
|
|
|
449
576
|
/**
|
|
450
|
-
*
|
|
577
|
+
* @inheritDoc
|
|
451
578
|
*
|
|
452
|
-
* @
|
|
453
|
-
* @throws {Error} If swap is not in the correct state (must be CREATED)
|
|
579
|
+
* @throws {Error} If swap is not in the correct state (must be {@link ToBTCSwapState.CREATED})
|
|
454
580
|
*/
|
|
455
581
|
async waitTillCommited(abortSignal?: AbortSignal): Promise<void> {
|
|
456
|
-
if(this.
|
|
457
|
-
if(this.
|
|
582
|
+
if(this._state===ToBTCSwapState.COMMITED || this._state===ToBTCSwapState.CLAIMED) return Promise.resolve();
|
|
583
|
+
if(this._state!==ToBTCSwapState.CREATED && this._state!==ToBTCSwapState.QUOTE_SOFT_EXPIRED) throw new Error("Invalid state (not CREATED)");
|
|
458
584
|
|
|
459
585
|
const abortController = extendAbortController(abortSignal);
|
|
460
586
|
let result: number | boolean;
|
|
@@ -473,13 +599,13 @@ export abstract class IToBTCSwap<
|
|
|
473
599
|
if(result===true) this.logger.debug("waitTillCommited(): Resolved from watchdog - commited");
|
|
474
600
|
if(result===false) {
|
|
475
601
|
this.logger.debug("waitTillCommited(): Resolved from watchdog - signature expiry");
|
|
476
|
-
if(this.
|
|
602
|
+
if(this._state===ToBTCSwapState.QUOTE_SOFT_EXPIRED || this._state===ToBTCSwapState.CREATED) {
|
|
477
603
|
await this._saveAndEmit(ToBTCSwapState.QUOTE_EXPIRED);
|
|
478
604
|
}
|
|
479
605
|
throw new Error("Quote expired while waiting for transaction confirmation!");
|
|
480
606
|
}
|
|
481
607
|
|
|
482
|
-
if(this.
|
|
608
|
+
if(this._state===ToBTCSwapState.QUOTE_SOFT_EXPIRED || this._state===ToBTCSwapState.CREATED || this._state===ToBTCSwapState.QUOTE_EXPIRED) {
|
|
483
609
|
await this._saveAndEmit(ToBTCSwapState.COMMITED);
|
|
484
610
|
}
|
|
485
611
|
}
|
|
@@ -488,6 +614,13 @@ export abstract class IToBTCSwap<
|
|
|
488
614
|
//////////////////////////////
|
|
489
615
|
//// Payment
|
|
490
616
|
|
|
617
|
+
/**
|
|
618
|
+
* Waits till the swap is processed by the intermediary (LP)
|
|
619
|
+
*
|
|
620
|
+
* @param checkIntervalSeconds How often to poll the intermediary for status (5 seconds default)
|
|
621
|
+
* @param abortSignal Abort signal
|
|
622
|
+
* @internal
|
|
623
|
+
*/
|
|
491
624
|
protected async waitTillIntermediarySwapProcessed(
|
|
492
625
|
checkIntervalSeconds?: number,
|
|
493
626
|
abortSignal?: AbortSignal
|
|
@@ -498,11 +631,11 @@ export abstract class IToBTCSwap<
|
|
|
498
631
|
while(!abortSignal?.aborted && (
|
|
499
632
|
resp.code===RefundAuthorizationResponseCodes.PENDING || resp.code===RefundAuthorizationResponseCodes.NOT_FOUND
|
|
500
633
|
)) {
|
|
501
|
-
resp = await IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this.
|
|
634
|
+
resp = await IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this._data.getSequence());
|
|
502
635
|
if(resp.code===RefundAuthorizationResponseCodes.PAID) {
|
|
503
636
|
const validResponse = await this._setPaymentResult(resp.data, true);
|
|
504
637
|
if(validResponse) {
|
|
505
|
-
if(this.
|
|
638
|
+
if(this._state===ToBTCSwapState.COMMITED || this._state===ToBTCSwapState.REFUNDABLE) {
|
|
506
639
|
await this._saveAndEmit(ToBTCSwapState.SOFT_CLAIMED);
|
|
507
640
|
}
|
|
508
641
|
} else {
|
|
@@ -523,25 +656,26 @@ export abstract class IToBTCSwap<
|
|
|
523
656
|
* refund.
|
|
524
657
|
*
|
|
525
658
|
* @param save whether to save the data
|
|
526
|
-
* @returns true if swap is processed, false if the swap is still ongoing
|
|
527
|
-
*
|
|
659
|
+
* @returns `true` if swap is processed, `false` if the swap is still ongoing
|
|
660
|
+
*
|
|
661
|
+
* @internal
|
|
528
662
|
*/
|
|
529
663
|
protected async checkIntermediarySwapProcessed(save: boolean = true): Promise<boolean> {
|
|
530
|
-
if(this.
|
|
664
|
+
if(this._state===ToBTCSwapState.CREATED || this._state==ToBTCSwapState.QUOTE_EXPIRED || this.url==null) return false;
|
|
531
665
|
if(this.isFinished() || this.isRefundable()) return true;
|
|
532
666
|
//Check if that maybe already concluded according to the LP
|
|
533
|
-
const resp = await IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this.
|
|
667
|
+
const resp = await IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this._data.getSequence());
|
|
534
668
|
switch(resp.code) {
|
|
535
669
|
case RefundAuthorizationResponseCodes.PAID:
|
|
536
670
|
const processed = await this._setPaymentResult(resp.data, true);
|
|
537
671
|
if(processed) {
|
|
538
|
-
this.
|
|
672
|
+
this._state = ToBTCSwapState.SOFT_CLAIMED;
|
|
539
673
|
if(save) await this._saveAndEmit();
|
|
540
674
|
}
|
|
541
675
|
return processed;
|
|
542
676
|
case RefundAuthorizationResponseCodes.REFUND_DATA:
|
|
543
|
-
await this.wrapper.
|
|
544
|
-
this.
|
|
677
|
+
await this.wrapper._contract.isValidRefundAuthorization(this._data, resp.data);
|
|
678
|
+
this._state = ToBTCSwapState.REFUNDABLE;
|
|
545
679
|
if(save) await this._saveAndEmit();
|
|
546
680
|
return true;
|
|
547
681
|
default:
|
|
@@ -550,22 +684,23 @@ export abstract class IToBTCSwap<
|
|
|
550
684
|
}
|
|
551
685
|
|
|
552
686
|
/**
|
|
553
|
-
* A blocking promise resolving when swap was concluded by the intermediary,
|
|
687
|
+
* A blocking promise resolving when swap was concluded by the intermediary (LP),
|
|
554
688
|
* rejecting in case of failure
|
|
555
689
|
*
|
|
556
690
|
* @param maxWaitTimeSeconds Maximum time in seconds to wait for the swap to be settled, an error is thrown if the
|
|
557
691
|
* swap is taking too long to claim
|
|
558
|
-
* @param checkIntervalSeconds
|
|
559
|
-
* @param abortSignal
|
|
560
|
-
* @returns
|
|
692
|
+
* @param checkIntervalSeconds How often to poll the intermediary for answer
|
|
693
|
+
* @param abortSignal Abort signal
|
|
694
|
+
* @returns `true` if swap was successful, `false` if swap failed and we can refund
|
|
695
|
+
*
|
|
561
696
|
* @throws {IntermediaryError} If a swap is determined expired by the intermediary, but it is actually still valid
|
|
562
697
|
* @throws {SignatureVerificationError} If the swap should be cooperatively refundable but the intermediary returned
|
|
563
698
|
* invalid refund signature
|
|
564
|
-
* @throws {Error} When swap expires or if the swap has invalid state (must be COMMITED)
|
|
699
|
+
* @throws {Error} When swap expires or if the swap has invalid state (must be {@link ToBTCSwapState.COMMITED})
|
|
565
700
|
*/
|
|
566
701
|
async waitForPayment(maxWaitTimeSeconds?: number, checkIntervalSeconds?: number, abortSignal?: AbortSignal): Promise<boolean> {
|
|
567
|
-
if(this.
|
|
568
|
-
if(this.
|
|
702
|
+
if(this._state===ToBTCSwapState.CLAIMED) return Promise.resolve(true);
|
|
703
|
+
if(this._state!==ToBTCSwapState.COMMITED && this._state!==ToBTCSwapState.SOFT_CLAIMED) throw new Error("Invalid state (not COMMITED)");
|
|
569
704
|
|
|
570
705
|
const abortController = extendAbortController(abortSignal);
|
|
571
706
|
|
|
@@ -589,13 +724,13 @@ export abstract class IToBTCSwap<
|
|
|
589
724
|
abortController.abort();
|
|
590
725
|
if(timedOut) {
|
|
591
726
|
throw new Error("Timed out while waiting for LP to process the swap, the LP might be unresponsive or offline!" +
|
|
592
|
-
` Please check later or wait till ${new Date(Number(this.
|
|
727
|
+
` Please check later or wait till ${new Date(Number(this._data.getExpiry())*1000).toLocaleString()} to refund unilaterally!`);
|
|
593
728
|
}
|
|
594
729
|
throw e;
|
|
595
730
|
}
|
|
596
731
|
|
|
597
732
|
if(typeof result !== "object") {
|
|
598
|
-
if((this.
|
|
733
|
+
if((this._state as ToBTCSwapState)===ToBTCSwapState.REFUNDABLE) throw new Error("Swap expired");
|
|
599
734
|
this.logger.debug("waitTillRefunded(): Resolved from state change");
|
|
600
735
|
return true;
|
|
601
736
|
}
|
|
@@ -606,17 +741,17 @@ export abstract class IToBTCSwap<
|
|
|
606
741
|
return true;
|
|
607
742
|
case RefundAuthorizationResponseCodes.REFUND_DATA:
|
|
608
743
|
const resultData = result.data;
|
|
609
|
-
await this.wrapper.
|
|
610
|
-
this.
|
|
744
|
+
await this.wrapper._contract.isValidRefundAuthorization(
|
|
745
|
+
this._data,
|
|
611
746
|
resultData
|
|
612
747
|
);
|
|
613
748
|
await this._saveAndEmit(ToBTCSwapState.REFUNDABLE);
|
|
614
749
|
return false;
|
|
615
750
|
case RefundAuthorizationResponseCodes.EXPIRED:
|
|
616
|
-
if(await this.wrapper.
|
|
751
|
+
if(await this.wrapper._contract.isExpired(this._getInitiator(), this._data)) throw new Error("Swap expired");
|
|
617
752
|
throw new IntermediaryError("Swap expired");
|
|
618
753
|
case RefundAuthorizationResponseCodes.NOT_FOUND:
|
|
619
|
-
if((this.
|
|
754
|
+
if((this._state as ToBTCSwapState)===ToBTCSwapState.CLAIMED) return true;
|
|
620
755
|
throw new Error("LP swap not found");
|
|
621
756
|
}
|
|
622
757
|
|
|
@@ -630,12 +765,17 @@ export abstract class IToBTCSwap<
|
|
|
630
765
|
/**
|
|
631
766
|
* Get the estimated smart chain transaction fee of the refund transaction
|
|
632
767
|
*/
|
|
633
|
-
|
|
634
|
-
|
|
768
|
+
async getRefundNetworkFee(): Promise<TokenAmount<T["ChainId"], SCToken<T["ChainId"]>, true>> {
|
|
769
|
+
const swapContract: T["Contract"] = this.wrapper._contract;
|
|
770
|
+
return toTokenAmount(
|
|
771
|
+
await swapContract.getRefundFee(this._getInitiator(), this._data),
|
|
772
|
+
this.wrapper._getNativeToken(),
|
|
773
|
+
this.wrapper._prices
|
|
774
|
+
);
|
|
635
775
|
}
|
|
636
776
|
|
|
637
777
|
/**
|
|
638
|
-
*
|
|
778
|
+
* @inheritDoc
|
|
639
779
|
*
|
|
640
780
|
* @throws {IntermediaryError} If intermediary returns invalid response in case cooperative refund should be used
|
|
641
781
|
* @throws {SignatureVerificationError} If intermediary returned invalid cooperative refund signature
|
|
@@ -646,15 +786,15 @@ export abstract class IToBTCSwap<
|
|
|
646
786
|
|
|
647
787
|
signer ??= this._getInitiator();
|
|
648
788
|
|
|
649
|
-
if(await this.wrapper.
|
|
650
|
-
return await this.wrapper.
|
|
789
|
+
if(await this.wrapper._contract.isExpired(this._getInitiator(), this._data)) {
|
|
790
|
+
return await this.wrapper._contract.txsRefund(signer, this._data, true, true);
|
|
651
791
|
} else {
|
|
652
792
|
if(this.url==null) throw new Error("LP URL not known, cannot get cooperative refund message, wait till expiry to refund!");
|
|
653
|
-
const res = await IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this.
|
|
793
|
+
const res = await IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this._data.getSequence());
|
|
654
794
|
if(res.code===RefundAuthorizationResponseCodes.REFUND_DATA) {
|
|
655
|
-
return await this.wrapper.
|
|
795
|
+
return await this.wrapper._contract.txsRefundWithAuthorization(
|
|
656
796
|
signer,
|
|
657
|
-
this.
|
|
797
|
+
this._data,
|
|
658
798
|
res.data,
|
|
659
799
|
true,
|
|
660
800
|
true
|
|
@@ -665,33 +805,35 @@ export abstract class IToBTCSwap<
|
|
|
665
805
|
}
|
|
666
806
|
|
|
667
807
|
/**
|
|
668
|
-
*
|
|
808
|
+
* @inheritDoc
|
|
669
809
|
*
|
|
670
|
-
* @param _signer Signer to sign the transactions with, must be the same as used in the initialization
|
|
671
|
-
* @param abortSignal Abort signal
|
|
672
810
|
* @throws {Error} If invalid signer is provided that doesn't match the swap data
|
|
673
811
|
*/
|
|
674
812
|
async refund(_signer: T["Signer"] | T["NativeSigner"], abortSignal?: AbortSignal): Promise<string> {
|
|
675
|
-
const signer = isAbstractSigner(_signer) ? _signer : await this.wrapper.
|
|
676
|
-
const result = await this.wrapper.
|
|
813
|
+
const signer = isAbstractSigner(_signer) ? _signer : await this.wrapper._chain.wrapSigner(_signer);
|
|
814
|
+
const result = await this.wrapper._chain.sendAndConfirm(signer, await this.txsRefund(signer.getAddress()), true, abortSignal)
|
|
677
815
|
|
|
678
|
-
this.
|
|
679
|
-
if(this.
|
|
816
|
+
this._refundTxId = result[0];
|
|
817
|
+
if(this._state===ToBTCSwapState.COMMITED || this._state===ToBTCSwapState.REFUNDABLE || this._state===ToBTCSwapState.SOFT_CLAIMED) {
|
|
680
818
|
await this._saveAndEmit(ToBTCSwapState.REFUNDED);
|
|
681
819
|
}
|
|
682
820
|
return result[0];
|
|
683
821
|
}
|
|
684
822
|
|
|
685
823
|
/**
|
|
686
|
-
*
|
|
824
|
+
* @inheritDoc
|
|
687
825
|
*
|
|
688
|
-
* @
|
|
689
|
-
*
|
|
826
|
+
* @throws {Error} When swap is not in a valid state (must be {@link ToBTCSwapState.COMMITED} or
|
|
827
|
+
* {@link ToBTCSwapState.REFUNDABLE})
|
|
690
828
|
* @throws {Error} If we tried to refund but claimer was able to claim first
|
|
691
829
|
*/
|
|
692
830
|
async waitTillRefunded(abortSignal?: AbortSignal): Promise<void> {
|
|
693
|
-
if(this.
|
|
694
|
-
if(
|
|
831
|
+
if(this._state===ToBTCSwapState.REFUNDED) return Promise.resolve();
|
|
832
|
+
if(
|
|
833
|
+
this._state!==ToBTCSwapState.COMMITED &&
|
|
834
|
+
this._state!==ToBTCSwapState.SOFT_CLAIMED &&
|
|
835
|
+
this._state!==ToBTCSwapState.REFUNDABLE
|
|
836
|
+
) throw new Error("Invalid state (not COMMITED)");
|
|
695
837
|
|
|
696
838
|
const abortController = new AbortController();
|
|
697
839
|
if(abortSignal!=null) abortSignal.addEventListener("abort", () => abortController.abort(abortSignal.reason));
|
|
@@ -713,12 +855,12 @@ export abstract class IToBTCSwap<
|
|
|
713
855
|
this.logger.debug("waitTillRefunded(): Resolved from watchdog");
|
|
714
856
|
|
|
715
857
|
if(res?.type===SwapCommitStateType.PAID) {
|
|
716
|
-
if(this.
|
|
858
|
+
if(this._claimTxId==null) this._claimTxId = await res.getClaimTxId();
|
|
717
859
|
await this._saveAndEmit(ToBTCSwapState.CLAIMED);
|
|
718
860
|
throw new Error("Tried to refund swap, but claimer claimed it in the meantime!");
|
|
719
861
|
}
|
|
720
862
|
if(res?.type===SwapCommitStateType.NOT_COMMITED) {
|
|
721
|
-
if(this.
|
|
863
|
+
if(this._refundTxId==null && res.getRefundTxId!=null) this._refundTxId = await res.getRefundTxId();
|
|
722
864
|
await this._saveAndEmit(ToBTCSwapState.REFUNDED);
|
|
723
865
|
}
|
|
724
866
|
}
|
|
@@ -727,6 +869,9 @@ export abstract class IToBTCSwap<
|
|
|
727
869
|
//////////////////////////////
|
|
728
870
|
//// Storage
|
|
729
871
|
|
|
872
|
+
/**
|
|
873
|
+
* @inheritDoc
|
|
874
|
+
*/
|
|
730
875
|
serialize(): any {
|
|
731
876
|
const obj = super.serialize();
|
|
732
877
|
return {
|
|
@@ -748,55 +893,24 @@ export abstract class IToBTCSwap<
|
|
|
748
893
|
*/
|
|
749
894
|
private async syncStateFromChain(quoteDefinitelyExpired?: boolean, commitStatus?: SwapCommitState): Promise<boolean> {
|
|
750
895
|
if(
|
|
751
|
-
this.
|
|
752
|
-
this.
|
|
753
|
-
this.
|
|
754
|
-
this.
|
|
755
|
-
this.
|
|
896
|
+
this._state===ToBTCSwapState.CREATED ||
|
|
897
|
+
this._state===ToBTCSwapState.QUOTE_SOFT_EXPIRED ||
|
|
898
|
+
this._state===ToBTCSwapState.COMMITED ||
|
|
899
|
+
this._state===ToBTCSwapState.SOFT_CLAIMED ||
|
|
900
|
+
this._state===ToBTCSwapState.REFUNDABLE
|
|
756
901
|
) {
|
|
757
902
|
let quoteExpired = false;
|
|
758
|
-
if(this.
|
|
903
|
+
if(this._state===ToBTCSwapState.CREATED || this._state===ToBTCSwapState.QUOTE_SOFT_EXPIRED) {
|
|
759
904
|
//Check if quote is still valid
|
|
760
905
|
quoteExpired = quoteDefinitelyExpired ?? await this._verifyQuoteDefinitelyExpired();
|
|
761
906
|
}
|
|
762
907
|
|
|
763
|
-
commitStatus ??= await this.wrapper.
|
|
764
|
-
|
|
765
|
-
case SwapCommitStateType.PAID:
|
|
766
|
-
if(this.claimTxId==null && commitStatus.getClaimTxId) this.claimTxId = await commitStatus.getClaimTxId();
|
|
767
|
-
const eventResult = await commitStatus.getClaimResult();
|
|
768
|
-
try {
|
|
769
|
-
await this._setPaymentResult({secret: eventResult, txId: Buffer.from(eventResult, "hex").reverse().toString("hex")});
|
|
770
|
-
} catch (e) {
|
|
771
|
-
this.logger.error(`Failed to set payment result ${eventResult} on the swap!`);
|
|
772
|
-
}
|
|
773
|
-
this.state = ToBTCSwapState.CLAIMED;
|
|
774
|
-
return true;
|
|
775
|
-
case SwapCommitStateType.REFUNDABLE:
|
|
776
|
-
this.state = ToBTCSwapState.REFUNDABLE;
|
|
777
|
-
return true;
|
|
778
|
-
case SwapCommitStateType.EXPIRED:
|
|
779
|
-
if(this.refundTxId==null && commitStatus.getRefundTxId) this.refundTxId = await commitStatus.getRefundTxId();
|
|
780
|
-
this.state = ToBTCSwapState.QUOTE_EXPIRED;
|
|
781
|
-
return true;
|
|
782
|
-
case SwapCommitStateType.NOT_COMMITED:
|
|
783
|
-
if(this.refundTxId==null && commitStatus.getRefundTxId) this.refundTxId = await commitStatus.getRefundTxId();
|
|
784
|
-
if(this.state===ToBTCSwapState.COMMITED || this.state===ToBTCSwapState.REFUNDABLE) {
|
|
785
|
-
this.state = ToBTCSwapState.REFUNDED;
|
|
786
|
-
return true;
|
|
787
|
-
}
|
|
788
|
-
break;
|
|
789
|
-
case SwapCommitStateType.COMMITED:
|
|
790
|
-
if(this.state!==ToBTCSwapState.COMMITED && this.state!==ToBTCSwapState.REFUNDABLE) {
|
|
791
|
-
this.state = ToBTCSwapState.COMMITED;
|
|
792
|
-
return true;
|
|
793
|
-
}
|
|
794
|
-
break;
|
|
795
|
-
}
|
|
908
|
+
commitStatus ??= await this.wrapper._contract.getCommitStatus(this._getInitiator(), this._data);
|
|
909
|
+
if(commitStatus!=null && await this._forciblySetOnchainState(commitStatus)) return true;
|
|
796
910
|
|
|
797
|
-
if((this.
|
|
911
|
+
if((this._state===ToBTCSwapState.CREATED || this._state===ToBTCSwapState.QUOTE_SOFT_EXPIRED)) {
|
|
798
912
|
if(quoteExpired) {
|
|
799
|
-
this.
|
|
913
|
+
this._state = ToBTCSwapState.QUOTE_EXPIRED;
|
|
800
914
|
return true;
|
|
801
915
|
}
|
|
802
916
|
}
|
|
@@ -804,22 +918,34 @@ export abstract class IToBTCSwap<
|
|
|
804
918
|
return false;
|
|
805
919
|
}
|
|
806
920
|
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
921
|
+
/**
|
|
922
|
+
* @inheritDoc
|
|
923
|
+
* @internal
|
|
924
|
+
*/
|
|
925
|
+
_shouldFetchOnchainState(): boolean {
|
|
926
|
+
return this._state===ToBTCSwapState.CREATED ||
|
|
927
|
+
this._state===ToBTCSwapState.QUOTE_SOFT_EXPIRED ||
|
|
928
|
+
this._state===ToBTCSwapState.COMMITED ||
|
|
929
|
+
this._state===ToBTCSwapState.SOFT_CLAIMED ||
|
|
930
|
+
this._state===ToBTCSwapState.REFUNDABLE;
|
|
813
931
|
}
|
|
814
932
|
|
|
933
|
+
/**
|
|
934
|
+
* @inheritDoc
|
|
935
|
+
* @internal
|
|
936
|
+
*/
|
|
815
937
|
_shouldFetchExpiryStatus(): boolean {
|
|
816
|
-
return this.
|
|
938
|
+
return this._state===ToBTCSwapState.CREATED || this._state===ToBTCSwapState.QUOTE_SOFT_EXPIRED;
|
|
817
939
|
}
|
|
818
940
|
|
|
941
|
+
/**
|
|
942
|
+
* @inheritDoc
|
|
943
|
+
* @internal
|
|
944
|
+
*/
|
|
819
945
|
async _sync(save?: boolean, quoteDefinitelyExpired?: boolean, commitStatus?: SwapCommitState): Promise<boolean> {
|
|
820
946
|
let changed = await this.syncStateFromChain(quoteDefinitelyExpired, commitStatus);
|
|
821
947
|
|
|
822
|
-
if(this.
|
|
948
|
+
if(this._state===ToBTCSwapState.COMMITED || this._state===ToBTCSwapState.SOFT_CLAIMED) {
|
|
823
949
|
//Check if that maybe already concluded
|
|
824
950
|
try {
|
|
825
951
|
if(await this.checkIntermediarySwapProcessed(false)) changed = true;
|
|
@@ -833,20 +959,64 @@ export abstract class IToBTCSwap<
|
|
|
833
959
|
return changed;
|
|
834
960
|
}
|
|
835
961
|
|
|
962
|
+
/**
|
|
963
|
+
* @inheritDoc
|
|
964
|
+
* @internal
|
|
965
|
+
*/
|
|
966
|
+
async _forciblySetOnchainState(commitStatus: SwapCommitState): Promise<boolean> {
|
|
967
|
+
switch(commitStatus.type) {
|
|
968
|
+
case SwapCommitStateType.PAID:
|
|
969
|
+
if(this._claimTxId==null && commitStatus.getClaimTxId) this._claimTxId = await commitStatus.getClaimTxId();
|
|
970
|
+
const eventResult = await commitStatus.getClaimResult();
|
|
971
|
+
try {
|
|
972
|
+
await this._setPaymentResult({secret: eventResult, txId: Buffer.from(eventResult, "hex").reverse().toString("hex")});
|
|
973
|
+
} catch (e) {
|
|
974
|
+
this.logger.error(`Failed to set payment result ${eventResult} on the swap!`);
|
|
975
|
+
}
|
|
976
|
+
this._state = ToBTCSwapState.CLAIMED;
|
|
977
|
+
return true;
|
|
978
|
+
case SwapCommitStateType.REFUNDABLE:
|
|
979
|
+
this._state = ToBTCSwapState.REFUNDABLE;
|
|
980
|
+
return true;
|
|
981
|
+
case SwapCommitStateType.EXPIRED:
|
|
982
|
+
if(this._refundTxId==null && commitStatus.getRefundTxId) this._refundTxId = await commitStatus.getRefundTxId();
|
|
983
|
+
this._state = this._refundTxId==null ? ToBTCSwapState.QUOTE_EXPIRED : ToBTCSwapState.REFUNDED;
|
|
984
|
+
return true;
|
|
985
|
+
case SwapCommitStateType.NOT_COMMITED:
|
|
986
|
+
if(this._refundTxId==null && commitStatus.getRefundTxId) this._refundTxId = await commitStatus.getRefundTxId();
|
|
987
|
+
if(this._refundTxId!=null) {
|
|
988
|
+
this._state = ToBTCSwapState.REFUNDED;
|
|
989
|
+
return true;
|
|
990
|
+
}
|
|
991
|
+
break;
|
|
992
|
+
case SwapCommitStateType.COMMITED:
|
|
993
|
+
if(this._state!==ToBTCSwapState.COMMITED && this._state!==ToBTCSwapState.REFUNDABLE && this._state!==ToBTCSwapState.SOFT_CLAIMED) {
|
|
994
|
+
this._state = ToBTCSwapState.COMMITED;
|
|
995
|
+
return true;
|
|
996
|
+
}
|
|
997
|
+
break;
|
|
998
|
+
}
|
|
999
|
+
return false;
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
/**
|
|
1003
|
+
* @inheritDoc
|
|
1004
|
+
* @internal
|
|
1005
|
+
*/
|
|
836
1006
|
async _tick(save?: boolean): Promise<boolean> {
|
|
837
|
-
switch(this.
|
|
1007
|
+
switch(this._state) {
|
|
838
1008
|
case ToBTCSwapState.CREATED:
|
|
839
1009
|
if(this.expiry<Date.now()) {
|
|
840
|
-
this.
|
|
1010
|
+
this._state = ToBTCSwapState.QUOTE_SOFT_EXPIRED;
|
|
841
1011
|
if(save) await this._saveAndEmit();
|
|
842
1012
|
return true;
|
|
843
1013
|
}
|
|
844
1014
|
break;
|
|
845
1015
|
case ToBTCSwapState.COMMITED:
|
|
846
1016
|
case ToBTCSwapState.SOFT_CLAIMED:
|
|
847
|
-
const expired = await this.wrapper.
|
|
1017
|
+
const expired = await this.wrapper._contract.isExpired(this._getInitiator(), this._data);
|
|
848
1018
|
if(expired) {
|
|
849
|
-
this.
|
|
1019
|
+
this._state = ToBTCSwapState.REFUNDABLE;
|
|
850
1020
|
if(save) await this._saveAndEmit();
|
|
851
1021
|
return true;
|
|
852
1022
|
}
|
|
@@ -854,15 +1024,4 @@ export abstract class IToBTCSwap<
|
|
|
854
1024
|
}
|
|
855
1025
|
return false;
|
|
856
1026
|
}
|
|
857
|
-
}
|
|
858
|
-
|
|
859
|
-
export enum ToBTCSwapState {
|
|
860
|
-
REFUNDED = -3,
|
|
861
|
-
QUOTE_EXPIRED = -2,
|
|
862
|
-
QUOTE_SOFT_EXPIRED = -1,
|
|
863
|
-
CREATED = 0,
|
|
864
|
-
COMMITED = 1,
|
|
865
|
-
SOFT_CLAIMED = 2,
|
|
866
|
-
CLAIMED = 3,
|
|
867
|
-
REFUNDABLE = 4
|
|
868
|
-
}
|
|
1027
|
+
}
|