@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
package/src/swapper/Swapper.ts
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
import {ISwapPrice} from "../prices/abstract/ISwapPrice";
|
|
2
2
|
import {
|
|
3
|
-
BitcoinNetwork,
|
|
3
|
+
BitcoinNetwork, BitcoinRpc, BitcoinRpcWithAddressIndex, BtcBlock,
|
|
4
4
|
BtcRelay,
|
|
5
5
|
ChainData,
|
|
6
6
|
ChainSwapType,
|
|
7
|
-
ChainType,
|
|
7
|
+
ChainType, LightningNetworkApi,
|
|
8
8
|
Messenger,
|
|
9
9
|
RelaySynchronizer
|
|
10
10
|
} from "@atomiqlabs/base";
|
|
11
11
|
import {
|
|
12
|
-
InvoiceCreateService,
|
|
13
|
-
isInvoiceCreateService,
|
|
14
12
|
ToBTCLNOptions,
|
|
15
13
|
ToBTCLNWrapper
|
|
16
14
|
} from "../swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper";
|
|
@@ -54,7 +52,7 @@ import {UserError} from "../errors/UserError";
|
|
|
54
52
|
import {SwapAmountType} from "../enums/SwapAmountType";
|
|
55
53
|
import {IClaimableSwap} from "../swaps/IClaimableSwap";
|
|
56
54
|
import {correctClock} from "../utils/AutomaticClockDriftCorrection";
|
|
57
|
-
import {isSwapType, SwapTypeMapping} from "../utils/SwapUtils";
|
|
55
|
+
import {isSwapType, SwapProtocolInfo, SwapTypeMapping} from "../utils/SwapUtils";
|
|
58
56
|
import {IndexedDBUnifiedStorage} from "../storage-browser/IndexedDBUnifiedStorage";
|
|
59
57
|
import {TokenAmount, toTokenAmount} from "../types/TokenAmount";
|
|
60
58
|
import {BitcoinTokens, BtcToken, isBtcToken, isSCToken, SCToken, Token} from "../types/Token";
|
|
@@ -64,32 +62,104 @@ import {isLNURLWithdraw, LNURLWithdraw} from "../types/lnurl/LNURLWithdraw";
|
|
|
64
62
|
import {isLNURLPay, LNURLPay} from "../types/lnurl/LNURLPay";
|
|
65
63
|
import {tryWithRetries} from "../utils/RetryUtils";
|
|
66
64
|
import {NotNever} from "../utils/TypeUtils";
|
|
67
|
-
import {
|
|
65
|
+
import {IEscrowSwap} from "../swaps/escrow_swaps/IEscrowSwap";
|
|
66
|
+
import {LightningInvoiceCreateService, isLightningInvoiceCreateService} from "../types/wallets/LightningInvoiceCreateService";
|
|
68
67
|
|
|
69
68
|
/**
|
|
70
69
|
* Configuration options for the Swapper
|
|
71
70
|
* @category Core
|
|
72
71
|
*/
|
|
73
72
|
export type SwapperOptions = {
|
|
73
|
+
/**
|
|
74
|
+
* Manual override for the intermediary (LP) URLs for the SDK to use, by default these are fetched automatically
|
|
75
|
+
* from the registry
|
|
76
|
+
*/
|
|
74
77
|
intermediaryUrl?: string | string[],
|
|
78
|
+
/**
|
|
79
|
+
* Registry URL for where to look for active intermediary (LP) endpoint URLs
|
|
80
|
+
*/
|
|
75
81
|
registryUrl?: string,
|
|
76
82
|
|
|
83
|
+
/**
|
|
84
|
+
* Bitcoin network to use for the swaps,
|
|
85
|
+
*/
|
|
77
86
|
bitcoinNetwork?: BitcoinNetwork,
|
|
78
87
|
|
|
88
|
+
/**
|
|
89
|
+
* Timeout (in milliseconds) for HTTP GET requests done by the SDK
|
|
90
|
+
*/
|
|
79
91
|
getRequestTimeout?: number,
|
|
92
|
+
/**
|
|
93
|
+
* Timeout (in milliseconds) for HTTP POST requests done by the SDK
|
|
94
|
+
*/
|
|
80
95
|
postRequestTimeout?: number,
|
|
96
|
+
/**
|
|
97
|
+
* Additional parameters to be sent to the intermediaries (LPs), when requesting quotes from them
|
|
98
|
+
*/
|
|
81
99
|
defaultAdditionalParameters?: {[key: string]: any},
|
|
82
|
-
|
|
100
|
+
/**
|
|
101
|
+
* Optional name prefix to use when creating a swap storage, you can use this to create separate storage
|
|
102
|
+
* instances that don't overlap.
|
|
103
|
+
*/
|
|
104
|
+
storagePrefix?: string,
|
|
105
|
+
/**
|
|
106
|
+
* Sets the default intermediary (LP) to use for the trusted gas swaps, if not set the SDK uses a default one
|
|
107
|
+
*/
|
|
83
108
|
defaultTrustedIntermediaryUrl?: string,
|
|
84
109
|
|
|
85
|
-
|
|
110
|
+
/**
|
|
111
|
+
* A function callback to retrieve a specific named storage container for swap persistency. If not present, the
|
|
112
|
+
* default IndexedDB storage adapter is used. When you use the SDK in non-browser based environments you need to
|
|
113
|
+
* provide this callback such that the SDK is able to use a custom storage adapter.
|
|
114
|
+
*
|
|
115
|
+
* @param storageName Name of the container to retrieve
|
|
116
|
+
*/
|
|
117
|
+
swapStorage?: (storageName: string) => IUnifiedStorage<UnifiedSwapStorageIndexes, UnifiedSwapStorageCompositeIndexes>,
|
|
86
118
|
|
|
119
|
+
/**
|
|
120
|
+
* By setting this flag, the swapper doesn't schedule automatic tick timers. To make sure the swap states are
|
|
121
|
+
* properly updated (e.g. the expired swaps properly move to the expired state), you should call the
|
|
122
|
+
* {@link Swapper._syncSwaps} function periodically. This flag should be set when you run an environment that
|
|
123
|
+
* doesn't support long-running timers - e.g. serverless environments like Azure Function Apps or AWS Lambda
|
|
124
|
+
*/
|
|
87
125
|
noTimers?: boolean,
|
|
126
|
+
/**
|
|
127
|
+
* By setting this flag, the swapper doesn't subscribe to on-chain events. To make sure the swap states are
|
|
128
|
+
* properly updated you should call the {@link Swapper._syncSwaps} function periodically. This flag should be
|
|
129
|
+
* set when you run an environment that doesn't support long-running timers and websocket connections - e.g.
|
|
130
|
+
* serverless environments like Azure Function Apps or AWS Lambda
|
|
131
|
+
*/
|
|
88
132
|
noEvents?: boolean,
|
|
133
|
+
/**
|
|
134
|
+
* By setting this flag, the swap objects will not be cached in the SDK and instead will always be loaded from
|
|
135
|
+
* the persistent storage. By default, the SDK uses a `WeakRef` mapping of swaps, to ensure that when the same
|
|
136
|
+
* swap is loaded concurrently, it returns the same object reference to both, making the changes on the object
|
|
137
|
+
* atomic. This flag should be set to `true` when running in an environment where multiple instances of the SDK
|
|
138
|
+
* access the same swap database - e.g. serverless environments like Azure Function Apps or AWS Lambda
|
|
139
|
+
*/
|
|
89
140
|
noSwapCache?: boolean,
|
|
141
|
+
/**
|
|
142
|
+
* Skip checking past swaps when the swapper is initiated with {@link Swapper.init}, you can call the
|
|
143
|
+
* {@link Swapper._syncSwaps} function later, to check the swaps. By default, the SDK checks the state
|
|
144
|
+
* of all the known swaps during init.
|
|
145
|
+
*/
|
|
90
146
|
dontCheckPastSwaps?: boolean,
|
|
147
|
+
/**
|
|
148
|
+
* Skip fetching the LPs when the swapper is initiated with {@link Swapper.init}, this means the list of available
|
|
149
|
+
* tokens and swap limits won't be available immediately. LPs will be fetched automatically later, when a swap
|
|
150
|
+
* is requested
|
|
151
|
+
*/
|
|
91
152
|
dontFetchLPs?: boolean,
|
|
92
|
-
|
|
153
|
+
/**
|
|
154
|
+
* By setting this flag the SDK persists all created swaps. By default, the SDK only saves and persists swaps that
|
|
155
|
+
* are considered initiated, i.e. when `commit()`, `execute()` or `waitTillPayment` is called (or their respective
|
|
156
|
+
* txs... prefixed variations).
|
|
157
|
+
*/
|
|
158
|
+
saveUninitializedSwaps?: boolean,
|
|
159
|
+
/**
|
|
160
|
+
* Automatically checks system time on initialize, if the system time drifts too far from the actual time
|
|
161
|
+
* (as checked from multiple server sources) it adjusts the `Date.now()` function to return proper actual time.
|
|
162
|
+
*/
|
|
93
163
|
automaticClockDriftCorrection?: boolean
|
|
94
164
|
};
|
|
95
165
|
|
|
@@ -116,8 +186,8 @@ type ChainSpecificData<T extends ChainType> = {
|
|
|
116
186
|
swapContract: T["Contract"],
|
|
117
187
|
spvVaultContract: T["SpvVaultContract"],
|
|
118
188
|
chainInterface: T["ChainInterface"],
|
|
119
|
-
btcRelay: BtcRelay<any, T["TX"],
|
|
120
|
-
synchronizer: RelaySynchronizer<any, T["TX"],
|
|
189
|
+
btcRelay: BtcRelay<any, T["TX"], BtcBlock, T["Signer"]>,
|
|
190
|
+
synchronizer: RelaySynchronizer<any, T["TX"], BtcBlock>,
|
|
121
191
|
unifiedChainEvents: UnifiedSwapEventListener<T>,
|
|
122
192
|
unifiedSwapStorage: UnifiedSwapStorage<T>,
|
|
123
193
|
reviver: (val: any) => ISwap<T>
|
|
@@ -152,7 +222,8 @@ export type SupportsSwapType<
|
|
|
152
222
|
NotNever<C["Contract"]>;
|
|
153
223
|
|
|
154
224
|
/**
|
|
155
|
-
* Core orchestrator for all swap operations
|
|
225
|
+
* Core orchestrator for all atomiq swap operations
|
|
226
|
+
*
|
|
156
227
|
* @category Core
|
|
157
228
|
*/
|
|
158
229
|
export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
@@ -161,37 +232,71 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
161
232
|
swapState: [ISwap],
|
|
162
233
|
swapLimitsChanged: []
|
|
163
234
|
}> {
|
|
164
|
-
|
|
165
|
-
|
|
235
|
+
private readonly logger = getLogger(this.constructor.name+": ");
|
|
236
|
+
private readonly swapStateListener: (swap: ISwap) => void;
|
|
166
237
|
|
|
167
238
|
private defaultTrustedIntermediary?: Intermediary;
|
|
168
|
-
private readonly
|
|
239
|
+
private readonly bitcoinNetwork: BitcoinNetwork;
|
|
169
240
|
private readonly options: SwapperOptions;
|
|
241
|
+
/**
|
|
242
|
+
* Data propagation layer used for broadcasting messages to watchtowers
|
|
243
|
+
*/
|
|
244
|
+
private readonly messenger: Messenger;
|
|
170
245
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
readonly
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
readonly
|
|
181
|
-
|
|
182
|
-
|
|
246
|
+
/**
|
|
247
|
+
* A dictionary of smart chains used by the SDK
|
|
248
|
+
* @internal
|
|
249
|
+
*/
|
|
250
|
+
readonly _chains: MultiChainData<T>;
|
|
251
|
+
/**
|
|
252
|
+
* Bitcoin RPC for fetching bitcoin chain data
|
|
253
|
+
* @internal
|
|
254
|
+
*/
|
|
255
|
+
readonly _bitcoinRpc: BitcoinRpcWithAddressIndex<any>;
|
|
256
|
+
/**
|
|
257
|
+
* Bitcoin network specification
|
|
258
|
+
* @internal
|
|
259
|
+
*/
|
|
260
|
+
readonly _btcNetwork: BTC_NETWORK;
|
|
261
|
+
/**
|
|
262
|
+
* Token data indexed by chain identifier and token addresses
|
|
263
|
+
* @internal
|
|
264
|
+
*/
|
|
265
|
+
readonly _tokens: {
|
|
183
266
|
[chainId: string]: {
|
|
184
267
|
[tokenAddress: string]: SCToken
|
|
185
268
|
}
|
|
186
269
|
};
|
|
187
|
-
|
|
270
|
+
/**
|
|
271
|
+
* Token data indexed by chain identifier and token tickers
|
|
272
|
+
* @internal
|
|
273
|
+
*/
|
|
274
|
+
readonly _tokensByTicker: {
|
|
188
275
|
[chainId: string]: {
|
|
189
276
|
[tokenTicker: string]: SCToken
|
|
190
277
|
}
|
|
191
278
|
};
|
|
192
279
|
|
|
280
|
+
/**
|
|
281
|
+
* Pricing API used by the SDK
|
|
282
|
+
*/
|
|
283
|
+
readonly prices: ISwapPrice<T>;
|
|
284
|
+
/**
|
|
285
|
+
* Intermediary discovery instance
|
|
286
|
+
*/
|
|
287
|
+
readonly intermediaryDiscovery: IntermediaryDiscovery;
|
|
288
|
+
/**
|
|
289
|
+
* Miscellaneous utility functions
|
|
290
|
+
*/
|
|
291
|
+
readonly Utils: SwapperUtils<T>;
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* @internal
|
|
295
|
+
*/
|
|
193
296
|
constructor(
|
|
194
|
-
bitcoinRpc:
|
|
297
|
+
bitcoinRpc: BitcoinRpcWithAddressIndex<any>,
|
|
298
|
+
lightningApi: LightningNetworkApi,
|
|
299
|
+
bitcoinSynchronizer: (btcRelay: BtcRelay<any, any, any>) => RelaySynchronizer<any, any, any>,
|
|
195
300
|
chainsData: CtorMultiChainData<T>,
|
|
196
301
|
pricing: ISwapPrice<T>,
|
|
197
302
|
tokens: WrapperCtorTokens<T>,
|
|
@@ -207,8 +312,8 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
207
312
|
|
|
208
313
|
this.options = options;
|
|
209
314
|
|
|
210
|
-
this.
|
|
211
|
-
this.
|
|
315
|
+
this.bitcoinNetwork = options.bitcoinNetwork;
|
|
316
|
+
this._btcNetwork = options.bitcoinNetwork===BitcoinNetwork.MAINNET ? NETWORK :
|
|
212
317
|
(options.bitcoinNetwork===BitcoinNetwork.TESTNET || options.bitcoinNetwork===BitcoinNetwork.TESTNET4) ? TEST_NETWORK : {
|
|
213
318
|
bech32: 'bcrt',
|
|
214
319
|
pubKeyHash: 111,
|
|
@@ -218,18 +323,17 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
218
323
|
this.Utils = new SwapperUtils(this);
|
|
219
324
|
|
|
220
325
|
this.prices = pricing;
|
|
221
|
-
this.
|
|
222
|
-
this.mempoolApi = bitcoinRpc.api;
|
|
326
|
+
this._bitcoinRpc = bitcoinRpc;
|
|
223
327
|
this.messenger = messenger;
|
|
224
328
|
|
|
225
|
-
this.
|
|
226
|
-
this.
|
|
329
|
+
this._tokens = {};
|
|
330
|
+
this._tokensByTicker = {};
|
|
227
331
|
for(let tokenData of tokens) {
|
|
228
332
|
for(let chainId in tokenData.chains) {
|
|
229
333
|
const chainData = tokenData.chains[chainId]!;
|
|
230
|
-
this.
|
|
231
|
-
this.
|
|
232
|
-
this.
|
|
334
|
+
this._tokens[chainId] ??= {};
|
|
335
|
+
this._tokensByTicker[chainId] ??= {};
|
|
336
|
+
this._tokens[chainId][chainData.address] = this._tokensByTicker[chainId][tokenData.ticker] = {
|
|
233
337
|
chain: "SC",
|
|
234
338
|
chainId,
|
|
235
339
|
ticker: tokenData.ticker,
|
|
@@ -245,12 +349,12 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
245
349
|
this.emit("swapState", swap);
|
|
246
350
|
};
|
|
247
351
|
|
|
248
|
-
this.
|
|
352
|
+
this._chains = objectMap<CtorMultiChainData<T>, MultiChainData<T>>(chainsData, <InputKey extends keyof CtorMultiChainData<T>>(chainData: CtorMultiChainData<T>[InputKey], key: string) => {
|
|
249
353
|
const {
|
|
250
354
|
swapContract, chainEvents, btcRelay,
|
|
251
355
|
chainInterface, spvVaultContract, spvVaultWithdrawalDataConstructor
|
|
252
356
|
} = chainData;
|
|
253
|
-
const synchronizer =
|
|
357
|
+
const synchronizer = bitcoinSynchronizer(btcRelay);
|
|
254
358
|
|
|
255
359
|
const storageHandler = swapStorage(storagePrefix + chainData.chainId);
|
|
256
360
|
const unifiedSwapStorage = new UnifiedSwapStorage<T[InputKey]>(storageHandler, this.options.noSwapCache);
|
|
@@ -281,11 +385,11 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
281
385
|
pricing,
|
|
282
386
|
tokens,
|
|
283
387
|
chainData.swapDataConstructor,
|
|
284
|
-
this.
|
|
388
|
+
this._bitcoinRpc,
|
|
285
389
|
{
|
|
286
390
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
287
391
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
288
|
-
bitcoinNetwork: this.
|
|
392
|
+
bitcoinNetwork: this._btcNetwork
|
|
289
393
|
}
|
|
290
394
|
);
|
|
291
395
|
wrappers[SwapType.FROM_BTCLN] = new FromBTCLNWrapper<T[InputKey]>(
|
|
@@ -297,11 +401,11 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
297
401
|
pricing,
|
|
298
402
|
tokens,
|
|
299
403
|
chainData.swapDataConstructor,
|
|
300
|
-
|
|
404
|
+
lightningApi,
|
|
301
405
|
{
|
|
302
406
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
303
407
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
304
|
-
unsafeSkipLnNodeCheck: this.
|
|
408
|
+
unsafeSkipLnNodeCheck: this.bitcoinNetwork===BitcoinNetwork.TESTNET4 || this.bitcoinNetwork===BitcoinNetwork.REGTEST
|
|
305
409
|
}
|
|
306
410
|
);
|
|
307
411
|
wrappers[SwapType.FROM_BTC] = new FromBTCWrapper<T[InputKey]>(
|
|
@@ -315,11 +419,11 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
315
419
|
chainData.swapDataConstructor,
|
|
316
420
|
btcRelay,
|
|
317
421
|
synchronizer,
|
|
318
|
-
this.
|
|
422
|
+
this._bitcoinRpc,
|
|
319
423
|
{
|
|
320
424
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
321
425
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
322
|
-
bitcoinNetwork: this.
|
|
426
|
+
bitcoinNetwork: this._btcNetwork
|
|
323
427
|
}
|
|
324
428
|
);
|
|
325
429
|
wrappers[SwapType.TRUSTED_FROM_BTCLN] = new LnForGasWrapper<T[InputKey]>(
|
|
@@ -345,7 +449,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
345
449
|
{
|
|
346
450
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
347
451
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
348
|
-
bitcoinNetwork: this.
|
|
452
|
+
bitcoinNetwork: this._btcNetwork
|
|
349
453
|
}
|
|
350
454
|
);
|
|
351
455
|
|
|
@@ -365,7 +469,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
365
469
|
{
|
|
366
470
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
367
471
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
368
|
-
bitcoinNetwork: this.
|
|
472
|
+
bitcoinNetwork: this._btcNetwork
|
|
369
473
|
}
|
|
370
474
|
);
|
|
371
475
|
}
|
|
@@ -380,12 +484,12 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
380
484
|
pricing,
|
|
381
485
|
tokens,
|
|
382
486
|
chainData.swapDataConstructor,
|
|
383
|
-
|
|
487
|
+
lightningApi,
|
|
384
488
|
this.messenger,
|
|
385
489
|
{
|
|
386
490
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
387
491
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
388
|
-
unsafeSkipLnNodeCheck: this.
|
|
492
|
+
unsafeSkipLnNodeCheck: this.bitcoinNetwork===BitcoinNetwork.TESTNET4 || this.bitcoinNetwork===BitcoinNetwork.REGTEST
|
|
389
493
|
}
|
|
390
494
|
);
|
|
391
495
|
}
|
|
@@ -393,9 +497,9 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
393
497
|
Object.keys(wrappers).forEach(key => wrappers[key].events.on("swapState", this.swapStateListener));
|
|
394
498
|
|
|
395
499
|
const reviver = (val: any) => {
|
|
396
|
-
const wrapper = wrappers[val.type];
|
|
500
|
+
const wrapper: ISwapWrapper<any, any> = wrappers[val.type];
|
|
397
501
|
if(wrapper==null) return null;
|
|
398
|
-
return new wrapper.
|
|
502
|
+
return new wrapper._swapDeserializer(wrapper, val);
|
|
399
503
|
};
|
|
400
504
|
|
|
401
505
|
return {
|
|
@@ -470,7 +574,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
470
574
|
}
|
|
471
575
|
|
|
472
576
|
const chainPromises = [];
|
|
473
|
-
for(let chainIdentifier in this.
|
|
577
|
+
for(let chainIdentifier in this._chains) {
|
|
474
578
|
chainPromises.push((async() => {
|
|
475
579
|
const {
|
|
476
580
|
swapContract,
|
|
@@ -478,7 +582,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
478
582
|
unifiedSwapStorage,
|
|
479
583
|
wrappers,
|
|
480
584
|
reviver
|
|
481
|
-
} = this.
|
|
585
|
+
} = this._chains[chainIdentifier];
|
|
482
586
|
await swapContract.start();
|
|
483
587
|
this.logger.debug("init(): Intialized swap contract: "+chainIdentifier);
|
|
484
588
|
|
|
@@ -486,8 +590,8 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
486
590
|
if(unifiedSwapStorage.storage instanceof IndexedDBUnifiedStorage) {
|
|
487
591
|
//Try to migrate the data here
|
|
488
592
|
const storagePrefix = chainIdentifier==="SOLANA" ?
|
|
489
|
-
"SOLv4-"+this.
|
|
490
|
-
"atomiqsdk-"+this.
|
|
593
|
+
"SOLv4-"+this.bitcoinNetwork+"-Swaps-" :
|
|
594
|
+
"atomiqsdk-"+this.bitcoinNetwork+chainIdentifier+"-Swaps-";
|
|
491
595
|
await unifiedSwapStorage.storage.tryMigrate(
|
|
492
596
|
[
|
|
493
597
|
[storagePrefix+"FromBTC", SwapType.FROM_BTC],
|
|
@@ -497,9 +601,9 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
497
601
|
],
|
|
498
602
|
(obj: any) => {
|
|
499
603
|
const swap = reviver(obj);
|
|
500
|
-
if(swap.
|
|
604
|
+
if(swap._randomNonce==null) {
|
|
501
605
|
const oldIdentifierHash = swap.getId();
|
|
502
|
-
swap.
|
|
606
|
+
swap._randomNonce = randomBytes(16).toString("hex");
|
|
503
607
|
const newIdentifierHash = swap.getId();
|
|
504
608
|
this.logger.info("init(): Found older swap version without randomNonce, replacing, old hash: "+oldIdentifierHash+
|
|
505
609
|
" new hash: "+newIdentifierHash);
|
|
@@ -553,11 +657,11 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
553
657
|
*/
|
|
554
658
|
async stop() {
|
|
555
659
|
if(this.initPromise) await this.initPromise;
|
|
556
|
-
for(let chainIdentifier in this.
|
|
660
|
+
for(let chainIdentifier in this._chains) {
|
|
557
661
|
const {
|
|
558
662
|
wrappers,
|
|
559
663
|
unifiedChainEvents
|
|
560
|
-
} = this.
|
|
664
|
+
} = this._chains[chainIdentifier];
|
|
561
665
|
for(let key in wrappers) {
|
|
562
666
|
const wrapper = wrappers[key as unknown as SwapType];
|
|
563
667
|
wrapper.events.removeListener("swapState", this.swapStateListener);
|
|
@@ -592,7 +696,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
592
696
|
maxWaitTimeMS: number = 2000
|
|
593
697
|
): Promise<S> {
|
|
594
698
|
if(!this.initialized) throw new Error("Swapper not initialized, init first with swapper.init()!");
|
|
595
|
-
if(this.
|
|
699
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
596
700
|
let candidates: Intermediary[];
|
|
597
701
|
|
|
598
702
|
const inBtc: boolean = swapType===SwapType.TO_BTCLN || swapType===SwapType.TO_BTC ? !amountData.exactIn : amountData.exactIn;
|
|
@@ -632,7 +736,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
632
736
|
|
|
633
737
|
const abortController = new AbortController();
|
|
634
738
|
this.logger.debug("createSwap() Swap candidates: ", candidates.map(lp => lp.url).join());
|
|
635
|
-
const quotePromises: {quote: Promise<S>, intermediary: Intermediary}[] = await create(candidates, abortController.signal, this.
|
|
739
|
+
const quotePromises: {quote: Promise<S>, intermediary: Intermediary}[] = await create(candidates, abortController.signal, this._chains[chainIdentifier]);
|
|
636
740
|
|
|
637
741
|
const promiseAll = new Promise<{
|
|
638
742
|
quote: S,
|
|
@@ -743,16 +847,16 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
743
847
|
}
|
|
744
848
|
|
|
745
849
|
/**
|
|
746
|
-
* Creates
|
|
850
|
+
* Creates Smart chain -> Bitcoin ({@link SwapType.TO_BTC}) swap
|
|
747
851
|
*
|
|
748
|
-
* @param chainIdentifier
|
|
749
|
-
* @param signer
|
|
750
|
-
* @param tokenAddress
|
|
751
|
-
* @param address
|
|
752
|
-
* @param amount
|
|
753
|
-
* @param exactIn
|
|
754
|
-
* @param additionalParams
|
|
755
|
-
* @param options
|
|
852
|
+
* @param chainIdentifier Chain identifier string of the source smart chain
|
|
853
|
+
* @param signer Signer's address on the source chain
|
|
854
|
+
* @param tokenAddress Token address to pay with
|
|
855
|
+
* @param address Recipient's bitcoin address
|
|
856
|
+
* @param amount Amount to send in token based units (if `exactIn=true`) or receive in satoshis (if `exactIn=false`)
|
|
857
|
+
* @param exactIn Whether to use exact in instead of exact out
|
|
858
|
+
* @param additionalParams Additional parameters sent to the LP when creating the swap
|
|
859
|
+
* @param options Additional options for the swap
|
|
756
860
|
*/
|
|
757
861
|
createToBTCSwap<ChainIdentifier extends ChainIds<T>>(
|
|
758
862
|
chainIdentifier: ChainIdentifier,
|
|
@@ -764,13 +868,13 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
764
868
|
additionalParams: Record<string, any> | undefined = this.options.defaultAdditionalParameters,
|
|
765
869
|
options?: ToBTCOptions
|
|
766
870
|
): Promise<ToBTCSwap<T[ChainIdentifier]>> {
|
|
767
|
-
if(this.
|
|
871
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
768
872
|
if(address.startsWith("bitcoin:")) {
|
|
769
873
|
address = address.substring(8).split("?")[0];
|
|
770
874
|
}
|
|
771
875
|
if(!this.Utils.isValidBitcoinAddress(address)) throw new Error("Invalid bitcoin address");
|
|
772
|
-
if(!this.
|
|
773
|
-
signer = this.
|
|
876
|
+
if(!this._chains[chainIdentifier].chainInterface.isValidAddress(signer, true)) throw new Error("Invalid "+chainIdentifier+" address");
|
|
877
|
+
signer = this._chains[chainIdentifier].chainInterface.normalizeAddress(signer);
|
|
774
878
|
options ??= {};
|
|
775
879
|
options.confirmationTarget ??= 3;
|
|
776
880
|
options.confirmations ??= 2;
|
|
@@ -796,14 +900,15 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
796
900
|
}
|
|
797
901
|
|
|
798
902
|
/**
|
|
799
|
-
* Creates
|
|
903
|
+
* Creates Smart chain -> Bitcoin Lightning ({@link SwapType.TO_BTCLN}) swap
|
|
800
904
|
*
|
|
801
|
-
* @param chainIdentifier
|
|
802
|
-
* @param signer
|
|
803
|
-
* @param tokenAddress
|
|
804
|
-
* @param paymentRequest
|
|
805
|
-
*
|
|
806
|
-
* @param
|
|
905
|
+
* @param chainIdentifier Chain identifier string of the source smart chain
|
|
906
|
+
* @param signer Signer's address on the source chain
|
|
907
|
+
* @param tokenAddress Token address to pay with
|
|
908
|
+
* @param paymentRequest BOLT11 lightning network invoice to be paid (needs to have a fixed amount), and the swap
|
|
909
|
+
* amount is taken from this fixed amount, hence only exact output swaps are supported
|
|
910
|
+
* @param additionalParams Additional parameters sent to the LP when creating the swap
|
|
911
|
+
* @param options Additional options for the swap
|
|
807
912
|
*/
|
|
808
913
|
async createToBTCLNSwap<ChainIdentifier extends ChainIds<T>>(
|
|
809
914
|
chainIdentifier: ChainIdentifier,
|
|
@@ -813,12 +918,12 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
813
918
|
additionalParams: Record<string, any> | undefined = this.options.defaultAdditionalParameters,
|
|
814
919
|
options?: ToBTCLNOptions
|
|
815
920
|
): Promise<ToBTCLNSwap<T[ChainIdentifier]>> {
|
|
816
|
-
if(this.
|
|
921
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
817
922
|
options ??= {};
|
|
818
923
|
if(paymentRequest.startsWith("lightning:")) paymentRequest = paymentRequest.substring(10);
|
|
819
924
|
if(!this.Utils.isValidLightningInvoice(paymentRequest)) throw new Error("Invalid lightning network invoice");
|
|
820
|
-
if(!this.
|
|
821
|
-
signer = this.
|
|
925
|
+
if(!this._chains[chainIdentifier].chainInterface.isValidAddress(signer, true)) throw new Error("Invalid "+chainIdentifier+" address");
|
|
926
|
+
signer = this._chains[chainIdentifier].chainInterface.normalizeAddress(signer);
|
|
822
927
|
const parsedPR = bolt11Decode(paymentRequest);
|
|
823
928
|
if(parsedPR.millisatoshis==null) throw new Error("Invalid lightning network invoice, no msat value field!");
|
|
824
929
|
const amountData = {
|
|
@@ -844,16 +949,16 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
844
949
|
}
|
|
845
950
|
|
|
846
951
|
/**
|
|
847
|
-
* Creates
|
|
952
|
+
* Creates Smart chain -> Bitcoin Lightning ({@link SwapType.TO_BTCLN}) swap via LNURL-pay link
|
|
848
953
|
*
|
|
849
|
-
* @param chainIdentifier
|
|
850
|
-
* @param signer
|
|
851
|
-
* @param tokenAddress
|
|
852
|
-
* @param lnurlPay
|
|
853
|
-
* @param amount
|
|
854
|
-
* @param exactIn
|
|
855
|
-
* @param additionalParams
|
|
856
|
-
* @param options
|
|
954
|
+
* @param chainIdentifier Chain identifier string of the source smart chain
|
|
955
|
+
* @param signer Signer's address on the source chain
|
|
956
|
+
* @param tokenAddress Token address to pay with
|
|
957
|
+
* @param lnurlPay LNURL-pay link to use for the payment
|
|
958
|
+
* @param amount Amount to send in token based units (if `exactIn=true`) or receive in satoshis (if `exactIn=false`)
|
|
959
|
+
* @param exactIn Whether to do an exact in swap instead of exact out
|
|
960
|
+
* @param additionalParams Additional parameters sent to the LP when creating the swap
|
|
961
|
+
* @param options Additional options for the swap
|
|
857
962
|
*/
|
|
858
963
|
async createToBTCLNSwapViaLNURL<ChainIdentifier extends ChainIds<T>>(
|
|
859
964
|
chainIdentifier: ChainIdentifier,
|
|
@@ -865,10 +970,10 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
865
970
|
additionalParams: Record<string, any> | undefined = this.options.defaultAdditionalParameters,
|
|
866
971
|
options?: ToBTCLNOptions & {comment?: string}
|
|
867
972
|
): Promise<ToBTCLNSwap<T[ChainIdentifier]>> {
|
|
868
|
-
if(this.
|
|
973
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
869
974
|
if(typeof(lnurlPay)==="string" && !this.Utils.isValidLNURL(lnurlPay)) throw new Error("Invalid LNURL-pay link");
|
|
870
|
-
if(!this.
|
|
871
|
-
signer = this.
|
|
975
|
+
if(!this._chains[chainIdentifier].chainInterface.isValidAddress(signer, true)) throw new Error("Invalid "+chainIdentifier+" address");
|
|
976
|
+
signer = this._chains[chainIdentifier].chainInterface.normalizeAddress(signer);
|
|
872
977
|
const amountData = {
|
|
873
978
|
amount,
|
|
874
979
|
token: tokenAddress,
|
|
@@ -893,30 +998,30 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
893
998
|
}
|
|
894
999
|
|
|
895
1000
|
/**
|
|
896
|
-
* Creates
|
|
1001
|
+
* Creates Smart chain -> Bitcoin Lightning ({@link SwapType.TO_BTCLN}) swap via {@link LightningInvoiceCreateService}
|
|
897
1002
|
*
|
|
898
|
-
* @param chainIdentifier
|
|
899
|
-
* @param signer
|
|
900
|
-
* @param tokenAddress
|
|
901
|
-
* @param service
|
|
902
|
-
* @param amount
|
|
903
|
-
* @param exactIn
|
|
904
|
-
* @param additionalParams
|
|
905
|
-
* @param options
|
|
1003
|
+
* @param chainIdentifier Chain identifier string of the source smart chain
|
|
1004
|
+
* @param signer Signer's address on the source chain
|
|
1005
|
+
* @param tokenAddress Token address to pay with
|
|
1006
|
+
* @param service Invoice create service object which facilitates the creation of fixed amount LN invoices
|
|
1007
|
+
* @param amount Amount to send in token based units (if `exactIn=true`) or receive in satoshis (if `exactIn=false`)
|
|
1008
|
+
* @param exactIn Whether to do an exact in swap instead of exact out
|
|
1009
|
+
* @param additionalParams Additional parameters sent to the LP when creating the swap
|
|
1010
|
+
* @param options Additional options for the swap
|
|
906
1011
|
*/
|
|
907
1012
|
async createToBTCLNSwapViaInvoiceCreateService<ChainIdentifier extends ChainIds<T>>(
|
|
908
1013
|
chainIdentifier: ChainIdentifier,
|
|
909
1014
|
signer: string,
|
|
910
1015
|
tokenAddress: string,
|
|
911
|
-
service:
|
|
1016
|
+
service: LightningInvoiceCreateService,
|
|
912
1017
|
amount: bigint,
|
|
913
1018
|
exactIn: boolean = false,
|
|
914
1019
|
additionalParams: Record<string, any> | undefined = this.options.defaultAdditionalParameters,
|
|
915
1020
|
options?: ToBTCLNOptions
|
|
916
1021
|
): Promise<ToBTCLNSwap<T[ChainIdentifier]>> {
|
|
917
|
-
if(this.
|
|
918
|
-
if(!this.
|
|
919
|
-
signer = this.
|
|
1022
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
1023
|
+
if(!this._chains[chainIdentifier].chainInterface.isValidAddress(signer, true)) throw new Error("Invalid "+chainIdentifier+" address");
|
|
1024
|
+
signer = this._chains[chainIdentifier].chainInterface.normalizeAddress(signer);
|
|
920
1025
|
options ??= {};
|
|
921
1026
|
const amountData = {
|
|
922
1027
|
amount,
|
|
@@ -941,28 +1046,28 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
941
1046
|
}
|
|
942
1047
|
|
|
943
1048
|
/**
|
|
944
|
-
* Creates
|
|
1049
|
+
* Creates Bitcoin -> Smart chain ({@link SwapType.SPV_VAULT_FROM_BTC}) swap
|
|
945
1050
|
*
|
|
946
|
-
* @param chainIdentifier
|
|
947
|
-
* @param
|
|
948
|
-
* @param tokenAddress
|
|
949
|
-
* @param amount
|
|
950
|
-
* @param exactOut
|
|
951
|
-
* @param additionalParams
|
|
952
|
-
* @param options
|
|
1051
|
+
* @param chainIdentifier Chain identifier string of the destination smart chain
|
|
1052
|
+
* @param recipient Recipient address on the destination chain
|
|
1053
|
+
* @param tokenAddress Token address to receive
|
|
1054
|
+
* @param amount Amount to send in satoshis (if `exactOut=false`) or receive in token based units (if `exactOut=true`)
|
|
1055
|
+
* @param exactOut Whether to use a exact out instead of exact in
|
|
1056
|
+
* @param additionalParams Additional parameters sent to the LP when creating the swap
|
|
1057
|
+
* @param options Additional options for the swap
|
|
953
1058
|
*/
|
|
954
1059
|
async createFromBTCSwapNew<ChainIdentifier extends ChainIds<T>>(
|
|
955
1060
|
chainIdentifier: ChainIdentifier,
|
|
956
|
-
|
|
1061
|
+
recipient: string,
|
|
957
1062
|
tokenAddress: string,
|
|
958
1063
|
amount: bigint,
|
|
959
1064
|
exactOut: boolean = false,
|
|
960
1065
|
additionalParams: Record<string, any> | undefined = this.options.defaultAdditionalParameters,
|
|
961
1066
|
options?: SpvFromBTCOptions
|
|
962
1067
|
): Promise<SpvFromBTCSwap<T[ChainIdentifier]>> {
|
|
963
|
-
if(this.
|
|
964
|
-
if(!this.
|
|
965
|
-
|
|
1068
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
1069
|
+
if(!this._chains[chainIdentifier].chainInterface.isValidAddress(recipient, true)) throw new Error("Invalid "+chainIdentifier+" address");
|
|
1070
|
+
recipient = this._chains[chainIdentifier].chainInterface.normalizeAddress(recipient);
|
|
966
1071
|
const amountData = {
|
|
967
1072
|
amount,
|
|
968
1073
|
token: tokenAddress,
|
|
@@ -971,7 +1076,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
971
1076
|
return this.createSwap(
|
|
972
1077
|
chainIdentifier as ChainIdentifier,
|
|
973
1078
|
(candidates: Intermediary[], abortSignal: AbortSignal, chain) => Promise.resolve(chain.wrappers[SwapType.SPV_VAULT_FROM_BTC].create(
|
|
974
|
-
|
|
1079
|
+
recipient,
|
|
975
1080
|
amountData,
|
|
976
1081
|
candidates,
|
|
977
1082
|
options,
|
|
@@ -984,28 +1089,28 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
984
1089
|
}
|
|
985
1090
|
|
|
986
1091
|
/**
|
|
987
|
-
* Creates
|
|
1092
|
+
* Creates LEGACY Bitcoin -> Smart chain ({@link SwapType.FROM_BTC}) swap
|
|
988
1093
|
*
|
|
989
|
-
* @param chainIdentifier
|
|
990
|
-
* @param
|
|
991
|
-
* @param tokenAddress
|
|
992
|
-
* @param amount
|
|
993
|
-
* @param exactOut
|
|
994
|
-
* @param additionalParams
|
|
995
|
-
* @param options
|
|
1094
|
+
* @param chainIdentifier Chain identifier string of the destination smart chain
|
|
1095
|
+
* @param recipient Recipient address on the destination chain
|
|
1096
|
+
* @param tokenAddress Token address to receive
|
|
1097
|
+
* @param amount Amount to send in satoshis (if `exactOut=false`) or receive in token based units (if `exactOut=true`)
|
|
1098
|
+
* @param exactOut Whether to use a exact out instead of exact in
|
|
1099
|
+
* @param additionalParams Additional parameters sent to the LP when creating the swap
|
|
1100
|
+
* @param options Additional options for the swap
|
|
996
1101
|
*/
|
|
997
1102
|
async createFromBTCSwap<ChainIdentifier extends ChainIds<T>>(
|
|
998
1103
|
chainIdentifier: ChainIdentifier,
|
|
999
|
-
|
|
1104
|
+
recipient: string,
|
|
1000
1105
|
tokenAddress: string,
|
|
1001
1106
|
amount: bigint,
|
|
1002
1107
|
exactOut: boolean = false,
|
|
1003
1108
|
additionalParams: Record<string, any> | undefined = this.options.defaultAdditionalParameters,
|
|
1004
1109
|
options?: FromBTCOptions
|
|
1005
1110
|
): Promise<FromBTCSwap<T[ChainIdentifier]>> {
|
|
1006
|
-
if(this.
|
|
1007
|
-
if(!this.
|
|
1008
|
-
|
|
1111
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
1112
|
+
if(!this._chains[chainIdentifier].chainInterface.isValidAddress(recipient, true)) throw new Error("Invalid "+chainIdentifier+" address");
|
|
1113
|
+
recipient = this._chains[chainIdentifier].chainInterface.normalizeAddress(recipient);
|
|
1009
1114
|
const amountData = {
|
|
1010
1115
|
amount,
|
|
1011
1116
|
token: tokenAddress,
|
|
@@ -1014,7 +1119,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1014
1119
|
return this.createSwap(
|
|
1015
1120
|
chainIdentifier as ChainIdentifier,
|
|
1016
1121
|
(candidates: Intermediary[], abortSignal: AbortSignal, chain) => Promise.resolve(chain.wrappers[SwapType.FROM_BTC].create(
|
|
1017
|
-
|
|
1122
|
+
recipient,
|
|
1018
1123
|
amountData,
|
|
1019
1124
|
candidates,
|
|
1020
1125
|
options,
|
|
@@ -1027,28 +1132,28 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1027
1132
|
}
|
|
1028
1133
|
|
|
1029
1134
|
/**
|
|
1030
|
-
* Creates
|
|
1135
|
+
* Creates LEGACY Bitcoin Lightning -> Smart chain ({@link SwapType.FROM_BTCLN}) swap
|
|
1031
1136
|
*
|
|
1032
|
-
* @param chainIdentifier
|
|
1033
|
-
* @param
|
|
1034
|
-
* @param tokenAddress
|
|
1035
|
-
* @param amount
|
|
1036
|
-
* @param exactOut
|
|
1037
|
-
* @param additionalParams
|
|
1038
|
-
* @param options
|
|
1137
|
+
* @param chainIdentifier Chain identifier string of the destination smart chain
|
|
1138
|
+
* @param recipient Recipient address on the destination chain
|
|
1139
|
+
* @param tokenAddress Token address to receive
|
|
1140
|
+
* @param amount Amount to send in satoshis (if `exactOut=false`) or receive in token based units (if `exactOut=true`)
|
|
1141
|
+
* @param exactOut Whether to use a exact out instead of exact in
|
|
1142
|
+
* @param additionalParams Additional parameters sent to the LP when creating the swap
|
|
1143
|
+
* @param options Additional options for the swap
|
|
1039
1144
|
*/
|
|
1040
1145
|
async createFromBTCLNSwap<ChainIdentifier extends ChainIds<T>>(
|
|
1041
1146
|
chainIdentifier: ChainIdentifier,
|
|
1042
|
-
|
|
1147
|
+
recipient: string,
|
|
1043
1148
|
tokenAddress: string,
|
|
1044
1149
|
amount: bigint,
|
|
1045
1150
|
exactOut: boolean = false,
|
|
1046
1151
|
additionalParams: Record<string, any> | undefined = this.options.defaultAdditionalParameters,
|
|
1047
1152
|
options?: FromBTCLNOptions
|
|
1048
1153
|
): Promise<FromBTCLNSwap<T[ChainIdentifier]>> {
|
|
1049
|
-
if(this.
|
|
1050
|
-
if(!this.
|
|
1051
|
-
|
|
1154
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
1155
|
+
if(!this._chains[chainIdentifier].chainInterface.isValidAddress(recipient, true)) throw new Error("Invalid "+chainIdentifier+" address");
|
|
1156
|
+
recipient = this._chains[chainIdentifier].chainInterface.normalizeAddress(recipient);
|
|
1052
1157
|
const amountData = {
|
|
1053
1158
|
amount,
|
|
1054
1159
|
token: tokenAddress,
|
|
@@ -1057,7 +1162,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1057
1162
|
return this.createSwap(
|
|
1058
1163
|
chainIdentifier as ChainIdentifier,
|
|
1059
1164
|
(candidates: Intermediary[], abortSignal: AbortSignal, chain) => Promise.resolve(chain.wrappers[SwapType.FROM_BTCLN].create(
|
|
1060
|
-
|
|
1165
|
+
recipient,
|
|
1061
1166
|
amountData,
|
|
1062
1167
|
candidates,
|
|
1063
1168
|
options,
|
|
@@ -1070,29 +1175,30 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1070
1175
|
}
|
|
1071
1176
|
|
|
1072
1177
|
/**
|
|
1073
|
-
* Creates
|
|
1178
|
+
* Creates LEGACY Bitcoin Lightning -> Smart chain ({@link SwapType.FROM_BTCLN}) swap, withdrawing from
|
|
1179
|
+
* an LNURL-withdraw link
|
|
1074
1180
|
*
|
|
1075
|
-
* @param chainIdentifier
|
|
1076
|
-
* @param
|
|
1077
|
-
* @param tokenAddress
|
|
1078
|
-
* @param lnurl
|
|
1079
|
-
* @param amount
|
|
1080
|
-
* @param exactOut
|
|
1081
|
-
* @param additionalParams
|
|
1181
|
+
* @param chainIdentifier Chain identifier string of the destination smart chain
|
|
1182
|
+
* @param recipient Recipient address on the destination chain
|
|
1183
|
+
* @param tokenAddress Token address to receive
|
|
1184
|
+
* @param lnurl LNURL-withdraw link to pull the funds from
|
|
1185
|
+
* @param amount Amount to send in satoshis (if `exactOut=false`) or receive in token based units (if `exactOut=true`)
|
|
1186
|
+
* @param exactOut Whether to use a exact out instead of exact in
|
|
1187
|
+
* @param additionalParams Additional parameters sent to the LP when creating the swap
|
|
1082
1188
|
*/
|
|
1083
1189
|
async createFromBTCLNSwapViaLNURL<ChainIdentifier extends ChainIds<T>>(
|
|
1084
1190
|
chainIdentifier: ChainIdentifier,
|
|
1085
|
-
|
|
1191
|
+
recipient: string,
|
|
1086
1192
|
tokenAddress: string,
|
|
1087
1193
|
lnurl: string | LNURLWithdraw,
|
|
1088
1194
|
amount: bigint,
|
|
1089
1195
|
exactOut: boolean = false,
|
|
1090
1196
|
additionalParams: Record<string, any> | undefined = this.options.defaultAdditionalParameters
|
|
1091
1197
|
): Promise<FromBTCLNSwap<T[ChainIdentifier]>> {
|
|
1092
|
-
if(this.
|
|
1198
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
1093
1199
|
if(typeof(lnurl)==="string" && !this.Utils.isValidLNURL(lnurl)) throw new Error("Invalid LNURL-withdraw link");
|
|
1094
|
-
if(!this.
|
|
1095
|
-
|
|
1200
|
+
if(!this._chains[chainIdentifier].chainInterface.isValidAddress(recipient, true)) throw new Error("Invalid "+chainIdentifier+" address");
|
|
1201
|
+
recipient = this._chains[chainIdentifier].chainInterface.normalizeAddress(recipient);
|
|
1096
1202
|
const amountData = {
|
|
1097
1203
|
amount,
|
|
1098
1204
|
token: tokenAddress,
|
|
@@ -1101,7 +1207,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1101
1207
|
return this.createSwap(
|
|
1102
1208
|
chainIdentifier as ChainIdentifier,
|
|
1103
1209
|
(candidates: Intermediary[], abortSignal: AbortSignal, chain) => chain.wrappers[SwapType.FROM_BTCLN].createViaLNURL(
|
|
1104
|
-
|
|
1210
|
+
recipient,
|
|
1105
1211
|
typeof(lnurl)==="string" ? (lnurl.startsWith("lightning:") ? lnurl.substring(10): lnurl) : lnurl.params,
|
|
1106
1212
|
amountData,
|
|
1107
1213
|
candidates,
|
|
@@ -1114,28 +1220,28 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1114
1220
|
}
|
|
1115
1221
|
|
|
1116
1222
|
/**
|
|
1117
|
-
* Creates
|
|
1223
|
+
* Creates Bitcoin Lightning -> Smart chain ({@link SwapType.FROM_BTCLN_AUTO}) swap
|
|
1118
1224
|
*
|
|
1119
|
-
* @param chainIdentifier
|
|
1120
|
-
* @param
|
|
1121
|
-
* @param tokenAddress
|
|
1122
|
-
* @param amount
|
|
1123
|
-
* @param exactOut
|
|
1124
|
-
* @param additionalParams
|
|
1125
|
-
* @param options
|
|
1225
|
+
* @param chainIdentifier Chain identifier string of the destination smart chain
|
|
1226
|
+
* @param recipient Recipient address on the destination chain
|
|
1227
|
+
* @param tokenAddress Token address to receive
|
|
1228
|
+
* @param amount Amount to send in satoshis (if `exactOut=false`) or receive in token based units (if `exactOut=true`)
|
|
1229
|
+
* @param exactOut Whether to use a exact out instead of exact in
|
|
1230
|
+
* @param additionalParams Additional parameters sent to the LP when creating the swap
|
|
1231
|
+
* @param options Additional options for the swap
|
|
1126
1232
|
*/
|
|
1127
1233
|
async createFromBTCLNSwapNew<ChainIdentifier extends ChainIds<T>>(
|
|
1128
1234
|
chainIdentifier: ChainIdentifier,
|
|
1129
|
-
|
|
1235
|
+
recipient: string,
|
|
1130
1236
|
tokenAddress: string,
|
|
1131
1237
|
amount: bigint,
|
|
1132
1238
|
exactOut: boolean = false,
|
|
1133
1239
|
additionalParams: Record<string, any> | undefined = this.options.defaultAdditionalParameters,
|
|
1134
1240
|
options?: FromBTCLNAutoOptions
|
|
1135
1241
|
): Promise<FromBTCLNAutoSwap<T[ChainIdentifier]>> {
|
|
1136
|
-
if(this.
|
|
1137
|
-
if(!this.
|
|
1138
|
-
|
|
1242
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
1243
|
+
if(!this._chains[chainIdentifier].chainInterface.isValidAddress(recipient, true)) throw new Error("Invalid "+chainIdentifier+" address");
|
|
1244
|
+
recipient = this._chains[chainIdentifier].chainInterface.normalizeAddress(recipient);
|
|
1139
1245
|
const amountData = {
|
|
1140
1246
|
amount,
|
|
1141
1247
|
token: tokenAddress,
|
|
@@ -1144,7 +1250,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1144
1250
|
return this.createSwap(
|
|
1145
1251
|
chainIdentifier as ChainIdentifier,
|
|
1146
1252
|
(candidates: Intermediary[], abortSignal: AbortSignal, chain) => Promise.resolve(chain.wrappers[SwapType.FROM_BTCLN_AUTO].create(
|
|
1147
|
-
|
|
1253
|
+
recipient,
|
|
1148
1254
|
amountData,
|
|
1149
1255
|
candidates,
|
|
1150
1256
|
options,
|
|
@@ -1157,20 +1263,21 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1157
1263
|
}
|
|
1158
1264
|
|
|
1159
1265
|
/**
|
|
1160
|
-
* Creates
|
|
1266
|
+
* Creates Bitcoin Lightning -> Smart chain ({@link SwapType.FROM_BTCLN_AUTO}) swap, withdrawing from
|
|
1267
|
+
* an LNURL-withdraw link
|
|
1161
1268
|
*
|
|
1162
|
-
* @param chainIdentifier
|
|
1163
|
-
* @param
|
|
1164
|
-
* @param tokenAddress
|
|
1165
|
-
* @param lnurl
|
|
1166
|
-
* @param amount
|
|
1167
|
-
* @param exactOut
|
|
1168
|
-
* @param additionalParams
|
|
1169
|
-
* @param options
|
|
1269
|
+
* @param chainIdentifier Chain identifier string of the destination smart chain
|
|
1270
|
+
* @param recipient Recipient address on the destination chain
|
|
1271
|
+
* @param tokenAddress Token address to receive
|
|
1272
|
+
* @param lnurl LNURL-withdraw link to pull the funds from
|
|
1273
|
+
* @param amount Amount to send in satoshis (if `exactOut=false`) or receive in token based units (if `exactOut=true`)
|
|
1274
|
+
* @param exactOut Whether to use a exact out instead of exact in
|
|
1275
|
+
* @param additionalParams Additional parameters sent to the LP when creating the swap
|
|
1276
|
+
* @param options Additional options for the swap
|
|
1170
1277
|
*/
|
|
1171
1278
|
async createFromBTCLNSwapNewViaLNURL<ChainIdentifier extends ChainIds<T>>(
|
|
1172
1279
|
chainIdentifier: ChainIdentifier,
|
|
1173
|
-
|
|
1280
|
+
recipient: string,
|
|
1174
1281
|
tokenAddress: string,
|
|
1175
1282
|
lnurl: string | LNURLWithdraw,
|
|
1176
1283
|
amount: bigint,
|
|
@@ -1178,10 +1285,10 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1178
1285
|
additionalParams: Record<string, any> | undefined = this.options.defaultAdditionalParameters,
|
|
1179
1286
|
options?: FromBTCLNAutoOptions
|
|
1180
1287
|
): Promise<FromBTCLNAutoSwap<T[ChainIdentifier]>> {
|
|
1181
|
-
if(this.
|
|
1288
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
1182
1289
|
if(typeof(lnurl)==="string" && !this.Utils.isValidLNURL(lnurl)) throw new Error("Invalid LNURL-withdraw link");
|
|
1183
|
-
if(!this.
|
|
1184
|
-
|
|
1290
|
+
if(!this._chains[chainIdentifier].chainInterface.isValidAddress(recipient, true)) throw new Error("Invalid "+chainIdentifier+" address");
|
|
1291
|
+
recipient = this._chains[chainIdentifier].chainInterface.normalizeAddress(recipient);
|
|
1185
1292
|
const amountData = {
|
|
1186
1293
|
amount,
|
|
1187
1294
|
token: tokenAddress,
|
|
@@ -1190,7 +1297,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1190
1297
|
return this.createSwap(
|
|
1191
1298
|
chainIdentifier as ChainIdentifier,
|
|
1192
1299
|
(candidates: Intermediary[], abortSignal: AbortSignal, chain) => chain.wrappers[SwapType.FROM_BTCLN_AUTO].createViaLNURL(
|
|
1193
|
-
|
|
1300
|
+
recipient,
|
|
1194
1301
|
typeof(lnurl)==="string" ? (lnurl.startsWith("lightning:") ? lnurl.substring(10): lnurl) : lnurl.params,
|
|
1195
1302
|
amountData,
|
|
1196
1303
|
candidates,
|
|
@@ -1204,57 +1311,86 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1204
1311
|
}
|
|
1205
1312
|
|
|
1206
1313
|
/**
|
|
1207
|
-
* Creates trusted
|
|
1314
|
+
* Creates a trusted Bitcoin Lightning -> Smart chain ({@link SwapType.TRUSTED_FROM_BTCLN}) gas swap
|
|
1208
1315
|
*
|
|
1209
|
-
* @param
|
|
1210
|
-
* @param
|
|
1211
|
-
* @param amount
|
|
1316
|
+
* @param chainIdentifier Chain identifier string of the destination smart chain
|
|
1317
|
+
* @param recipient Recipient address on the destination chain
|
|
1318
|
+
* @param amount Amount of native token to receive, in base units
|
|
1212
1319
|
* @param trustedIntermediaryOrUrl URL or Intermediary object of the trusted intermediary to use, otherwise uses default
|
|
1213
|
-
* @throws {Error}
|
|
1320
|
+
* @throws {Error} If no trusted intermediary specified
|
|
1214
1321
|
*/
|
|
1215
|
-
createTrustedLNForGasSwap<C extends ChainIds<T>>(
|
|
1216
|
-
if(this.
|
|
1217
|
-
if(!this.
|
|
1218
|
-
|
|
1322
|
+
createTrustedLNForGasSwap<C extends ChainIds<T>>(chainIdentifier: C, recipient: string, amount: bigint, trustedIntermediaryOrUrl?: Intermediary | string): Promise<LnForGasSwap<T[C]>> {
|
|
1323
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
1324
|
+
if(!this._chains[chainIdentifier].chainInterface.isValidAddress(recipient, true)) throw new Error("Invalid "+chainIdentifier+" address");
|
|
1325
|
+
recipient = this._chains[chainIdentifier].chainInterface.normalizeAddress(recipient);
|
|
1219
1326
|
const useUrl = trustedIntermediaryOrUrl ?? this.defaultTrustedIntermediary ?? this.options.defaultTrustedIntermediaryUrl;
|
|
1220
1327
|
if(useUrl==null) throw new Error("No trusted intermediary specified!");
|
|
1221
|
-
return this.
|
|
1328
|
+
return this._chains[chainIdentifier as C].wrappers[SwapType.TRUSTED_FROM_BTCLN].create(recipient, amount, useUrl);
|
|
1222
1329
|
}
|
|
1223
1330
|
|
|
1224
1331
|
/**
|
|
1225
|
-
* Creates trusted
|
|
1332
|
+
* Creates a trusted Bitcoin -> Smart chain ({@link SwapType.TRUSTED_FROM_BTC}) gas swap
|
|
1226
1333
|
*
|
|
1227
|
-
* @param
|
|
1228
|
-
* @param
|
|
1229
|
-
* @param amount
|
|
1230
|
-
* @param refundAddress
|
|
1231
|
-
* @param trustedIntermediaryOrUrl
|
|
1232
|
-
* @throws {Error}
|
|
1334
|
+
* @param chainIdentifier Chain identifier string of the destination smart chain
|
|
1335
|
+
* @param recipient Recipient address on the destination chain
|
|
1336
|
+
* @param amount Amount of native token to receive, in base units
|
|
1337
|
+
* @param refundAddress Bitcoin refund address, in case the swap fails the funds are refunded here
|
|
1338
|
+
* @param trustedIntermediaryOrUrl URL or Intermediary object of the trusted intermediary to use, otherwise uses default
|
|
1339
|
+
* @throws {Error} If no trusted intermediary specified
|
|
1233
1340
|
*/
|
|
1234
1341
|
createTrustedOnchainForGasSwap<C extends ChainIds<T>>(
|
|
1235
|
-
|
|
1342
|
+
chainIdentifier: C, recipient: string,
|
|
1236
1343
|
amount: bigint, refundAddress?: string,
|
|
1237
1344
|
trustedIntermediaryOrUrl?: Intermediary | string
|
|
1238
1345
|
): Promise<OnchainForGasSwap<T[C]>> {
|
|
1239
|
-
if(this.
|
|
1240
|
-
if(!this.
|
|
1241
|
-
|
|
1346
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
1347
|
+
if(!this._chains[chainIdentifier].chainInterface.isValidAddress(recipient, true)) throw new Error("Invalid "+chainIdentifier+" address");
|
|
1348
|
+
recipient = this._chains[chainIdentifier].chainInterface.normalizeAddress(recipient);
|
|
1242
1349
|
const useUrl = trustedIntermediaryOrUrl ?? this.defaultTrustedIntermediary ?? this.options.defaultTrustedIntermediaryUrl;
|
|
1243
1350
|
if(useUrl==null) throw new Error("No trusted intermediary specified!");
|
|
1244
|
-
return this.
|
|
1351
|
+
return this._chains[chainIdentifier as C].wrappers[SwapType.TRUSTED_FROM_BTC].create(recipient, amount, useUrl, refundAddress);
|
|
1245
1352
|
}
|
|
1246
1353
|
|
|
1354
|
+
/**
|
|
1355
|
+
* @internal
|
|
1356
|
+
*/
|
|
1247
1357
|
create<C extends ChainIds<T>>(signer: string, srcToken: BtcToken<true>, dstToken: SCToken<C>, amount: bigint, exactIn: boolean, lnurlWithdraw?: string | LNURLWithdraw): Promise<(SupportsSwapType<T[C], SwapType.FROM_BTCLN_AUTO> extends true ? FromBTCLNAutoSwap<T[C]> : FromBTCLNSwap<T[C]>)>;
|
|
1358
|
+
/**
|
|
1359
|
+
* @internal
|
|
1360
|
+
*/
|
|
1248
1361
|
create<C extends ChainIds<T>>(signer: string, srcToken: BtcToken<false>, dstToken: SCToken<C>, amount: bigint, exactIn: boolean): Promise<(SupportsSwapType<T[C], SwapType.SPV_VAULT_FROM_BTC> extends true ? SpvFromBTCSwap<T[C]> : FromBTCSwap<T[C]>)>;
|
|
1362
|
+
/**
|
|
1363
|
+
* @internal
|
|
1364
|
+
*/
|
|
1249
1365
|
create<C extends ChainIds<T>>(signer: string, srcToken: SCToken<C>, dstToken: BtcToken<false>, amount: bigint, exactIn: boolean, address: string): Promise<ToBTCSwap<T[C]>>;
|
|
1366
|
+
/**
|
|
1367
|
+
* @internal
|
|
1368
|
+
*/
|
|
1250
1369
|
create<C extends ChainIds<T>>(signer: string, srcToken: SCToken<C>, dstToken: BtcToken<true>, amount: bigint, exactIn: boolean, lnurlPay: string | LNURLPay): Promise<ToBTCLNSwap<T[C]>>;
|
|
1370
|
+
/**
|
|
1371
|
+
* @internal
|
|
1372
|
+
*/
|
|
1251
1373
|
create<C extends ChainIds<T>>(signer: string, srcToken: SCToken<C>, dstToken: BtcToken<true>, amount: undefined, exactIn: false, lightningInvoice: string): Promise<ToBTCLNSwap<T[C]>>;
|
|
1374
|
+
/**
|
|
1375
|
+
* Creates a swap from srcToken to dstToken, of a specific token amount, either specifying input amount (exactIn=true)
|
|
1376
|
+
* or output amount (exactIn=false), NOTE: For regular -> BTC-LN (lightning) swaps the passed amount is ignored and
|
|
1377
|
+
* invoice's pre-set amount is used instead.
|
|
1378
|
+
* @deprecated Use {@link swap} instead
|
|
1379
|
+
*
|
|
1380
|
+
* @param signer Smartchain (Solana, Starknet, etc.) address of the user
|
|
1381
|
+
* @param srcToken Source token of the swap, user pays this token
|
|
1382
|
+
* @param dstToken Destination token of the swap, user receives this token
|
|
1383
|
+
* @param amount Amount of the swap
|
|
1384
|
+
* @param exactIn Whether the amount specified is an input amount (exactIn=true) or an output amount (exactIn=false)
|
|
1385
|
+
* @param addressLnurlLightningInvoice Bitcoin on-chain address, lightning invoice, LNURL-pay to pay or
|
|
1386
|
+
* LNURL-withdrawal to withdraw money from
|
|
1387
|
+
*/
|
|
1252
1388
|
create<C extends ChainIds<T>>(signer: string, srcToken: Token<C>, dstToken: Token<C>, amount: bigint | undefined, exactIn: boolean, addressLnurlLightningInvoice?: string | LNURLWithdraw | LNURLPay): Promise<ISwap<T[C]>>;
|
|
1253
1389
|
/**
|
|
1254
1390
|
* Creates a swap from srcToken to dstToken, of a specific token amount, either specifying input amount (exactIn=true)
|
|
1255
1391
|
* or output amount (exactIn=false), NOTE: For regular -> BTC-LN (lightning) swaps the passed amount is ignored and
|
|
1256
1392
|
* invoice's pre-set amount is used instead.
|
|
1257
|
-
* @deprecated Use swap
|
|
1393
|
+
* @deprecated Use {@link swap} instead
|
|
1258
1394
|
*
|
|
1259
1395
|
* @param signer Smartchain (Solana, Starknet, etc.) address of the user
|
|
1260
1396
|
* @param srcToken Source token of the swap, user pays this token
|
|
@@ -1272,16 +1408,44 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1272
1408
|
}
|
|
1273
1409
|
}
|
|
1274
1410
|
|
|
1411
|
+
/**
|
|
1412
|
+
* @internal
|
|
1413
|
+
*/
|
|
1275
1414
|
swap<C extends ChainIds<T>>(srcToken: BtcToken<true>, dstToken: SCToken<C>, amount: bigint | string, exactIn: boolean | SwapAmountType, src: undefined | string | LNURLWithdraw, dstSmartchainWallet: string, options?: (SupportsSwapType<T[C], SwapType.FROM_BTCLN_AUTO> extends true ? FromBTCLNAutoOptions : FromBTCLNOptions)): Promise<(SupportsSwapType<T[C], SwapType.FROM_BTCLN_AUTO> extends true ? FromBTCLNAutoSwap<T[C]> : FromBTCLNSwap<T[C]>)>;
|
|
1415
|
+
/**
|
|
1416
|
+
* @internal
|
|
1417
|
+
*/
|
|
1276
1418
|
swap<C extends ChainIds<T>>(srcToken: BtcToken<false>, dstToken: SCToken<C>, amount: bigint | string, exactIn: boolean | SwapAmountType, src: undefined | string, dstSmartchainWallet: string, options?: (SupportsSwapType<T[C], SwapType.SPV_VAULT_FROM_BTC> extends true ? SpvFromBTCOptions : FromBTCOptions)): Promise<(SupportsSwapType<T[C], SwapType.SPV_VAULT_FROM_BTC> extends true ? SpvFromBTCSwap<T[C]> : FromBTCSwap<T[C]>)>;
|
|
1419
|
+
/**
|
|
1420
|
+
* @internal
|
|
1421
|
+
*/
|
|
1277
1422
|
swap<C extends ChainIds<T>>(srcToken: SCToken<C>, dstToken: BtcToken<false>, amount: bigint | string, exactIn: boolean | SwapAmountType, src: string, dstAddress: string, options?: ToBTCOptions): Promise<ToBTCSwap<T[C]>>;
|
|
1278
|
-
|
|
1423
|
+
/**
|
|
1424
|
+
* @internal
|
|
1425
|
+
*/
|
|
1426
|
+
swap<C extends ChainIds<T>>(srcToken: SCToken<C>, dstToken: BtcToken<true>, amount: bigint | string, exactIn: boolean | SwapAmountType, src: string, dstLnurlPayOrInvoiceCreateService: string | LNURLPay | LightningInvoiceCreateService, options?: ToBTCLNOptions & {comment?: string}): Promise<ToBTCLNSwap<T[C]>>;
|
|
1427
|
+
/**
|
|
1428
|
+
* @internal
|
|
1429
|
+
*/
|
|
1279
1430
|
swap<C extends ChainIds<T>>(srcToken: SCToken<C>, dstToken: BtcToken<true>, amount: undefined, exactIn: false | SwapAmountType.EXACT_OUT, src: string, dstLightningInvoice: string, options?: ToBTCLNOptions): Promise<ToBTCLNSwap<T[C]>>;
|
|
1280
|
-
swap<C extends ChainIds<T>>(srcToken: Token<C> | string, dstToken: Token<C> | string, amount: bigint | string | undefined, exactIn: boolean | SwapAmountType, src: undefined | string | LNURLWithdraw, dst: string | LNURLPay | InvoiceCreateService, options?: FromBTCLNOptions | SpvFromBTCOptions | FromBTCOptions | ToBTCOptions | (ToBTCLNOptions & {comment?: string}) | FromBTCLNAutoOptions): Promise<ISwap<T[C]>>;
|
|
1281
1431
|
/**
|
|
1282
|
-
* Creates a swap from srcToken to dstToken, of a specific token amount, either specifying input amount (exactIn=true)
|
|
1283
|
-
* or output amount (exactIn=false), NOTE: For regular
|
|
1284
|
-
* invoice's pre-set amount is used instead, use LNURL-pay for dynamic amounts
|
|
1432
|
+
* Creates a swap from srcToken to dstToken, of a specific token amount, either specifying input amount (if `exactIn=true`)
|
|
1433
|
+
* or output amount (if `exactIn=false`), NOTE: For regular Smart chain -> BTC-LN (lightning) swaps the passed amount is ignored and
|
|
1434
|
+
* invoice's pre-set amount is used instead, use LNURL-pay links for dynamic amounts
|
|
1435
|
+
*
|
|
1436
|
+
* @param srcToken Source token of the swap, user pays this token
|
|
1437
|
+
* @param dstToken Destination token of the swap, user receives this token
|
|
1438
|
+
* @param amount Amount of the swap either in base units as {bigint} or in human readable format (with decimals) as {string}
|
|
1439
|
+
* @param exactIn Whether the amount specified is an input amount (exactIn=true) or an output amount (exactIn=false)
|
|
1440
|
+
* @param src Source wallet/lnurl-withdraw of the swap
|
|
1441
|
+
* @param dst Destination smart chain address, bitcoin on-chain address, lightning invoice, LNURL-pay
|
|
1442
|
+
* @param options Options for the swap
|
|
1443
|
+
*/
|
|
1444
|
+
swap<C extends ChainIds<T>>(srcToken: Token<C> | string, dstToken: Token<C> | string, amount: bigint | string | undefined, exactIn: boolean | SwapAmountType, src: undefined | string | LNURLWithdraw, dst: string | LNURLPay | LightningInvoiceCreateService, options?: FromBTCLNOptions | SpvFromBTCOptions | FromBTCOptions | ToBTCOptions | (ToBTCLNOptions & {comment?: string}) | FromBTCLNAutoOptions): Promise<ISwap<T[C]>>;
|
|
1445
|
+
/**
|
|
1446
|
+
* Creates a swap from srcToken to dstToken, of a specific token amount, either specifying input amount (if `exactIn=true`)
|
|
1447
|
+
* or output amount (if `exactIn=false`), NOTE: For regular Smart chain -> BTC-LN (lightning) swaps the passed amount is ignored and
|
|
1448
|
+
* invoice's pre-set amount is used instead, use LNURL-pay links for dynamic amounts
|
|
1285
1449
|
*
|
|
1286
1450
|
* @param _srcToken Source token of the swap, user pays this token
|
|
1287
1451
|
* @param _dstToken Destination token of the swap, user receives this token
|
|
@@ -1297,7 +1461,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1297
1461
|
_amount: bigint | string | undefined,
|
|
1298
1462
|
exactIn: boolean | SwapAmountType,
|
|
1299
1463
|
src: undefined | string | LNURLWithdraw,
|
|
1300
|
-
dst: string | LNURLPay |
|
|
1464
|
+
dst: string | LNURLPay | LightningInvoiceCreateService,
|
|
1301
1465
|
options?: FromBTCLNOptions | SpvFromBTCOptions | FromBTCOptions | ToBTCOptions | (ToBTCLNOptions & {comment?: string}) | FromBTCLNAutoOptions
|
|
1302
1466
|
): Promise<ISwap<T[C]>> {
|
|
1303
1467
|
const srcToken = typeof(_srcToken)==="string" ? this.getToken(_srcToken) as Token<C> : _srcToken;
|
|
@@ -1337,7 +1501,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1337
1501
|
if(isLNURLPay(dst) || this.Utils.isValidLNURL(dst)) {
|
|
1338
1502
|
if(amount==null) throw new Error("Amount cannot be null for to btcln swaps via LNURL-pay!");
|
|
1339
1503
|
return this.createToBTCLNSwapViaLNURL(srcToken.chainId, src, srcToken.address, dst, amount, !!exactIn, undefined, options as any);
|
|
1340
|
-
} else if(
|
|
1504
|
+
} else if(isLightningInvoiceCreateService(dst)) {
|
|
1341
1505
|
if(amount==null) throw new Error("Amount cannot be null for to btcln swaps via InvoiceCreateService!");
|
|
1342
1506
|
return this.createToBTCLNSwapViaInvoiceCreateService(srcToken.chainId, src, srcToken.address, dst, amount, !!exactIn, undefined, options as any);
|
|
1343
1507
|
} else if(this.Utils.isLightningInvoice(dst)) {
|
|
@@ -1373,13 +1537,13 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1373
1537
|
if(signer!=null) queryParams.push({key: "initiator", value: signer});
|
|
1374
1538
|
|
|
1375
1539
|
if(chainId==null) {
|
|
1376
|
-
const res: ISwap[][] = await Promise.all(Object.keys(this.
|
|
1377
|
-
const {unifiedSwapStorage, reviver} = this.
|
|
1540
|
+
const res: ISwap[][] = await Promise.all(Object.keys(this._chains).map((chainId) => {
|
|
1541
|
+
const {unifiedSwapStorage, reviver} = this._chains[chainId];
|
|
1378
1542
|
return unifiedSwapStorage.query([queryParams], reviver);
|
|
1379
1543
|
}));
|
|
1380
1544
|
return res.flat();
|
|
1381
1545
|
} else {
|
|
1382
|
-
const {unifiedSwapStorage, reviver} = this.
|
|
1546
|
+
const {unifiedSwapStorage, reviver} = this._chains[chainId];
|
|
1383
1547
|
return await unifiedSwapStorage.query([queryParams], reviver);
|
|
1384
1548
|
}
|
|
1385
1549
|
}
|
|
@@ -1394,27 +1558,27 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1394
1558
|
getActionableSwaps<C extends ChainIds<T>>(chainId: C, signer?: string): Promise<ISwap<T[C]>[]>;
|
|
1395
1559
|
async getActionableSwaps<C extends ChainIds<T>>(chainId?: C, signer?: string): Promise<ISwap[]> {
|
|
1396
1560
|
if(chainId==null) {
|
|
1397
|
-
const res: ISwap[][] = await Promise.all(Object.keys(this.
|
|
1398
|
-
const {unifiedSwapStorage, reviver, wrappers} = this.
|
|
1561
|
+
const res: ISwap[][] = await Promise.all(Object.keys(this._chains).map((chainId) => {
|
|
1562
|
+
const {unifiedSwapStorage, reviver, wrappers} = this._chains[chainId];
|
|
1399
1563
|
const queryParams: Array<QueryParams[]> = [];
|
|
1400
1564
|
for(let key in wrappers) {
|
|
1401
1565
|
const wrapper = wrappers[key as unknown as SwapType];
|
|
1402
1566
|
const swapTypeQueryParams: QueryParams[] = [{key: "type", value: wrapper.TYPE}];
|
|
1403
1567
|
if(signer!=null) swapTypeQueryParams.push({key: "initiator", value: signer});
|
|
1404
|
-
swapTypeQueryParams.push({key: "state", value: wrapper.
|
|
1568
|
+
swapTypeQueryParams.push({key: "state", value: wrapper._pendingSwapStates});
|
|
1405
1569
|
queryParams.push(swapTypeQueryParams);
|
|
1406
1570
|
}
|
|
1407
1571
|
return unifiedSwapStorage.query(queryParams, reviver);
|
|
1408
1572
|
}));
|
|
1409
1573
|
return res.flat().filter(swap => swap.requiresAction());
|
|
1410
1574
|
} else {
|
|
1411
|
-
const {unifiedSwapStorage, reviver, wrappers} = this.
|
|
1575
|
+
const {unifiedSwapStorage, reviver, wrappers} = this._chains[chainId];
|
|
1412
1576
|
const queryParams: Array<QueryParams[]> = [];
|
|
1413
1577
|
for(let key in wrappers) {
|
|
1414
1578
|
const wrapper = wrappers[key as unknown as SwapType];
|
|
1415
1579
|
const swapTypeQueryParams: QueryParams[] = [{key: "type", value: wrapper.TYPE}];
|
|
1416
1580
|
if(signer!=null) swapTypeQueryParams.push({key: "initiator", value: signer});
|
|
1417
|
-
swapTypeQueryParams.push({key: "state", value: wrapper.
|
|
1581
|
+
swapTypeQueryParams.push({key: "state", value: wrapper._pendingSwapStates});
|
|
1418
1582
|
queryParams.push(swapTypeQueryParams);
|
|
1419
1583
|
}
|
|
1420
1584
|
return (await unifiedSwapStorage.query(queryParams, reviver)).filter(swap => swap.requiresAction());
|
|
@@ -1431,25 +1595,25 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1431
1595
|
getRefundableSwaps<C extends ChainIds<T>>(chainId: C, signer?: string): Promise<IToBTCSwap<T[C]>[]>;
|
|
1432
1596
|
async getRefundableSwaps<C extends ChainIds<T>>(chainId?: C, signer?: string): Promise<IToBTCSwap[]> {
|
|
1433
1597
|
if(chainId==null) {
|
|
1434
|
-
const res: IToBTCSwap[][] = await Promise.all(Object.keys(this.
|
|
1435
|
-
const {unifiedSwapStorage, reviver, wrappers} = this.
|
|
1598
|
+
const res: IToBTCSwap[][] = await Promise.all(Object.keys(this._chains).map((chainId) => {
|
|
1599
|
+
const {unifiedSwapStorage, reviver, wrappers} = this._chains[chainId];
|
|
1436
1600
|
const queryParams: Array<QueryParams[]> = [];
|
|
1437
1601
|
for(let wrapper of [wrappers[SwapType.TO_BTCLN], wrappers[SwapType.TO_BTC]]) {
|
|
1438
1602
|
const swapTypeQueryParams: QueryParams[] = [{key: "type", value: wrapper.TYPE}];
|
|
1439
1603
|
if(signer!=null) swapTypeQueryParams.push({key: "initiator", value: signer});
|
|
1440
|
-
swapTypeQueryParams.push({key: "state", value: wrapper.
|
|
1604
|
+
swapTypeQueryParams.push({key: "state", value: wrapper._refundableSwapStates});
|
|
1441
1605
|
queryParams.push(swapTypeQueryParams);
|
|
1442
1606
|
}
|
|
1443
1607
|
return unifiedSwapStorage.query<IToBTCSwap<T[C]>>(queryParams, reviver as (val: any) => IToBTCSwap<T[C]>);
|
|
1444
1608
|
}));
|
|
1445
1609
|
return res.flat().filter(swap => swap.isRefundable());
|
|
1446
1610
|
} else {
|
|
1447
|
-
const {unifiedSwapStorage, reviver, wrappers} = this.
|
|
1611
|
+
const {unifiedSwapStorage, reviver, wrappers} = this._chains[chainId];
|
|
1448
1612
|
const queryParams: Array<QueryParams[]> = [];
|
|
1449
1613
|
for(let wrapper of [wrappers[SwapType.TO_BTCLN], wrappers[SwapType.TO_BTC]]) {
|
|
1450
1614
|
const swapTypeQueryParams: QueryParams[] = [{key: "type", value: wrapper.TYPE}];
|
|
1451
1615
|
if(signer!=null) swapTypeQueryParams.push({key: "initiator", value: signer});
|
|
1452
|
-
swapTypeQueryParams.push({key: "state", value: wrapper.
|
|
1616
|
+
swapTypeQueryParams.push({key: "state", value: wrapper._refundableSwapStates});
|
|
1453
1617
|
queryParams.push(swapTypeQueryParams);
|
|
1454
1618
|
}
|
|
1455
1619
|
const result = await unifiedSwapStorage.query<IToBTCSwap<T[C]>>(queryParams, reviver as (val: any) => IToBTCSwap<T[C]>);
|
|
@@ -1467,27 +1631,27 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1467
1631
|
getClaimableSwaps<C extends ChainIds<T>>(chainId: C, signer?: string): Promise<IClaimableSwap<T[C]>[]>;
|
|
1468
1632
|
async getClaimableSwaps<C extends ChainIds<T>>(chainId?: C, signer?: string): Promise<IClaimableSwap[]> {
|
|
1469
1633
|
if(chainId==null) {
|
|
1470
|
-
const res: IClaimableSwap[][] = await Promise.all(Object.keys(this.
|
|
1471
|
-
const {unifiedSwapStorage, reviver, wrappers} = this.
|
|
1634
|
+
const res: IClaimableSwap[][] = await Promise.all(Object.keys(this._chains).map((chainId) => {
|
|
1635
|
+
const {unifiedSwapStorage, reviver, wrappers} = this._chains[chainId];
|
|
1472
1636
|
const queryParams: Array<QueryParams[]> = [];
|
|
1473
1637
|
for(let wrapper of [wrappers[SwapType.FROM_BTC], wrappers[SwapType.FROM_BTCLN], wrappers[SwapType.SPV_VAULT_FROM_BTC], wrappers[SwapType.FROM_BTCLN_AUTO]]) {
|
|
1474
1638
|
if(wrapper==null) continue;
|
|
1475
1639
|
const swapTypeQueryParams: QueryParams[] = [{key: "type", value: wrapper.TYPE}];
|
|
1476
1640
|
if(signer!=null) swapTypeQueryParams.push({key: "initiator", value: signer});
|
|
1477
|
-
swapTypeQueryParams.push({key: "state", value: wrapper.
|
|
1641
|
+
swapTypeQueryParams.push({key: "state", value: wrapper._claimableSwapStates});
|
|
1478
1642
|
queryParams.push(swapTypeQueryParams);
|
|
1479
1643
|
}
|
|
1480
1644
|
return unifiedSwapStorage.query<IClaimableSwap<T[C]>>(queryParams, reviver as any as (val: any) => IClaimableSwap<T[C]>);
|
|
1481
1645
|
}));
|
|
1482
1646
|
return res.flat().filter(swap => swap.isClaimable());
|
|
1483
1647
|
} else {
|
|
1484
|
-
const {unifiedSwapStorage, reviver, wrappers} = this.
|
|
1648
|
+
const {unifiedSwapStorage, reviver, wrappers} = this._chains[chainId];
|
|
1485
1649
|
const queryParams: Array<QueryParams[]> = [];
|
|
1486
1650
|
for(let wrapper of [wrappers[SwapType.FROM_BTC], wrappers[SwapType.FROM_BTCLN], wrappers[SwapType.SPV_VAULT_FROM_BTC], wrappers[SwapType.FROM_BTCLN_AUTO]]) {
|
|
1487
1651
|
if(wrapper==null) continue;
|
|
1488
1652
|
const swapTypeQueryParams: QueryParams[] = [{key: "type", value: wrapper.TYPE}];
|
|
1489
1653
|
if(signer!=null) swapTypeQueryParams.push({key: "initiator", value: signer});
|
|
1490
|
-
swapTypeQueryParams.push({key: "state", value: wrapper.
|
|
1654
|
+
swapTypeQueryParams.push({key: "state", value: wrapper._claimableSwapStates});
|
|
1491
1655
|
queryParams.push(swapTypeQueryParams);
|
|
1492
1656
|
}
|
|
1493
1657
|
const result = await unifiedSwapStorage.query<IClaimableSwap<T[C]>>(queryParams, reviver as any as (val: any) => IClaimableSwap<T[C]>);
|
|
@@ -1506,9 +1670,9 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1506
1670
|
async getSwapById<C extends ChainIds<T>>(id: string, chainId?: C, signer?: string): Promise<ISwap> {
|
|
1507
1671
|
//Check in pending swaps first
|
|
1508
1672
|
if(chainId!=null) {
|
|
1509
|
-
for(let key in this.
|
|
1510
|
-
const wrapper = this.
|
|
1511
|
-
const result = wrapper.
|
|
1673
|
+
for(let key in this._chains[chainId].wrappers) {
|
|
1674
|
+
const wrapper = this._chains[chainId].wrappers[key as unknown as SwapType];
|
|
1675
|
+
const result = wrapper._getPendingSwap(id);
|
|
1512
1676
|
if(result!=null) {
|
|
1513
1677
|
if (signer != null) {
|
|
1514
1678
|
if (result._getInitiator() === signer) return result;
|
|
@@ -1518,10 +1682,10 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1518
1682
|
}
|
|
1519
1683
|
}
|
|
1520
1684
|
} else {
|
|
1521
|
-
for(let chainId in this.
|
|
1522
|
-
for(let key in this.
|
|
1523
|
-
const wrapper = this.
|
|
1524
|
-
const result = wrapper.
|
|
1685
|
+
for(let chainId in this._chains) {
|
|
1686
|
+
for(let key in this._chains[chainId].wrappers) {
|
|
1687
|
+
const wrapper = this._chains[chainId].wrappers[key as unknown as SwapType];
|
|
1688
|
+
const result = wrapper._getPendingSwap(id);
|
|
1525
1689
|
if(result!=null) {
|
|
1526
1690
|
if(signer!=null) {
|
|
1527
1691
|
if(result._getInitiator()===signer) return result;
|
|
@@ -1537,24 +1701,24 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1537
1701
|
if(signer!=null) queryParams.push({key: "initiator", value: signer});
|
|
1538
1702
|
queryParams.push({key: "id", value: id});
|
|
1539
1703
|
if(chainId==null) {
|
|
1540
|
-
const res: ISwap[][] = await Promise.all(Object.keys(this.
|
|
1541
|
-
const {unifiedSwapStorage, reviver} = this.
|
|
1704
|
+
const res: ISwap[][] = await Promise.all(Object.keys(this._chains).map((chainId) => {
|
|
1705
|
+
const {unifiedSwapStorage, reviver} = this._chains[chainId];
|
|
1542
1706
|
return unifiedSwapStorage.query([queryParams], reviver);
|
|
1543
1707
|
}));
|
|
1544
1708
|
return res.flat()[0];
|
|
1545
1709
|
} else {
|
|
1546
|
-
const {unifiedSwapStorage, reviver} = this.
|
|
1710
|
+
const {unifiedSwapStorage, reviver} = this._chains[chainId];
|
|
1547
1711
|
return (await unifiedSwapStorage.query([queryParams], reviver))[0];
|
|
1548
1712
|
}
|
|
1549
1713
|
}
|
|
1550
1714
|
|
|
1551
1715
|
/**
|
|
1552
|
-
* Returns the swap with a proper return type, or undefined
|
|
1716
|
+
* Returns the swap with a proper return type, or `undefined` if not found or has wrong type
|
|
1553
1717
|
*
|
|
1554
|
-
* @param id
|
|
1555
|
-
* @param chainId
|
|
1556
|
-
* @param swapType
|
|
1557
|
-
* @param signer
|
|
1718
|
+
* @param id An ID of the swap ({@link ISwap.getId})
|
|
1719
|
+
* @param chainId Chain identifier of the smart chain where the swap was initiated
|
|
1720
|
+
* @param swapType Type of the swap
|
|
1721
|
+
* @param signer An optional required smart chain signer address to fetch the swap for
|
|
1558
1722
|
*/
|
|
1559
1723
|
async getTypedSwapById<C extends ChainIds<T>, S extends SwapType>(id: string, chainId: C, swapType: S, signer?: string): Promise<SwapTypeMapping<T[C]>[S] | undefined> {
|
|
1560
1724
|
let _swapType: SwapType = swapType;
|
|
@@ -1563,10 +1727,10 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1563
1727
|
if(swapType===SwapType.FROM_BTCLN && this.supportsSwapType(chainId, SwapType.FROM_BTCLN_AUTO))
|
|
1564
1728
|
_swapType = SwapType.FROM_BTCLN_AUTO;
|
|
1565
1729
|
|
|
1566
|
-
const wrapper = this.
|
|
1730
|
+
const wrapper = this._chains[chainId].wrappers[_swapType];
|
|
1567
1731
|
if(wrapper==null) return;
|
|
1568
1732
|
|
|
1569
|
-
const result = wrapper.
|
|
1733
|
+
const result = wrapper._getPendingSwap(id);
|
|
1570
1734
|
if(result!=null) {
|
|
1571
1735
|
if (signer != null) {
|
|
1572
1736
|
if (result._getInitiator() === signer) return result as any;
|
|
@@ -1578,19 +1742,19 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1578
1742
|
const queryParams: QueryParams[] = [];
|
|
1579
1743
|
if(signer!=null) queryParams.push({key: "initiator", value: signer});
|
|
1580
1744
|
queryParams.push({key: "id", value: id});
|
|
1581
|
-
const {unifiedSwapStorage, reviver} = this.
|
|
1745
|
+
const {unifiedSwapStorage, reviver} = this._chains[chainId];
|
|
1582
1746
|
const swap = (await unifiedSwapStorage.query([queryParams], reviver))[0];
|
|
1583
1747
|
if(isSwapType(swap, swapType)) return swap;
|
|
1584
1748
|
}
|
|
1585
1749
|
|
|
1586
1750
|
private async syncSwapsForChain<C extends ChainIds<T>>(chainId: C, signer?: string): Promise<void> {
|
|
1587
|
-
const {unifiedSwapStorage, reviver, wrappers} = this.
|
|
1751
|
+
const {unifiedSwapStorage, reviver, wrappers} = this._chains[chainId];
|
|
1588
1752
|
const queryParams: Array<QueryParams[]> = [];
|
|
1589
1753
|
for(let key in wrappers) {
|
|
1590
1754
|
const wrapper = wrappers[key as unknown as SwapType];
|
|
1591
1755
|
const swapTypeQueryParams: QueryParams[] = [{key: "type", value: wrapper.TYPE}];
|
|
1592
1756
|
if(signer!=null) swapTypeQueryParams.push({key: "initiator", value: signer});
|
|
1593
|
-
swapTypeQueryParams.push({key: "state", value: wrapper.
|
|
1757
|
+
swapTypeQueryParams.push({key: "state", value: wrapper._pendingSwapStates});
|
|
1594
1758
|
queryParams.push(swapTypeQueryParams);
|
|
1595
1759
|
}
|
|
1596
1760
|
this.logger.debug("_syncSwaps(): Querying swaps swaps for chain "+chainId+"!");
|
|
@@ -1623,14 +1787,49 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1623
1787
|
}
|
|
1624
1788
|
|
|
1625
1789
|
/**
|
|
1626
|
-
*
|
|
1790
|
+
* Deletes the swaps from the persistent storage backend. Note that some data (like lightning network
|
|
1791
|
+
* amounts and bolt11 invoices) are purely off-chain and can never be recovered later just from
|
|
1792
|
+
* on-chain data!
|
|
1627
1793
|
*
|
|
1628
|
-
* @param chainId
|
|
1629
|
-
* @param signer
|
|
1794
|
+
* @param chainId Optional, to only delete swaps for this smart chain
|
|
1795
|
+
* @param signer Optional, to only delete swaps for this smart chain signer (`chainId` param must be
|
|
1796
|
+
* set to delete only signer's swaps)
|
|
1797
|
+
*/
|
|
1798
|
+
async wipeStorage<C extends ChainIds<T>>(chainId?: C, signer?: string) {
|
|
1799
|
+
if(chainId==null) {
|
|
1800
|
+
const swaps = await this.getAllSwaps();
|
|
1801
|
+
const chainSwaps: {
|
|
1802
|
+
[chainId: string]: ISwap<any>[]
|
|
1803
|
+
} = {};
|
|
1804
|
+
swaps.forEach(swap => (chainSwaps[swap.chainIdentifier] ??= []).push(swap));
|
|
1805
|
+
for(let chainId in chainSwaps) {
|
|
1806
|
+
const currentChainSwaps = chainSwaps[chainId];
|
|
1807
|
+
if(this._chains[chainId]==null) {
|
|
1808
|
+
this.logger.warn(`wipeStorage(): Attempted to remove ${currentChainSwaps.length} swaps on ${chainId}, but smart chain not known!`);
|
|
1809
|
+
continue;
|
|
1810
|
+
}
|
|
1811
|
+
await this._chains[chainId].unifiedSwapStorage.removeAll(currentChainSwaps);
|
|
1812
|
+
this.logger.debug(`wipeStorage(): Successfully removed ${currentChainSwaps.length} swaps on ${chainId}!`);
|
|
1813
|
+
}
|
|
1814
|
+
} else {
|
|
1815
|
+
if(this._chains[chainId]==null) throw new Error(`wipeStorage(): Smart chain with identifier ${chainId} not found!`);
|
|
1816
|
+
const swaps = await this.getAllSwaps(chainId, signer);
|
|
1817
|
+
await this._chains[chainId].unifiedSwapStorage.removeAll(swaps);
|
|
1818
|
+
this.logger.debug(`wipeStorage(): Successfully removed ${swaps.length} swaps on ${chainId}!`);
|
|
1819
|
+
}
|
|
1820
|
+
}
|
|
1821
|
+
|
|
1822
|
+
/**
|
|
1823
|
+
* Synchronizes swaps from on-chain, this is ran automatically when SDK is initialized, hence
|
|
1824
|
+
* should only be ran manually when `dontCheckPastSwaps=true` is passed in the swapper options,
|
|
1825
|
+
* also deletes expired quotes
|
|
1826
|
+
*
|
|
1827
|
+
* @param chainId Optional chain identifier to only run swap sync for a single smart chain
|
|
1828
|
+
* @param signer Optional signer to only run swap sync for swaps initiated by this signer
|
|
1630
1829
|
*/
|
|
1631
1830
|
async _syncSwaps<C extends ChainIds<T>>(chainId?: C, signer?: string): Promise<void> {
|
|
1632
1831
|
if(chainId==null) {
|
|
1633
|
-
await Promise.all(Object.keys(this.
|
|
1832
|
+
await Promise.all(Object.keys(this._chains).map((chainId) => {
|
|
1634
1833
|
return this.syncSwapsForChain(chainId, signer);
|
|
1635
1834
|
}));
|
|
1636
1835
|
} else {
|
|
@@ -1639,20 +1838,30 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1639
1838
|
}
|
|
1640
1839
|
|
|
1641
1840
|
/**
|
|
1642
|
-
*
|
|
1841
|
+
* Recovers swaps from on-chain historical data.
|
|
1842
|
+
*
|
|
1843
|
+
* Please note that the recovered swaps might not be complete (i.e. missing amounts or addresses), as some
|
|
1844
|
+
* of the swap data is purely off-chain and can never be recovered purely from on-chain data. This
|
|
1845
|
+
* functions tries to recover as much swap data as possible.
|
|
1643
1846
|
*
|
|
1644
|
-
* @param chainId
|
|
1645
|
-
* @param signer
|
|
1646
|
-
* @param startBlockheight
|
|
1847
|
+
* @param chainId Smart chain identifier string to recover the swaps from
|
|
1848
|
+
* @param signer Signer address to recover the swaps for
|
|
1849
|
+
* @param startBlockheight Optional starting blockheight for swap data recovery, will only check swaps
|
|
1850
|
+
* initiated after this blockheight
|
|
1647
1851
|
*/
|
|
1648
1852
|
async recoverSwaps<C extends ChainIds<T>>(chainId: C, signer: string, startBlockheight?: number): Promise<ISwap<T[C]>[]> {
|
|
1649
|
-
const {swapContract, unifiedSwapStorage, reviver, wrappers} = this.
|
|
1853
|
+
const {spvVaultContract, swapContract, unifiedSwapStorage, reviver, wrappers} = this._chains[chainId];
|
|
1650
1854
|
|
|
1651
|
-
if(
|
|
1855
|
+
if(
|
|
1856
|
+
swapContract.getHistoricalSwaps==null ||
|
|
1857
|
+
(spvVaultContract!=null && spvVaultContract.getHistoricalWithdrawalStates==null)
|
|
1858
|
+
) throw new Error(`Historical swap recovery is not supported for ${chainId}`);
|
|
1652
1859
|
|
|
1653
|
-
const {swaps} = await swapContract.getHistoricalSwaps(signer);
|
|
1860
|
+
const {swaps} = await swapContract.getHistoricalSwaps(signer, startBlockheight);
|
|
1861
|
+
const spvVaultData = await spvVaultContract?.getHistoricalWithdrawalStates!(signer, startBlockheight);
|
|
1654
1862
|
|
|
1655
1863
|
const escrowHashes = Object.keys(swaps);
|
|
1864
|
+
if(spvVaultData!=null) Object.keys(spvVaultData.withdrawals).forEach(btcTxId => escrowHashes.push(btcTxId));
|
|
1656
1865
|
this.logger.debug(`recoverSwaps(): Loaded on-chain data for ${escrowHashes.length} swaps`);
|
|
1657
1866
|
this.logger.debug(`recoverSwaps(): Fetching if swap escrowHashes are known: ${escrowHashes.join(", ")}`);
|
|
1658
1867
|
const knownSwapsArray = await unifiedSwapStorage.query([[{key: "escrowHash", value: escrowHashes}]], reviver);
|
|
@@ -1668,17 +1877,23 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1668
1877
|
for(let escrowHash in swaps) {
|
|
1669
1878
|
const {init, state} = swaps[escrowHash];
|
|
1670
1879
|
const knownSwap = knownSwaps[escrowHash];
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1880
|
+
|
|
1881
|
+
if(knownSwap==null) {
|
|
1882
|
+
if(init==null) {
|
|
1883
|
+
this.logger.warn(`recoverSwaps(escrow): Fetched ${escrowHash} swap state, but swap not found locally!`);
|
|
1884
|
+
continue;
|
|
1885
|
+
}
|
|
1886
|
+
} else if(knownSwap instanceof IEscrowSwap) {
|
|
1887
|
+
this.logger.debug(`recoverSwaps(escrow): Forcibly updating ${escrowHash} swap: swap already known and in local storage!`);
|
|
1888
|
+
if(await knownSwap._forciblySetOnchainState(state)) {
|
|
1889
|
+
await knownSwap._save();
|
|
1890
|
+
}
|
|
1675
1891
|
continue;
|
|
1676
|
-
}
|
|
1677
|
-
|
|
1678
|
-
//TODO: Update the existing swaps here
|
|
1679
|
-
this.logger.debug(`recoverSwaps(): Skipping ${escrowHash} swap: swap already known and in local storage!`);
|
|
1892
|
+
} else {
|
|
1893
|
+
this.logger.debug(`recoverSwaps(escrow): Skipping ${escrowHash} swap: swap already known and in local storage!`);
|
|
1680
1894
|
continue;
|
|
1681
1895
|
}
|
|
1896
|
+
|
|
1682
1897
|
const data = init.data;
|
|
1683
1898
|
|
|
1684
1899
|
//Classify swap
|
|
@@ -1715,7 +1930,50 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1715
1930
|
if(swap!=null) {
|
|
1716
1931
|
recoveredSwaps.push(swap);
|
|
1717
1932
|
} else {
|
|
1718
|
-
if(typeIdentified) this.logger.debug(`recoverSwaps(): Swap data type correctly identified but swap returned is null for swap ${escrowHash}`);
|
|
1933
|
+
if(typeIdentified) this.logger.debug(`recoverSwaps(escrow): Swap data type correctly identified but swap returned is null for swap ${escrowHash}`);
|
|
1934
|
+
}
|
|
1935
|
+
}
|
|
1936
|
+
|
|
1937
|
+
if(spvVaultContract!=null && spvVaultData!=null) {
|
|
1938
|
+
const vaultsData = await spvVaultContract.getMultipleVaultData(
|
|
1939
|
+
Object.keys(spvVaultData.withdrawals)
|
|
1940
|
+
.map(btcTxId => ({
|
|
1941
|
+
owner: spvVaultData.withdrawals[btcTxId].owner,
|
|
1942
|
+
vaultId: spvVaultData.withdrawals[btcTxId].vaultId
|
|
1943
|
+
}))
|
|
1944
|
+
);
|
|
1945
|
+
|
|
1946
|
+
for(let btcTxId in spvVaultData.withdrawals) {
|
|
1947
|
+
const state = spvVaultData.withdrawals[btcTxId];
|
|
1948
|
+
const knownSwap = knownSwaps[btcTxId];
|
|
1949
|
+
|
|
1950
|
+
if(knownSwap!=null) {
|
|
1951
|
+
if(knownSwap instanceof SpvFromBTCSwap) {
|
|
1952
|
+
this.logger.debug(`recoverSwaps(spv_vault): Forcibly updating ${btcTxId} swap: swap already known and in local storage!`);
|
|
1953
|
+
//TODO: Forcibly set on-chain state to the swap
|
|
1954
|
+
// if(await knownSwap._forciblySetOnchainState(state)) {
|
|
1955
|
+
// await knownSwap._save();
|
|
1956
|
+
// }
|
|
1957
|
+
continue;
|
|
1958
|
+
} else {
|
|
1959
|
+
this.logger.debug(`recoverSwaps(spv_vault): Skipping ${btcTxId} swap: swap already known and in local storage!`);
|
|
1960
|
+
continue;
|
|
1961
|
+
}
|
|
1962
|
+
}
|
|
1963
|
+
|
|
1964
|
+
const lp = this.intermediaryDiscovery.intermediaries.find(
|
|
1965
|
+
val => val.supportsChain(chainId) && state.owner.toLowerCase()===val.getAddress(chainId).toLowerCase()
|
|
1966
|
+
);
|
|
1967
|
+
const swap = await wrappers[SwapType.SPV_VAULT_FROM_BTC].recoverFromState(
|
|
1968
|
+
state,
|
|
1969
|
+
vaultsData[state.owner]?.[state.vaultId.toString(10)],
|
|
1970
|
+
lp
|
|
1971
|
+
);
|
|
1972
|
+
if(swap!=null) {
|
|
1973
|
+
recoveredSwaps.push(swap);
|
|
1974
|
+
} else {
|
|
1975
|
+
this.logger.debug(`recoverSwaps(spv_vault): Swap data type correctly identified but swap returned is null for swap ${btcTxId}`);
|
|
1976
|
+
}
|
|
1719
1977
|
}
|
|
1720
1978
|
}
|
|
1721
1979
|
|
|
@@ -1724,6 +1982,14 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1724
1982
|
return recoveredSwaps;
|
|
1725
1983
|
}
|
|
1726
1984
|
|
|
1985
|
+
/**
|
|
1986
|
+
* Returns the {@link Token} object for a given token
|
|
1987
|
+
*
|
|
1988
|
+
* @param tickerOrAddress Token to return the object for, can use multiple formats:
|
|
1989
|
+
* - a) token ticker, such as `"BTC"`, `"SOL"`, etc.
|
|
1990
|
+
* - b) token ticker prefixed with smart chain identifier, such as `"SOLANA-SOL"`, `"SOLANA-USDC"`, etc.
|
|
1991
|
+
* - c) token address
|
|
1992
|
+
*/
|
|
1727
1993
|
getToken(tickerOrAddress: string): Token<ChainIds<T>> {
|
|
1728
1994
|
//Btc tokens - BTC, BTCLN, BTC-LN
|
|
1729
1995
|
if(tickerOrAddress==="BTC") return BitcoinTokens.BTC;
|
|
@@ -1732,21 +1998,21 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1732
1998
|
//Check if the ticker is in format <chainId>-<ticker>, i.e. SOLANA-USDC, STARKNET-WBTC
|
|
1733
1999
|
if(tickerOrAddress.includes("-")) {
|
|
1734
2000
|
const [chainId, ticker] = tickerOrAddress.split("-");
|
|
1735
|
-
const token = this.
|
|
2001
|
+
const token = this._tokensByTicker[chainId]?.[ticker];
|
|
1736
2002
|
if(token==null) throw new UserError(`Not found ticker: ${ticker} for chainId: ${chainId}`);
|
|
1737
2003
|
return token;
|
|
1738
2004
|
}
|
|
1739
2005
|
|
|
1740
2006
|
const possibleTokens: SCToken[] = [];
|
|
1741
|
-
for(let chainId in this.
|
|
1742
|
-
const chain = this.
|
|
2007
|
+
for(let chainId in this._chains) {
|
|
2008
|
+
const chain = this._chains[chainId];
|
|
1743
2009
|
if(chain.chainInterface.isValidToken(tickerOrAddress)) {
|
|
1744
2010
|
//Try to find in known token addresses
|
|
1745
|
-
const token = this.
|
|
2011
|
+
const token = this._tokens[chainId]?.[tickerOrAddress];
|
|
1746
2012
|
if(token!=null) return token;
|
|
1747
2013
|
} else {
|
|
1748
2014
|
//Check in known tickers
|
|
1749
|
-
const token = this.
|
|
2015
|
+
const token = this._tokensByTicker[chainId]?.[tickerOrAddress];
|
|
1750
2016
|
if(token!=null) possibleTokens.push(token);
|
|
1751
2017
|
}
|
|
1752
2018
|
}
|
|
@@ -1760,44 +2026,62 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1760
2026
|
/**
|
|
1761
2027
|
* Creates a child swapper instance with a given smart chain
|
|
1762
2028
|
*
|
|
1763
|
-
* @param chainIdentifier
|
|
2029
|
+
* @param chainIdentifier Smart chain identifier for the created child swapper instance
|
|
1764
2030
|
*/
|
|
1765
2031
|
withChain<ChainIdentifier extends ChainIds<T>>(chainIdentifier: ChainIdentifier): SwapperWithChain<T, ChainIdentifier> {
|
|
1766
|
-
if(this.
|
|
2032
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
1767
2033
|
return new SwapperWithChain<T, ChainIdentifier>(this, chainIdentifier as ChainIdentifier);
|
|
1768
2034
|
}
|
|
1769
2035
|
|
|
1770
2036
|
/**
|
|
1771
|
-
* Returns supported smart chains
|
|
2037
|
+
* Returns an array of all the supported smart chains
|
|
1772
2038
|
*/
|
|
1773
2039
|
getSmartChains(): ChainIds<T>[] {
|
|
1774
|
-
return Object.keys(this.
|
|
2040
|
+
return Object.keys(this._chains);
|
|
1775
2041
|
}
|
|
1776
2042
|
|
|
1777
2043
|
/**
|
|
1778
2044
|
* Returns whether the SDK supports a given swap type on a given chain based on currently known LPs
|
|
1779
2045
|
*
|
|
1780
|
-
* @param chainId
|
|
1781
|
-
* @param swapType
|
|
2046
|
+
* @param chainId Smart chain identifier string
|
|
2047
|
+
* @param swapType Swap protocol type
|
|
1782
2048
|
*/
|
|
1783
2049
|
supportsSwapType<
|
|
1784
2050
|
ChainIdentifier extends ChainIds<T>,
|
|
1785
2051
|
Type extends SwapType
|
|
1786
2052
|
>(chainId: ChainIdentifier, swapType: Type): SupportsSwapType<T[ChainIdentifier], Type> {
|
|
1787
|
-
return (this.
|
|
2053
|
+
return (this._chains[chainId]?.wrappers[swapType] != null) as any;
|
|
1788
2054
|
}
|
|
1789
2055
|
|
|
1790
2056
|
/**
|
|
1791
|
-
*
|
|
1792
|
-
*
|
|
1793
|
-
* @param srcToken
|
|
1794
|
-
* @param dstToken
|
|
2057
|
+
* @internal
|
|
1795
2058
|
*/
|
|
1796
2059
|
getSwapType<C extends ChainIds<T>>(srcToken: BtcToken<true>, dstToken: SCToken<C>): (SupportsSwapType<T[C], SwapType.FROM_BTCLN_AUTO> extends true ? SwapType.FROM_BTCLN_AUTO : SwapType.FROM_BTCLN);
|
|
2060
|
+
/**
|
|
2061
|
+
* @internal
|
|
2062
|
+
*/
|
|
1797
2063
|
getSwapType<C extends ChainIds<T>>(srcToken: BtcToken<false>, dstToken: SCToken<C>): (SupportsSwapType<T[C], SwapType.SPV_VAULT_FROM_BTC> extends true ? SwapType.SPV_VAULT_FROM_BTC : SwapType.FROM_BTC);
|
|
2064
|
+
/**
|
|
2065
|
+
* @internal
|
|
2066
|
+
*/
|
|
1798
2067
|
getSwapType<C extends ChainIds<T>>(srcToken: SCToken<C>, dstToken: BtcToken<false>): SwapType.TO_BTC;
|
|
2068
|
+
/**
|
|
2069
|
+
* @internal
|
|
2070
|
+
*/
|
|
1799
2071
|
getSwapType<C extends ChainIds<T>>(srcToken: SCToken<C>, dstToken: BtcToken<true>): SwapType.TO_BTCLN;
|
|
2072
|
+
/**
|
|
2073
|
+
* Returns type of the swap based on input and output tokens specified
|
|
2074
|
+
*
|
|
2075
|
+
* @param srcToken Source token
|
|
2076
|
+
* @param dstToken Destination token
|
|
2077
|
+
*/
|
|
1800
2078
|
getSwapType<C extends ChainIds<T>>(srcToken: Token<C>, dstToken: Token<C>): SwapType.FROM_BTCLN_AUTO | SwapType.FROM_BTCLN | SwapType.SPV_VAULT_FROM_BTC | SwapType.FROM_BTC | SwapType.TO_BTC | SwapType.TO_BTCLN;
|
|
2079
|
+
/**
|
|
2080
|
+
* Returns type of the swap based on input and output tokens specified
|
|
2081
|
+
*
|
|
2082
|
+
* @param srcToken Source token
|
|
2083
|
+
* @param dstToken Destination token
|
|
2084
|
+
*/
|
|
1801
2085
|
getSwapType<C extends ChainIds<T>>(srcToken: Token<C>, dstToken: Token<C>): SwapType.FROM_BTCLN_AUTO | SwapType.FROM_BTCLN | SwapType.SPV_VAULT_FROM_BTC | SwapType.FROM_BTC | SwapType.TO_BTC | SwapType.TO_BTCLN {
|
|
1802
2086
|
if(isSCToken(srcToken)) {
|
|
1803
2087
|
if(!isBtcToken(dstToken)) throw new Error("Swap not supported");
|
|
@@ -1825,54 +2109,29 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1825
2109
|
throw new Error("Swap not supported");
|
|
1826
2110
|
}
|
|
1827
2111
|
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
requiresInputWallet: false,
|
|
1846
|
-
requiresOutputWallet: true,
|
|
1847
|
-
supportsGasDrop: false
|
|
1848
|
-
},
|
|
1849
|
-
[SwapType.SPV_VAULT_FROM_BTC]: {
|
|
1850
|
-
requiresInputWallet: true,
|
|
1851
|
-
requiresOutputWallet: false,
|
|
1852
|
-
supportsGasDrop: true
|
|
1853
|
-
},
|
|
1854
|
-
[SwapType.FROM_BTCLN_AUTO]: {
|
|
1855
|
-
requiresInputWallet: false,
|
|
1856
|
-
requiresOutputWallet: false,
|
|
1857
|
-
supportsGasDrop: true
|
|
1858
|
-
},
|
|
1859
|
-
[SwapType.TRUSTED_FROM_BTC]: {
|
|
1860
|
-
requiresInputWallet: false,
|
|
1861
|
-
requiresOutputWallet: false,
|
|
1862
|
-
supportsGasDrop: false
|
|
1863
|
-
},
|
|
1864
|
-
[SwapType.TRUSTED_FROM_BTCLN]: {
|
|
1865
|
-
requiresInputWallet: false,
|
|
1866
|
-
requiresOutputWallet: false,
|
|
1867
|
-
supportsGasDrop: false
|
|
1868
|
-
}
|
|
1869
|
-
} as const;
|
|
2112
|
+
/**
|
|
2113
|
+
* Helper information about various swap protocol and their features:
|
|
2114
|
+
* - `requiresInputWallet`: Whether a swap requires a connected wallet on the input chain able to sign
|
|
2115
|
+
* arbitrary transaction
|
|
2116
|
+
* - `requiresOutputWallet`: Whether a swap requires a connected wallet on the output chain able to sign
|
|
2117
|
+
* arbitrary transactions
|
|
2118
|
+
* - `supportsGasDrop`: Whether a swap supports the "gas drop" feature, allowing to user to receive a small
|
|
2119
|
+
* amount of native token as part of the swap when swapping to smart chains
|
|
2120
|
+
*
|
|
2121
|
+
* Uses a `Record` type here, use the {@link SwapProtocolInfo} import for a literal readonly type, with
|
|
2122
|
+
* pre-filled exact values in the type.
|
|
2123
|
+
*/
|
|
2124
|
+
readonly SwapTypeInfo = SwapProtocolInfo as Record<SwapType, {
|
|
2125
|
+
requiresInputWallet: boolean,
|
|
2126
|
+
requiresOutputWallet: boolean,
|
|
2127
|
+
supportsGasDrop: boolean
|
|
2128
|
+
}>;
|
|
1870
2129
|
|
|
1871
2130
|
/**
|
|
1872
2131
|
* Returns minimum/maximum limits for inputs and outputs for a swap between given tokens
|
|
1873
2132
|
*
|
|
1874
|
-
* @param srcToken
|
|
1875
|
-
* @param dstToken
|
|
2133
|
+
* @param srcToken Source token
|
|
2134
|
+
* @param dstToken Destination token
|
|
1876
2135
|
*/
|
|
1877
2136
|
getSwapLimits<C extends ChainIds<T>, A extends Token<C>, B extends Token<C>>(srcToken: A, dstToken: B): {
|
|
1878
2137
|
input: {min: TokenAmount<string, A>, max?: TokenAmount<string, A>},
|
|
@@ -1906,7 +2165,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1906
2165
|
}
|
|
1907
2166
|
|
|
1908
2167
|
/**
|
|
1909
|
-
* Returns supported tokens
|
|
2168
|
+
* Returns an array of supported tokens either on the input or on the output of a swap
|
|
1910
2169
|
*
|
|
1911
2170
|
* @param input Whether to return input tokens or output tokens
|
|
1912
2171
|
*/
|
|
@@ -1955,7 +2214,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1955
2214
|
if(btc) output.push(BitcoinTokens.BTC);
|
|
1956
2215
|
for(let chainId in tokens) {
|
|
1957
2216
|
tokens[chainId].forEach(tokenAddress => {
|
|
1958
|
-
const token = this.
|
|
2217
|
+
const token = this._tokens?.[chainId]?.[tokenAddress];
|
|
1959
2218
|
if(token!=null) output.push(token);
|
|
1960
2219
|
})
|
|
1961
2220
|
}
|
|
@@ -1986,7 +2245,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1986
2245
|
const output: SCToken[] = [];
|
|
1987
2246
|
for(let chainId in tokens) {
|
|
1988
2247
|
tokens[chainId].forEach(tokenAddress => {
|
|
1989
|
-
const token = this.
|
|
2248
|
+
const token = this._tokens?.[chainId]?.[tokenAddress];
|
|
1990
2249
|
if(token!=null) output.push(token);
|
|
1991
2250
|
})
|
|
1992
2251
|
}
|
|
@@ -1996,7 +2255,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1996
2255
|
/**
|
|
1997
2256
|
* Returns the set of supported token addresses by all the intermediaries we know of offering a specific swapType service
|
|
1998
2257
|
*
|
|
1999
|
-
* @param chainIdentifier
|
|
2258
|
+
* @param chainIdentifier Chain identifier string
|
|
2000
2259
|
* @param swapType Specific swap type for which to obtain supported tokens
|
|
2001
2260
|
*/
|
|
2002
2261
|
private getSupportedTokenAddresses<ChainIdentifier extends ChainIds<T>>(chainIdentifier: ChainIdentifier, swapType: SwapType): Set<string> {
|
|
@@ -2053,54 +2312,4 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
2053
2312
|
}
|
|
2054
2313
|
}
|
|
2055
2314
|
|
|
2056
|
-
|
|
2057
|
-
///////////////////////////////////
|
|
2058
|
-
/// Deprecated
|
|
2059
|
-
|
|
2060
|
-
/**
|
|
2061
|
-
* Returns swap bounds (minimums & maximums) for different swap types & tokens
|
|
2062
|
-
* @deprecated Use getSwapLimits() instead!
|
|
2063
|
-
*/
|
|
2064
|
-
getSwapBounds<ChainIdentifier extends ChainIds<T>>(chainIdentifier: ChainIdentifier): SwapBounds;
|
|
2065
|
-
getSwapBounds(): MultichainSwapBounds;
|
|
2066
|
-
getSwapBounds<ChainIdentifier extends ChainIds<T>>(chainIdentifier?: ChainIdentifier): SwapBounds | MultichainSwapBounds {
|
|
2067
|
-
if(chainIdentifier==null) {
|
|
2068
|
-
return this.intermediaryDiscovery.getMultichainSwapBounds();
|
|
2069
|
-
} else {
|
|
2070
|
-
return this.intermediaryDiscovery.getSwapBounds(chainIdentifier);
|
|
2071
|
-
}
|
|
2072
|
-
}
|
|
2073
|
-
|
|
2074
|
-
/**
|
|
2075
|
-
* Returns maximum possible swap amount
|
|
2076
|
-
* @deprecated Use getSwapLimits() instead!
|
|
2077
|
-
*
|
|
2078
|
-
* @param chainIdentifier
|
|
2079
|
-
* @param type Type of the swap
|
|
2080
|
-
* @param token Token of the swap
|
|
2081
|
-
*/
|
|
2082
|
-
getMaximum<ChainIdentifier extends ChainIds<T>>(chainIdentifier: ChainIdentifier, type: SwapType, token: string): bigint {
|
|
2083
|
-
if(this.intermediaryDiscovery!=null) {
|
|
2084
|
-
const max = this.intermediaryDiscovery.getSwapMaximum(chainIdentifier, type, token);
|
|
2085
|
-
if(max!=null) return BigInt(max);
|
|
2086
|
-
}
|
|
2087
|
-
return 0n;
|
|
2088
|
-
}
|
|
2089
|
-
|
|
2090
|
-
/**
|
|
2091
|
-
* Returns minimum possible swap amount
|
|
2092
|
-
* @deprecated Use getSwapLimits() instead!
|
|
2093
|
-
*
|
|
2094
|
-
* @param chainIdentifier
|
|
2095
|
-
* @param type Type of swap
|
|
2096
|
-
* @param token Token of the swap
|
|
2097
|
-
*/
|
|
2098
|
-
getMinimum<ChainIdentifier extends ChainIds<T>>(chainIdentifier: ChainIdentifier, type: SwapType, token: string): bigint {
|
|
2099
|
-
if(this.intermediaryDiscovery!=null) {
|
|
2100
|
-
const min = this.intermediaryDiscovery.getSwapMinimum(chainIdentifier, type, token);
|
|
2101
|
-
if(min!=null) return BigInt(min);
|
|
2102
|
-
}
|
|
2103
|
-
return 0n;
|
|
2104
|
-
}
|
|
2105
|
-
|
|
2106
2315
|
}
|