@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
|
@@ -13,7 +13,8 @@ import {PriceInfoType} from "../types/PriceInfoType";
|
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* Options for swap wrapper configuration
|
|
16
|
-
*
|
|
16
|
+
*
|
|
17
|
+
* @category Swaps/Base
|
|
17
18
|
*/
|
|
18
19
|
export type ISwapWrapperOptions = {
|
|
19
20
|
getRequestTimeout?: number,
|
|
@@ -22,7 +23,8 @@ export type ISwapWrapperOptions = {
|
|
|
22
23
|
|
|
23
24
|
/**
|
|
24
25
|
* Token configuration for wrapper constructors
|
|
25
|
-
*
|
|
26
|
+
*
|
|
27
|
+
* @category Swaps/Base
|
|
26
28
|
*/
|
|
27
29
|
export type WrapperCtorTokens<T extends MultiChain = MultiChain> = {
|
|
28
30
|
ticker: string,
|
|
@@ -36,7 +38,8 @@ export type WrapperCtorTokens<T extends MultiChain = MultiChain> = {
|
|
|
36
38
|
|
|
37
39
|
/**
|
|
38
40
|
* Type definition linking wrapper and swap types
|
|
39
|
-
*
|
|
41
|
+
*
|
|
42
|
+
* @category Swaps/Base
|
|
40
43
|
*/
|
|
41
44
|
export type SwapTypeDefinition<T extends ChainType, W extends ISwapWrapper<T, any>, S extends ISwap<T>> = {
|
|
42
45
|
Wrapper: W;
|
|
@@ -44,45 +47,105 @@ export type SwapTypeDefinition<T extends ChainType, W extends ISwapWrapper<T, an
|
|
|
44
47
|
};
|
|
45
48
|
|
|
46
49
|
/**
|
|
47
|
-
* Base abstract class for swap
|
|
48
|
-
*
|
|
50
|
+
* Base abstract class for swap handler implementations
|
|
51
|
+
*
|
|
52
|
+
* @category Swaps/Base
|
|
49
53
|
*/
|
|
50
54
|
export abstract class ISwapWrapper<
|
|
51
55
|
T extends ChainType,
|
|
52
56
|
D extends SwapTypeDefinition<T, ISwapWrapper<T, D>, ISwap<T, D>>,
|
|
53
57
|
O extends ISwapWrapperOptions = ISwapWrapperOptions
|
|
54
58
|
> {
|
|
59
|
+
/**
|
|
60
|
+
* Swap type
|
|
61
|
+
*/
|
|
55
62
|
abstract readonly TYPE: SwapType;
|
|
63
|
+
/**
|
|
64
|
+
* Function for deserializing swaps
|
|
65
|
+
* @internal
|
|
66
|
+
*/
|
|
67
|
+
abstract readonly _swapDeserializer: new (wrapper: D["Wrapper"], data: any) => D["Swap"];
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Logger instance
|
|
72
|
+
* @internal
|
|
73
|
+
*/
|
|
56
74
|
protected readonly logger = getLogger(this.constructor.name+": ");
|
|
75
|
+
/**
|
|
76
|
+
* Persistent storage backend for the swaps
|
|
77
|
+
* @internal
|
|
78
|
+
*/
|
|
79
|
+
protected readonly unifiedStorage: UnifiedSwapStorage<T>;
|
|
80
|
+
/**
|
|
81
|
+
* Smart chain events listener for listening to and parsing of on-chain events
|
|
82
|
+
* @internal
|
|
83
|
+
*/
|
|
84
|
+
protected readonly unifiedChainEvents: UnifiedSwapEventListener<T>;
|
|
85
|
+
/**
|
|
86
|
+
* States of the swaps where {@link ISwap._tick} should be called every second
|
|
87
|
+
* @internal
|
|
88
|
+
*/
|
|
89
|
+
protected readonly tickSwapState?: Array<D["Swap"]["_state"]>;
|
|
90
|
+
/**
|
|
91
|
+
* In-memory mapping of pending (not initiated) swaps, utilizing weak references to automatically
|
|
92
|
+
* free memory when swaps are dereferenced in not initiated state
|
|
93
|
+
* @internal
|
|
94
|
+
*/
|
|
95
|
+
protected readonly pendingSwaps: Map<string, WeakRef<D["Swap"]>> = new Map();
|
|
96
|
+
|
|
57
97
|
|
|
58
|
-
|
|
98
|
+
/**
|
|
99
|
+
* Whether this wrapper is initialized (have to call {@link init} to initialize a wrapper)
|
|
100
|
+
* @internal
|
|
101
|
+
*/
|
|
102
|
+
protected isInitialized: boolean = false;
|
|
103
|
+
/**
|
|
104
|
+
* An interval for calling tick functions on the underlying swaps
|
|
105
|
+
* @internal
|
|
106
|
+
*/
|
|
107
|
+
protected tickInterval?: NodeJS.Timeout;
|
|
59
108
|
|
|
60
|
-
readonly unifiedStorage: UnifiedSwapStorage<T>;
|
|
61
|
-
readonly unifiedChainEvents: UnifiedSwapEventListener<T>;
|
|
62
109
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
readonly
|
|
68
|
-
|
|
110
|
+
/**
|
|
111
|
+
* States of the swaps in pending (non-final state), these are checked automatically on initial swap synchronization
|
|
112
|
+
* @internal
|
|
113
|
+
*/
|
|
114
|
+
abstract readonly _pendingSwapStates: Array<D["Swap"]["_state"]>;
|
|
115
|
+
/**
|
|
116
|
+
* Chain interface of the underlying smart chain
|
|
117
|
+
* @internal
|
|
118
|
+
*/
|
|
119
|
+
readonly _chain: T["ChainInterface"];
|
|
120
|
+
/**
|
|
121
|
+
* Pricing API
|
|
122
|
+
* @internal
|
|
123
|
+
*/
|
|
124
|
+
readonly _prices: ISwapPrice;
|
|
125
|
+
/**
|
|
126
|
+
* Wrapper options
|
|
127
|
+
* @internal
|
|
128
|
+
*/
|
|
129
|
+
readonly _options: O;
|
|
130
|
+
/**
|
|
131
|
+
* Tokens indexed by their token address
|
|
132
|
+
* @internal
|
|
133
|
+
*/
|
|
134
|
+
readonly _tokens: {
|
|
69
135
|
[tokenAddress: string]: SCToken<T["ChainId"]>
|
|
70
136
|
};
|
|
71
|
-
readonly pendingSwaps: Map<string, WeakRef<D["Swap"]>> = new Map();
|
|
72
137
|
|
|
73
|
-
isInitialized: boolean = false;
|
|
74
|
-
tickInterval?: NodeJS.Timeout;
|
|
75
138
|
|
|
76
139
|
/**
|
|
77
|
-
*
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
*
|
|
82
|
-
* @param tokens Chain specific token data
|
|
83
|
-
* @param options
|
|
84
|
-
* @param events Instance to use for emitting events
|
|
140
|
+
* Chain identifier string of this wrapper
|
|
141
|
+
*/
|
|
142
|
+
readonly chainIdentifier: T["ChainId"];
|
|
143
|
+
/**
|
|
144
|
+
* Event emitter emitting `"swapState"` event when swap's state changes
|
|
85
145
|
*/
|
|
146
|
+
readonly events: EventEmitter<{swapState: [D["Swap"]]}>;
|
|
147
|
+
|
|
148
|
+
|
|
86
149
|
constructor(
|
|
87
150
|
chainIdentifier: T["ChainId"],
|
|
88
151
|
unifiedStorage: UnifiedSwapStorage<T>,
|
|
@@ -97,15 +160,15 @@ export abstract class ISwapWrapper<
|
|
|
97
160
|
this.unifiedChainEvents = unifiedChainEvents;
|
|
98
161
|
|
|
99
162
|
this.chainIdentifier = chainIdentifier;
|
|
100
|
-
this.
|
|
101
|
-
this.
|
|
163
|
+
this._chain = chain;
|
|
164
|
+
this._prices = prices;
|
|
102
165
|
this.events = events || new EventEmitter();
|
|
103
|
-
this.
|
|
104
|
-
this.
|
|
166
|
+
this._options = options;
|
|
167
|
+
this._tokens = {};
|
|
105
168
|
for(let tokenData of tokens) {
|
|
106
169
|
const chainData = tokenData.chains[chainIdentifier];
|
|
107
170
|
if(chainData==null) continue;
|
|
108
|
-
this.
|
|
171
|
+
this._tokens[chainData.address] = {
|
|
109
172
|
chain: "SC",
|
|
110
173
|
chainId: this.chainIdentifier,
|
|
111
174
|
address: chainData.address,
|
|
@@ -117,16 +180,17 @@ export abstract class ISwapWrapper<
|
|
|
117
180
|
}
|
|
118
181
|
}
|
|
119
182
|
|
|
183
|
+
|
|
120
184
|
/**
|
|
121
185
|
* Pre-fetches swap price for a given swap
|
|
122
186
|
*
|
|
123
|
-
* @param amountData
|
|
124
|
-
* @param abortSignal
|
|
125
|
-
* @protected
|
|
187
|
+
* @param amountData Amount data
|
|
188
|
+
* @param abortSignal Abort signal
|
|
126
189
|
* @returns Price of the token in uSats (micro sats)
|
|
190
|
+
* @internal
|
|
127
191
|
*/
|
|
128
192
|
protected preFetchPrice(amountData: { token: string }, abortSignal?: AbortSignal): Promise<bigint | undefined> {
|
|
129
|
-
return this.
|
|
193
|
+
return this._prices.preFetchPrice(this.chainIdentifier, amountData.token, abortSignal).catch(e => {
|
|
130
194
|
this.logger.error("preFetchPrice.token(): Error: ", e);
|
|
131
195
|
return undefined;
|
|
132
196
|
});
|
|
@@ -135,31 +199,32 @@ export abstract class ISwapWrapper<
|
|
|
135
199
|
/**
|
|
136
200
|
* Pre-fetches bitcoin's USD price
|
|
137
201
|
*
|
|
138
|
-
* @param abortSignal
|
|
139
|
-
* @
|
|
202
|
+
* @param abortSignal Abort signal
|
|
203
|
+
* @internal
|
|
140
204
|
*/
|
|
141
205
|
protected preFetchUsdPrice(abortSignal?: AbortSignal): Promise<number | undefined> {
|
|
142
|
-
return this.
|
|
206
|
+
return this._prices.preFetchUsdPrice(abortSignal).catch(e => {
|
|
143
207
|
this.logger.error("preFetchPrice.usd(): Error: ", e);
|
|
144
208
|
return undefined;
|
|
145
209
|
})
|
|
146
210
|
}
|
|
147
211
|
|
|
148
212
|
/**
|
|
149
|
-
* Verifies returned
|
|
213
|
+
* Verifies returned price for swaps
|
|
150
214
|
*
|
|
151
215
|
* @param lpServiceData Service data for the service in question (TO_BTCLN, TO_BTC, etc.) of the given intermediary
|
|
152
|
-
* @param send Whether this is a send (
|
|
216
|
+
* @param send Whether this is a send (Smart chain -> Bitcoin) or receive (Bitcoin -> Smart chain) swap
|
|
153
217
|
* @param amountSats Amount in BTC
|
|
154
218
|
* @param amountToken Amount in token
|
|
155
219
|
* @param token Token used in the swap
|
|
156
220
|
* @param feeData Fee data as returned by the intermediary
|
|
157
|
-
* @param pricePrefetchPromise
|
|
158
|
-
* @param usdPricePrefetchPromise
|
|
159
|
-
* @param abortSignal
|
|
160
|
-
* @protected
|
|
221
|
+
* @param pricePrefetchPromise Optional price pre-fetch promise
|
|
222
|
+
* @param usdPricePrefetchPromise Optiona USD price pre-fetch promise
|
|
223
|
+
* @param abortSignal Abort signal
|
|
161
224
|
* @returns Price info object
|
|
162
225
|
* @throws {IntermediaryError} if the calculated fee is too high
|
|
226
|
+
*
|
|
227
|
+
* @internal
|
|
163
228
|
*/
|
|
164
229
|
protected async verifyReturnedPrice(
|
|
165
230
|
lpServiceData: {swapBaseFee: number, swapFeePPM: number},
|
|
@@ -180,11 +245,11 @@ export abstract class ISwapWrapper<
|
|
|
180
245
|
|
|
181
246
|
const [isValidAmount, usdPrice] = await Promise.all([
|
|
182
247
|
send ?
|
|
183
|
-
this.
|
|
184
|
-
this.
|
|
248
|
+
this._prices.isValidAmountSend(this.chainIdentifier, amountSats, swapBaseFee, swapFeePPM, amountToken, token, abortSignal, await pricePrefetchPromise) :
|
|
249
|
+
this._prices.isValidAmountReceive(this.chainIdentifier, amountSats, swapBaseFee, swapFeePPM, amountToken, token, abortSignal, await pricePrefetchPromise),
|
|
185
250
|
usdPricePrefetchPromise.then(value => {
|
|
186
251
|
if(value!=null) return value;
|
|
187
|
-
return this.
|
|
252
|
+
return this._prices.preFetchUsdPrice(abortSignal);
|
|
188
253
|
})
|
|
189
254
|
]);
|
|
190
255
|
if(!isValidAmount.isValid) throw new IntermediaryError("Fee too high");
|
|
@@ -193,19 +258,58 @@ export abstract class ISwapWrapper<
|
|
|
193
258
|
return isValidAmount;
|
|
194
259
|
}
|
|
195
260
|
|
|
196
|
-
public abstract readonly pendingSwapStates: Array<D["Swap"]["state"]>;
|
|
197
|
-
public abstract readonly tickSwapState?: Array<D["Swap"]["state"]>;
|
|
198
|
-
|
|
199
261
|
/**
|
|
200
|
-
* Processes a single
|
|
201
|
-
*
|
|
202
|
-
* @param event
|
|
203
|
-
* @param swap
|
|
262
|
+
* Processes a single smart chain on-chain event
|
|
263
|
+
*
|
|
264
|
+
* @param event Smart chain event to process
|
|
265
|
+
* @param swap A swap related to the event
|
|
266
|
+
* @internal
|
|
204
267
|
*/
|
|
205
268
|
protected abstract processEvent?(event: ChainEvent<T["Data"]>, swap: D["Swap"]): Promise<void>;
|
|
206
269
|
|
|
270
|
+
/**
|
|
271
|
+
* Starts the interval calling the {@link ISwap._tick} on all the known swaps in tick-enabled states
|
|
272
|
+
* @internal
|
|
273
|
+
*/
|
|
274
|
+
protected startTickInterval(): void {
|
|
275
|
+
if(this.tickSwapState==null || this.tickSwapState.length===0) return;
|
|
276
|
+
this.tickInterval = setInterval(() => {
|
|
277
|
+
this.tick();
|
|
278
|
+
}, 1000);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Runs checks on passed swaps, syncing their state from on-chain data
|
|
283
|
+
*
|
|
284
|
+
* @param pastSwaps Swaps to check
|
|
285
|
+
* @internal
|
|
286
|
+
*/
|
|
287
|
+
protected async _checkPastSwaps(pastSwaps: D["Swap"][]): Promise<{changedSwaps: D["Swap"][], removeSwaps: D["Swap"][]}> {
|
|
288
|
+
const changedSwaps: D["Swap"][] = [];
|
|
289
|
+
const removeSwaps: D["Swap"][] = [];
|
|
290
|
+
|
|
291
|
+
await Promise.all(pastSwaps.map((swap: D["Swap"]) =>
|
|
292
|
+
swap._sync(false).then(changed => {
|
|
293
|
+
if(swap.isQuoteExpired()) {
|
|
294
|
+
removeSwaps.push(swap);
|
|
295
|
+
this.logger.debug("_checkPastSwaps(): Removing expired swap: "+swap.getId());
|
|
296
|
+
} else {
|
|
297
|
+
if(changed) changedSwaps.push(swap);
|
|
298
|
+
}
|
|
299
|
+
}).catch(e => this.logger.error("_checkPastSwaps(): Error when checking swap "+swap.getId()+": ", e))
|
|
300
|
+
));
|
|
301
|
+
|
|
302
|
+
return {changedSwaps, removeSwaps};
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
|
|
207
306
|
/**
|
|
208
307
|
* Initializes the swap wrapper, needs to be called before any other action can be taken
|
|
308
|
+
*
|
|
309
|
+
* @param noTimers Whether to skip scheduling a tick timer for the swaps, if the tick timer is not initiated
|
|
310
|
+
* the swap states depending on e.g. expiry can be out of sync with the actual expiration of the swap
|
|
311
|
+
* @param noCheckPastSwaps Whether to skip checking past swaps on initialization (by default all pending swaps
|
|
312
|
+
* are re-checked on init, and their state is synchronized from the on-chain data)
|
|
209
313
|
*/
|
|
210
314
|
public async init(noTimers: boolean = false, noCheckPastSwaps: boolean = false): Promise<void> {
|
|
211
315
|
if(this.isInitialized) return;
|
|
@@ -221,7 +325,7 @@ export abstract class ISwapWrapper<
|
|
|
221
325
|
eventQueue.push({event, swap});
|
|
222
326
|
return Promise.resolve();
|
|
223
327
|
}
|
|
224
|
-
if(this.processEvent!=null) this.unifiedChainEvents.registerListener(this.TYPE, initListener, this.
|
|
328
|
+
if(this.processEvent!=null) this.unifiedChainEvents.registerListener(this.TYPE, initListener, this._swapDeserializer.bind(null, this));
|
|
225
329
|
|
|
226
330
|
await this.checkPastSwaps();
|
|
227
331
|
|
|
@@ -236,7 +340,7 @@ export abstract class ISwapWrapper<
|
|
|
236
340
|
}
|
|
237
341
|
}
|
|
238
342
|
|
|
239
|
-
if(this.processEvent!=null) this.unifiedChainEvents.registerListener(this.TYPE, this.processEvent.bind(this), this.
|
|
343
|
+
if(this.processEvent!=null) this.unifiedChainEvents.registerListener(this.TYPE, this.processEvent.bind(this), this._swapDeserializer.bind(null, this));
|
|
240
344
|
|
|
241
345
|
if(!noTimers) this.startTickInterval();
|
|
242
346
|
|
|
@@ -245,35 +349,27 @@ export abstract class ISwapWrapper<
|
|
|
245
349
|
this.isInitialized = true;
|
|
246
350
|
}
|
|
247
351
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
const changedSwaps: D["Swap"][] = [];
|
|
257
|
-
const removeSwaps: D["Swap"][] = [];
|
|
258
|
-
|
|
259
|
-
await Promise.all(pastSwaps.map((swap: D["Swap"]) =>
|
|
260
|
-
swap._sync(false).then(changed => {
|
|
261
|
-
if(swap.isQuoteExpired()) {
|
|
262
|
-
removeSwaps.push(swap);
|
|
263
|
-
this.logger.debug("_checkPastSwaps(): Removing expired swap: "+swap.getId());
|
|
264
|
-
} else {
|
|
265
|
-
if(changed) changedSwaps.push(swap);
|
|
266
|
-
}
|
|
267
|
-
}).catch(e => this.logger.error("_checkPastSwaps(): Error when checking swap "+swap.getId()+": ", e))
|
|
268
|
-
));
|
|
269
|
-
|
|
270
|
-
return {changedSwaps, removeSwaps};
|
|
352
|
+
/**
|
|
353
|
+
* Un-subscribes from event listeners on the smart chain, terminates the tick interval and stops this wrapper
|
|
354
|
+
*/
|
|
355
|
+
public async stop() {
|
|
356
|
+
this.isInitialized = false;
|
|
357
|
+
this.unifiedChainEvents.unregisterListener(this.TYPE);
|
|
358
|
+
this.logger.info("stop(): Swap wrapper stopped");
|
|
359
|
+
if(this.tickInterval!=null) clearInterval(this.tickInterval);
|
|
271
360
|
}
|
|
272
361
|
|
|
273
|
-
|
|
362
|
+
/**
|
|
363
|
+
* Runs checks on all the known pending swaps, syncing their state from on-chain data
|
|
364
|
+
*
|
|
365
|
+
* @param pastSwaps Optional array of past swaps to check, otherwise all relevant swaps will be fetched
|
|
366
|
+
* from the persistent storage
|
|
367
|
+
* @param noSave Whether to skip saving the swap changes in the persistent storage
|
|
368
|
+
*/
|
|
369
|
+
public async checkPastSwaps(pastSwaps?: D["Swap"][], noSave?: boolean): Promise<{ removeSwaps: D["Swap"][], changedSwaps: D["Swap"][] }> {
|
|
274
370
|
if (pastSwaps == null) pastSwaps = await this.unifiedStorage.query<D["Swap"]>(
|
|
275
|
-
[[{key: "type", value: this.TYPE}, {key: "state", value: this.
|
|
276
|
-
(val: any) => new this.
|
|
371
|
+
[[{key: "type", value: this.TYPE}, {key: "state", value: this._pendingSwapStates}]],
|
|
372
|
+
(val: any) => new this._swapDeserializer(this, val)
|
|
277
373
|
);
|
|
278
374
|
|
|
279
375
|
const {removeSwaps, changedSwaps} = await this._checkPastSwaps(pastSwaps);
|
|
@@ -291,10 +387,16 @@ export abstract class ISwapWrapper<
|
|
|
291
387
|
}
|
|
292
388
|
}
|
|
293
389
|
|
|
294
|
-
|
|
390
|
+
/**
|
|
391
|
+
* Invokes {@link ISwap._tick} on all the known swaps
|
|
392
|
+
*
|
|
393
|
+
* @param swaps Optional array of swaps to invoke `_tick()` on, otherwise all relevant swaps will be fetched
|
|
394
|
+
* from the persistent storage
|
|
395
|
+
*/
|
|
396
|
+
public async tick(swaps?: D["Swap"][]): Promise<void> {
|
|
295
397
|
if(swaps==null) swaps = await this.unifiedStorage.query<D["Swap"]>(
|
|
296
398
|
[[{key: "type", value: this.TYPE}, {key: "state", value: this.tickSwapState}]],
|
|
297
|
-
(val: any) => new this.
|
|
399
|
+
(val: any) => new this._swapDeserializer(this, val)
|
|
298
400
|
);
|
|
299
401
|
|
|
300
402
|
for(let pendingSwap of this.pendingSwaps.values()) {
|
|
@@ -307,7 +409,22 @@ export abstract class ISwapWrapper<
|
|
|
307
409
|
});
|
|
308
410
|
}
|
|
309
411
|
|
|
310
|
-
|
|
412
|
+
/**
|
|
413
|
+
* Returns the smart chain's native token used to pay for fees
|
|
414
|
+
* @internal
|
|
415
|
+
*/
|
|
416
|
+
_getNativeToken(): SCToken<T["ChainId"]> {
|
|
417
|
+
return this._tokens[this._chain.getNativeCurrencyAddress()];
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Saves the swap, if it is not initiated it is only saved to pending swaps
|
|
422
|
+
*
|
|
423
|
+
* @param swap Swap to save
|
|
424
|
+
*
|
|
425
|
+
* @internal
|
|
426
|
+
*/
|
|
427
|
+
_saveSwapData(swap: D["Swap"]): Promise<void> {
|
|
311
428
|
if(!swap.isInitiated()) {
|
|
312
429
|
this.logger.debug("saveSwapData(): Swap "+swap.getId()+" not initiated, saving to pending swaps");
|
|
313
430
|
this.pendingSwaps.set(swap.getId(), new WeakRef<D["Swap"]>(swap));
|
|
@@ -318,27 +435,28 @@ export abstract class ISwapWrapper<
|
|
|
318
435
|
return this.unifiedStorage.save(swap);
|
|
319
436
|
}
|
|
320
437
|
|
|
321
|
-
|
|
438
|
+
/**
|
|
439
|
+
* Removes the swap from the persistent storage and pending swaps
|
|
440
|
+
*
|
|
441
|
+
* @param swap Swap to remove
|
|
442
|
+
*
|
|
443
|
+
* @internal
|
|
444
|
+
*/
|
|
445
|
+
_removeSwapData(swap: D["Swap"]): Promise<void> {
|
|
322
446
|
this.pendingSwaps.delete(swap.getId());
|
|
323
447
|
if(!swap.isInitiated()) return Promise.resolve();
|
|
324
448
|
return this.unifiedStorage.remove(swap);
|
|
325
449
|
}
|
|
326
450
|
|
|
327
451
|
/**
|
|
328
|
-
*
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
this.logger.info("stop(): Swap wrapper stopped");
|
|
334
|
-
if(this.tickInterval!=null) clearInterval(this.tickInterval);
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
/**
|
|
338
|
-
* Returns the smart chain's native token used to pay for fees
|
|
452
|
+
* Retrieves a swap by its ID from the pending swap mapping
|
|
453
|
+
*
|
|
454
|
+
* @param id
|
|
455
|
+
*
|
|
456
|
+
* @internal
|
|
339
457
|
*/
|
|
340
|
-
|
|
341
|
-
return this.
|
|
458
|
+
_getPendingSwap(id: string): D["Swap"] | null {
|
|
459
|
+
return this.pendingSwaps.get(id)?.deref() ?? null;
|
|
342
460
|
}
|
|
343
461
|
|
|
344
462
|
}
|
|
@@ -25,14 +25,29 @@ export function isIEscrowSelfInitSwapInit<T extends SwapData>(obj: any): obj is
|
|
|
25
25
|
|
|
26
26
|
export type IEscrowSelfInitSwapDefinition<T extends ChainType, W extends IEscrowSwapWrapper<T, any>, S extends IEscrowSelfInitSwap<T>> = SwapTypeDefinition<T, W, S>;
|
|
27
27
|
|
|
28
|
+
/**
|
|
29
|
+
* Base class for escrow-based swaps (i.e. swaps utilizing PrTLC and HTLC primitives) where the
|
|
30
|
+
* user needs to initiate the escrow on the smart chain side
|
|
31
|
+
*
|
|
32
|
+
* @category Swaps/Abstract
|
|
33
|
+
*/
|
|
28
34
|
export abstract class IEscrowSelfInitSwap<
|
|
29
35
|
T extends ChainType = ChainType,
|
|
30
36
|
D extends IEscrowSelfInitSwapDefinition<T, IEscrowSwapWrapper<T, D>, IEscrowSelfInitSwap<T, D, S>> = IEscrowSwapDefinition<T, IEscrowSwapWrapper<T, any>, IEscrowSelfInitSwap<T, any, any>>,
|
|
31
37
|
S extends number = number
|
|
32
38
|
> extends IEscrowSwap<T, D, S> {
|
|
33
39
|
|
|
34
|
-
|
|
35
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Fee rate to be used for the escrow initiation transaction
|
|
42
|
+
* @internal
|
|
43
|
+
*/
|
|
44
|
+
protected readonly feeRate: string;
|
|
45
|
+
/**
|
|
46
|
+
* Signature data received from the intermediary (LP) allowing the user
|
|
47
|
+
* to initiate the swap escrow
|
|
48
|
+
* @internal
|
|
49
|
+
*/
|
|
50
|
+
protected signatureData?: SignatureData;
|
|
36
51
|
|
|
37
52
|
protected constructor(wrapper: D["Wrapper"], obj: any);
|
|
38
53
|
protected constructor(wrapper: D["Wrapper"], swapInit: IEscrowSelfInitSwapInit<T["Data"]>);
|
|
@@ -63,10 +78,10 @@ export abstract class IEscrowSelfInitSwap<
|
|
|
63
78
|
*
|
|
64
79
|
* @param intervalSeconds How often to check (in seconds), default to 5s
|
|
65
80
|
* @param abortSignal
|
|
66
|
-
* @
|
|
81
|
+
* @internal
|
|
67
82
|
*/
|
|
68
83
|
protected async watchdogWaitTillSignatureExpiry(intervalSeconds?: number, abortSignal?: AbortSignal): Promise<void> {
|
|
69
|
-
if(this.
|
|
84
|
+
if(this._data==null || this.signatureData==null)
|
|
70
85
|
throw new Error("Tried to await signature expiry but data or signature is null, invalid state?");
|
|
71
86
|
|
|
72
87
|
intervalSeconds ??= 5;
|
|
@@ -74,7 +89,7 @@ export abstract class IEscrowSelfInitSwap<
|
|
|
74
89
|
while(!expired) {
|
|
75
90
|
await timeoutPromise(intervalSeconds*1000, abortSignal);
|
|
76
91
|
try {
|
|
77
|
-
expired = await this.wrapper.
|
|
92
|
+
expired = await this.wrapper._contract.isInitAuthorizationExpired(this._data, this.signatureData);
|
|
78
93
|
} catch (e) {
|
|
79
94
|
this.logger.error("watchdogWaitTillSignatureExpiry(): Error when checking signature expiry: ", e);
|
|
80
95
|
}
|
|
@@ -88,29 +103,31 @@ export abstract class IEscrowSelfInitSwap<
|
|
|
88
103
|
|
|
89
104
|
/**
|
|
90
105
|
* Get the estimated smart chain fee of the commit transaction
|
|
106
|
+
* @internal
|
|
91
107
|
*/
|
|
92
108
|
protected getCommitFee(): Promise<bigint> {
|
|
93
|
-
return this.wrapper.
|
|
109
|
+
return this.wrapper._contract.getCommitFee(this._getInitiator(), this.getSwapData(), this.feeRate);
|
|
94
110
|
}
|
|
95
111
|
|
|
96
112
|
/**
|
|
97
|
-
* Returns the transaction fee paid on the smart chain
|
|
113
|
+
* Returns the transaction fee paid on the smart chain side to initiate the escrow
|
|
98
114
|
*/
|
|
99
115
|
async getSmartChainNetworkFee(): Promise<TokenAmount<T["ChainId"], SCToken<T["ChainId"]>, true>> {
|
|
100
|
-
const swapContract: T["Contract"] = this.wrapper.
|
|
116
|
+
const swapContract: T["Contract"] = this.wrapper._contract;
|
|
101
117
|
return toTokenAmount(
|
|
102
118
|
await (
|
|
103
119
|
swapContract.getRawCommitFee!=null ?
|
|
104
120
|
swapContract.getRawCommitFee(this._getInitiator(), this.getSwapData(), this.feeRate) :
|
|
105
121
|
swapContract.getCommitFee(this._getInitiator(), this.getSwapData(), this.feeRate)
|
|
106
122
|
),
|
|
107
|
-
this.wrapper.
|
|
108
|
-
this.wrapper.
|
|
123
|
+
this.wrapper._getNativeToken(),
|
|
124
|
+
this.wrapper._prices
|
|
109
125
|
);
|
|
110
126
|
}
|
|
111
127
|
|
|
112
128
|
/**
|
|
113
|
-
* Checks if the initiator/sender has enough balance
|
|
129
|
+
* Checks if the initiator/sender has enough balance on the smart chain side
|
|
130
|
+
* to cover the transaction fee for processing the swap
|
|
114
131
|
*/
|
|
115
132
|
abstract hasEnoughForTxFees(): Promise<{
|
|
116
133
|
enoughBalance: boolean,
|
|
@@ -122,9 +139,35 @@ export abstract class IEscrowSelfInitSwap<
|
|
|
122
139
|
//////////////////////////////
|
|
123
140
|
//// Commit and claim
|
|
124
141
|
|
|
142
|
+
/**
|
|
143
|
+
* Returns transactions for initiating (committing) the escrow on the smart chain side. After sending the
|
|
144
|
+
* transactions manually be sure to call the {@link waitTillCommited} function to wait till the initiation
|
|
145
|
+
* transaction is observed, processed by the SDK and state of the swap properly updated.
|
|
146
|
+
*
|
|
147
|
+
* @param skipChecks Skip checks like making sure init signature is still valid and swap wasn't commited yet
|
|
148
|
+
* (this is handled on swap creation, if you commit right after quoting, you can use `skipChecks=true`)
|
|
149
|
+
*/
|
|
125
150
|
abstract txsCommit(skipChecks?: boolean): Promise<T["TX"][]>;
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Initiates (commits) the escrow on the smart chain side
|
|
154
|
+
*
|
|
155
|
+
* @param _signer Signer to sign the transactions with, must be the same as used in the initialization
|
|
156
|
+
* @param abortSignal Abort signal
|
|
157
|
+
* @param skipChecks Skip checks like making sure init signature is still valid and swap wasn't commited yet
|
|
158
|
+
* (this is handled on swap creation, if you commit right after quoting, you can use `skipChecks=true`)
|
|
159
|
+
* @param onBeforeTxSent Callback called before the transactions are broadcasted
|
|
160
|
+
*/
|
|
126
161
|
abstract commit(_signer: T["Signer"] | T["NativeSigner"], abortSignal?: AbortSignal, skipChecks?: boolean, onBeforeTxSent?: (txId: string) => void): Promise<string>;
|
|
127
162
|
|
|
163
|
+
/**
|
|
164
|
+
* Waits till a swap is initiated (committed) on-chain, should be called after sending the commit
|
|
165
|
+
* transactions ({@link txsCommit}) manually to wait till the SDK processes the escrow
|
|
166
|
+
* initialization and updates the swap state accordingly
|
|
167
|
+
*
|
|
168
|
+
* @param abortSignal AbortSignal
|
|
169
|
+
*/
|
|
170
|
+
abstract waitTillCommited(abortSignal?: AbortSignal): Promise<void>
|
|
128
171
|
|
|
129
172
|
//////////////////////////////
|
|
130
173
|
//// Quote verification
|
|
@@ -133,22 +176,22 @@ export abstract class IEscrowSelfInitSwap<
|
|
|
133
176
|
* Checks if the swap's quote is expired for good (i.e. the swap strictly cannot be committed on-chain anymore)
|
|
134
177
|
*/
|
|
135
178
|
async _verifyQuoteDefinitelyExpired(): Promise<boolean> {
|
|
136
|
-
if(this.
|
|
179
|
+
if(this._data==null || this.signatureData==null) throw new Error("data or signature data are null!");
|
|
137
180
|
|
|
138
|
-
return this.wrapper.
|
|
139
|
-
this.
|
|
181
|
+
return this.wrapper._contract.isInitAuthorizationExpired(
|
|
182
|
+
this._data!, this.signatureData!
|
|
140
183
|
);
|
|
141
184
|
}
|
|
142
185
|
|
|
143
186
|
/**
|
|
144
187
|
* Checks if the swap's quote is still valid
|
|
145
188
|
*/
|
|
146
|
-
async
|
|
147
|
-
if(this.
|
|
189
|
+
async _verifyQuoteValid(): Promise<boolean> {
|
|
190
|
+
if(this._data==null || this.signatureData==null) throw new Error("data or signature data are null!");
|
|
148
191
|
|
|
149
192
|
try {
|
|
150
|
-
await this.wrapper.
|
|
151
|
-
this._getInitiator(), this.
|
|
193
|
+
await this.wrapper._contract.isValidInitAuthorization(
|
|
194
|
+
this._getInitiator(), this._data!, this.signatureData!, this.feeRate
|
|
152
195
|
);
|
|
153
196
|
return true;
|
|
154
197
|
} catch (e) {
|
|
@@ -159,6 +202,9 @@ export abstract class IEscrowSelfInitSwap<
|
|
|
159
202
|
}
|
|
160
203
|
}
|
|
161
204
|
|
|
205
|
+
/**
|
|
206
|
+
* @inheritDoc
|
|
207
|
+
*/
|
|
162
208
|
serialize(): any {
|
|
163
209
|
return {
|
|
164
210
|
...super.serialize(),
|