@atomiqlabs/sdk 8.1.8 → 8.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bitcoin/coinselect2/utils.d.ts +6 -0
- package/dist/bitcoin/wallet/BitcoinWallet.d.ts +41 -5
- package/dist/bitcoin/wallet/BitcoinWallet.js +36 -1
- package/dist/bitcoin/wallet/IBitcoinWallet.d.ts +52 -2
- package/dist/bitcoin/wallet/IBitcoinWallet.js +2 -1
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.d.ts +42 -7
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.js +36 -1
- package/dist/enums/FeeType.d.ts +8 -1
- package/dist/enums/FeeType.js +8 -1
- package/dist/enums/SwapAmountType.d.ts +7 -0
- package/dist/enums/SwapAmountType.js +7 -0
- package/dist/enums/SwapDirection.d.ts +7 -0
- package/dist/enums/SwapDirection.js +7 -0
- package/dist/enums/SwapType.d.ts +62 -1
- package/dist/enums/SwapType.js +62 -1
- package/dist/errors/IntermediaryError.d.ts +4 -0
- package/dist/errors/IntermediaryError.js +1 -0
- package/dist/errors/RequestError.d.ts +15 -1
- package/dist/errors/RequestError.js +8 -0
- package/dist/errors/UserError.d.ts +1 -0
- package/dist/errors/UserError.js +1 -0
- package/dist/index.d.ts +5 -5
- package/dist/index.js +7 -6
- package/dist/intermediaries/Intermediary.d.ts +61 -14
- package/dist/intermediaries/Intermediary.js +38 -11
- package/dist/intermediaries/IntermediaryDiscovery.d.ts +62 -29
- package/dist/intermediaries/IntermediaryDiscovery.js +39 -24
- package/dist/prices/RedundantSwapPrice.d.ts +26 -5
- package/dist/prices/RedundantSwapPrice.js +22 -2
- package/dist/prices/SingleSwapPrice.d.ts +10 -7
- package/dist/prices/SingleSwapPrice.js +11 -8
- package/dist/prices/SwapPriceWithChain.d.ts +56 -19
- package/dist/prices/SwapPriceWithChain.js +62 -25
- package/dist/prices/abstract/IPriceProvider.d.ts +4 -4
- package/dist/prices/abstract/IPriceProvider.js +1 -1
- package/dist/prices/abstract/ISwapPrice.d.ts +95 -46
- package/dist/prices/abstract/ISwapPrice.js +104 -56
- package/dist/prices/providers/BinancePriceProvider.d.ts +8 -1
- package/dist/prices/providers/BinancePriceProvider.js +8 -1
- package/dist/prices/providers/CoinGeckoPriceProvider.d.ts +7 -1
- package/dist/prices/providers/CoinGeckoPriceProvider.js +7 -1
- package/dist/prices/providers/CoinPaprikaPriceProvider.d.ts +7 -1
- package/dist/prices/providers/CoinPaprikaPriceProvider.js +7 -1
- package/dist/prices/providers/CustomPriceProvider.d.ts +12 -1
- package/dist/prices/providers/CustomPriceProvider.js +12 -1
- package/dist/prices/providers/KrakenPriceProvider.d.ts +10 -1
- package/dist/prices/providers/KrakenPriceProvider.js +10 -1
- package/dist/prices/providers/OKXPriceProvider.d.ts +7 -1
- package/dist/prices/providers/OKXPriceProvider.js +7 -1
- package/dist/prices/providers/abstract/ExchangePriceProvider.d.ts +3 -0
- package/dist/prices/providers/abstract/ExchangePriceProvider.js +3 -0
- package/dist/storage/IUnifiedStorage.d.ts +19 -7
- package/dist/storage/UnifiedSwapStorage.d.ts +33 -3
- package/dist/storage/UnifiedSwapStorage.js +29 -1
- package/dist/storage-browser/IndexedDBUnifiedStorage.d.ts +31 -7
- package/dist/storage-browser/IndexedDBUnifiedStorage.js +29 -6
- package/dist/storage-browser/LocalStorageManager.d.ts +25 -1
- package/dist/storage-browser/LocalStorageManager.js +25 -1
- package/dist/swapper/Swapper.d.ts +380 -226
- package/dist/swapper/Swapper.js +383 -349
- package/dist/swapper/SwapperFactory.d.ts +66 -18
- package/dist/swapper/SwapperFactory.js +24 -3
- package/dist/swapper/SwapperUtils.d.ts +75 -28
- package/dist/swapper/SwapperUtils.js +107 -60
- package/dist/swapper/SwapperWithChain.d.ts +286 -91
- package/dist/swapper/SwapperWithChain.js +218 -64
- package/dist/swapper/SwapperWithSigner.d.ts +229 -80
- package/dist/swapper/SwapperWithSigner.js +190 -44
- package/dist/swaps/IAddressSwap.d.ts +12 -3
- package/dist/swaps/IAddressSwap.js +3 -2
- package/dist/swaps/IBTCWalletSwap.d.ts +26 -8
- package/dist/swaps/IBTCWalletSwap.js +3 -2
- package/dist/swaps/IClaimableSwap.d.ts +38 -6
- package/dist/swaps/IClaimableSwap.js +3 -2
- package/dist/swaps/IClaimableSwapWrapper.d.ts +11 -1
- package/dist/swaps/IRefundableSwap.d.ts +31 -5
- package/dist/swaps/IRefundableSwap.js +3 -2
- package/dist/swaps/ISwap.d.ts +162 -24
- package/dist/swaps/ISwap.js +92 -35
- package/dist/swaps/ISwapWithGasDrop.d.ts +8 -2
- package/dist/swaps/ISwapWithGasDrop.js +2 -1
- package/dist/swaps/ISwapWrapper.d.ts +161 -52
- package/dist/swaps/ISwapWrapper.js +131 -73
- package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.d.ts +51 -6
- package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.js +22 -12
- package/dist/swaps/escrow_swaps/IEscrowSwap.d.ts +65 -12
- package/dist/swaps/escrow_swaps/IEscrowSwap.js +38 -19
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.d.ts +39 -9
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.js +30 -21
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.d.ts +31 -15
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.js +33 -18
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.d.ts +97 -28
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.js +91 -27
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.d.ts +22 -9
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.js +24 -11
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.d.ts +278 -60
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +519 -241
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +77 -26
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +132 -50
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.d.ts +313 -52
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +544 -194
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +87 -26
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +147 -58
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.d.ts +222 -55
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +462 -244
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.d.ts +77 -23
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.js +116 -46
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.d.ts +195 -58
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +324 -191
- package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.d.ts +30 -5
- package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.js +44 -19
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.d.ts +61 -20
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.js +75 -32
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.d.ts +76 -50
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.js +106 -101
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.d.ts +37 -14
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.js +66 -20
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.d.ts +46 -17
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.js +82 -27
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +350 -88
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +482 -215
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.d.ts +76 -24
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.js +247 -124
- package/dist/swaps/trusted/ln/LnForGasSwap.d.ts +148 -20
- package/dist/swaps/trusted/ln/LnForGasSwap.js +175 -45
- package/dist/swaps/trusted/ln/LnForGasWrapper.d.ts +29 -10
- package/dist/swaps/trusted/ln/LnForGasWrapper.js +30 -11
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.d.ts +202 -49
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.js +232 -80
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.d.ts +34 -12
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.js +33 -14
- package/dist/types/AmountData.d.ts +2 -1
- package/dist/types/CustomPriceFunction.d.ts +8 -2
- package/dist/types/PriceInfoType.d.ts +4 -4
- package/dist/types/PriceInfoType.js +3 -3
- package/dist/types/SwapExecutionAction.d.ts +85 -4
- package/dist/types/SwapWithSigner.d.ts +5 -2
- package/dist/types/SwapWithSigner.js +5 -2
- package/dist/types/Token.d.ts +11 -5
- package/dist/types/Token.js +6 -3
- package/dist/types/TokenAmount.d.ts +3 -0
- package/dist/types/TokenAmount.js +2 -0
- package/dist/types/fees/Fee.d.ts +3 -2
- package/dist/types/fees/FeeBreakdown.d.ts +3 -2
- package/dist/types/fees/PercentagePPM.d.ts +4 -2
- package/dist/types/fees/PercentagePPM.js +2 -1
- package/dist/types/lnurl/LNURLPay.d.ts +20 -12
- package/dist/types/lnurl/LNURLPay.js +8 -4
- package/dist/types/lnurl/LNURLWithdraw.d.ts +17 -10
- package/dist/types/lnurl/LNURLWithdraw.js +8 -4
- package/dist/types/wallets/LightningInvoiceCreateService.d.ts +24 -0
- package/dist/types/wallets/LightningInvoiceCreateService.js +15 -0
- package/dist/types/wallets/MinimalBitcoinWalletInterface.d.ts +3 -1
- package/dist/types/wallets/MinimalLightningNetworkWalletInterface.d.ts +4 -2
- package/dist/utils/BitcoinUtils.d.ts +1 -0
- package/dist/utils/BitcoinUtils.js +5 -1
- package/dist/utils/SwapUtils.d.ts +58 -1
- package/dist/utils/SwapUtils.js +55 -1
- package/dist/utils/TokenUtils.d.ts +10 -2
- package/dist/utils/TokenUtils.js +12 -4
- package/package.json +3 -3
- package/src/bitcoin/coinselect2/utils.ts +6 -0
- package/src/bitcoin/wallet/BitcoinWallet.ts +41 -5
- package/src/bitcoin/wallet/IBitcoinWallet.ts +57 -2
- package/src/bitcoin/wallet/SingleAddressBitcoinWallet.ts +42 -6
- package/src/enums/FeeType.ts +8 -1
- package/src/enums/SwapAmountType.ts +7 -0
- package/src/enums/SwapDirection.ts +7 -0
- package/src/enums/SwapType.ts +62 -2
- package/src/errors/IntermediaryError.ts +4 -0
- package/src/errors/RequestError.ts +15 -1
- package/src/errors/UserError.ts +1 -0
- package/src/index.ts +12 -5
- package/src/intermediaries/Intermediary.ts +61 -14
- package/src/intermediaries/IntermediaryDiscovery.ts +69 -34
- package/src/prices/RedundantSwapPrice.ts +26 -6
- package/src/prices/SingleSwapPrice.ts +11 -8
- package/src/prices/SwapPriceWithChain.ts +63 -26
- package/src/prices/abstract/IPriceProvider.ts +4 -4
- package/src/prices/abstract/ISwapPrice.ts +115 -66
- package/src/prices/providers/BinancePriceProvider.ts +8 -1
- package/src/prices/providers/CoinGeckoPriceProvider.ts +7 -1
- package/src/prices/providers/CoinPaprikaPriceProvider.ts +7 -1
- package/src/prices/providers/CustomPriceProvider.ts +12 -1
- package/src/prices/providers/KrakenPriceProvider.ts +10 -1
- package/src/prices/providers/OKXPriceProvider.ts +7 -1
- package/src/prices/providers/abstract/ExchangePriceProvider.ts +3 -0
- package/src/storage/IUnifiedStorage.ts +19 -7
- package/src/storage/UnifiedSwapStorage.ts +33 -3
- package/src/storage-browser/IndexedDBUnifiedStorage.ts +31 -8
- package/src/storage-browser/LocalStorageManager.ts +25 -1
- package/src/swapper/Swapper.ts +599 -390
- package/src/swapper/SwapperFactory.ts +73 -24
- package/src/swapper/SwapperUtils.ts +107 -60
- package/src/swapper/SwapperWithChain.ts +320 -81
- package/src/swapper/SwapperWithSigner.ts +263 -56
- package/src/swaps/IAddressSwap.ts +13 -3
- package/src/swaps/IBTCWalletSwap.ts +26 -10
- package/src/swaps/IClaimableSwap.ts +41 -6
- package/src/swaps/IClaimableSwapWrapper.ts +11 -2
- package/src/swaps/IRefundableSwap.ts +34 -5
- package/src/swaps/ISwap.ts +224 -85
- package/src/swaps/ISwapWithGasDrop.ts +8 -2
- package/src/swaps/ISwapWrapper.ts +216 -98
- package/src/swaps/escrow_swaps/IEscrowSelfInitSwap.ts +64 -18
- package/src/swaps/escrow_swaps/IEscrowSwap.ts +83 -37
- package/src/swaps/escrow_swaps/IEscrowSwapWrapper.ts +61 -30
- package/src/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.ts +37 -19
- package/src/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.ts +123 -50
- package/src/swaps/escrow_swaps/frombtc/IFromBTCWrapper.ts +24 -11
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.ts +562 -258
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +156 -62
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +592 -227
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +177 -74
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.ts +483 -245
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.ts +141 -59
- package/src/swaps/escrow_swaps/tobtc/IToBTCSwap.ts +350 -195
- package/src/swaps/escrow_swaps/tobtc/IToBTCWrapper.ts +48 -23
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.ts +87 -40
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.ts +110 -110
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.ts +89 -34
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.ts +101 -31
- package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +556 -259
- package/src/swaps/spv_swaps/SpvFromBTCWrapper.ts +292 -148
- package/src/swaps/trusted/ln/LnForGasSwap.ts +186 -47
- package/src/swaps/trusted/ln/LnForGasWrapper.ts +34 -15
- package/src/swaps/trusted/onchain/OnchainForGasSwap.ts +262 -88
- package/src/swaps/trusted/onchain/OnchainForGasWrapper.ts +41 -19
- package/src/types/AmountData.ts +2 -1
- package/src/types/CustomPriceFunction.ts +8 -2
- package/src/types/PriceInfoType.ts +4 -4
- package/src/types/SwapExecutionAction.ts +97 -5
- package/src/types/SwapWithSigner.ts +8 -4
- package/src/types/Token.ts +12 -5
- package/src/types/TokenAmount.ts +3 -0
- package/src/types/fees/Fee.ts +3 -2
- package/src/types/fees/FeeBreakdown.ts +3 -2
- package/src/types/fees/PercentagePPM.ts +4 -2
- package/src/types/lnurl/LNURLPay.ts +20 -12
- package/src/types/lnurl/LNURLWithdraw.ts +17 -10
- package/src/types/wallets/LightningInvoiceCreateService.ts +30 -0
- package/src/types/wallets/MinimalBitcoinWalletInterface.ts +3 -1
- package/src/types/wallets/MinimalLightningNetworkWalletInterface.ts +4 -2
- package/src/utils/BitcoinUtils.ts +5 -0
- package/src/utils/SwapUtils.ts +63 -1
- package/src/utils/TokenUtils.ts +12 -4
- package/dist/bitcoin/BitcoinRpcWithAddressIndex.d.ts +0 -68
- package/dist/bitcoin/BitcoinRpcWithAddressIndex.js +0 -2
- package/dist/bitcoin/LightningNetworkApi.d.ts +0 -12
- package/dist/bitcoin/LightningNetworkApi.js +0 -2
- package/dist/bitcoin/mempool/MempoolApi.d.ts +0 -350
- package/dist/bitcoin/mempool/MempoolApi.js +0 -311
- package/dist/bitcoin/mempool/MempoolBitcoinBlock.d.ts +0 -44
- package/dist/bitcoin/mempool/MempoolBitcoinBlock.js +0 -48
- package/dist/bitcoin/mempool/MempoolBitcoinRpc.d.ts +0 -119
- package/dist/bitcoin/mempool/MempoolBitcoinRpc.js +0 -361
- package/dist/bitcoin/mempool/synchronizer/MempoolBtcRelaySynchronizer.d.ts +0 -22
- package/dist/bitcoin/mempool/synchronizer/MempoolBtcRelaySynchronizer.js +0 -105
- package/dist/errors/PaymentAuthError.d.ts +0 -11
- package/dist/errors/PaymentAuthError.js +0 -23
- package/src/errors/PaymentAuthError.ts +0 -26
|
@@ -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/Smart chain → Bitcoin
|
|
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/Smart chain → Bitcoin
|
|
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,12 @@ export abstract class IToBTCSwap<
|
|
|
395
526
|
//// Commit
|
|
396
527
|
|
|
397
528
|
/**
|
|
398
|
-
*
|
|
399
|
-
*
|
|
400
|
-
* @param skipChecks Skip checks like making sure init signature is still valid and swap wasn't commited yet
|
|
401
|
-
* (this is handled on swap creation, if you commit right after quoting, you can use skipChecks=true)
|
|
529
|
+
* @inheritDoc
|
|
402
530
|
*
|
|
403
|
-
* @throws {Error} When in invalid state (not
|
|
531
|
+
* @throws {Error} When in invalid state (not {@link ToBTCSwapState.CREATED})
|
|
404
532
|
*/
|
|
405
533
|
async txsCommit(skipChecks?: boolean): Promise<T["TX"][]> {
|
|
406
|
-
if(this.
|
|
534
|
+
if(this._state!==ToBTCSwapState.CREATED) throw new Error("Must be in CREATED state!");
|
|
407
535
|
if(this.signatureData==null) throw new Error("Init signature data not known, cannot commit!");
|
|
408
536
|
|
|
409
537
|
if(!this.initiated) {
|
|
@@ -411,27 +539,22 @@ export abstract class IToBTCSwap<
|
|
|
411
539
|
await this._saveAndEmit();
|
|
412
540
|
}
|
|
413
541
|
|
|
414
|
-
return await this.wrapper.
|
|
415
|
-
this._getInitiator(), this.
|
|
542
|
+
return await this.wrapper._contract.txsInit(
|
|
543
|
+
this._getInitiator(), this._data, this.signatureData, skipChecks, this.feeRate
|
|
416
544
|
).catch(e => Promise.reject(e instanceof SignatureVerificationError ? new Error("Request timed out") : e));
|
|
417
545
|
}
|
|
418
546
|
|
|
419
547
|
/**
|
|
420
|
-
*
|
|
548
|
+
* @inheritDoc
|
|
421
549
|
*
|
|
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
550
|
* @throws {Error} If invalid signer is provided that doesn't match the swap data
|
|
428
551
|
*/
|
|
429
552
|
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.
|
|
553
|
+
const signer = isAbstractSigner(_signer) ? _signer : await this.wrapper._chain.wrapSigner(_signer);
|
|
431
554
|
this.checkSigner(signer);
|
|
432
555
|
const txs = await this.txsCommit(skipChecks);
|
|
433
556
|
let txCount = 0;
|
|
434
|
-
const result = await this.wrapper.
|
|
557
|
+
const result = await this.wrapper._chain.sendAndConfirm(
|
|
435
558
|
signer, txs, true, abortSignal, false, (txId, rawTx) => {
|
|
436
559
|
txCount++;
|
|
437
560
|
if(onBeforeTxSent!=null && txCount===txs.length) onBeforeTxSent(txId);
|
|
@@ -439,22 +562,21 @@ export abstract class IToBTCSwap<
|
|
|
439
562
|
}
|
|
440
563
|
);
|
|
441
564
|
|
|
442
|
-
this.
|
|
443
|
-
if(this.
|
|
565
|
+
this._commitTxId = result[result.length-1];
|
|
566
|
+
if(this._state===ToBTCSwapState.CREATED || this._state===ToBTCSwapState.QUOTE_SOFT_EXPIRED || this._state===ToBTCSwapState.QUOTE_EXPIRED) {
|
|
444
567
|
await this._saveAndEmit(ToBTCSwapState.COMMITED);
|
|
445
568
|
}
|
|
446
|
-
return this.
|
|
569
|
+
return this._commitTxId;
|
|
447
570
|
}
|
|
448
571
|
|
|
449
572
|
/**
|
|
450
|
-
*
|
|
573
|
+
* @inheritDoc
|
|
451
574
|
*
|
|
452
|
-
* @
|
|
453
|
-
* @throws {Error} If swap is not in the correct state (must be CREATED)
|
|
575
|
+
* @throws {Error} If swap is not in the correct state (must be {@link ToBTCSwapState.CREATED})
|
|
454
576
|
*/
|
|
455
577
|
async waitTillCommited(abortSignal?: AbortSignal): Promise<void> {
|
|
456
|
-
if(this.
|
|
457
|
-
if(this.
|
|
578
|
+
if(this._state===ToBTCSwapState.COMMITED || this._state===ToBTCSwapState.CLAIMED) return Promise.resolve();
|
|
579
|
+
if(this._state!==ToBTCSwapState.CREATED && this._state!==ToBTCSwapState.QUOTE_SOFT_EXPIRED) throw new Error("Invalid state (not CREATED)");
|
|
458
580
|
|
|
459
581
|
const abortController = extendAbortController(abortSignal);
|
|
460
582
|
let result: number | boolean;
|
|
@@ -473,13 +595,13 @@ export abstract class IToBTCSwap<
|
|
|
473
595
|
if(result===true) this.logger.debug("waitTillCommited(): Resolved from watchdog - commited");
|
|
474
596
|
if(result===false) {
|
|
475
597
|
this.logger.debug("waitTillCommited(): Resolved from watchdog - signature expiry");
|
|
476
|
-
if(this.
|
|
598
|
+
if(this._state===ToBTCSwapState.QUOTE_SOFT_EXPIRED || this._state===ToBTCSwapState.CREATED) {
|
|
477
599
|
await this._saveAndEmit(ToBTCSwapState.QUOTE_EXPIRED);
|
|
478
600
|
}
|
|
479
601
|
throw new Error("Quote expired while waiting for transaction confirmation!");
|
|
480
602
|
}
|
|
481
603
|
|
|
482
|
-
if(this.
|
|
604
|
+
if(this._state===ToBTCSwapState.QUOTE_SOFT_EXPIRED || this._state===ToBTCSwapState.CREATED || this._state===ToBTCSwapState.QUOTE_EXPIRED) {
|
|
483
605
|
await this._saveAndEmit(ToBTCSwapState.COMMITED);
|
|
484
606
|
}
|
|
485
607
|
}
|
|
@@ -488,6 +610,13 @@ export abstract class IToBTCSwap<
|
|
|
488
610
|
//////////////////////////////
|
|
489
611
|
//// Payment
|
|
490
612
|
|
|
613
|
+
/**
|
|
614
|
+
* Waits till the swap is processed by the intermediary (LP)
|
|
615
|
+
*
|
|
616
|
+
* @param checkIntervalSeconds How often to poll the intermediary for status (5 seconds default)
|
|
617
|
+
* @param abortSignal Abort signal
|
|
618
|
+
* @internal
|
|
619
|
+
*/
|
|
491
620
|
protected async waitTillIntermediarySwapProcessed(
|
|
492
621
|
checkIntervalSeconds?: number,
|
|
493
622
|
abortSignal?: AbortSignal
|
|
@@ -498,11 +627,11 @@ export abstract class IToBTCSwap<
|
|
|
498
627
|
while(!abortSignal?.aborted && (
|
|
499
628
|
resp.code===RefundAuthorizationResponseCodes.PENDING || resp.code===RefundAuthorizationResponseCodes.NOT_FOUND
|
|
500
629
|
)) {
|
|
501
|
-
resp = await IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this.
|
|
630
|
+
resp = await IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this._data.getSequence());
|
|
502
631
|
if(resp.code===RefundAuthorizationResponseCodes.PAID) {
|
|
503
632
|
const validResponse = await this._setPaymentResult(resp.data, true);
|
|
504
633
|
if(validResponse) {
|
|
505
|
-
if(this.
|
|
634
|
+
if(this._state===ToBTCSwapState.COMMITED || this._state===ToBTCSwapState.REFUNDABLE) {
|
|
506
635
|
await this._saveAndEmit(ToBTCSwapState.SOFT_CLAIMED);
|
|
507
636
|
}
|
|
508
637
|
} else {
|
|
@@ -523,25 +652,26 @@ export abstract class IToBTCSwap<
|
|
|
523
652
|
* refund.
|
|
524
653
|
*
|
|
525
654
|
* @param save whether to save the data
|
|
526
|
-
* @returns true if swap is processed, false if the swap is still ongoing
|
|
527
|
-
*
|
|
655
|
+
* @returns `true` if swap is processed, `false` if the swap is still ongoing
|
|
656
|
+
*
|
|
657
|
+
* @internal
|
|
528
658
|
*/
|
|
529
659
|
protected async checkIntermediarySwapProcessed(save: boolean = true): Promise<boolean> {
|
|
530
|
-
if(this.
|
|
660
|
+
if(this._state===ToBTCSwapState.CREATED || this._state==ToBTCSwapState.QUOTE_EXPIRED || this.url==null) return false;
|
|
531
661
|
if(this.isFinished() || this.isRefundable()) return true;
|
|
532
662
|
//Check if that maybe already concluded according to the LP
|
|
533
|
-
const resp = await IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this.
|
|
663
|
+
const resp = await IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this._data.getSequence());
|
|
534
664
|
switch(resp.code) {
|
|
535
665
|
case RefundAuthorizationResponseCodes.PAID:
|
|
536
666
|
const processed = await this._setPaymentResult(resp.data, true);
|
|
537
667
|
if(processed) {
|
|
538
|
-
this.
|
|
668
|
+
this._state = ToBTCSwapState.SOFT_CLAIMED;
|
|
539
669
|
if(save) await this._saveAndEmit();
|
|
540
670
|
}
|
|
541
671
|
return processed;
|
|
542
672
|
case RefundAuthorizationResponseCodes.REFUND_DATA:
|
|
543
|
-
await this.wrapper.
|
|
544
|
-
this.
|
|
673
|
+
await this.wrapper._contract.isValidRefundAuthorization(this._data, resp.data);
|
|
674
|
+
this._state = ToBTCSwapState.REFUNDABLE;
|
|
545
675
|
if(save) await this._saveAndEmit();
|
|
546
676
|
return true;
|
|
547
677
|
default:
|
|
@@ -550,22 +680,23 @@ export abstract class IToBTCSwap<
|
|
|
550
680
|
}
|
|
551
681
|
|
|
552
682
|
/**
|
|
553
|
-
* A blocking promise resolving when swap was concluded by the intermediary,
|
|
683
|
+
* A blocking promise resolving when swap was concluded by the intermediary (LP),
|
|
554
684
|
* rejecting in case of failure
|
|
555
685
|
*
|
|
556
686
|
* @param maxWaitTimeSeconds Maximum time in seconds to wait for the swap to be settled, an error is thrown if the
|
|
557
687
|
* swap is taking too long to claim
|
|
558
|
-
* @param checkIntervalSeconds
|
|
559
|
-
* @param abortSignal
|
|
560
|
-
* @returns
|
|
688
|
+
* @param checkIntervalSeconds How often to poll the intermediary for answer
|
|
689
|
+
* @param abortSignal Abort signal
|
|
690
|
+
* @returns `true` if swap was successful, `false` if swap failed and we can refund
|
|
691
|
+
*
|
|
561
692
|
* @throws {IntermediaryError} If a swap is determined expired by the intermediary, but it is actually still valid
|
|
562
693
|
* @throws {SignatureVerificationError} If the swap should be cooperatively refundable but the intermediary returned
|
|
563
694
|
* invalid refund signature
|
|
564
|
-
* @throws {Error} When swap expires or if the swap has invalid state (must be COMMITED)
|
|
695
|
+
* @throws {Error} When swap expires or if the swap has invalid state (must be {@link ToBTCSwapState.COMMITED})
|
|
565
696
|
*/
|
|
566
697
|
async waitForPayment(maxWaitTimeSeconds?: number, checkIntervalSeconds?: number, abortSignal?: AbortSignal): Promise<boolean> {
|
|
567
|
-
if(this.
|
|
568
|
-
if(this.
|
|
698
|
+
if(this._state===ToBTCSwapState.CLAIMED) return Promise.resolve(true);
|
|
699
|
+
if(this._state!==ToBTCSwapState.COMMITED && this._state!==ToBTCSwapState.SOFT_CLAIMED) throw new Error("Invalid state (not COMMITED)");
|
|
569
700
|
|
|
570
701
|
const abortController = extendAbortController(abortSignal);
|
|
571
702
|
|
|
@@ -589,13 +720,13 @@ export abstract class IToBTCSwap<
|
|
|
589
720
|
abortController.abort();
|
|
590
721
|
if(timedOut) {
|
|
591
722
|
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.
|
|
723
|
+
` Please check later or wait till ${new Date(Number(this._data.getExpiry())*1000).toLocaleString()} to refund unilaterally!`);
|
|
593
724
|
}
|
|
594
725
|
throw e;
|
|
595
726
|
}
|
|
596
727
|
|
|
597
728
|
if(typeof result !== "object") {
|
|
598
|
-
if((this.
|
|
729
|
+
if((this._state as ToBTCSwapState)===ToBTCSwapState.REFUNDABLE) throw new Error("Swap expired");
|
|
599
730
|
this.logger.debug("waitTillRefunded(): Resolved from state change");
|
|
600
731
|
return true;
|
|
601
732
|
}
|
|
@@ -606,17 +737,17 @@ export abstract class IToBTCSwap<
|
|
|
606
737
|
return true;
|
|
607
738
|
case RefundAuthorizationResponseCodes.REFUND_DATA:
|
|
608
739
|
const resultData = result.data;
|
|
609
|
-
await this.wrapper.
|
|
610
|
-
this.
|
|
740
|
+
await this.wrapper._contract.isValidRefundAuthorization(
|
|
741
|
+
this._data,
|
|
611
742
|
resultData
|
|
612
743
|
);
|
|
613
744
|
await this._saveAndEmit(ToBTCSwapState.REFUNDABLE);
|
|
614
745
|
return false;
|
|
615
746
|
case RefundAuthorizationResponseCodes.EXPIRED:
|
|
616
|
-
if(await this.wrapper.
|
|
747
|
+
if(await this.wrapper._contract.isExpired(this._getInitiator(), this._data)) throw new Error("Swap expired");
|
|
617
748
|
throw new IntermediaryError("Swap expired");
|
|
618
749
|
case RefundAuthorizationResponseCodes.NOT_FOUND:
|
|
619
|
-
if((this.
|
|
750
|
+
if((this._state as ToBTCSwapState)===ToBTCSwapState.CLAIMED) return true;
|
|
620
751
|
throw new Error("LP swap not found");
|
|
621
752
|
}
|
|
622
753
|
|
|
@@ -630,12 +761,17 @@ export abstract class IToBTCSwap<
|
|
|
630
761
|
/**
|
|
631
762
|
* Get the estimated smart chain transaction fee of the refund transaction
|
|
632
763
|
*/
|
|
633
|
-
|
|
634
|
-
|
|
764
|
+
async getRefundNetworkFee(): Promise<TokenAmount<T["ChainId"], SCToken<T["ChainId"]>, true>> {
|
|
765
|
+
const swapContract: T["Contract"] = this.wrapper._contract;
|
|
766
|
+
return toTokenAmount(
|
|
767
|
+
await swapContract.getRefundFee(this._getInitiator(), this._data),
|
|
768
|
+
this.wrapper._getNativeToken(),
|
|
769
|
+
this.wrapper._prices
|
|
770
|
+
);
|
|
635
771
|
}
|
|
636
772
|
|
|
637
773
|
/**
|
|
638
|
-
*
|
|
774
|
+
* @inheritDoc
|
|
639
775
|
*
|
|
640
776
|
* @throws {IntermediaryError} If intermediary returns invalid response in case cooperative refund should be used
|
|
641
777
|
* @throws {SignatureVerificationError} If intermediary returned invalid cooperative refund signature
|
|
@@ -646,15 +782,15 @@ export abstract class IToBTCSwap<
|
|
|
646
782
|
|
|
647
783
|
signer ??= this._getInitiator();
|
|
648
784
|
|
|
649
|
-
if(await this.wrapper.
|
|
650
|
-
return await this.wrapper.
|
|
785
|
+
if(await this.wrapper._contract.isExpired(this._getInitiator(), this._data)) {
|
|
786
|
+
return await this.wrapper._contract.txsRefund(signer, this._data, true, true);
|
|
651
787
|
} else {
|
|
652
788
|
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.
|
|
789
|
+
const res = await IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this._data.getSequence());
|
|
654
790
|
if(res.code===RefundAuthorizationResponseCodes.REFUND_DATA) {
|
|
655
|
-
return await this.wrapper.
|
|
791
|
+
return await this.wrapper._contract.txsRefundWithAuthorization(
|
|
656
792
|
signer,
|
|
657
|
-
this.
|
|
793
|
+
this._data,
|
|
658
794
|
res.data,
|
|
659
795
|
true,
|
|
660
796
|
true
|
|
@@ -665,33 +801,35 @@ export abstract class IToBTCSwap<
|
|
|
665
801
|
}
|
|
666
802
|
|
|
667
803
|
/**
|
|
668
|
-
*
|
|
804
|
+
* @inheritDoc
|
|
669
805
|
*
|
|
670
|
-
* @param _signer Signer to sign the transactions with, must be the same as used in the initialization
|
|
671
|
-
* @param abortSignal Abort signal
|
|
672
806
|
* @throws {Error} If invalid signer is provided that doesn't match the swap data
|
|
673
807
|
*/
|
|
674
808
|
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.
|
|
809
|
+
const signer = isAbstractSigner(_signer) ? _signer : await this.wrapper._chain.wrapSigner(_signer);
|
|
810
|
+
const result = await this.wrapper._chain.sendAndConfirm(signer, await this.txsRefund(signer.getAddress()), true, abortSignal)
|
|
677
811
|
|
|
678
|
-
this.
|
|
679
|
-
if(this.
|
|
812
|
+
this._refundTxId = result[0];
|
|
813
|
+
if(this._state===ToBTCSwapState.COMMITED || this._state===ToBTCSwapState.REFUNDABLE || this._state===ToBTCSwapState.SOFT_CLAIMED) {
|
|
680
814
|
await this._saveAndEmit(ToBTCSwapState.REFUNDED);
|
|
681
815
|
}
|
|
682
816
|
return result[0];
|
|
683
817
|
}
|
|
684
818
|
|
|
685
819
|
/**
|
|
686
|
-
*
|
|
820
|
+
* @inheritDoc
|
|
687
821
|
*
|
|
688
|
-
* @
|
|
689
|
-
*
|
|
822
|
+
* @throws {Error} When swap is not in a valid state (must be {@link ToBTCSwapState.COMMITED} or
|
|
823
|
+
* {@link ToBTCSwapState.REFUNDABLE})
|
|
690
824
|
* @throws {Error} If we tried to refund but claimer was able to claim first
|
|
691
825
|
*/
|
|
692
826
|
async waitTillRefunded(abortSignal?: AbortSignal): Promise<void> {
|
|
693
|
-
if(this.
|
|
694
|
-
if(
|
|
827
|
+
if(this._state===ToBTCSwapState.REFUNDED) return Promise.resolve();
|
|
828
|
+
if(
|
|
829
|
+
this._state!==ToBTCSwapState.COMMITED &&
|
|
830
|
+
this._state!==ToBTCSwapState.SOFT_CLAIMED &&
|
|
831
|
+
this._state!==ToBTCSwapState.REFUNDABLE
|
|
832
|
+
) throw new Error("Invalid state (not COMMITED)");
|
|
695
833
|
|
|
696
834
|
const abortController = new AbortController();
|
|
697
835
|
if(abortSignal!=null) abortSignal.addEventListener("abort", () => abortController.abort(abortSignal.reason));
|
|
@@ -713,12 +851,12 @@ export abstract class IToBTCSwap<
|
|
|
713
851
|
this.logger.debug("waitTillRefunded(): Resolved from watchdog");
|
|
714
852
|
|
|
715
853
|
if(res?.type===SwapCommitStateType.PAID) {
|
|
716
|
-
if(this.
|
|
854
|
+
if(this._claimTxId==null) this._claimTxId = await res.getClaimTxId();
|
|
717
855
|
await this._saveAndEmit(ToBTCSwapState.CLAIMED);
|
|
718
856
|
throw new Error("Tried to refund swap, but claimer claimed it in the meantime!");
|
|
719
857
|
}
|
|
720
858
|
if(res?.type===SwapCommitStateType.NOT_COMMITED) {
|
|
721
|
-
if(this.
|
|
859
|
+
if(this._refundTxId==null && res.getRefundTxId!=null) this._refundTxId = await res.getRefundTxId();
|
|
722
860
|
await this._saveAndEmit(ToBTCSwapState.REFUNDED);
|
|
723
861
|
}
|
|
724
862
|
}
|
|
@@ -727,6 +865,9 @@ export abstract class IToBTCSwap<
|
|
|
727
865
|
//////////////////////////////
|
|
728
866
|
//// Storage
|
|
729
867
|
|
|
868
|
+
/**
|
|
869
|
+
* @inheritDoc
|
|
870
|
+
*/
|
|
730
871
|
serialize(): any {
|
|
731
872
|
const obj = super.serialize();
|
|
732
873
|
return {
|
|
@@ -748,55 +889,24 @@ export abstract class IToBTCSwap<
|
|
|
748
889
|
*/
|
|
749
890
|
private async syncStateFromChain(quoteDefinitelyExpired?: boolean, commitStatus?: SwapCommitState): Promise<boolean> {
|
|
750
891
|
if(
|
|
751
|
-
this.
|
|
752
|
-
this.
|
|
753
|
-
this.
|
|
754
|
-
this.
|
|
755
|
-
this.
|
|
892
|
+
this._state===ToBTCSwapState.CREATED ||
|
|
893
|
+
this._state===ToBTCSwapState.QUOTE_SOFT_EXPIRED ||
|
|
894
|
+
this._state===ToBTCSwapState.COMMITED ||
|
|
895
|
+
this._state===ToBTCSwapState.SOFT_CLAIMED ||
|
|
896
|
+
this._state===ToBTCSwapState.REFUNDABLE
|
|
756
897
|
) {
|
|
757
898
|
let quoteExpired = false;
|
|
758
|
-
if(this.
|
|
899
|
+
if(this._state===ToBTCSwapState.CREATED || this._state===ToBTCSwapState.QUOTE_SOFT_EXPIRED) {
|
|
759
900
|
//Check if quote is still valid
|
|
760
901
|
quoteExpired = quoteDefinitelyExpired ?? await this._verifyQuoteDefinitelyExpired();
|
|
761
902
|
}
|
|
762
903
|
|
|
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
|
-
}
|
|
904
|
+
commitStatus ??= await this.wrapper._contract.getCommitStatus(this._getInitiator(), this._data);
|
|
905
|
+
if(commitStatus!=null && await this._forciblySetOnchainState(commitStatus)) return true;
|
|
796
906
|
|
|
797
|
-
if((this.
|
|
907
|
+
if((this._state===ToBTCSwapState.CREATED || this._state===ToBTCSwapState.QUOTE_SOFT_EXPIRED)) {
|
|
798
908
|
if(quoteExpired) {
|
|
799
|
-
this.
|
|
909
|
+
this._state = ToBTCSwapState.QUOTE_EXPIRED;
|
|
800
910
|
return true;
|
|
801
911
|
}
|
|
802
912
|
}
|
|
@@ -804,22 +914,34 @@ export abstract class IToBTCSwap<
|
|
|
804
914
|
return false;
|
|
805
915
|
}
|
|
806
916
|
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
917
|
+
/**
|
|
918
|
+
* @inheritDoc
|
|
919
|
+
* @internal
|
|
920
|
+
*/
|
|
921
|
+
_shouldFetchOnchainState(): boolean {
|
|
922
|
+
return this._state===ToBTCSwapState.CREATED ||
|
|
923
|
+
this._state===ToBTCSwapState.QUOTE_SOFT_EXPIRED ||
|
|
924
|
+
this._state===ToBTCSwapState.COMMITED ||
|
|
925
|
+
this._state===ToBTCSwapState.SOFT_CLAIMED ||
|
|
926
|
+
this._state===ToBTCSwapState.REFUNDABLE;
|
|
813
927
|
}
|
|
814
928
|
|
|
929
|
+
/**
|
|
930
|
+
* @inheritDoc
|
|
931
|
+
* @internal
|
|
932
|
+
*/
|
|
815
933
|
_shouldFetchExpiryStatus(): boolean {
|
|
816
|
-
return this.
|
|
934
|
+
return this._state===ToBTCSwapState.CREATED || this._state===ToBTCSwapState.QUOTE_SOFT_EXPIRED;
|
|
817
935
|
}
|
|
818
936
|
|
|
937
|
+
/**
|
|
938
|
+
* @inheritDoc
|
|
939
|
+
* @internal
|
|
940
|
+
*/
|
|
819
941
|
async _sync(save?: boolean, quoteDefinitelyExpired?: boolean, commitStatus?: SwapCommitState): Promise<boolean> {
|
|
820
942
|
let changed = await this.syncStateFromChain(quoteDefinitelyExpired, commitStatus);
|
|
821
943
|
|
|
822
|
-
if(this.
|
|
944
|
+
if(this._state===ToBTCSwapState.COMMITED || this._state===ToBTCSwapState.SOFT_CLAIMED) {
|
|
823
945
|
//Check if that maybe already concluded
|
|
824
946
|
try {
|
|
825
947
|
if(await this.checkIntermediarySwapProcessed(false)) changed = true;
|
|
@@ -833,20 +955,64 @@ export abstract class IToBTCSwap<
|
|
|
833
955
|
return changed;
|
|
834
956
|
}
|
|
835
957
|
|
|
958
|
+
/**
|
|
959
|
+
* @inheritDoc
|
|
960
|
+
* @internal
|
|
961
|
+
*/
|
|
962
|
+
async _forciblySetOnchainState(commitStatus: SwapCommitState): Promise<boolean> {
|
|
963
|
+
switch(commitStatus.type) {
|
|
964
|
+
case SwapCommitStateType.PAID:
|
|
965
|
+
if(this._claimTxId==null && commitStatus.getClaimTxId) this._claimTxId = await commitStatus.getClaimTxId();
|
|
966
|
+
const eventResult = await commitStatus.getClaimResult();
|
|
967
|
+
try {
|
|
968
|
+
await this._setPaymentResult({secret: eventResult, txId: Buffer.from(eventResult, "hex").reverse().toString("hex")});
|
|
969
|
+
} catch (e) {
|
|
970
|
+
this.logger.error(`Failed to set payment result ${eventResult} on the swap!`);
|
|
971
|
+
}
|
|
972
|
+
this._state = ToBTCSwapState.CLAIMED;
|
|
973
|
+
return true;
|
|
974
|
+
case SwapCommitStateType.REFUNDABLE:
|
|
975
|
+
this._state = ToBTCSwapState.REFUNDABLE;
|
|
976
|
+
return true;
|
|
977
|
+
case SwapCommitStateType.EXPIRED:
|
|
978
|
+
if(this._refundTxId==null && commitStatus.getRefundTxId) this._refundTxId = await commitStatus.getRefundTxId();
|
|
979
|
+
this._state = this._refundTxId==null ? ToBTCSwapState.QUOTE_EXPIRED : ToBTCSwapState.REFUNDED;
|
|
980
|
+
return true;
|
|
981
|
+
case SwapCommitStateType.NOT_COMMITED:
|
|
982
|
+
if(this._refundTxId==null && commitStatus.getRefundTxId) this._refundTxId = await commitStatus.getRefundTxId();
|
|
983
|
+
if(this._refundTxId!=null) {
|
|
984
|
+
this._state = ToBTCSwapState.REFUNDED;
|
|
985
|
+
return true;
|
|
986
|
+
}
|
|
987
|
+
break;
|
|
988
|
+
case SwapCommitStateType.COMMITED:
|
|
989
|
+
if(this._state!==ToBTCSwapState.COMMITED && this._state!==ToBTCSwapState.REFUNDABLE && this._state!==ToBTCSwapState.SOFT_CLAIMED) {
|
|
990
|
+
this._state = ToBTCSwapState.COMMITED;
|
|
991
|
+
return true;
|
|
992
|
+
}
|
|
993
|
+
break;
|
|
994
|
+
}
|
|
995
|
+
return false;
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
/**
|
|
999
|
+
* @inheritDoc
|
|
1000
|
+
* @internal
|
|
1001
|
+
*/
|
|
836
1002
|
async _tick(save?: boolean): Promise<boolean> {
|
|
837
|
-
switch(this.
|
|
1003
|
+
switch(this._state) {
|
|
838
1004
|
case ToBTCSwapState.CREATED:
|
|
839
1005
|
if(this.expiry<Date.now()) {
|
|
840
|
-
this.
|
|
1006
|
+
this._state = ToBTCSwapState.QUOTE_SOFT_EXPIRED;
|
|
841
1007
|
if(save) await this._saveAndEmit();
|
|
842
1008
|
return true;
|
|
843
1009
|
}
|
|
844
1010
|
break;
|
|
845
1011
|
case ToBTCSwapState.COMMITED:
|
|
846
1012
|
case ToBTCSwapState.SOFT_CLAIMED:
|
|
847
|
-
const expired = await this.wrapper.
|
|
1013
|
+
const expired = await this.wrapper._contract.isExpired(this._getInitiator(), this._data);
|
|
848
1014
|
if(expired) {
|
|
849
|
-
this.
|
|
1015
|
+
this._state = ToBTCSwapState.REFUNDABLE;
|
|
850
1016
|
if(save) await this._saveAndEmit();
|
|
851
1017
|
return true;
|
|
852
1018
|
}
|
|
@@ -854,15 +1020,4 @@ export abstract class IToBTCSwap<
|
|
|
854
1020
|
}
|
|
855
1021
|
return false;
|
|
856
1022
|
}
|
|
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
|
-
}
|
|
1023
|
+
}
|