@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
|
@@ -12,7 +12,8 @@ import { SCToken } from "../types/Token";
|
|
|
12
12
|
import { PriceInfoType } from "../types/PriceInfoType";
|
|
13
13
|
/**
|
|
14
14
|
* Options for swap wrapper configuration
|
|
15
|
-
*
|
|
15
|
+
*
|
|
16
|
+
* @category Swaps/Base
|
|
16
17
|
*/
|
|
17
18
|
export type ISwapWrapperOptions = {
|
|
18
19
|
getRequestTimeout?: number;
|
|
@@ -20,7 +21,8 @@ export type ISwapWrapperOptions = {
|
|
|
20
21
|
};
|
|
21
22
|
/**
|
|
22
23
|
* Token configuration for wrapper constructors
|
|
23
|
-
*
|
|
24
|
+
*
|
|
25
|
+
* @category Swaps/Base
|
|
24
26
|
*/
|
|
25
27
|
export type WrapperCtorTokens<T extends MultiChain = MultiChain> = {
|
|
26
28
|
ticker: string;
|
|
@@ -35,55 +37,111 @@ export type WrapperCtorTokens<T extends MultiChain = MultiChain> = {
|
|
|
35
37
|
}[];
|
|
36
38
|
/**
|
|
37
39
|
* Type definition linking wrapper and swap types
|
|
38
|
-
*
|
|
40
|
+
*
|
|
41
|
+
* @category Swaps/Base
|
|
39
42
|
*/
|
|
40
43
|
export type SwapTypeDefinition<T extends ChainType, W extends ISwapWrapper<T, any>, S extends ISwap<T>> = {
|
|
41
44
|
Wrapper: W;
|
|
42
45
|
Swap: S;
|
|
43
46
|
};
|
|
44
47
|
/**
|
|
45
|
-
* Base abstract class for swap
|
|
46
|
-
*
|
|
48
|
+
* Base abstract class for swap handler implementations
|
|
49
|
+
*
|
|
50
|
+
* @category Swaps/Base
|
|
47
51
|
*/
|
|
48
52
|
export declare abstract class ISwapWrapper<T extends ChainType, D extends SwapTypeDefinition<T, ISwapWrapper<T, D>, ISwap<T, D>>, O extends ISwapWrapperOptions = ISwapWrapperOptions> {
|
|
53
|
+
/**
|
|
54
|
+
* Swap type
|
|
55
|
+
*/
|
|
49
56
|
abstract readonly TYPE: SwapType;
|
|
57
|
+
/**
|
|
58
|
+
* Function for deserializing swaps
|
|
59
|
+
* @internal
|
|
60
|
+
*/
|
|
61
|
+
abstract readonly _swapDeserializer: new (wrapper: D["Wrapper"], data: any) => D["Swap"];
|
|
62
|
+
/**
|
|
63
|
+
* Logger instance
|
|
64
|
+
* @internal
|
|
65
|
+
*/
|
|
50
66
|
protected readonly logger: import("../utils/Logger").LoggerType;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
67
|
+
/**
|
|
68
|
+
* Persistent storage backend for the swaps
|
|
69
|
+
* @internal
|
|
70
|
+
*/
|
|
71
|
+
protected readonly unifiedStorage: UnifiedSwapStorage<T>;
|
|
72
|
+
/**
|
|
73
|
+
* Smart chain events listener for listening to and parsing of on-chain events
|
|
74
|
+
* @internal
|
|
75
|
+
*/
|
|
76
|
+
protected readonly unifiedChainEvents: UnifiedSwapEventListener<T>;
|
|
77
|
+
/**
|
|
78
|
+
* States of the swaps where {@link ISwap._tick} should be called every second
|
|
79
|
+
* @internal
|
|
80
|
+
*/
|
|
81
|
+
protected readonly tickSwapState?: Array<D["Swap"]["_state"]>;
|
|
82
|
+
/**
|
|
83
|
+
* In-memory mapping of pending (not initiated) swaps, utilizing weak references to automatically
|
|
84
|
+
* free memory when swaps are dereferenced in not initiated state
|
|
85
|
+
* @internal
|
|
86
|
+
*/
|
|
87
|
+
protected readonly pendingSwaps: Map<string, WeakRef<D["Swap"]>>;
|
|
88
|
+
/**
|
|
89
|
+
* Whether this wrapper is initialized (have to call {@link init} to initialize a wrapper)
|
|
90
|
+
* @internal
|
|
91
|
+
*/
|
|
92
|
+
protected isInitialized: boolean;
|
|
93
|
+
/**
|
|
94
|
+
* An interval for calling tick functions on the underlying swaps
|
|
95
|
+
* @internal
|
|
96
|
+
*/
|
|
97
|
+
protected tickInterval?: NodeJS.Timeout;
|
|
98
|
+
/**
|
|
99
|
+
* States of the swaps in pending (non-final state), these are checked automatically on initial swap synchronization
|
|
100
|
+
* @internal
|
|
101
|
+
*/
|
|
102
|
+
abstract readonly _pendingSwapStates: Array<D["Swap"]["_state"]>;
|
|
103
|
+
/**
|
|
104
|
+
* Chain interface of the underlying smart chain
|
|
105
|
+
* @internal
|
|
106
|
+
*/
|
|
107
|
+
readonly _chain: T["ChainInterface"];
|
|
108
|
+
/**
|
|
109
|
+
* Pricing API
|
|
110
|
+
* @internal
|
|
111
|
+
*/
|
|
112
|
+
readonly _prices: ISwapPrice;
|
|
113
|
+
/**
|
|
114
|
+
* Wrapper options
|
|
115
|
+
* @internal
|
|
116
|
+
*/
|
|
117
|
+
readonly _options: O;
|
|
118
|
+
/**
|
|
119
|
+
* Tokens indexed by their token address
|
|
120
|
+
* @internal
|
|
121
|
+
*/
|
|
122
|
+
readonly _tokens: {
|
|
123
|
+
[tokenAddress: string]: SCToken<T["ChainId"]>;
|
|
124
|
+
};
|
|
125
|
+
/**
|
|
126
|
+
* Chain identifier string of this wrapper
|
|
127
|
+
*/
|
|
54
128
|
readonly chainIdentifier: T["ChainId"];
|
|
55
|
-
|
|
56
|
-
|
|
129
|
+
/**
|
|
130
|
+
* Event emitter emitting `"swapState"` event when swap's state changes
|
|
131
|
+
*/
|
|
57
132
|
readonly events: EventEmitter<{
|
|
58
133
|
swapState: [D["Swap"]];
|
|
59
134
|
}>;
|
|
60
|
-
readonly options: O;
|
|
61
|
-
readonly tokens: {
|
|
62
|
-
[tokenAddress: string]: SCToken<T["ChainId"]>;
|
|
63
|
-
};
|
|
64
|
-
readonly pendingSwaps: Map<string, WeakRef<D["Swap"]>>;
|
|
65
|
-
isInitialized: boolean;
|
|
66
|
-
tickInterval?: NodeJS.Timeout;
|
|
67
|
-
/**
|
|
68
|
-
* @param chainIdentifier
|
|
69
|
-
* @param unifiedStorage
|
|
70
|
-
* @param unifiedChainEvents
|
|
71
|
-
* @param chain
|
|
72
|
-
* @param prices Swap pricing handler
|
|
73
|
-
* @param tokens Chain specific token data
|
|
74
|
-
* @param options
|
|
75
|
-
* @param events Instance to use for emitting events
|
|
76
|
-
*/
|
|
77
135
|
constructor(chainIdentifier: T["ChainId"], unifiedStorage: UnifiedSwapStorage<T>, unifiedChainEvents: UnifiedSwapEventListener<T>, chain: T["ChainInterface"], prices: ISwapPrice, tokens: WrapperCtorTokens, options: O, events?: EventEmitter<{
|
|
78
136
|
swapState: [ISwap];
|
|
79
137
|
}>);
|
|
80
138
|
/**
|
|
81
139
|
* Pre-fetches swap price for a given swap
|
|
82
140
|
*
|
|
83
|
-
* @param amountData
|
|
84
|
-
* @param abortSignal
|
|
85
|
-
* @protected
|
|
141
|
+
* @param amountData Amount data
|
|
142
|
+
* @param abortSignal Abort signal
|
|
86
143
|
* @returns Price of the token in uSats (micro sats)
|
|
144
|
+
* @internal
|
|
87
145
|
*/
|
|
88
146
|
protected preFetchPrice(amountData: {
|
|
89
147
|
token: string;
|
|
@@ -91,25 +149,26 @@ export declare abstract class ISwapWrapper<T extends ChainType, D extends SwapTy
|
|
|
91
149
|
/**
|
|
92
150
|
* Pre-fetches bitcoin's USD price
|
|
93
151
|
*
|
|
94
|
-
* @param abortSignal
|
|
95
|
-
* @
|
|
152
|
+
* @param abortSignal Abort signal
|
|
153
|
+
* @internal
|
|
96
154
|
*/
|
|
97
155
|
protected preFetchUsdPrice(abortSignal?: AbortSignal): Promise<number | undefined>;
|
|
98
156
|
/**
|
|
99
|
-
* Verifies returned
|
|
157
|
+
* Verifies returned price for swaps
|
|
100
158
|
*
|
|
101
159
|
* @param lpServiceData Service data for the service in question (TO_BTCLN, TO_BTC, etc.) of the given intermediary
|
|
102
|
-
* @param send Whether this is a send (
|
|
160
|
+
* @param send Whether this is a send (Smart chain -> Bitcoin) or receive (Bitcoin -> Smart chain) swap
|
|
103
161
|
* @param amountSats Amount in BTC
|
|
104
162
|
* @param amountToken Amount in token
|
|
105
163
|
* @param token Token used in the swap
|
|
106
164
|
* @param feeData Fee data as returned by the intermediary
|
|
107
|
-
* @param pricePrefetchPromise
|
|
108
|
-
* @param usdPricePrefetchPromise
|
|
109
|
-
* @param abortSignal
|
|
110
|
-
* @protected
|
|
165
|
+
* @param pricePrefetchPromise Optional price pre-fetch promise
|
|
166
|
+
* @param usdPricePrefetchPromise Optiona USD price pre-fetch promise
|
|
167
|
+
* @param abortSignal Abort signal
|
|
111
168
|
* @returns Price info object
|
|
112
169
|
* @throws {IntermediaryError} if the calculated fee is too high
|
|
170
|
+
*
|
|
171
|
+
* @internal
|
|
113
172
|
*/
|
|
114
173
|
protected verifyReturnedPrice(lpServiceData: {
|
|
115
174
|
swapBaseFee: number;
|
|
@@ -117,37 +176,87 @@ export declare abstract class ISwapWrapper<T extends ChainType, D extends SwapTy
|
|
|
117
176
|
}, send: boolean, amountSats: bigint, amountToken: bigint, token: string, feeData: {
|
|
118
177
|
networkFee?: bigint;
|
|
119
178
|
}, pricePrefetchPromise?: Promise<bigint | undefined>, usdPricePrefetchPromise?: Promise<number | undefined>, abortSignal?: AbortSignal): Promise<PriceInfoType>;
|
|
120
|
-
abstract readonly pendingSwapStates: Array<D["Swap"]["state"]>;
|
|
121
|
-
abstract readonly tickSwapState?: Array<D["Swap"]["state"]>;
|
|
122
179
|
/**
|
|
123
|
-
* Processes a single
|
|
124
|
-
*
|
|
125
|
-
* @param event
|
|
126
|
-
* @param swap
|
|
180
|
+
* Processes a single smart chain on-chain event
|
|
181
|
+
*
|
|
182
|
+
* @param event Smart chain event to process
|
|
183
|
+
* @param swap A swap related to the event
|
|
184
|
+
* @internal
|
|
127
185
|
*/
|
|
128
186
|
protected abstract processEvent?(event: ChainEvent<T["Data"]>, swap: D["Swap"]): Promise<void>;
|
|
129
187
|
/**
|
|
130
|
-
*
|
|
188
|
+
* Starts the interval calling the {@link ISwap._tick} on all the known swaps in tick-enabled states
|
|
189
|
+
* @internal
|
|
131
190
|
*/
|
|
132
|
-
init(noTimers?: boolean, noCheckPastSwaps?: boolean): Promise<void>;
|
|
133
191
|
protected startTickInterval(): void;
|
|
192
|
+
/**
|
|
193
|
+
* Runs checks on passed swaps, syncing their state from on-chain data
|
|
194
|
+
*
|
|
195
|
+
* @param pastSwaps Swaps to check
|
|
196
|
+
* @internal
|
|
197
|
+
*/
|
|
134
198
|
protected _checkPastSwaps(pastSwaps: D["Swap"][]): Promise<{
|
|
135
199
|
changedSwaps: D["Swap"][];
|
|
136
200
|
removeSwaps: D["Swap"][];
|
|
137
201
|
}>;
|
|
202
|
+
/**
|
|
203
|
+
* Initializes the swap wrapper, needs to be called before any other action can be taken
|
|
204
|
+
*
|
|
205
|
+
* @param noTimers Whether to skip scheduling a tick timer for the swaps, if the tick timer is not initiated
|
|
206
|
+
* the swap states depending on e.g. expiry can be out of sync with the actual expiration of the swap
|
|
207
|
+
* @param noCheckPastSwaps Whether to skip checking past swaps on initialization (by default all pending swaps
|
|
208
|
+
* are re-checked on init, and their state is synchronized from the on-chain data)
|
|
209
|
+
*/
|
|
210
|
+
init(noTimers?: boolean, noCheckPastSwaps?: boolean): Promise<void>;
|
|
211
|
+
/**
|
|
212
|
+
* Un-subscribes from event listeners on the smart chain, terminates the tick interval and stops this wrapper
|
|
213
|
+
*/
|
|
214
|
+
stop(): Promise<void>;
|
|
215
|
+
/**
|
|
216
|
+
* Runs checks on all the known pending swaps, syncing their state from on-chain data
|
|
217
|
+
*
|
|
218
|
+
* @param pastSwaps Optional array of past swaps to check, otherwise all relevant swaps will be fetched
|
|
219
|
+
* from the persistent storage
|
|
220
|
+
* @param noSave Whether to skip saving the swap changes in the persistent storage
|
|
221
|
+
*/
|
|
138
222
|
checkPastSwaps(pastSwaps?: D["Swap"][], noSave?: boolean): Promise<{
|
|
139
223
|
removeSwaps: D["Swap"][];
|
|
140
224
|
changedSwaps: D["Swap"][];
|
|
141
225
|
}>;
|
|
142
|
-
tick(swaps?: D["Swap"][]): Promise<void>;
|
|
143
|
-
saveSwapData(swap: D["Swap"]): Promise<void>;
|
|
144
|
-
removeSwapData(swap: D["Swap"]): Promise<void>;
|
|
145
226
|
/**
|
|
146
|
-
*
|
|
227
|
+
* Invokes {@link ISwap._tick} on all the known swaps
|
|
228
|
+
*
|
|
229
|
+
* @param swaps Optional array of swaps to invoke `_tick()` on, otherwise all relevant swaps will be fetched
|
|
230
|
+
* from the persistent storage
|
|
147
231
|
*/
|
|
148
|
-
|
|
232
|
+
tick(swaps?: D["Swap"][]): Promise<void>;
|
|
149
233
|
/**
|
|
150
234
|
* Returns the smart chain's native token used to pay for fees
|
|
235
|
+
* @internal
|
|
236
|
+
*/
|
|
237
|
+
_getNativeToken(): SCToken<T["ChainId"]>;
|
|
238
|
+
/**
|
|
239
|
+
* Saves the swap, if it is not initiated it is only saved to pending swaps
|
|
240
|
+
*
|
|
241
|
+
* @param swap Swap to save
|
|
242
|
+
*
|
|
243
|
+
* @internal
|
|
244
|
+
*/
|
|
245
|
+
_saveSwapData(swap: D["Swap"]): Promise<void>;
|
|
246
|
+
/**
|
|
247
|
+
* Removes the swap from the persistent storage and pending swaps
|
|
248
|
+
*
|
|
249
|
+
* @param swap Swap to remove
|
|
250
|
+
*
|
|
251
|
+
* @internal
|
|
252
|
+
*/
|
|
253
|
+
_removeSwapData(swap: D["Swap"]): Promise<void>;
|
|
254
|
+
/**
|
|
255
|
+
* Retrieves a swap by its ID from the pending swap mapping
|
|
256
|
+
*
|
|
257
|
+
* @param id
|
|
258
|
+
*
|
|
259
|
+
* @internal
|
|
151
260
|
*/
|
|
152
|
-
|
|
261
|
+
_getPendingSwap(id: string): D["Swap"] | null;
|
|
153
262
|
}
|
|
@@ -5,37 +5,41 @@ const events_1 = require("events");
|
|
|
5
5
|
const IntermediaryError_1 = require("../errors/IntermediaryError");
|
|
6
6
|
const Logger_1 = require("../utils/Logger");
|
|
7
7
|
/**
|
|
8
|
-
* Base abstract class for swap
|
|
9
|
-
*
|
|
8
|
+
* Base abstract class for swap handler implementations
|
|
9
|
+
*
|
|
10
|
+
* @category Swaps/Base
|
|
10
11
|
*/
|
|
11
12
|
class ISwapWrapper {
|
|
12
|
-
/**
|
|
13
|
-
* @param chainIdentifier
|
|
14
|
-
* @param unifiedStorage
|
|
15
|
-
* @param unifiedChainEvents
|
|
16
|
-
* @param chain
|
|
17
|
-
* @param prices Swap pricing handler
|
|
18
|
-
* @param tokens Chain specific token data
|
|
19
|
-
* @param options
|
|
20
|
-
* @param events Instance to use for emitting events
|
|
21
|
-
*/
|
|
22
13
|
constructor(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, prices, tokens, options, events) {
|
|
14
|
+
/**
|
|
15
|
+
* Logger instance
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
23
18
|
this.logger = (0, Logger_1.getLogger)(this.constructor.name + ": ");
|
|
19
|
+
/**
|
|
20
|
+
* In-memory mapping of pending (not initiated) swaps, utilizing weak references to automatically
|
|
21
|
+
* free memory when swaps are dereferenced in not initiated state
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
24
|
this.pendingSwaps = new Map();
|
|
25
|
+
/**
|
|
26
|
+
* Whether this wrapper is initialized (have to call {@link init} to initialize a wrapper)
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
25
29
|
this.isInitialized = false;
|
|
26
30
|
this.unifiedStorage = unifiedStorage;
|
|
27
31
|
this.unifiedChainEvents = unifiedChainEvents;
|
|
28
32
|
this.chainIdentifier = chainIdentifier;
|
|
29
|
-
this.
|
|
30
|
-
this.
|
|
33
|
+
this._chain = chain;
|
|
34
|
+
this._prices = prices;
|
|
31
35
|
this.events = events || new events_1.EventEmitter();
|
|
32
|
-
this.
|
|
33
|
-
this.
|
|
36
|
+
this._options = options;
|
|
37
|
+
this._tokens = {};
|
|
34
38
|
for (let tokenData of tokens) {
|
|
35
39
|
const chainData = tokenData.chains[chainIdentifier];
|
|
36
40
|
if (chainData == null)
|
|
37
41
|
continue;
|
|
38
|
-
this.
|
|
42
|
+
this._tokens[chainData.address] = {
|
|
39
43
|
chain: "SC",
|
|
40
44
|
chainId: this.chainIdentifier,
|
|
41
45
|
address: chainData.address,
|
|
@@ -49,13 +53,13 @@ class ISwapWrapper {
|
|
|
49
53
|
/**
|
|
50
54
|
* Pre-fetches swap price for a given swap
|
|
51
55
|
*
|
|
52
|
-
* @param amountData
|
|
53
|
-
* @param abortSignal
|
|
54
|
-
* @protected
|
|
56
|
+
* @param amountData Amount data
|
|
57
|
+
* @param abortSignal Abort signal
|
|
55
58
|
* @returns Price of the token in uSats (micro sats)
|
|
59
|
+
* @internal
|
|
56
60
|
*/
|
|
57
61
|
preFetchPrice(amountData, abortSignal) {
|
|
58
|
-
return this.
|
|
62
|
+
return this._prices.preFetchPrice(this.chainIdentifier, amountData.token, abortSignal).catch(e => {
|
|
59
63
|
this.logger.error("preFetchPrice.token(): Error: ", e);
|
|
60
64
|
return undefined;
|
|
61
65
|
});
|
|
@@ -63,30 +67,31 @@ class ISwapWrapper {
|
|
|
63
67
|
/**
|
|
64
68
|
* Pre-fetches bitcoin's USD price
|
|
65
69
|
*
|
|
66
|
-
* @param abortSignal
|
|
67
|
-
* @
|
|
70
|
+
* @param abortSignal Abort signal
|
|
71
|
+
* @internal
|
|
68
72
|
*/
|
|
69
73
|
preFetchUsdPrice(abortSignal) {
|
|
70
|
-
return this.
|
|
74
|
+
return this._prices.preFetchUsdPrice(abortSignal).catch(e => {
|
|
71
75
|
this.logger.error("preFetchPrice.usd(): Error: ", e);
|
|
72
76
|
return undefined;
|
|
73
77
|
});
|
|
74
78
|
}
|
|
75
79
|
/**
|
|
76
|
-
* Verifies returned
|
|
80
|
+
* Verifies returned price for swaps
|
|
77
81
|
*
|
|
78
82
|
* @param lpServiceData Service data for the service in question (TO_BTCLN, TO_BTC, etc.) of the given intermediary
|
|
79
|
-
* @param send Whether this is a send (
|
|
83
|
+
* @param send Whether this is a send (Smart chain -> Bitcoin) or receive (Bitcoin -> Smart chain) swap
|
|
80
84
|
* @param amountSats Amount in BTC
|
|
81
85
|
* @param amountToken Amount in token
|
|
82
86
|
* @param token Token used in the swap
|
|
83
87
|
* @param feeData Fee data as returned by the intermediary
|
|
84
|
-
* @param pricePrefetchPromise
|
|
85
|
-
* @param usdPricePrefetchPromise
|
|
86
|
-
* @param abortSignal
|
|
87
|
-
* @protected
|
|
88
|
+
* @param pricePrefetchPromise Optional price pre-fetch promise
|
|
89
|
+
* @param usdPricePrefetchPromise Optiona USD price pre-fetch promise
|
|
90
|
+
* @param abortSignal Abort signal
|
|
88
91
|
* @returns Price info object
|
|
89
92
|
* @throws {IntermediaryError} if the calculated fee is too high
|
|
93
|
+
*
|
|
94
|
+
* @internal
|
|
90
95
|
*/
|
|
91
96
|
async verifyReturnedPrice(lpServiceData, send, amountSats, amountToken, token, feeData, pricePrefetchPromise = Promise.resolve(undefined), usdPricePrefetchPromise = Promise.resolve(undefined), abortSignal) {
|
|
92
97
|
const swapBaseFee = BigInt(lpServiceData.swapBaseFee);
|
|
@@ -95,12 +100,12 @@ class ISwapWrapper {
|
|
|
95
100
|
amountToken = amountToken - feeData.networkFee;
|
|
96
101
|
const [isValidAmount, usdPrice] = await Promise.all([
|
|
97
102
|
send ?
|
|
98
|
-
this.
|
|
99
|
-
this.
|
|
103
|
+
this._prices.isValidAmountSend(this.chainIdentifier, amountSats, swapBaseFee, swapFeePPM, amountToken, token, abortSignal, await pricePrefetchPromise) :
|
|
104
|
+
this._prices.isValidAmountReceive(this.chainIdentifier, amountSats, swapBaseFee, swapFeePPM, amountToken, token, abortSignal, await pricePrefetchPromise),
|
|
100
105
|
usdPricePrefetchPromise.then(value => {
|
|
101
106
|
if (value != null)
|
|
102
107
|
return value;
|
|
103
|
-
return this.
|
|
108
|
+
return this._prices.preFetchUsdPrice(abortSignal);
|
|
104
109
|
})
|
|
105
110
|
]);
|
|
106
111
|
if (!isValidAmount.isValid)
|
|
@@ -108,8 +113,45 @@ class ISwapWrapper {
|
|
|
108
113
|
isValidAmount.realPriceUsdPerBitcoin = usdPrice;
|
|
109
114
|
return isValidAmount;
|
|
110
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* Starts the interval calling the {@link ISwap._tick} on all the known swaps in tick-enabled states
|
|
118
|
+
* @internal
|
|
119
|
+
*/
|
|
120
|
+
startTickInterval() {
|
|
121
|
+
if (this.tickSwapState == null || this.tickSwapState.length === 0)
|
|
122
|
+
return;
|
|
123
|
+
this.tickInterval = setInterval(() => {
|
|
124
|
+
this.tick();
|
|
125
|
+
}, 1000);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Runs checks on passed swaps, syncing their state from on-chain data
|
|
129
|
+
*
|
|
130
|
+
* @param pastSwaps Swaps to check
|
|
131
|
+
* @internal
|
|
132
|
+
*/
|
|
133
|
+
async _checkPastSwaps(pastSwaps) {
|
|
134
|
+
const changedSwaps = [];
|
|
135
|
+
const removeSwaps = [];
|
|
136
|
+
await Promise.all(pastSwaps.map((swap) => swap._sync(false).then(changed => {
|
|
137
|
+
if (swap.isQuoteExpired()) {
|
|
138
|
+
removeSwaps.push(swap);
|
|
139
|
+
this.logger.debug("_checkPastSwaps(): Removing expired swap: " + swap.getId());
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
if (changed)
|
|
143
|
+
changedSwaps.push(swap);
|
|
144
|
+
}
|
|
145
|
+
}).catch(e => this.logger.error("_checkPastSwaps(): Error when checking swap " + swap.getId() + ": ", e))));
|
|
146
|
+
return { changedSwaps, removeSwaps };
|
|
147
|
+
}
|
|
111
148
|
/**
|
|
112
149
|
* Initializes the swap wrapper, needs to be called before any other action can be taken
|
|
150
|
+
*
|
|
151
|
+
* @param noTimers Whether to skip scheduling a tick timer for the swaps, if the tick timer is not initiated
|
|
152
|
+
* the swap states depending on e.g. expiry can be out of sync with the actual expiration of the swap
|
|
153
|
+
* @param noCheckPastSwaps Whether to skip checking past swaps on initialization (by default all pending swaps
|
|
154
|
+
* are re-checked on init, and their state is synchronized from the on-chain data)
|
|
113
155
|
*/
|
|
114
156
|
async init(noTimers = false, noCheckPastSwaps = false) {
|
|
115
157
|
if (this.isInitialized)
|
|
@@ -123,7 +165,7 @@ class ISwapWrapper {
|
|
|
123
165
|
return Promise.resolve();
|
|
124
166
|
};
|
|
125
167
|
if (this.processEvent != null)
|
|
126
|
-
this.unifiedChainEvents.registerListener(this.TYPE, initListener, this.
|
|
168
|
+
this.unifiedChainEvents.registerListener(this.TYPE, initListener, this._swapDeserializer.bind(null, this));
|
|
127
169
|
await this.checkPastSwaps();
|
|
128
170
|
if (this.processEvent != null) {
|
|
129
171
|
//Process accumulated event queue
|
|
@@ -135,37 +177,32 @@ class ISwapWrapper {
|
|
|
135
177
|
}
|
|
136
178
|
}
|
|
137
179
|
if (this.processEvent != null)
|
|
138
|
-
this.unifiedChainEvents.registerListener(this.TYPE, this.processEvent.bind(this), this.
|
|
180
|
+
this.unifiedChainEvents.registerListener(this.TYPE, this.processEvent.bind(this), this._swapDeserializer.bind(null, this));
|
|
139
181
|
if (!noTimers)
|
|
140
182
|
this.startTickInterval();
|
|
141
183
|
// this.logger.info("init(): Swap wrapper initialized");
|
|
142
184
|
this.isInitialized = true;
|
|
143
185
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
const removeSwaps = [];
|
|
154
|
-
await Promise.all(pastSwaps.map((swap) => swap._sync(false).then(changed => {
|
|
155
|
-
if (swap.isQuoteExpired()) {
|
|
156
|
-
removeSwaps.push(swap);
|
|
157
|
-
this.logger.debug("_checkPastSwaps(): Removing expired swap: " + swap.getId());
|
|
158
|
-
}
|
|
159
|
-
else {
|
|
160
|
-
if (changed)
|
|
161
|
-
changedSwaps.push(swap);
|
|
162
|
-
}
|
|
163
|
-
}).catch(e => this.logger.error("_checkPastSwaps(): Error when checking swap " + swap.getId() + ": ", e))));
|
|
164
|
-
return { changedSwaps, removeSwaps };
|
|
186
|
+
/**
|
|
187
|
+
* Un-subscribes from event listeners on the smart chain, terminates the tick interval and stops this wrapper
|
|
188
|
+
*/
|
|
189
|
+
async stop() {
|
|
190
|
+
this.isInitialized = false;
|
|
191
|
+
this.unifiedChainEvents.unregisterListener(this.TYPE);
|
|
192
|
+
this.logger.info("stop(): Swap wrapper stopped");
|
|
193
|
+
if (this.tickInterval != null)
|
|
194
|
+
clearInterval(this.tickInterval);
|
|
165
195
|
}
|
|
196
|
+
/**
|
|
197
|
+
* Runs checks on all the known pending swaps, syncing their state from on-chain data
|
|
198
|
+
*
|
|
199
|
+
* @param pastSwaps Optional array of past swaps to check, otherwise all relevant swaps will be fetched
|
|
200
|
+
* from the persistent storage
|
|
201
|
+
* @param noSave Whether to skip saving the swap changes in the persistent storage
|
|
202
|
+
*/
|
|
166
203
|
async checkPastSwaps(pastSwaps, noSave) {
|
|
167
204
|
if (pastSwaps == null)
|
|
168
|
-
pastSwaps = await this.unifiedStorage.query([[{ key: "type", value: this.TYPE }, { key: "state", value: this.
|
|
205
|
+
pastSwaps = await this.unifiedStorage.query([[{ key: "type", value: this.TYPE }, { key: "state", value: this._pendingSwapStates }]], (val) => new this._swapDeserializer(this, val));
|
|
169
206
|
const { removeSwaps, changedSwaps } = await this._checkPastSwaps(pastSwaps);
|
|
170
207
|
if (!noSave) {
|
|
171
208
|
await this.unifiedStorage.removeAll(removeSwaps);
|
|
@@ -178,9 +215,15 @@ class ISwapWrapper {
|
|
|
178
215
|
changedSwaps
|
|
179
216
|
};
|
|
180
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* Invokes {@link ISwap._tick} on all the known swaps
|
|
220
|
+
*
|
|
221
|
+
* @param swaps Optional array of swaps to invoke `_tick()` on, otherwise all relevant swaps will be fetched
|
|
222
|
+
* from the persistent storage
|
|
223
|
+
*/
|
|
181
224
|
async tick(swaps) {
|
|
182
225
|
if (swaps == null)
|
|
183
|
-
swaps = await this.unifiedStorage.query([[{ key: "type", value: this.TYPE }, { key: "state", value: this.tickSwapState }]], (val) => new this.
|
|
226
|
+
swaps = await this.unifiedStorage.query([[{ key: "type", value: this.TYPE }, { key: "state", value: this.tickSwapState }]], (val) => new this._swapDeserializer(this, val));
|
|
184
227
|
for (let pendingSwap of this.pendingSwaps.values()) {
|
|
185
228
|
const value = pendingSwap.deref();
|
|
186
229
|
if (value != null)
|
|
@@ -190,7 +233,21 @@ class ISwapWrapper {
|
|
|
190
233
|
value._tick(true);
|
|
191
234
|
});
|
|
192
235
|
}
|
|
193
|
-
|
|
236
|
+
/**
|
|
237
|
+
* Returns the smart chain's native token used to pay for fees
|
|
238
|
+
* @internal
|
|
239
|
+
*/
|
|
240
|
+
_getNativeToken() {
|
|
241
|
+
return this._tokens[this._chain.getNativeCurrencyAddress()];
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Saves the swap, if it is not initiated it is only saved to pending swaps
|
|
245
|
+
*
|
|
246
|
+
* @param swap Swap to save
|
|
247
|
+
*
|
|
248
|
+
* @internal
|
|
249
|
+
*/
|
|
250
|
+
_saveSwapData(swap) {
|
|
194
251
|
if (!swap.isInitiated()) {
|
|
195
252
|
this.logger.debug("saveSwapData(): Swap " + swap.getId() + " not initiated, saving to pending swaps");
|
|
196
253
|
this.pendingSwaps.set(swap.getId(), new WeakRef(swap));
|
|
@@ -201,27 +258,28 @@ class ISwapWrapper {
|
|
|
201
258
|
}
|
|
202
259
|
return this.unifiedStorage.save(swap);
|
|
203
260
|
}
|
|
204
|
-
|
|
261
|
+
/**
|
|
262
|
+
* Removes the swap from the persistent storage and pending swaps
|
|
263
|
+
*
|
|
264
|
+
* @param swap Swap to remove
|
|
265
|
+
*
|
|
266
|
+
* @internal
|
|
267
|
+
*/
|
|
268
|
+
_removeSwapData(swap) {
|
|
205
269
|
this.pendingSwaps.delete(swap.getId());
|
|
206
270
|
if (!swap.isInitiated())
|
|
207
271
|
return Promise.resolve();
|
|
208
272
|
return this.unifiedStorage.remove(swap);
|
|
209
273
|
}
|
|
210
274
|
/**
|
|
211
|
-
*
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
this.logger.info("stop(): Swap wrapper stopped");
|
|
217
|
-
if (this.tickInterval != null)
|
|
218
|
-
clearInterval(this.tickInterval);
|
|
219
|
-
}
|
|
220
|
-
/**
|
|
221
|
-
* Returns the smart chain's native token used to pay for fees
|
|
275
|
+
* Retrieves a swap by its ID from the pending swap mapping
|
|
276
|
+
*
|
|
277
|
+
* @param id
|
|
278
|
+
*
|
|
279
|
+
* @internal
|
|
222
280
|
*/
|
|
223
|
-
|
|
224
|
-
return this.
|
|
281
|
+
_getPendingSwap(id) {
|
|
282
|
+
return this.pendingSwaps.get(id)?.deref() ?? null;
|
|
225
283
|
}
|
|
226
284
|
}
|
|
227
285
|
exports.ISwapWrapper = ISwapWrapper;
|