@atomiqlabs/sdk 8.1.7 → 8.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bitcoin/wallet/BitcoinWallet.d.ts +41 -5
- package/dist/bitcoin/wallet/BitcoinWallet.js +36 -1
- package/dist/bitcoin/wallet/IBitcoinWallet.d.ts +52 -2
- package/dist/bitcoin/wallet/IBitcoinWallet.js +2 -1
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.d.ts +42 -7
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.js +36 -1
- package/dist/enums/FeeType.d.ts +7 -0
- package/dist/enums/FeeType.js +7 -0
- package/dist/enums/SwapAmountType.d.ts +7 -0
- package/dist/enums/SwapAmountType.js +7 -0
- package/dist/enums/SwapDirection.d.ts +7 -0
- package/dist/enums/SwapDirection.js +7 -0
- package/dist/enums/SwapType.d.ts +62 -1
- package/dist/enums/SwapType.js +62 -1
- package/dist/errors/IntermediaryError.d.ts +4 -0
- package/dist/errors/IntermediaryError.js +1 -0
- package/dist/errors/RequestError.d.ts +15 -1
- package/dist/errors/RequestError.js +8 -0
- package/dist/errors/UserError.d.ts +1 -0
- package/dist/errors/UserError.js +1 -0
- package/dist/index.d.ts +4 -5
- package/dist/index.js +3 -4
- package/dist/intermediaries/Intermediary.d.ts +57 -10
- package/dist/intermediaries/Intermediary.js +37 -10
- package/dist/intermediaries/IntermediaryDiscovery.d.ts +55 -22
- package/dist/intermediaries/IntermediaryDiscovery.js +35 -22
- package/dist/prices/RedundantSwapPrice.d.ts +24 -3
- package/dist/prices/RedundantSwapPrice.js +21 -1
- package/dist/prices/SingleSwapPrice.d.ts +9 -6
- package/dist/prices/SingleSwapPrice.js +10 -7
- package/dist/prices/SwapPriceWithChain.d.ts +54 -16
- package/dist/prices/SwapPriceWithChain.js +58 -20
- package/dist/prices/abstract/ISwapPrice.d.ts +94 -45
- package/dist/prices/abstract/ISwapPrice.js +103 -55
- package/dist/prices/providers/BinancePriceProvider.d.ts +7 -0
- package/dist/prices/providers/BinancePriceProvider.js +7 -0
- package/dist/prices/providers/CoinGeckoPriceProvider.d.ts +6 -0
- package/dist/prices/providers/CoinGeckoPriceProvider.js +6 -0
- package/dist/prices/providers/CoinPaprikaPriceProvider.d.ts +6 -0
- package/dist/prices/providers/CoinPaprikaPriceProvider.js +6 -0
- package/dist/prices/providers/CustomPriceProvider.d.ts +11 -0
- package/dist/prices/providers/CustomPriceProvider.js +11 -0
- package/dist/prices/providers/KrakenPriceProvider.d.ts +9 -0
- package/dist/prices/providers/KrakenPriceProvider.js +9 -0
- package/dist/prices/providers/OKXPriceProvider.d.ts +6 -0
- package/dist/prices/providers/OKXPriceProvider.js +6 -0
- package/dist/prices/providers/abstract/ExchangePriceProvider.d.ts +3 -0
- package/dist/prices/providers/abstract/ExchangePriceProvider.js +3 -0
- package/dist/storage/IUnifiedStorage.d.ts +19 -7
- package/dist/storage/UnifiedSwapStorage.d.ts +33 -3
- package/dist/storage/UnifiedSwapStorage.js +29 -1
- package/dist/storage-browser/IndexedDBUnifiedStorage.d.ts +31 -7
- package/dist/storage-browser/IndexedDBUnifiedStorage.js +29 -6
- package/dist/storage-browser/LocalStorageManager.d.ts +25 -1
- package/dist/storage-browser/LocalStorageManager.js +25 -1
- package/dist/swapper/Swapper.d.ts +303 -222
- package/dist/swapper/Swapper.js +376 -344
- package/dist/swapper/SwapperFactory.d.ts +41 -17
- package/dist/swapper/SwapperFactory.js +23 -2
- package/dist/swapper/SwapperUtils.d.ts +75 -28
- package/dist/swapper/SwapperUtils.js +107 -60
- package/dist/swapper/SwapperWithChain.d.ts +286 -91
- package/dist/swapper/SwapperWithChain.js +218 -64
- package/dist/swapper/SwapperWithSigner.d.ts +229 -80
- package/dist/swapper/SwapperWithSigner.js +190 -44
- package/dist/swaps/IAddressSwap.d.ts +10 -1
- package/dist/swaps/IAddressSwap.js +2 -1
- package/dist/swaps/IBTCWalletSwap.d.ts +24 -6
- package/dist/swaps/IBTCWalletSwap.js +2 -1
- package/dist/swaps/IClaimableSwap.d.ts +36 -4
- package/dist/swaps/IClaimableSwap.js +2 -1
- package/dist/swaps/IClaimableSwapWrapper.d.ts +11 -1
- package/dist/swaps/IRefundableSwap.d.ts +29 -3
- package/dist/swaps/IRefundableSwap.js +2 -1
- package/dist/swaps/ISwap.d.ts +159 -21
- package/dist/swaps/ISwap.js +90 -33
- package/dist/swaps/ISwapWithGasDrop.d.ts +6 -0
- package/dist/swaps/ISwapWithGasDrop.js +1 -0
- package/dist/swaps/ISwapWrapper.d.ts +157 -48
- package/dist/swaps/ISwapWrapper.js +130 -72
- package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.d.ts +49 -6
- package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.js +22 -12
- package/dist/swaps/escrow_swaps/IEscrowSwap.d.ts +65 -12
- package/dist/swaps/escrow_swaps/IEscrowSwap.js +38 -19
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.d.ts +39 -9
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.js +30 -21
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.d.ts +31 -15
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.js +33 -18
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.d.ts +94 -29
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.js +90 -27
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.d.ts +22 -9
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.js +24 -11
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.d.ts +275 -58
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +516 -239
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +76 -25
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +131 -49
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.d.ts +311 -51
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +542 -193
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +87 -26
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +147 -58
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.d.ts +209 -53
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +449 -242
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.d.ts +77 -23
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.js +116 -46
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.d.ts +197 -56
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +326 -189
- package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.d.ts +30 -5
- package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.js +44 -19
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.d.ts +60 -19
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.js +74 -31
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.d.ts +76 -50
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.js +106 -101
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.d.ts +36 -13
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.js +65 -19
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.d.ts +46 -17
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.js +82 -27
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +328 -92
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +460 -219
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.d.ts +76 -24
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.js +244 -124
- package/dist/swaps/trusted/ln/LnForGasSwap.d.ts +146 -18
- package/dist/swaps/trusted/ln/LnForGasSwap.js +173 -43
- package/dist/swaps/trusted/ln/LnForGasWrapper.d.ts +29 -10
- package/dist/swaps/trusted/ln/LnForGasWrapper.js +30 -11
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.d.ts +200 -47
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.js +230 -78
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.d.ts +34 -12
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.js +33 -14
- package/dist/types/AmountData.d.ts +2 -1
- package/dist/types/CustomPriceFunction.d.ts +7 -1
- package/dist/types/SwapExecutionAction.d.ts +74 -4
- package/dist/types/SwapWithSigner.d.ts +4 -1
- package/dist/types/SwapWithSigner.js +5 -2
- package/dist/types/Token.d.ts +11 -5
- package/dist/types/Token.js +6 -3
- package/dist/types/TokenAmount.d.ts +3 -0
- package/dist/types/TokenAmount.js +2 -0
- package/dist/types/fees/Fee.d.ts +2 -1
- package/dist/types/fees/FeeBreakdown.d.ts +2 -1
- package/dist/types/fees/PercentagePPM.d.ts +2 -0
- package/dist/types/fees/PercentagePPM.js +1 -0
- package/dist/types/lnurl/LNURLPay.d.ts +14 -6
- package/dist/types/lnurl/LNURLPay.js +6 -2
- package/dist/types/lnurl/LNURLWithdraw.d.ts +12 -5
- package/dist/types/lnurl/LNURLWithdraw.js +6 -2
- package/dist/types/wallets/LightningInvoiceCreateService.d.ts +20 -0
- package/dist/types/wallets/LightningInvoiceCreateService.js +15 -0
- package/dist/types/wallets/MinimalBitcoinWalletInterface.d.ts +3 -1
- package/dist/types/wallets/MinimalLightningNetworkWalletInterface.d.ts +3 -1
- package/dist/utils/BitcoinUtils.d.ts +1 -0
- package/dist/utils/BitcoinUtils.js +5 -1
- package/dist/utils/SwapUtils.d.ts +56 -1
- package/dist/utils/SwapUtils.js +53 -1
- package/dist/utils/TokenUtils.d.ts +10 -2
- package/dist/utils/TokenUtils.js +12 -4
- package/package.json +3 -3
- package/src/bitcoin/wallet/BitcoinWallet.ts +41 -5
- package/src/bitcoin/wallet/IBitcoinWallet.ts +57 -2
- package/src/bitcoin/wallet/SingleAddressBitcoinWallet.ts +42 -6
- package/src/enums/FeeType.ts +7 -0
- package/src/enums/SwapAmountType.ts +7 -0
- package/src/enums/SwapDirection.ts +7 -0
- package/src/enums/SwapType.ts +62 -2
- package/src/errors/IntermediaryError.ts +4 -0
- package/src/errors/RequestError.ts +15 -1
- package/src/errors/UserError.ts +1 -0
- package/src/index.ts +6 -5
- package/src/intermediaries/Intermediary.ts +57 -10
- package/src/intermediaries/IntermediaryDiscovery.ts +60 -27
- package/src/prices/RedundantSwapPrice.ts +24 -4
- package/src/prices/SingleSwapPrice.ts +10 -7
- package/src/prices/SwapPriceWithChain.ts +59 -21
- package/src/prices/abstract/ISwapPrice.ts +114 -65
- package/src/prices/providers/BinancePriceProvider.ts +7 -0
- package/src/prices/providers/CoinGeckoPriceProvider.ts +6 -0
- package/src/prices/providers/CoinPaprikaPriceProvider.ts +6 -0
- package/src/prices/providers/CustomPriceProvider.ts +11 -0
- package/src/prices/providers/KrakenPriceProvider.ts +9 -0
- package/src/prices/providers/OKXPriceProvider.ts +6 -0
- package/src/prices/providers/abstract/ExchangePriceProvider.ts +3 -0
- package/src/storage/IUnifiedStorage.ts +19 -7
- package/src/storage/UnifiedSwapStorage.ts +33 -3
- package/src/storage-browser/IndexedDBUnifiedStorage.ts +31 -8
- package/src/storage-browser/LocalStorageManager.ts +25 -1
- package/src/swapper/Swapper.ts +513 -379
- package/src/swapper/SwapperFactory.ts +44 -21
- package/src/swapper/SwapperUtils.ts +107 -60
- package/src/swapper/SwapperWithChain.ts +320 -81
- package/src/swapper/SwapperWithSigner.ts +263 -56
- package/src/swaps/IAddressSwap.ts +11 -1
- package/src/swaps/IBTCWalletSwap.ts +24 -8
- package/src/swaps/IClaimableSwap.ts +39 -4
- package/src/swaps/IClaimableSwapWrapper.ts +11 -2
- package/src/swaps/IRefundableSwap.ts +32 -3
- package/src/swaps/ISwap.ts +221 -82
- package/src/swaps/ISwapWithGasDrop.ts +6 -0
- package/src/swaps/ISwapWrapper.ts +212 -94
- package/src/swaps/escrow_swaps/IEscrowSelfInitSwap.ts +62 -18
- package/src/swaps/escrow_swaps/IEscrowSwap.ts +83 -37
- package/src/swaps/escrow_swaps/IEscrowSwapWrapper.ts +61 -30
- package/src/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.ts +37 -19
- package/src/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.ts +120 -51
- package/src/swaps/escrow_swaps/frombtc/IFromBTCWrapper.ts +24 -11
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.ts +559 -256
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +155 -61
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +590 -226
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +177 -74
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.ts +470 -243
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.ts +141 -59
- package/src/swaps/escrow_swaps/tobtc/IToBTCSwap.ts +352 -193
- package/src/swaps/escrow_swaps/tobtc/IToBTCWrapper.ts +48 -23
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.ts +86 -39
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.ts +110 -110
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.ts +88 -33
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.ts +101 -31
- package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +534 -263
- package/src/swaps/spv_swaps/SpvFromBTCWrapper.ts +289 -148
- package/src/swaps/trusted/ln/LnForGasSwap.ts +184 -45
- package/src/swaps/trusted/ln/LnForGasWrapper.ts +34 -15
- package/src/swaps/trusted/onchain/OnchainForGasSwap.ts +260 -86
- package/src/swaps/trusted/onchain/OnchainForGasWrapper.ts +41 -19
- package/src/types/AmountData.ts +2 -1
- package/src/types/CustomPriceFunction.ts +7 -1
- package/src/types/SwapExecutionAction.ts +84 -5
- package/src/types/SwapWithSigner.ts +7 -3
- package/src/types/Token.ts +12 -5
- package/src/types/TokenAmount.ts +3 -0
- package/src/types/fees/Fee.ts +2 -1
- package/src/types/fees/FeeBreakdown.ts +2 -1
- package/src/types/fees/PercentagePPM.ts +2 -0
- package/src/types/lnurl/LNURLPay.ts +14 -6
- package/src/types/lnurl/LNURLWithdraw.ts +12 -5
- package/src/types/wallets/LightningInvoiceCreateService.ts +26 -0
- package/src/types/wallets/MinimalBitcoinWalletInterface.ts +3 -1
- package/src/types/wallets/MinimalLightningNetworkWalletInterface.ts +3 -1
- package/src/utils/BitcoinUtils.ts +5 -0
- package/src/utils/SwapUtils.ts +61 -1
- package/src/utils/TokenUtils.ts +12 -4
- package/dist/bitcoin/BitcoinRpcWithAddressIndex.d.ts +0 -68
- package/dist/bitcoin/BitcoinRpcWithAddressIndex.js +0 -2
- package/dist/bitcoin/LightningNetworkApi.d.ts +0 -12
- package/dist/bitcoin/LightningNetworkApi.js +0 -2
- package/dist/bitcoin/mempool/MempoolApi.d.ts +0 -350
- package/dist/bitcoin/mempool/MempoolApi.js +0 -311
- package/dist/bitcoin/mempool/MempoolBitcoinBlock.d.ts +0 -44
- package/dist/bitcoin/mempool/MempoolBitcoinBlock.js +0 -48
- package/dist/bitcoin/mempool/MempoolBitcoinRpc.d.ts +0 -119
- package/dist/bitcoin/mempool/MempoolBitcoinRpc.js +0 -361
- package/dist/bitcoin/mempool/synchronizer/MempoolBtcRelaySynchronizer.d.ts +0 -22
- package/dist/bitcoin/mempool/synchronizer/MempoolBtcRelaySynchronizer.js +0 -105
- package/dist/errors/PaymentAuthError.d.ts +0 -11
- package/dist/errors/PaymentAuthError.js +0 -23
- package/src/errors/PaymentAuthError.ts +0 -26
package/src/swapper/Swapper.ts
CHANGED
|
@@ -9,8 +9,6 @@ import {
|
|
|
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";
|
|
@@ -65,6 +63,8 @@ import {isLNURLPay, LNURLPay} from "../types/lnurl/LNURLPay";
|
|
|
65
63
|
import {tryWithRetries} from "../utils/RetryUtils";
|
|
66
64
|
import {NotNever} from "../utils/TypeUtils";
|
|
67
65
|
import {MempoolApi, MempoolBitcoinBlock, MempoolBitcoinRpc, MempoolBtcRelaySynchronizer} from "@atomiqlabs/btc-mempool";
|
|
66
|
+
import {IEscrowSwap} from "../swaps/escrow_swaps/IEscrowSwap";
|
|
67
|
+
import {LightningInvoiceCreateService, isLightningInvoiceCreateService} from "../types/wallets/LightningInvoiceCreateService";
|
|
68
68
|
|
|
69
69
|
/**
|
|
70
70
|
* Configuration options for the Swapper
|
|
@@ -152,7 +152,8 @@ export type SupportsSwapType<
|
|
|
152
152
|
NotNever<C["Contract"]>;
|
|
153
153
|
|
|
154
154
|
/**
|
|
155
|
-
* Core orchestrator for all swap operations
|
|
155
|
+
* Core orchestrator for all atomiq swap operations
|
|
156
|
+
*
|
|
156
157
|
* @category Core
|
|
157
158
|
*/
|
|
158
159
|
export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
@@ -161,35 +162,64 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
161
162
|
swapState: [ISwap],
|
|
162
163
|
swapLimitsChanged: []
|
|
163
164
|
}> {
|
|
164
|
-
|
|
165
|
-
|
|
165
|
+
private readonly logger = getLogger(this.constructor.name+": ");
|
|
166
|
+
private readonly swapStateListener: (swap: ISwap) => void;
|
|
166
167
|
|
|
167
168
|
private defaultTrustedIntermediary?: Intermediary;
|
|
168
|
-
private readonly
|
|
169
|
+
private readonly bitcoinNetwork: BitcoinNetwork;
|
|
169
170
|
private readonly options: SwapperOptions;
|
|
171
|
+
/**
|
|
172
|
+
* Data propagation layer used for broadcasting messages to watchtowers
|
|
173
|
+
*/
|
|
174
|
+
private readonly messenger: Messenger;
|
|
170
175
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
readonly
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
readonly
|
|
181
|
-
|
|
182
|
-
|
|
176
|
+
/**
|
|
177
|
+
* A dictionary of smart chains used by the SDK
|
|
178
|
+
* @internal
|
|
179
|
+
*/
|
|
180
|
+
readonly _chains: MultiChainData<T>;
|
|
181
|
+
/**
|
|
182
|
+
* Bitcoin RPC for fetching bitcoin chain data
|
|
183
|
+
* @internal
|
|
184
|
+
*/
|
|
185
|
+
readonly _bitcoinRpc: MempoolBitcoinRpc;
|
|
186
|
+
/**
|
|
187
|
+
* Bitcoin network specification
|
|
188
|
+
* @internal
|
|
189
|
+
*/
|
|
190
|
+
readonly _btcNetwork: BTC_NETWORK;
|
|
191
|
+
/**
|
|
192
|
+
* Token data indexed by chain identifier and token addresses
|
|
193
|
+
* @internal
|
|
194
|
+
*/
|
|
195
|
+
readonly _tokens: {
|
|
183
196
|
[chainId: string]: {
|
|
184
197
|
[tokenAddress: string]: SCToken
|
|
185
198
|
}
|
|
186
199
|
};
|
|
187
|
-
|
|
200
|
+
/**
|
|
201
|
+
* Token data indexed by chain identifier and token tickers
|
|
202
|
+
* @internal
|
|
203
|
+
*/
|
|
204
|
+
readonly _tokensByTicker: {
|
|
188
205
|
[chainId: string]: {
|
|
189
206
|
[tokenTicker: string]: SCToken
|
|
190
207
|
}
|
|
191
208
|
};
|
|
192
209
|
|
|
210
|
+
/**
|
|
211
|
+
* Pricing API used by the SDK
|
|
212
|
+
*/
|
|
213
|
+
readonly prices: ISwapPrice<T>;
|
|
214
|
+
/**
|
|
215
|
+
* Intermediary discovery instance
|
|
216
|
+
*/
|
|
217
|
+
readonly intermediaryDiscovery: IntermediaryDiscovery;
|
|
218
|
+
/**
|
|
219
|
+
* Miscellaneous utility functions
|
|
220
|
+
*/
|
|
221
|
+
readonly Utils: SwapperUtils<T>;
|
|
222
|
+
|
|
193
223
|
constructor(
|
|
194
224
|
bitcoinRpc: MempoolBitcoinRpc,
|
|
195
225
|
chainsData: CtorMultiChainData<T>,
|
|
@@ -207,8 +237,8 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
207
237
|
|
|
208
238
|
this.options = options;
|
|
209
239
|
|
|
210
|
-
this.
|
|
211
|
-
this.
|
|
240
|
+
this.bitcoinNetwork = options.bitcoinNetwork;
|
|
241
|
+
this._btcNetwork = options.bitcoinNetwork===BitcoinNetwork.MAINNET ? NETWORK :
|
|
212
242
|
(options.bitcoinNetwork===BitcoinNetwork.TESTNET || options.bitcoinNetwork===BitcoinNetwork.TESTNET4) ? TEST_NETWORK : {
|
|
213
243
|
bech32: 'bcrt',
|
|
214
244
|
pubKeyHash: 111,
|
|
@@ -218,18 +248,17 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
218
248
|
this.Utils = new SwapperUtils(this);
|
|
219
249
|
|
|
220
250
|
this.prices = pricing;
|
|
221
|
-
this.
|
|
222
|
-
this.mempoolApi = bitcoinRpc.api;
|
|
251
|
+
this._bitcoinRpc = bitcoinRpc;
|
|
223
252
|
this.messenger = messenger;
|
|
224
253
|
|
|
225
|
-
this.
|
|
226
|
-
this.
|
|
254
|
+
this._tokens = {};
|
|
255
|
+
this._tokensByTicker = {};
|
|
227
256
|
for(let tokenData of tokens) {
|
|
228
257
|
for(let chainId in tokenData.chains) {
|
|
229
258
|
const chainData = tokenData.chains[chainId]!;
|
|
230
|
-
this.
|
|
231
|
-
this.
|
|
232
|
-
this.
|
|
259
|
+
this._tokens[chainId] ??= {};
|
|
260
|
+
this._tokensByTicker[chainId] ??= {};
|
|
261
|
+
this._tokens[chainId][chainData.address] = this._tokensByTicker[chainId][tokenData.ticker] = {
|
|
233
262
|
chain: "SC",
|
|
234
263
|
chainId,
|
|
235
264
|
ticker: tokenData.ticker,
|
|
@@ -245,7 +274,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
245
274
|
this.emit("swapState", swap);
|
|
246
275
|
};
|
|
247
276
|
|
|
248
|
-
this.
|
|
277
|
+
this._chains = objectMap<CtorMultiChainData<T>, MultiChainData<T>>(chainsData, <InputKey extends keyof CtorMultiChainData<T>>(chainData: CtorMultiChainData<T>[InputKey], key: string) => {
|
|
249
278
|
const {
|
|
250
279
|
swapContract, chainEvents, btcRelay,
|
|
251
280
|
chainInterface, spvVaultContract, spvVaultWithdrawalDataConstructor
|
|
@@ -281,11 +310,11 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
281
310
|
pricing,
|
|
282
311
|
tokens,
|
|
283
312
|
chainData.swapDataConstructor,
|
|
284
|
-
this.
|
|
313
|
+
this._bitcoinRpc,
|
|
285
314
|
{
|
|
286
315
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
287
316
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
288
|
-
bitcoinNetwork: this.
|
|
317
|
+
bitcoinNetwork: this._btcNetwork
|
|
289
318
|
}
|
|
290
319
|
);
|
|
291
320
|
wrappers[SwapType.FROM_BTCLN] = new FromBTCLNWrapper<T[InputKey]>(
|
|
@@ -301,7 +330,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
301
330
|
{
|
|
302
331
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
303
332
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
304
|
-
unsafeSkipLnNodeCheck: this.
|
|
333
|
+
unsafeSkipLnNodeCheck: this.bitcoinNetwork===BitcoinNetwork.TESTNET4 || this.bitcoinNetwork===BitcoinNetwork.REGTEST
|
|
305
334
|
}
|
|
306
335
|
);
|
|
307
336
|
wrappers[SwapType.FROM_BTC] = new FromBTCWrapper<T[InputKey]>(
|
|
@@ -315,11 +344,11 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
315
344
|
chainData.swapDataConstructor,
|
|
316
345
|
btcRelay,
|
|
317
346
|
synchronizer,
|
|
318
|
-
this.
|
|
347
|
+
this._bitcoinRpc,
|
|
319
348
|
{
|
|
320
349
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
321
350
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
322
|
-
bitcoinNetwork: this.
|
|
351
|
+
bitcoinNetwork: this._btcNetwork
|
|
323
352
|
}
|
|
324
353
|
);
|
|
325
354
|
wrappers[SwapType.TRUSTED_FROM_BTCLN] = new LnForGasWrapper<T[InputKey]>(
|
|
@@ -345,7 +374,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
345
374
|
{
|
|
346
375
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
347
376
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
348
|
-
bitcoinNetwork: this.
|
|
377
|
+
bitcoinNetwork: this._btcNetwork
|
|
349
378
|
}
|
|
350
379
|
);
|
|
351
380
|
|
|
@@ -365,7 +394,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
365
394
|
{
|
|
366
395
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
367
396
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
368
|
-
bitcoinNetwork: this.
|
|
397
|
+
bitcoinNetwork: this._btcNetwork
|
|
369
398
|
}
|
|
370
399
|
);
|
|
371
400
|
}
|
|
@@ -385,7 +414,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
385
414
|
{
|
|
386
415
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
387
416
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
388
|
-
unsafeSkipLnNodeCheck: this.
|
|
417
|
+
unsafeSkipLnNodeCheck: this.bitcoinNetwork===BitcoinNetwork.TESTNET4 || this.bitcoinNetwork===BitcoinNetwork.REGTEST
|
|
389
418
|
}
|
|
390
419
|
);
|
|
391
420
|
}
|
|
@@ -393,9 +422,9 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
393
422
|
Object.keys(wrappers).forEach(key => wrappers[key].events.on("swapState", this.swapStateListener));
|
|
394
423
|
|
|
395
424
|
const reviver = (val: any) => {
|
|
396
|
-
const wrapper = wrappers[val.type];
|
|
425
|
+
const wrapper: ISwapWrapper<any, any> = wrappers[val.type];
|
|
397
426
|
if(wrapper==null) return null;
|
|
398
|
-
return new wrapper.
|
|
427
|
+
return new wrapper._swapDeserializer(wrapper, val);
|
|
399
428
|
};
|
|
400
429
|
|
|
401
430
|
return {
|
|
@@ -470,7 +499,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
470
499
|
}
|
|
471
500
|
|
|
472
501
|
const chainPromises = [];
|
|
473
|
-
for(let chainIdentifier in this.
|
|
502
|
+
for(let chainIdentifier in this._chains) {
|
|
474
503
|
chainPromises.push((async() => {
|
|
475
504
|
const {
|
|
476
505
|
swapContract,
|
|
@@ -478,7 +507,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
478
507
|
unifiedSwapStorage,
|
|
479
508
|
wrappers,
|
|
480
509
|
reviver
|
|
481
|
-
} = this.
|
|
510
|
+
} = this._chains[chainIdentifier];
|
|
482
511
|
await swapContract.start();
|
|
483
512
|
this.logger.debug("init(): Intialized swap contract: "+chainIdentifier);
|
|
484
513
|
|
|
@@ -486,8 +515,8 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
486
515
|
if(unifiedSwapStorage.storage instanceof IndexedDBUnifiedStorage) {
|
|
487
516
|
//Try to migrate the data here
|
|
488
517
|
const storagePrefix = chainIdentifier==="SOLANA" ?
|
|
489
|
-
"SOLv4-"+this.
|
|
490
|
-
"atomiqsdk-"+this.
|
|
518
|
+
"SOLv4-"+this.bitcoinNetwork+"-Swaps-" :
|
|
519
|
+
"atomiqsdk-"+this.bitcoinNetwork+chainIdentifier+"-Swaps-";
|
|
491
520
|
await unifiedSwapStorage.storage.tryMigrate(
|
|
492
521
|
[
|
|
493
522
|
[storagePrefix+"FromBTC", SwapType.FROM_BTC],
|
|
@@ -497,9 +526,9 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
497
526
|
],
|
|
498
527
|
(obj: any) => {
|
|
499
528
|
const swap = reviver(obj);
|
|
500
|
-
if(swap.
|
|
529
|
+
if(swap._randomNonce==null) {
|
|
501
530
|
const oldIdentifierHash = swap.getId();
|
|
502
|
-
swap.
|
|
531
|
+
swap._randomNonce = randomBytes(16).toString("hex");
|
|
503
532
|
const newIdentifierHash = swap.getId();
|
|
504
533
|
this.logger.info("init(): Found older swap version without randomNonce, replacing, old hash: "+oldIdentifierHash+
|
|
505
534
|
" new hash: "+newIdentifierHash);
|
|
@@ -553,11 +582,11 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
553
582
|
*/
|
|
554
583
|
async stop() {
|
|
555
584
|
if(this.initPromise) await this.initPromise;
|
|
556
|
-
for(let chainIdentifier in this.
|
|
585
|
+
for(let chainIdentifier in this._chains) {
|
|
557
586
|
const {
|
|
558
587
|
wrappers,
|
|
559
588
|
unifiedChainEvents
|
|
560
|
-
} = this.
|
|
589
|
+
} = this._chains[chainIdentifier];
|
|
561
590
|
for(let key in wrappers) {
|
|
562
591
|
const wrapper = wrappers[key as unknown as SwapType];
|
|
563
592
|
wrapper.events.removeListener("swapState", this.swapStateListener);
|
|
@@ -592,7 +621,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
592
621
|
maxWaitTimeMS: number = 2000
|
|
593
622
|
): Promise<S> {
|
|
594
623
|
if(!this.initialized) throw new Error("Swapper not initialized, init first with swapper.init()!");
|
|
595
|
-
if(this.
|
|
624
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
596
625
|
let candidates: Intermediary[];
|
|
597
626
|
|
|
598
627
|
const inBtc: boolean = swapType===SwapType.TO_BTCLN || swapType===SwapType.TO_BTC ? !amountData.exactIn : amountData.exactIn;
|
|
@@ -632,7 +661,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
632
661
|
|
|
633
662
|
const abortController = new AbortController();
|
|
634
663
|
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.
|
|
664
|
+
const quotePromises: {quote: Promise<S>, intermediary: Intermediary}[] = await create(candidates, abortController.signal, this._chains[chainIdentifier]);
|
|
636
665
|
|
|
637
666
|
const promiseAll = new Promise<{
|
|
638
667
|
quote: S,
|
|
@@ -743,16 +772,16 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
743
772
|
}
|
|
744
773
|
|
|
745
774
|
/**
|
|
746
|
-
* Creates
|
|
775
|
+
* Creates Smart chain -> Bitcoin ({@link SwapType.TO_BTC}) swap
|
|
747
776
|
*
|
|
748
|
-
* @param chainIdentifier
|
|
749
|
-
* @param signer
|
|
750
|
-
* @param tokenAddress
|
|
751
|
-
* @param address
|
|
752
|
-
* @param amount
|
|
753
|
-
* @param exactIn
|
|
754
|
-
* @param additionalParams
|
|
755
|
-
* @param options
|
|
777
|
+
* @param chainIdentifier Chain identifier string of the source smart chain
|
|
778
|
+
* @param signer Signer's address on the source chain
|
|
779
|
+
* @param tokenAddress Token address to pay with
|
|
780
|
+
* @param address Recipient's bitcoin address
|
|
781
|
+
* @param amount Amount to send in token based units (if `exactIn=true`) or receive in satoshis (if `exactIn=false`)
|
|
782
|
+
* @param exactIn Whether to use exact in instead of exact out
|
|
783
|
+
* @param additionalParams Additional parameters sent to the LP when creating the swap
|
|
784
|
+
* @param options Additional options for the swap
|
|
756
785
|
*/
|
|
757
786
|
createToBTCSwap<ChainIdentifier extends ChainIds<T>>(
|
|
758
787
|
chainIdentifier: ChainIdentifier,
|
|
@@ -764,13 +793,13 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
764
793
|
additionalParams: Record<string, any> | undefined = this.options.defaultAdditionalParameters,
|
|
765
794
|
options?: ToBTCOptions
|
|
766
795
|
): Promise<ToBTCSwap<T[ChainIdentifier]>> {
|
|
767
|
-
if(this.
|
|
796
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
768
797
|
if(address.startsWith("bitcoin:")) {
|
|
769
798
|
address = address.substring(8).split("?")[0];
|
|
770
799
|
}
|
|
771
800
|
if(!this.Utils.isValidBitcoinAddress(address)) throw new Error("Invalid bitcoin address");
|
|
772
|
-
if(!this.
|
|
773
|
-
signer = this.
|
|
801
|
+
if(!this._chains[chainIdentifier].chainInterface.isValidAddress(signer, true)) throw new Error("Invalid "+chainIdentifier+" address");
|
|
802
|
+
signer = this._chains[chainIdentifier].chainInterface.normalizeAddress(signer);
|
|
774
803
|
options ??= {};
|
|
775
804
|
options.confirmationTarget ??= 3;
|
|
776
805
|
options.confirmations ??= 2;
|
|
@@ -796,14 +825,15 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
796
825
|
}
|
|
797
826
|
|
|
798
827
|
/**
|
|
799
|
-
* Creates
|
|
828
|
+
* Creates Smart chain -> Bitcoin Lightning ({@link SwapType.TO_BTCLN}) swap
|
|
800
829
|
*
|
|
801
|
-
* @param chainIdentifier
|
|
802
|
-
* @param signer
|
|
803
|
-
* @param tokenAddress
|
|
804
|
-
* @param paymentRequest
|
|
805
|
-
*
|
|
806
|
-
* @param
|
|
830
|
+
* @param chainIdentifier Chain identifier string of the source smart chain
|
|
831
|
+
* @param signer Signer's address on the source chain
|
|
832
|
+
* @param tokenAddress Token address to pay with
|
|
833
|
+
* @param paymentRequest BOLT11 lightning network invoice to be paid (needs to have a fixed amount), and the swap
|
|
834
|
+
* amount is taken from this fixed amount, hence only exact output swaps are supported
|
|
835
|
+
* @param additionalParams Additional parameters sent to the LP when creating the swap
|
|
836
|
+
* @param options Additional options for the swap
|
|
807
837
|
*/
|
|
808
838
|
async createToBTCLNSwap<ChainIdentifier extends ChainIds<T>>(
|
|
809
839
|
chainIdentifier: ChainIdentifier,
|
|
@@ -813,12 +843,12 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
813
843
|
additionalParams: Record<string, any> | undefined = this.options.defaultAdditionalParameters,
|
|
814
844
|
options?: ToBTCLNOptions
|
|
815
845
|
): Promise<ToBTCLNSwap<T[ChainIdentifier]>> {
|
|
816
|
-
if(this.
|
|
846
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
817
847
|
options ??= {};
|
|
818
848
|
if(paymentRequest.startsWith("lightning:")) paymentRequest = paymentRequest.substring(10);
|
|
819
849
|
if(!this.Utils.isValidLightningInvoice(paymentRequest)) throw new Error("Invalid lightning network invoice");
|
|
820
|
-
if(!this.
|
|
821
|
-
signer = this.
|
|
850
|
+
if(!this._chains[chainIdentifier].chainInterface.isValidAddress(signer, true)) throw new Error("Invalid "+chainIdentifier+" address");
|
|
851
|
+
signer = this._chains[chainIdentifier].chainInterface.normalizeAddress(signer);
|
|
822
852
|
const parsedPR = bolt11Decode(paymentRequest);
|
|
823
853
|
if(parsedPR.millisatoshis==null) throw new Error("Invalid lightning network invoice, no msat value field!");
|
|
824
854
|
const amountData = {
|
|
@@ -844,16 +874,16 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
844
874
|
}
|
|
845
875
|
|
|
846
876
|
/**
|
|
847
|
-
* Creates
|
|
877
|
+
* Creates Smart chain -> Bitcoin Lightning ({@link SwapType.TO_BTCLN}) swap via LNURL-pay link
|
|
848
878
|
*
|
|
849
|
-
* @param chainIdentifier
|
|
850
|
-
* @param signer
|
|
851
|
-
* @param tokenAddress
|
|
852
|
-
* @param lnurlPay
|
|
853
|
-
* @param amount
|
|
854
|
-
* @param exactIn
|
|
855
|
-
* @param additionalParams
|
|
856
|
-
* @param options
|
|
879
|
+
* @param chainIdentifier Chain identifier string of the source smart chain
|
|
880
|
+
* @param signer Signer's address on the source chain
|
|
881
|
+
* @param tokenAddress Token address to pay with
|
|
882
|
+
* @param lnurlPay LNURL-pay link to use for the payment
|
|
883
|
+
* @param amount Amount to send in token based units (if `exactIn=true`) or receive in satoshis (if `exactIn=false`)
|
|
884
|
+
* @param exactIn Whether to do an exact in swap instead of exact out
|
|
885
|
+
* @param additionalParams Additional parameters sent to the LP when creating the swap
|
|
886
|
+
* @param options Additional options for the swap
|
|
857
887
|
*/
|
|
858
888
|
async createToBTCLNSwapViaLNURL<ChainIdentifier extends ChainIds<T>>(
|
|
859
889
|
chainIdentifier: ChainIdentifier,
|
|
@@ -865,10 +895,10 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
865
895
|
additionalParams: Record<string, any> | undefined = this.options.defaultAdditionalParameters,
|
|
866
896
|
options?: ToBTCLNOptions & {comment?: string}
|
|
867
897
|
): Promise<ToBTCLNSwap<T[ChainIdentifier]>> {
|
|
868
|
-
if(this.
|
|
898
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
869
899
|
if(typeof(lnurlPay)==="string" && !this.Utils.isValidLNURL(lnurlPay)) throw new Error("Invalid LNURL-pay link");
|
|
870
|
-
if(!this.
|
|
871
|
-
signer = this.
|
|
900
|
+
if(!this._chains[chainIdentifier].chainInterface.isValidAddress(signer, true)) throw new Error("Invalid "+chainIdentifier+" address");
|
|
901
|
+
signer = this._chains[chainIdentifier].chainInterface.normalizeAddress(signer);
|
|
872
902
|
const amountData = {
|
|
873
903
|
amount,
|
|
874
904
|
token: tokenAddress,
|
|
@@ -893,30 +923,30 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
893
923
|
}
|
|
894
924
|
|
|
895
925
|
/**
|
|
896
|
-
* Creates
|
|
926
|
+
* Creates Smart chain -> Bitcoin Lightning ({@link SwapType.TO_BTCLN}) swap via {@link LightningInvoiceCreateService}
|
|
897
927
|
*
|
|
898
|
-
* @param chainIdentifier
|
|
899
|
-
* @param signer
|
|
900
|
-
* @param tokenAddress
|
|
901
|
-
* @param service
|
|
902
|
-
* @param amount
|
|
903
|
-
* @param exactIn
|
|
904
|
-
* @param additionalParams
|
|
905
|
-
* @param options
|
|
928
|
+
* @param chainIdentifier Chain identifier string of the source smart chain
|
|
929
|
+
* @param signer Signer's address on the source chain
|
|
930
|
+
* @param tokenAddress Token address to pay with
|
|
931
|
+
* @param service Invoice create service object which facilitates the creation of fixed amount LN invoices
|
|
932
|
+
* @param amount Amount to send in token based units (if `exactIn=true`) or receive in satoshis (if `exactIn=false`)
|
|
933
|
+
* @param exactIn Whether to do an exact in swap instead of exact out
|
|
934
|
+
* @param additionalParams Additional parameters sent to the LP when creating the swap
|
|
935
|
+
* @param options Additional options for the swap
|
|
906
936
|
*/
|
|
907
937
|
async createToBTCLNSwapViaInvoiceCreateService<ChainIdentifier extends ChainIds<T>>(
|
|
908
938
|
chainIdentifier: ChainIdentifier,
|
|
909
939
|
signer: string,
|
|
910
940
|
tokenAddress: string,
|
|
911
|
-
service:
|
|
941
|
+
service: LightningInvoiceCreateService,
|
|
912
942
|
amount: bigint,
|
|
913
943
|
exactIn: boolean = false,
|
|
914
944
|
additionalParams: Record<string, any> | undefined = this.options.defaultAdditionalParameters,
|
|
915
945
|
options?: ToBTCLNOptions
|
|
916
946
|
): Promise<ToBTCLNSwap<T[ChainIdentifier]>> {
|
|
917
|
-
if(this.
|
|
918
|
-
if(!this.
|
|
919
|
-
signer = this.
|
|
947
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
948
|
+
if(!this._chains[chainIdentifier].chainInterface.isValidAddress(signer, true)) throw new Error("Invalid "+chainIdentifier+" address");
|
|
949
|
+
signer = this._chains[chainIdentifier].chainInterface.normalizeAddress(signer);
|
|
920
950
|
options ??= {};
|
|
921
951
|
const amountData = {
|
|
922
952
|
amount,
|
|
@@ -941,28 +971,28 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
941
971
|
}
|
|
942
972
|
|
|
943
973
|
/**
|
|
944
|
-
* Creates
|
|
974
|
+
* Creates Bitcoin -> Smart chain ({@link SwapType.SPV_VAULT_FROM_BTC}) swap
|
|
945
975
|
*
|
|
946
|
-
* @param chainIdentifier
|
|
947
|
-
* @param
|
|
948
|
-
* @param tokenAddress
|
|
949
|
-
* @param amount
|
|
950
|
-
* @param exactOut
|
|
951
|
-
* @param additionalParams
|
|
952
|
-
* @param options
|
|
976
|
+
* @param chainIdentifier Chain identifier string of the destination smart chain
|
|
977
|
+
* @param recipient Recipient address on the destination chain
|
|
978
|
+
* @param tokenAddress Token address to receive
|
|
979
|
+
* @param amount Amount to send in satoshis (if `exactOut=false`) or receive in token based units (if `exactOut=true`)
|
|
980
|
+
* @param exactOut Whether to use a exact out instead of exact in
|
|
981
|
+
* @param additionalParams Additional parameters sent to the LP when creating the swap
|
|
982
|
+
* @param options Additional options for the swap
|
|
953
983
|
*/
|
|
954
984
|
async createFromBTCSwapNew<ChainIdentifier extends ChainIds<T>>(
|
|
955
985
|
chainIdentifier: ChainIdentifier,
|
|
956
|
-
|
|
986
|
+
recipient: string,
|
|
957
987
|
tokenAddress: string,
|
|
958
988
|
amount: bigint,
|
|
959
989
|
exactOut: boolean = false,
|
|
960
990
|
additionalParams: Record<string, any> | undefined = this.options.defaultAdditionalParameters,
|
|
961
991
|
options?: SpvFromBTCOptions
|
|
962
992
|
): Promise<SpvFromBTCSwap<T[ChainIdentifier]>> {
|
|
963
|
-
if(this.
|
|
964
|
-
if(!this.
|
|
965
|
-
|
|
993
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
994
|
+
if(!this._chains[chainIdentifier].chainInterface.isValidAddress(recipient, true)) throw new Error("Invalid "+chainIdentifier+" address");
|
|
995
|
+
recipient = this._chains[chainIdentifier].chainInterface.normalizeAddress(recipient);
|
|
966
996
|
const amountData = {
|
|
967
997
|
amount,
|
|
968
998
|
token: tokenAddress,
|
|
@@ -971,7 +1001,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
971
1001
|
return this.createSwap(
|
|
972
1002
|
chainIdentifier as ChainIdentifier,
|
|
973
1003
|
(candidates: Intermediary[], abortSignal: AbortSignal, chain) => Promise.resolve(chain.wrappers[SwapType.SPV_VAULT_FROM_BTC].create(
|
|
974
|
-
|
|
1004
|
+
recipient,
|
|
975
1005
|
amountData,
|
|
976
1006
|
candidates,
|
|
977
1007
|
options,
|
|
@@ -984,28 +1014,28 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
984
1014
|
}
|
|
985
1015
|
|
|
986
1016
|
/**
|
|
987
|
-
* Creates
|
|
1017
|
+
* Creates LEGACY Bitcoin -> Smart chain ({@link SwapType.FROM_BTC}) swap
|
|
988
1018
|
*
|
|
989
|
-
* @param chainIdentifier
|
|
990
|
-
* @param
|
|
991
|
-
* @param tokenAddress
|
|
992
|
-
* @param amount
|
|
993
|
-
* @param exactOut
|
|
994
|
-
* @param additionalParams
|
|
995
|
-
* @param options
|
|
1019
|
+
* @param chainIdentifier Chain identifier string of the destination smart chain
|
|
1020
|
+
* @param recipient Recipient address on the destination chain
|
|
1021
|
+
* @param tokenAddress Token address to receive
|
|
1022
|
+
* @param amount Amount to send in satoshis (if `exactOut=false`) or receive in token based units (if `exactOut=true`)
|
|
1023
|
+
* @param exactOut Whether to use a exact out instead of exact in
|
|
1024
|
+
* @param additionalParams Additional parameters sent to the LP when creating the swap
|
|
1025
|
+
* @param options Additional options for the swap
|
|
996
1026
|
*/
|
|
997
1027
|
async createFromBTCSwap<ChainIdentifier extends ChainIds<T>>(
|
|
998
1028
|
chainIdentifier: ChainIdentifier,
|
|
999
|
-
|
|
1029
|
+
recipient: string,
|
|
1000
1030
|
tokenAddress: string,
|
|
1001
1031
|
amount: bigint,
|
|
1002
1032
|
exactOut: boolean = false,
|
|
1003
1033
|
additionalParams: Record<string, any> | undefined = this.options.defaultAdditionalParameters,
|
|
1004
1034
|
options?: FromBTCOptions
|
|
1005
1035
|
): Promise<FromBTCSwap<T[ChainIdentifier]>> {
|
|
1006
|
-
if(this.
|
|
1007
|
-
if(!this.
|
|
1008
|
-
|
|
1036
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
1037
|
+
if(!this._chains[chainIdentifier].chainInterface.isValidAddress(recipient, true)) throw new Error("Invalid "+chainIdentifier+" address");
|
|
1038
|
+
recipient = this._chains[chainIdentifier].chainInterface.normalizeAddress(recipient);
|
|
1009
1039
|
const amountData = {
|
|
1010
1040
|
amount,
|
|
1011
1041
|
token: tokenAddress,
|
|
@@ -1014,7 +1044,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1014
1044
|
return this.createSwap(
|
|
1015
1045
|
chainIdentifier as ChainIdentifier,
|
|
1016
1046
|
(candidates: Intermediary[], abortSignal: AbortSignal, chain) => Promise.resolve(chain.wrappers[SwapType.FROM_BTC].create(
|
|
1017
|
-
|
|
1047
|
+
recipient,
|
|
1018
1048
|
amountData,
|
|
1019
1049
|
candidates,
|
|
1020
1050
|
options,
|
|
@@ -1027,28 +1057,28 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1027
1057
|
}
|
|
1028
1058
|
|
|
1029
1059
|
/**
|
|
1030
|
-
* Creates
|
|
1060
|
+
* Creates LEGACY Bitcoin Lightning -> Smart chain ({@link SwapType.FROM_BTCLN}) swap
|
|
1031
1061
|
*
|
|
1032
|
-
* @param chainIdentifier
|
|
1033
|
-
* @param
|
|
1034
|
-
* @param tokenAddress
|
|
1035
|
-
* @param amount
|
|
1036
|
-
* @param exactOut
|
|
1037
|
-
* @param additionalParams
|
|
1038
|
-
* @param options
|
|
1062
|
+
* @param chainIdentifier Chain identifier string of the destination smart chain
|
|
1063
|
+
* @param recipient Recipient address on the destination chain
|
|
1064
|
+
* @param tokenAddress Token address to receive
|
|
1065
|
+
* @param amount Amount to send in satoshis (if `exactOut=false`) or receive in token based units (if `exactOut=true`)
|
|
1066
|
+
* @param exactOut Whether to use a exact out instead of exact in
|
|
1067
|
+
* @param additionalParams Additional parameters sent to the LP when creating the swap
|
|
1068
|
+
* @param options Additional options for the swap
|
|
1039
1069
|
*/
|
|
1040
1070
|
async createFromBTCLNSwap<ChainIdentifier extends ChainIds<T>>(
|
|
1041
1071
|
chainIdentifier: ChainIdentifier,
|
|
1042
|
-
|
|
1072
|
+
recipient: string,
|
|
1043
1073
|
tokenAddress: string,
|
|
1044
1074
|
amount: bigint,
|
|
1045
1075
|
exactOut: boolean = false,
|
|
1046
1076
|
additionalParams: Record<string, any> | undefined = this.options.defaultAdditionalParameters,
|
|
1047
1077
|
options?: FromBTCLNOptions
|
|
1048
1078
|
): Promise<FromBTCLNSwap<T[ChainIdentifier]>> {
|
|
1049
|
-
if(this.
|
|
1050
|
-
if(!this.
|
|
1051
|
-
|
|
1079
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
1080
|
+
if(!this._chains[chainIdentifier].chainInterface.isValidAddress(recipient, true)) throw new Error("Invalid "+chainIdentifier+" address");
|
|
1081
|
+
recipient = this._chains[chainIdentifier].chainInterface.normalizeAddress(recipient);
|
|
1052
1082
|
const amountData = {
|
|
1053
1083
|
amount,
|
|
1054
1084
|
token: tokenAddress,
|
|
@@ -1057,7 +1087,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1057
1087
|
return this.createSwap(
|
|
1058
1088
|
chainIdentifier as ChainIdentifier,
|
|
1059
1089
|
(candidates: Intermediary[], abortSignal: AbortSignal, chain) => Promise.resolve(chain.wrappers[SwapType.FROM_BTCLN].create(
|
|
1060
|
-
|
|
1090
|
+
recipient,
|
|
1061
1091
|
amountData,
|
|
1062
1092
|
candidates,
|
|
1063
1093
|
options,
|
|
@@ -1070,29 +1100,30 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1070
1100
|
}
|
|
1071
1101
|
|
|
1072
1102
|
/**
|
|
1073
|
-
* Creates
|
|
1103
|
+
* Creates LEGACY Bitcoin Lightning -> Smart chain ({@link SwapType.FROM_BTCLN}) swap, withdrawing from
|
|
1104
|
+
* an LNURL-withdraw link
|
|
1074
1105
|
*
|
|
1075
|
-
* @param chainIdentifier
|
|
1076
|
-
* @param
|
|
1077
|
-
* @param tokenAddress
|
|
1078
|
-
* @param lnurl
|
|
1079
|
-
* @param amount
|
|
1080
|
-
* @param exactOut
|
|
1081
|
-
* @param additionalParams
|
|
1106
|
+
* @param chainIdentifier Chain identifier string of the destination smart chain
|
|
1107
|
+
* @param recipient Recipient address on the destination chain
|
|
1108
|
+
* @param tokenAddress Token address to receive
|
|
1109
|
+
* @param lnurl LNURL-withdraw link to pull the funds from
|
|
1110
|
+
* @param amount Amount to send in satoshis (if `exactOut=false`) or receive in token based units (if `exactOut=true`)
|
|
1111
|
+
* @param exactOut Whether to use a exact out instead of exact in
|
|
1112
|
+
* @param additionalParams Additional parameters sent to the LP when creating the swap
|
|
1082
1113
|
*/
|
|
1083
1114
|
async createFromBTCLNSwapViaLNURL<ChainIdentifier extends ChainIds<T>>(
|
|
1084
1115
|
chainIdentifier: ChainIdentifier,
|
|
1085
|
-
|
|
1116
|
+
recipient: string,
|
|
1086
1117
|
tokenAddress: string,
|
|
1087
1118
|
lnurl: string | LNURLWithdraw,
|
|
1088
1119
|
amount: bigint,
|
|
1089
1120
|
exactOut: boolean = false,
|
|
1090
1121
|
additionalParams: Record<string, any> | undefined = this.options.defaultAdditionalParameters
|
|
1091
1122
|
): Promise<FromBTCLNSwap<T[ChainIdentifier]>> {
|
|
1092
|
-
if(this.
|
|
1123
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
1093
1124
|
if(typeof(lnurl)==="string" && !this.Utils.isValidLNURL(lnurl)) throw new Error("Invalid LNURL-withdraw link");
|
|
1094
|
-
if(!this.
|
|
1095
|
-
|
|
1125
|
+
if(!this._chains[chainIdentifier].chainInterface.isValidAddress(recipient, true)) throw new Error("Invalid "+chainIdentifier+" address");
|
|
1126
|
+
recipient = this._chains[chainIdentifier].chainInterface.normalizeAddress(recipient);
|
|
1096
1127
|
const amountData = {
|
|
1097
1128
|
amount,
|
|
1098
1129
|
token: tokenAddress,
|
|
@@ -1101,7 +1132,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1101
1132
|
return this.createSwap(
|
|
1102
1133
|
chainIdentifier as ChainIdentifier,
|
|
1103
1134
|
(candidates: Intermediary[], abortSignal: AbortSignal, chain) => chain.wrappers[SwapType.FROM_BTCLN].createViaLNURL(
|
|
1104
|
-
|
|
1135
|
+
recipient,
|
|
1105
1136
|
typeof(lnurl)==="string" ? (lnurl.startsWith("lightning:") ? lnurl.substring(10): lnurl) : lnurl.params,
|
|
1106
1137
|
amountData,
|
|
1107
1138
|
candidates,
|
|
@@ -1114,28 +1145,28 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1114
1145
|
}
|
|
1115
1146
|
|
|
1116
1147
|
/**
|
|
1117
|
-
* Creates
|
|
1148
|
+
* Creates Bitcoin Lightning -> Smart chain ({@link SwapType.FROM_BTCLN_AUTO}) swap
|
|
1118
1149
|
*
|
|
1119
|
-
* @param chainIdentifier
|
|
1120
|
-
* @param
|
|
1121
|
-
* @param tokenAddress
|
|
1122
|
-
* @param amount
|
|
1123
|
-
* @param exactOut
|
|
1124
|
-
* @param additionalParams
|
|
1125
|
-
* @param options
|
|
1150
|
+
* @param chainIdentifier Chain identifier string of the destination smart chain
|
|
1151
|
+
* @param recipient Recipient address on the destination chain
|
|
1152
|
+
* @param tokenAddress Token address to receive
|
|
1153
|
+
* @param amount Amount to send in satoshis (if `exactOut=false`) or receive in token based units (if `exactOut=true`)
|
|
1154
|
+
* @param exactOut Whether to use a exact out instead of exact in
|
|
1155
|
+
* @param additionalParams Additional parameters sent to the LP when creating the swap
|
|
1156
|
+
* @param options Additional options for the swap
|
|
1126
1157
|
*/
|
|
1127
1158
|
async createFromBTCLNSwapNew<ChainIdentifier extends ChainIds<T>>(
|
|
1128
1159
|
chainIdentifier: ChainIdentifier,
|
|
1129
|
-
|
|
1160
|
+
recipient: string,
|
|
1130
1161
|
tokenAddress: string,
|
|
1131
1162
|
amount: bigint,
|
|
1132
1163
|
exactOut: boolean = false,
|
|
1133
1164
|
additionalParams: Record<string, any> | undefined = this.options.defaultAdditionalParameters,
|
|
1134
1165
|
options?: FromBTCLNAutoOptions
|
|
1135
1166
|
): Promise<FromBTCLNAutoSwap<T[ChainIdentifier]>> {
|
|
1136
|
-
if(this.
|
|
1137
|
-
if(!this.
|
|
1138
|
-
|
|
1167
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
1168
|
+
if(!this._chains[chainIdentifier].chainInterface.isValidAddress(recipient, true)) throw new Error("Invalid "+chainIdentifier+" address");
|
|
1169
|
+
recipient = this._chains[chainIdentifier].chainInterface.normalizeAddress(recipient);
|
|
1139
1170
|
const amountData = {
|
|
1140
1171
|
amount,
|
|
1141
1172
|
token: tokenAddress,
|
|
@@ -1144,7 +1175,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1144
1175
|
return this.createSwap(
|
|
1145
1176
|
chainIdentifier as ChainIdentifier,
|
|
1146
1177
|
(candidates: Intermediary[], abortSignal: AbortSignal, chain) => Promise.resolve(chain.wrappers[SwapType.FROM_BTCLN_AUTO].create(
|
|
1147
|
-
|
|
1178
|
+
recipient,
|
|
1148
1179
|
amountData,
|
|
1149
1180
|
candidates,
|
|
1150
1181
|
options,
|
|
@@ -1157,20 +1188,21 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1157
1188
|
}
|
|
1158
1189
|
|
|
1159
1190
|
/**
|
|
1160
|
-
* Creates
|
|
1191
|
+
* Creates Bitcoin Lightning -> Smart chain ({@link SwapType.FROM_BTCLN_AUTO}) swap, withdrawing from
|
|
1192
|
+
* an LNURL-withdraw link
|
|
1161
1193
|
*
|
|
1162
|
-
* @param chainIdentifier
|
|
1163
|
-
* @param
|
|
1164
|
-
* @param tokenAddress
|
|
1165
|
-
* @param lnurl
|
|
1166
|
-
* @param amount
|
|
1167
|
-
* @param exactOut
|
|
1168
|
-
* @param additionalParams
|
|
1169
|
-
* @param options
|
|
1194
|
+
* @param chainIdentifier Chain identifier string of the destination smart chain
|
|
1195
|
+
* @param recipient Recipient address on the destination chain
|
|
1196
|
+
* @param tokenAddress Token address to receive
|
|
1197
|
+
* @param lnurl LNURL-withdraw link to pull the funds from
|
|
1198
|
+
* @param amount Amount to send in satoshis (if `exactOut=false`) or receive in token based units (if `exactOut=true`)
|
|
1199
|
+
* @param exactOut Whether to use a exact out instead of exact in
|
|
1200
|
+
* @param additionalParams Additional parameters sent to the LP when creating the swap
|
|
1201
|
+
* @param options Additional options for the swap
|
|
1170
1202
|
*/
|
|
1171
1203
|
async createFromBTCLNSwapNewViaLNURL<ChainIdentifier extends ChainIds<T>>(
|
|
1172
1204
|
chainIdentifier: ChainIdentifier,
|
|
1173
|
-
|
|
1205
|
+
recipient: string,
|
|
1174
1206
|
tokenAddress: string,
|
|
1175
1207
|
lnurl: string | LNURLWithdraw,
|
|
1176
1208
|
amount: bigint,
|
|
@@ -1178,10 +1210,10 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1178
1210
|
additionalParams: Record<string, any> | undefined = this.options.defaultAdditionalParameters,
|
|
1179
1211
|
options?: FromBTCLNAutoOptions
|
|
1180
1212
|
): Promise<FromBTCLNAutoSwap<T[ChainIdentifier]>> {
|
|
1181
|
-
if(this.
|
|
1213
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
1182
1214
|
if(typeof(lnurl)==="string" && !this.Utils.isValidLNURL(lnurl)) throw new Error("Invalid LNURL-withdraw link");
|
|
1183
|
-
if(!this.
|
|
1184
|
-
|
|
1215
|
+
if(!this._chains[chainIdentifier].chainInterface.isValidAddress(recipient, true)) throw new Error("Invalid "+chainIdentifier+" address");
|
|
1216
|
+
recipient = this._chains[chainIdentifier].chainInterface.normalizeAddress(recipient);
|
|
1185
1217
|
const amountData = {
|
|
1186
1218
|
amount,
|
|
1187
1219
|
token: tokenAddress,
|
|
@@ -1190,7 +1222,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1190
1222
|
return this.createSwap(
|
|
1191
1223
|
chainIdentifier as ChainIdentifier,
|
|
1192
1224
|
(candidates: Intermediary[], abortSignal: AbortSignal, chain) => chain.wrappers[SwapType.FROM_BTCLN_AUTO].createViaLNURL(
|
|
1193
|
-
|
|
1225
|
+
recipient,
|
|
1194
1226
|
typeof(lnurl)==="string" ? (lnurl.startsWith("lightning:") ? lnurl.substring(10): lnurl) : lnurl.params,
|
|
1195
1227
|
amountData,
|
|
1196
1228
|
candidates,
|
|
@@ -1204,57 +1236,86 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1204
1236
|
}
|
|
1205
1237
|
|
|
1206
1238
|
/**
|
|
1207
|
-
* Creates trusted
|
|
1239
|
+
* Creates a trusted Bitcoin Lightning -> Smart chain ({@link SwapType.TRUSTED_FROM_BTCLN}) gas swap
|
|
1208
1240
|
*
|
|
1209
|
-
* @param
|
|
1210
|
-
* @param
|
|
1211
|
-
* @param amount
|
|
1241
|
+
* @param chainIdentifier Chain identifier string of the destination smart chain
|
|
1242
|
+
* @param recipient Recipient address on the destination chain
|
|
1243
|
+
* @param amount Amount of native token to receive, in base units
|
|
1212
1244
|
* @param trustedIntermediaryOrUrl URL or Intermediary object of the trusted intermediary to use, otherwise uses default
|
|
1213
|
-
* @throws {Error}
|
|
1245
|
+
* @throws {Error} If no trusted intermediary specified
|
|
1214
1246
|
*/
|
|
1215
|
-
createTrustedLNForGasSwap<C extends ChainIds<T>>(
|
|
1216
|
-
if(this.
|
|
1217
|
-
if(!this.
|
|
1218
|
-
|
|
1247
|
+
createTrustedLNForGasSwap<C extends ChainIds<T>>(chainIdentifier: C, recipient: string, amount: bigint, trustedIntermediaryOrUrl?: Intermediary | string): Promise<LnForGasSwap<T[C]>> {
|
|
1248
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
1249
|
+
if(!this._chains[chainIdentifier].chainInterface.isValidAddress(recipient, true)) throw new Error("Invalid "+chainIdentifier+" address");
|
|
1250
|
+
recipient = this._chains[chainIdentifier].chainInterface.normalizeAddress(recipient);
|
|
1219
1251
|
const useUrl = trustedIntermediaryOrUrl ?? this.defaultTrustedIntermediary ?? this.options.defaultTrustedIntermediaryUrl;
|
|
1220
1252
|
if(useUrl==null) throw new Error("No trusted intermediary specified!");
|
|
1221
|
-
return this.
|
|
1253
|
+
return this._chains[chainIdentifier as C].wrappers[SwapType.TRUSTED_FROM_BTCLN].create(recipient, amount, useUrl);
|
|
1222
1254
|
}
|
|
1223
1255
|
|
|
1224
1256
|
/**
|
|
1225
|
-
* Creates trusted
|
|
1257
|
+
* Creates a trusted Bitcoin -> Smart chain ({@link SwapType.TRUSTED_FROM_BTC}) gas swap
|
|
1226
1258
|
*
|
|
1227
|
-
* @param
|
|
1228
|
-
* @param
|
|
1229
|
-
* @param amount
|
|
1230
|
-
* @param refundAddress
|
|
1231
|
-
* @param trustedIntermediaryOrUrl
|
|
1232
|
-
* @throws {Error}
|
|
1259
|
+
* @param chainIdentifier Chain identifier string of the destination smart chain
|
|
1260
|
+
* @param recipient Recipient address on the destination chain
|
|
1261
|
+
* @param amount Amount of native token to receive, in base units
|
|
1262
|
+
* @param refundAddress Bitcoin refund address, in case the swap fails the funds are refunded here
|
|
1263
|
+
* @param trustedIntermediaryOrUrl URL or Intermediary object of the trusted intermediary to use, otherwise uses default
|
|
1264
|
+
* @throws {Error} If no trusted intermediary specified
|
|
1233
1265
|
*/
|
|
1234
1266
|
createTrustedOnchainForGasSwap<C extends ChainIds<T>>(
|
|
1235
|
-
|
|
1267
|
+
chainIdentifier: C, recipient: string,
|
|
1236
1268
|
amount: bigint, refundAddress?: string,
|
|
1237
1269
|
trustedIntermediaryOrUrl?: Intermediary | string
|
|
1238
1270
|
): Promise<OnchainForGasSwap<T[C]>> {
|
|
1239
|
-
if(this.
|
|
1240
|
-
if(!this.
|
|
1241
|
-
|
|
1271
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
1272
|
+
if(!this._chains[chainIdentifier].chainInterface.isValidAddress(recipient, true)) throw new Error("Invalid "+chainIdentifier+" address");
|
|
1273
|
+
recipient = this._chains[chainIdentifier].chainInterface.normalizeAddress(recipient);
|
|
1242
1274
|
const useUrl = trustedIntermediaryOrUrl ?? this.defaultTrustedIntermediary ?? this.options.defaultTrustedIntermediaryUrl;
|
|
1243
1275
|
if(useUrl==null) throw new Error("No trusted intermediary specified!");
|
|
1244
|
-
return this.
|
|
1276
|
+
return this._chains[chainIdentifier as C].wrappers[SwapType.TRUSTED_FROM_BTC].create(recipient, amount, useUrl, refundAddress);
|
|
1245
1277
|
}
|
|
1246
1278
|
|
|
1279
|
+
/**
|
|
1280
|
+
* @internal
|
|
1281
|
+
*/
|
|
1247
1282
|
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]>)>;
|
|
1283
|
+
/**
|
|
1284
|
+
* @internal
|
|
1285
|
+
*/
|
|
1248
1286
|
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]>)>;
|
|
1287
|
+
/**
|
|
1288
|
+
* @internal
|
|
1289
|
+
*/
|
|
1249
1290
|
create<C extends ChainIds<T>>(signer: string, srcToken: SCToken<C>, dstToken: BtcToken<false>, amount: bigint, exactIn: boolean, address: string): Promise<ToBTCSwap<T[C]>>;
|
|
1291
|
+
/**
|
|
1292
|
+
* @internal
|
|
1293
|
+
*/
|
|
1250
1294
|
create<C extends ChainIds<T>>(signer: string, srcToken: SCToken<C>, dstToken: BtcToken<true>, amount: bigint, exactIn: boolean, lnurlPay: string | LNURLPay): Promise<ToBTCLNSwap<T[C]>>;
|
|
1295
|
+
/**
|
|
1296
|
+
* @internal
|
|
1297
|
+
*/
|
|
1251
1298
|
create<C extends ChainIds<T>>(signer: string, srcToken: SCToken<C>, dstToken: BtcToken<true>, amount: undefined, exactIn: false, lightningInvoice: string): Promise<ToBTCLNSwap<T[C]>>;
|
|
1299
|
+
/**
|
|
1300
|
+
* Creates a swap from srcToken to dstToken, of a specific token amount, either specifying input amount (exactIn=true)
|
|
1301
|
+
* or output amount (exactIn=false), NOTE: For regular -> BTC-LN (lightning) swaps the passed amount is ignored and
|
|
1302
|
+
* invoice's pre-set amount is used instead.
|
|
1303
|
+
* @deprecated Use {@link swap} instead
|
|
1304
|
+
*
|
|
1305
|
+
* @param signer Smartchain (Solana, Starknet, etc.) address of the user
|
|
1306
|
+
* @param srcToken Source token of the swap, user pays this token
|
|
1307
|
+
* @param dstToken Destination token of the swap, user receives this token
|
|
1308
|
+
* @param amount Amount of the swap
|
|
1309
|
+
* @param exactIn Whether the amount specified is an input amount (exactIn=true) or an output amount (exactIn=false)
|
|
1310
|
+
* @param addressLnurlLightningInvoice Bitcoin on-chain address, lightning invoice, LNURL-pay to pay or
|
|
1311
|
+
* LNURL-withdrawal to withdraw money from
|
|
1312
|
+
*/
|
|
1252
1313
|
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
1314
|
/**
|
|
1254
1315
|
* Creates a swap from srcToken to dstToken, of a specific token amount, either specifying input amount (exactIn=true)
|
|
1255
1316
|
* or output amount (exactIn=false), NOTE: For regular -> BTC-LN (lightning) swaps the passed amount is ignored and
|
|
1256
1317
|
* invoice's pre-set amount is used instead.
|
|
1257
|
-
* @deprecated Use swap
|
|
1318
|
+
* @deprecated Use {@link swap} instead
|
|
1258
1319
|
*
|
|
1259
1320
|
* @param signer Smartchain (Solana, Starknet, etc.) address of the user
|
|
1260
1321
|
* @param srcToken Source token of the swap, user pays this token
|
|
@@ -1272,16 +1333,44 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1272
1333
|
}
|
|
1273
1334
|
}
|
|
1274
1335
|
|
|
1336
|
+
/**
|
|
1337
|
+
* @internal
|
|
1338
|
+
*/
|
|
1275
1339
|
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]>)>;
|
|
1340
|
+
/**
|
|
1341
|
+
* @internal
|
|
1342
|
+
*/
|
|
1276
1343
|
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]>)>;
|
|
1344
|
+
/**
|
|
1345
|
+
* @internal
|
|
1346
|
+
*/
|
|
1277
1347
|
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
|
-
|
|
1348
|
+
/**
|
|
1349
|
+
* @internal
|
|
1350
|
+
*/
|
|
1351
|
+
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]>>;
|
|
1352
|
+
/**
|
|
1353
|
+
* @internal
|
|
1354
|
+
*/
|
|
1279
1355
|
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
1356
|
/**
|
|
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
|
|
1357
|
+
* Creates a swap from srcToken to dstToken, of a specific token amount, either specifying input amount (if `exactIn=true`)
|
|
1358
|
+
* or output amount (if `exactIn=false`), NOTE: For regular Smart chain -> BTC-LN (lightning) swaps the passed amount is ignored and
|
|
1359
|
+
* invoice's pre-set amount is used instead, use LNURL-pay links for dynamic amounts
|
|
1360
|
+
*
|
|
1361
|
+
* @param srcToken Source token of the swap, user pays this token
|
|
1362
|
+
* @param dstToken Destination token of the swap, user receives this token
|
|
1363
|
+
* @param amount Amount of the swap either in base units as {bigint} or in human readable format (with decimals) as {string}
|
|
1364
|
+
* @param exactIn Whether the amount specified is an input amount (exactIn=true) or an output amount (exactIn=false)
|
|
1365
|
+
* @param src Source wallet/lnurl-withdraw of the swap
|
|
1366
|
+
* @param dst Destination smart chain address, bitcoin on-chain address, lightning invoice, LNURL-pay
|
|
1367
|
+
* @param options Options for the swap
|
|
1368
|
+
*/
|
|
1369
|
+
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]>>;
|
|
1370
|
+
/**
|
|
1371
|
+
* Creates a swap from srcToken to dstToken, of a specific token amount, either specifying input amount (if `exactIn=true`)
|
|
1372
|
+
* or output amount (if `exactIn=false`), NOTE: For regular Smart chain -> BTC-LN (lightning) swaps the passed amount is ignored and
|
|
1373
|
+
* invoice's pre-set amount is used instead, use LNURL-pay links for dynamic amounts
|
|
1285
1374
|
*
|
|
1286
1375
|
* @param _srcToken Source token of the swap, user pays this token
|
|
1287
1376
|
* @param _dstToken Destination token of the swap, user receives this token
|
|
@@ -1297,7 +1386,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1297
1386
|
_amount: bigint | string | undefined,
|
|
1298
1387
|
exactIn: boolean | SwapAmountType,
|
|
1299
1388
|
src: undefined | string | LNURLWithdraw,
|
|
1300
|
-
dst: string | LNURLPay |
|
|
1389
|
+
dst: string | LNURLPay | LightningInvoiceCreateService,
|
|
1301
1390
|
options?: FromBTCLNOptions | SpvFromBTCOptions | FromBTCOptions | ToBTCOptions | (ToBTCLNOptions & {comment?: string}) | FromBTCLNAutoOptions
|
|
1302
1391
|
): Promise<ISwap<T[C]>> {
|
|
1303
1392
|
const srcToken = typeof(_srcToken)==="string" ? this.getToken(_srcToken) as Token<C> : _srcToken;
|
|
@@ -1337,7 +1426,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1337
1426
|
if(isLNURLPay(dst) || this.Utils.isValidLNURL(dst)) {
|
|
1338
1427
|
if(amount==null) throw new Error("Amount cannot be null for to btcln swaps via LNURL-pay!");
|
|
1339
1428
|
return this.createToBTCLNSwapViaLNURL(srcToken.chainId, src, srcToken.address, dst, amount, !!exactIn, undefined, options as any);
|
|
1340
|
-
} else if(
|
|
1429
|
+
} else if(isLightningInvoiceCreateService(dst)) {
|
|
1341
1430
|
if(amount==null) throw new Error("Amount cannot be null for to btcln swaps via InvoiceCreateService!");
|
|
1342
1431
|
return this.createToBTCLNSwapViaInvoiceCreateService(srcToken.chainId, src, srcToken.address, dst, amount, !!exactIn, undefined, options as any);
|
|
1343
1432
|
} else if(this.Utils.isLightningInvoice(dst)) {
|
|
@@ -1373,13 +1462,13 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1373
1462
|
if(signer!=null) queryParams.push({key: "initiator", value: signer});
|
|
1374
1463
|
|
|
1375
1464
|
if(chainId==null) {
|
|
1376
|
-
const res: ISwap[][] = await Promise.all(Object.keys(this.
|
|
1377
|
-
const {unifiedSwapStorage, reviver} = this.
|
|
1465
|
+
const res: ISwap[][] = await Promise.all(Object.keys(this._chains).map((chainId) => {
|
|
1466
|
+
const {unifiedSwapStorage, reviver} = this._chains[chainId];
|
|
1378
1467
|
return unifiedSwapStorage.query([queryParams], reviver);
|
|
1379
1468
|
}));
|
|
1380
1469
|
return res.flat();
|
|
1381
1470
|
} else {
|
|
1382
|
-
const {unifiedSwapStorage, reviver} = this.
|
|
1471
|
+
const {unifiedSwapStorage, reviver} = this._chains[chainId];
|
|
1383
1472
|
return await unifiedSwapStorage.query([queryParams], reviver);
|
|
1384
1473
|
}
|
|
1385
1474
|
}
|
|
@@ -1394,27 +1483,27 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1394
1483
|
getActionableSwaps<C extends ChainIds<T>>(chainId: C, signer?: string): Promise<ISwap<T[C]>[]>;
|
|
1395
1484
|
async getActionableSwaps<C extends ChainIds<T>>(chainId?: C, signer?: string): Promise<ISwap[]> {
|
|
1396
1485
|
if(chainId==null) {
|
|
1397
|
-
const res: ISwap[][] = await Promise.all(Object.keys(this.
|
|
1398
|
-
const {unifiedSwapStorage, reviver, wrappers} = this.
|
|
1486
|
+
const res: ISwap[][] = await Promise.all(Object.keys(this._chains).map((chainId) => {
|
|
1487
|
+
const {unifiedSwapStorage, reviver, wrappers} = this._chains[chainId];
|
|
1399
1488
|
const queryParams: Array<QueryParams[]> = [];
|
|
1400
1489
|
for(let key in wrappers) {
|
|
1401
1490
|
const wrapper = wrappers[key as unknown as SwapType];
|
|
1402
1491
|
const swapTypeQueryParams: QueryParams[] = [{key: "type", value: wrapper.TYPE}];
|
|
1403
1492
|
if(signer!=null) swapTypeQueryParams.push({key: "initiator", value: signer});
|
|
1404
|
-
swapTypeQueryParams.push({key: "state", value: wrapper.
|
|
1493
|
+
swapTypeQueryParams.push({key: "state", value: wrapper._pendingSwapStates});
|
|
1405
1494
|
queryParams.push(swapTypeQueryParams);
|
|
1406
1495
|
}
|
|
1407
1496
|
return unifiedSwapStorage.query(queryParams, reviver);
|
|
1408
1497
|
}));
|
|
1409
1498
|
return res.flat().filter(swap => swap.requiresAction());
|
|
1410
1499
|
} else {
|
|
1411
|
-
const {unifiedSwapStorage, reviver, wrappers} = this.
|
|
1500
|
+
const {unifiedSwapStorage, reviver, wrappers} = this._chains[chainId];
|
|
1412
1501
|
const queryParams: Array<QueryParams[]> = [];
|
|
1413
1502
|
for(let key in wrappers) {
|
|
1414
1503
|
const wrapper = wrappers[key as unknown as SwapType];
|
|
1415
1504
|
const swapTypeQueryParams: QueryParams[] = [{key: "type", value: wrapper.TYPE}];
|
|
1416
1505
|
if(signer!=null) swapTypeQueryParams.push({key: "initiator", value: signer});
|
|
1417
|
-
swapTypeQueryParams.push({key: "state", value: wrapper.
|
|
1506
|
+
swapTypeQueryParams.push({key: "state", value: wrapper._pendingSwapStates});
|
|
1418
1507
|
queryParams.push(swapTypeQueryParams);
|
|
1419
1508
|
}
|
|
1420
1509
|
return (await unifiedSwapStorage.query(queryParams, reviver)).filter(swap => swap.requiresAction());
|
|
@@ -1431,25 +1520,25 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1431
1520
|
getRefundableSwaps<C extends ChainIds<T>>(chainId: C, signer?: string): Promise<IToBTCSwap<T[C]>[]>;
|
|
1432
1521
|
async getRefundableSwaps<C extends ChainIds<T>>(chainId?: C, signer?: string): Promise<IToBTCSwap[]> {
|
|
1433
1522
|
if(chainId==null) {
|
|
1434
|
-
const res: IToBTCSwap[][] = await Promise.all(Object.keys(this.
|
|
1435
|
-
const {unifiedSwapStorage, reviver, wrappers} = this.
|
|
1523
|
+
const res: IToBTCSwap[][] = await Promise.all(Object.keys(this._chains).map((chainId) => {
|
|
1524
|
+
const {unifiedSwapStorage, reviver, wrappers} = this._chains[chainId];
|
|
1436
1525
|
const queryParams: Array<QueryParams[]> = [];
|
|
1437
1526
|
for(let wrapper of [wrappers[SwapType.TO_BTCLN], wrappers[SwapType.TO_BTC]]) {
|
|
1438
1527
|
const swapTypeQueryParams: QueryParams[] = [{key: "type", value: wrapper.TYPE}];
|
|
1439
1528
|
if(signer!=null) swapTypeQueryParams.push({key: "initiator", value: signer});
|
|
1440
|
-
swapTypeQueryParams.push({key: "state", value: wrapper.
|
|
1529
|
+
swapTypeQueryParams.push({key: "state", value: wrapper._refundableSwapStates});
|
|
1441
1530
|
queryParams.push(swapTypeQueryParams);
|
|
1442
1531
|
}
|
|
1443
1532
|
return unifiedSwapStorage.query<IToBTCSwap<T[C]>>(queryParams, reviver as (val: any) => IToBTCSwap<T[C]>);
|
|
1444
1533
|
}));
|
|
1445
1534
|
return res.flat().filter(swap => swap.isRefundable());
|
|
1446
1535
|
} else {
|
|
1447
|
-
const {unifiedSwapStorage, reviver, wrappers} = this.
|
|
1536
|
+
const {unifiedSwapStorage, reviver, wrappers} = this._chains[chainId];
|
|
1448
1537
|
const queryParams: Array<QueryParams[]> = [];
|
|
1449
1538
|
for(let wrapper of [wrappers[SwapType.TO_BTCLN], wrappers[SwapType.TO_BTC]]) {
|
|
1450
1539
|
const swapTypeQueryParams: QueryParams[] = [{key: "type", value: wrapper.TYPE}];
|
|
1451
1540
|
if(signer!=null) swapTypeQueryParams.push({key: "initiator", value: signer});
|
|
1452
|
-
swapTypeQueryParams.push({key: "state", value: wrapper.
|
|
1541
|
+
swapTypeQueryParams.push({key: "state", value: wrapper._refundableSwapStates});
|
|
1453
1542
|
queryParams.push(swapTypeQueryParams);
|
|
1454
1543
|
}
|
|
1455
1544
|
const result = await unifiedSwapStorage.query<IToBTCSwap<T[C]>>(queryParams, reviver as (val: any) => IToBTCSwap<T[C]>);
|
|
@@ -1467,27 +1556,27 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1467
1556
|
getClaimableSwaps<C extends ChainIds<T>>(chainId: C, signer?: string): Promise<IClaimableSwap<T[C]>[]>;
|
|
1468
1557
|
async getClaimableSwaps<C extends ChainIds<T>>(chainId?: C, signer?: string): Promise<IClaimableSwap[]> {
|
|
1469
1558
|
if(chainId==null) {
|
|
1470
|
-
const res: IClaimableSwap[][] = await Promise.all(Object.keys(this.
|
|
1471
|
-
const {unifiedSwapStorage, reviver, wrappers} = this.
|
|
1559
|
+
const res: IClaimableSwap[][] = await Promise.all(Object.keys(this._chains).map((chainId) => {
|
|
1560
|
+
const {unifiedSwapStorage, reviver, wrappers} = this._chains[chainId];
|
|
1472
1561
|
const queryParams: Array<QueryParams[]> = [];
|
|
1473
1562
|
for(let wrapper of [wrappers[SwapType.FROM_BTC], wrappers[SwapType.FROM_BTCLN], wrappers[SwapType.SPV_VAULT_FROM_BTC], wrappers[SwapType.FROM_BTCLN_AUTO]]) {
|
|
1474
1563
|
if(wrapper==null) continue;
|
|
1475
1564
|
const swapTypeQueryParams: QueryParams[] = [{key: "type", value: wrapper.TYPE}];
|
|
1476
1565
|
if(signer!=null) swapTypeQueryParams.push({key: "initiator", value: signer});
|
|
1477
|
-
swapTypeQueryParams.push({key: "state", value: wrapper.
|
|
1566
|
+
swapTypeQueryParams.push({key: "state", value: wrapper._claimableSwapStates});
|
|
1478
1567
|
queryParams.push(swapTypeQueryParams);
|
|
1479
1568
|
}
|
|
1480
1569
|
return unifiedSwapStorage.query<IClaimableSwap<T[C]>>(queryParams, reviver as any as (val: any) => IClaimableSwap<T[C]>);
|
|
1481
1570
|
}));
|
|
1482
1571
|
return res.flat().filter(swap => swap.isClaimable());
|
|
1483
1572
|
} else {
|
|
1484
|
-
const {unifiedSwapStorage, reviver, wrappers} = this.
|
|
1573
|
+
const {unifiedSwapStorage, reviver, wrappers} = this._chains[chainId];
|
|
1485
1574
|
const queryParams: Array<QueryParams[]> = [];
|
|
1486
1575
|
for(let wrapper of [wrappers[SwapType.FROM_BTC], wrappers[SwapType.FROM_BTCLN], wrappers[SwapType.SPV_VAULT_FROM_BTC], wrappers[SwapType.FROM_BTCLN_AUTO]]) {
|
|
1487
1576
|
if(wrapper==null) continue;
|
|
1488
1577
|
const swapTypeQueryParams: QueryParams[] = [{key: "type", value: wrapper.TYPE}];
|
|
1489
1578
|
if(signer!=null) swapTypeQueryParams.push({key: "initiator", value: signer});
|
|
1490
|
-
swapTypeQueryParams.push({key: "state", value: wrapper.
|
|
1579
|
+
swapTypeQueryParams.push({key: "state", value: wrapper._claimableSwapStates});
|
|
1491
1580
|
queryParams.push(swapTypeQueryParams);
|
|
1492
1581
|
}
|
|
1493
1582
|
const result = await unifiedSwapStorage.query<IClaimableSwap<T[C]>>(queryParams, reviver as any as (val: any) => IClaimableSwap<T[C]>);
|
|
@@ -1506,9 +1595,9 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1506
1595
|
async getSwapById<C extends ChainIds<T>>(id: string, chainId?: C, signer?: string): Promise<ISwap> {
|
|
1507
1596
|
//Check in pending swaps first
|
|
1508
1597
|
if(chainId!=null) {
|
|
1509
|
-
for(let key in this.
|
|
1510
|
-
const wrapper = this.
|
|
1511
|
-
const result = wrapper.
|
|
1598
|
+
for(let key in this._chains[chainId].wrappers) {
|
|
1599
|
+
const wrapper = this._chains[chainId].wrappers[key as unknown as SwapType];
|
|
1600
|
+
const result = wrapper._getPendingSwap(id);
|
|
1512
1601
|
if(result!=null) {
|
|
1513
1602
|
if (signer != null) {
|
|
1514
1603
|
if (result._getInitiator() === signer) return result;
|
|
@@ -1518,10 +1607,10 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1518
1607
|
}
|
|
1519
1608
|
}
|
|
1520
1609
|
} else {
|
|
1521
|
-
for(let chainId in this.
|
|
1522
|
-
for(let key in this.
|
|
1523
|
-
const wrapper = this.
|
|
1524
|
-
const result = wrapper.
|
|
1610
|
+
for(let chainId in this._chains) {
|
|
1611
|
+
for(let key in this._chains[chainId].wrappers) {
|
|
1612
|
+
const wrapper = this._chains[chainId].wrappers[key as unknown as SwapType];
|
|
1613
|
+
const result = wrapper._getPendingSwap(id);
|
|
1525
1614
|
if(result!=null) {
|
|
1526
1615
|
if(signer!=null) {
|
|
1527
1616
|
if(result._getInitiator()===signer) return result;
|
|
@@ -1537,24 +1626,24 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1537
1626
|
if(signer!=null) queryParams.push({key: "initiator", value: signer});
|
|
1538
1627
|
queryParams.push({key: "id", value: id});
|
|
1539
1628
|
if(chainId==null) {
|
|
1540
|
-
const res: ISwap[][] = await Promise.all(Object.keys(this.
|
|
1541
|
-
const {unifiedSwapStorage, reviver} = this.
|
|
1629
|
+
const res: ISwap[][] = await Promise.all(Object.keys(this._chains).map((chainId) => {
|
|
1630
|
+
const {unifiedSwapStorage, reviver} = this._chains[chainId];
|
|
1542
1631
|
return unifiedSwapStorage.query([queryParams], reviver);
|
|
1543
1632
|
}));
|
|
1544
1633
|
return res.flat()[0];
|
|
1545
1634
|
} else {
|
|
1546
|
-
const {unifiedSwapStorage, reviver} = this.
|
|
1635
|
+
const {unifiedSwapStorage, reviver} = this._chains[chainId];
|
|
1547
1636
|
return (await unifiedSwapStorage.query([queryParams], reviver))[0];
|
|
1548
1637
|
}
|
|
1549
1638
|
}
|
|
1550
1639
|
|
|
1551
1640
|
/**
|
|
1552
|
-
* Returns the swap with a proper return type, or undefined
|
|
1641
|
+
* Returns the swap with a proper return type, or `undefined` if not found or has wrong type
|
|
1553
1642
|
*
|
|
1554
|
-
* @param id
|
|
1555
|
-
* @param chainId
|
|
1556
|
-
* @param swapType
|
|
1557
|
-
* @param signer
|
|
1643
|
+
* @param id An ID of the swap ({@link ISwap.getId})
|
|
1644
|
+
* @param chainId Chain identifier of the smart chain where the swap was initiated
|
|
1645
|
+
* @param swapType Type of the swap
|
|
1646
|
+
* @param signer An optional required smart chain signer address to fetch the swap for
|
|
1558
1647
|
*/
|
|
1559
1648
|
async getTypedSwapById<C extends ChainIds<T>, S extends SwapType>(id: string, chainId: C, swapType: S, signer?: string): Promise<SwapTypeMapping<T[C]>[S] | undefined> {
|
|
1560
1649
|
let _swapType: SwapType = swapType;
|
|
@@ -1563,10 +1652,10 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1563
1652
|
if(swapType===SwapType.FROM_BTCLN && this.supportsSwapType(chainId, SwapType.FROM_BTCLN_AUTO))
|
|
1564
1653
|
_swapType = SwapType.FROM_BTCLN_AUTO;
|
|
1565
1654
|
|
|
1566
|
-
const wrapper = this.
|
|
1655
|
+
const wrapper = this._chains[chainId].wrappers[_swapType];
|
|
1567
1656
|
if(wrapper==null) return;
|
|
1568
1657
|
|
|
1569
|
-
const result = wrapper.
|
|
1658
|
+
const result = wrapper._getPendingSwap(id);
|
|
1570
1659
|
if(result!=null) {
|
|
1571
1660
|
if (signer != null) {
|
|
1572
1661
|
if (result._getInitiator() === signer) return result as any;
|
|
@@ -1578,19 +1667,19 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1578
1667
|
const queryParams: QueryParams[] = [];
|
|
1579
1668
|
if(signer!=null) queryParams.push({key: "initiator", value: signer});
|
|
1580
1669
|
queryParams.push({key: "id", value: id});
|
|
1581
|
-
const {unifiedSwapStorage, reviver} = this.
|
|
1670
|
+
const {unifiedSwapStorage, reviver} = this._chains[chainId];
|
|
1582
1671
|
const swap = (await unifiedSwapStorage.query([queryParams], reviver))[0];
|
|
1583
1672
|
if(isSwapType(swap, swapType)) return swap;
|
|
1584
1673
|
}
|
|
1585
1674
|
|
|
1586
1675
|
private async syncSwapsForChain<C extends ChainIds<T>>(chainId: C, signer?: string): Promise<void> {
|
|
1587
|
-
const {unifiedSwapStorage, reviver, wrappers} = this.
|
|
1676
|
+
const {unifiedSwapStorage, reviver, wrappers} = this._chains[chainId];
|
|
1588
1677
|
const queryParams: Array<QueryParams[]> = [];
|
|
1589
1678
|
for(let key in wrappers) {
|
|
1590
1679
|
const wrapper = wrappers[key as unknown as SwapType];
|
|
1591
1680
|
const swapTypeQueryParams: QueryParams[] = [{key: "type", value: wrapper.TYPE}];
|
|
1592
1681
|
if(signer!=null) swapTypeQueryParams.push({key: "initiator", value: signer});
|
|
1593
|
-
swapTypeQueryParams.push({key: "state", value: wrapper.
|
|
1682
|
+
swapTypeQueryParams.push({key: "state", value: wrapper._pendingSwapStates});
|
|
1594
1683
|
queryParams.push(swapTypeQueryParams);
|
|
1595
1684
|
}
|
|
1596
1685
|
this.logger.debug("_syncSwaps(): Querying swaps swaps for chain "+chainId+"!");
|
|
@@ -1623,14 +1712,49 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1623
1712
|
}
|
|
1624
1713
|
|
|
1625
1714
|
/**
|
|
1626
|
-
*
|
|
1715
|
+
* Deletes the swaps from the persistent storage backend. Note that some data (like lightning network
|
|
1716
|
+
* amounts and bolt11 invoices) are purely off-chain and can never be recovered later just from
|
|
1717
|
+
* on-chain data!
|
|
1718
|
+
*
|
|
1719
|
+
* @param chainId Optional, to only delete swaps for this smart chain
|
|
1720
|
+
* @param signer Optional, to only delete swaps for this smart chain signer (`chainId` param must be
|
|
1721
|
+
* set to delete only signer's swaps)
|
|
1722
|
+
*/
|
|
1723
|
+
async wipeStorage<C extends ChainIds<T>>(chainId?: C, signer?: string) {
|
|
1724
|
+
if(chainId==null) {
|
|
1725
|
+
const swaps = await this.getAllSwaps();
|
|
1726
|
+
const chainSwaps: {
|
|
1727
|
+
[chainId: string]: ISwap<any>[]
|
|
1728
|
+
} = {};
|
|
1729
|
+
swaps.forEach(swap => (chainSwaps[swap.chainIdentifier] ??= []).push(swap));
|
|
1730
|
+
for(let chainId in chainSwaps) {
|
|
1731
|
+
const currentChainSwaps = chainSwaps[chainId];
|
|
1732
|
+
if(this._chains[chainId]==null) {
|
|
1733
|
+
this.logger.warn(`wipeStorage(): Attempted to remove ${currentChainSwaps.length} swaps on ${chainId}, but smart chain not known!`);
|
|
1734
|
+
continue;
|
|
1735
|
+
}
|
|
1736
|
+
await this._chains[chainId].unifiedSwapStorage.removeAll(currentChainSwaps);
|
|
1737
|
+
this.logger.debug(`wipeStorage(): Successfully removed ${currentChainSwaps.length} swaps on ${chainId}!`);
|
|
1738
|
+
}
|
|
1739
|
+
} else {
|
|
1740
|
+
if(this._chains[chainId]==null) throw new Error(`wipeStorage(): Smart chain with identifier ${chainId} not found!`);
|
|
1741
|
+
const swaps = await this.getAllSwaps(chainId, signer);
|
|
1742
|
+
await this._chains[chainId].unifiedSwapStorage.removeAll(swaps);
|
|
1743
|
+
this.logger.debug(`wipeStorage(): Successfully removed ${swaps.length} swaps on ${chainId}!`);
|
|
1744
|
+
}
|
|
1745
|
+
}
|
|
1746
|
+
|
|
1747
|
+
/**
|
|
1748
|
+
* Synchronizes swaps from on-chain, this is ran automatically when SDK is initialized, hence
|
|
1749
|
+
* should only be ran manually when `dontCheckPastSwaps=true` is passed in the swapper options,
|
|
1750
|
+
* also deletes expired quotes
|
|
1627
1751
|
*
|
|
1628
|
-
* @param chainId
|
|
1629
|
-
* @param signer
|
|
1752
|
+
* @param chainId Optional chain identifier to only run swap sync for a single smart chain
|
|
1753
|
+
* @param signer Optional signer to only run swap sync for swaps initiated by this signer
|
|
1630
1754
|
*/
|
|
1631
1755
|
async _syncSwaps<C extends ChainIds<T>>(chainId?: C, signer?: string): Promise<void> {
|
|
1632
1756
|
if(chainId==null) {
|
|
1633
|
-
await Promise.all(Object.keys(this.
|
|
1757
|
+
await Promise.all(Object.keys(this._chains).map((chainId) => {
|
|
1634
1758
|
return this.syncSwapsForChain(chainId, signer);
|
|
1635
1759
|
}));
|
|
1636
1760
|
} else {
|
|
@@ -1639,20 +1763,30 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1639
1763
|
}
|
|
1640
1764
|
|
|
1641
1765
|
/**
|
|
1642
|
-
*
|
|
1766
|
+
* Recovers swaps from on-chain historical data.
|
|
1767
|
+
*
|
|
1768
|
+
* Please note that the recovered swaps might not be complete (i.e. missing amounts or addresses), as some
|
|
1769
|
+
* of the swap data is purely off-chain and can never be recovered purely from on-chain data. This
|
|
1770
|
+
* functions tries to recover as much swap data as possible.
|
|
1643
1771
|
*
|
|
1644
|
-
* @param chainId
|
|
1645
|
-
* @param signer
|
|
1646
|
-
* @param startBlockheight
|
|
1772
|
+
* @param chainId Smart chain identifier string to recover the swaps from
|
|
1773
|
+
* @param signer Signer address to recover the swaps for
|
|
1774
|
+
* @param startBlockheight Optional starting blockheight for swap data recovery, will only check swaps
|
|
1775
|
+
* initiated after this blockheight
|
|
1647
1776
|
*/
|
|
1648
1777
|
async recoverSwaps<C extends ChainIds<T>>(chainId: C, signer: string, startBlockheight?: number): Promise<ISwap<T[C]>[]> {
|
|
1649
|
-
const {swapContract, unifiedSwapStorage, reviver, wrappers} = this.
|
|
1778
|
+
const {spvVaultContract, swapContract, unifiedSwapStorage, reviver, wrappers} = this._chains[chainId];
|
|
1650
1779
|
|
|
1651
|
-
if(
|
|
1780
|
+
if(
|
|
1781
|
+
swapContract.getHistoricalSwaps==null ||
|
|
1782
|
+
(spvVaultContract!=null && spvVaultContract.getHistoricalWithdrawalStates==null)
|
|
1783
|
+
) throw new Error(`Historical swap recovery is not supported for ${chainId}`);
|
|
1652
1784
|
|
|
1653
|
-
const {swaps} = await swapContract.getHistoricalSwaps(signer);
|
|
1785
|
+
const {swaps} = await swapContract.getHistoricalSwaps(signer, startBlockheight);
|
|
1786
|
+
const spvVaultData = await spvVaultContract?.getHistoricalWithdrawalStates!(signer, startBlockheight);
|
|
1654
1787
|
|
|
1655
1788
|
const escrowHashes = Object.keys(swaps);
|
|
1789
|
+
if(spvVaultData!=null) Object.keys(spvVaultData.withdrawals).forEach(btcTxId => escrowHashes.push(btcTxId));
|
|
1656
1790
|
this.logger.debug(`recoverSwaps(): Loaded on-chain data for ${escrowHashes.length} swaps`);
|
|
1657
1791
|
this.logger.debug(`recoverSwaps(): Fetching if swap escrowHashes are known: ${escrowHashes.join(", ")}`);
|
|
1658
1792
|
const knownSwapsArray = await unifiedSwapStorage.query([[{key: "escrowHash", value: escrowHashes}]], reviver);
|
|
@@ -1668,17 +1802,23 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1668
1802
|
for(let escrowHash in swaps) {
|
|
1669
1803
|
const {init, state} = swaps[escrowHash];
|
|
1670
1804
|
const knownSwap = knownSwaps[escrowHash];
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1805
|
+
|
|
1806
|
+
if(knownSwap==null) {
|
|
1807
|
+
if(init==null) {
|
|
1808
|
+
this.logger.warn(`recoverSwaps(escrow): Fetched ${escrowHash} swap state, but swap not found locally!`);
|
|
1809
|
+
continue;
|
|
1810
|
+
}
|
|
1811
|
+
} else if(knownSwap instanceof IEscrowSwap) {
|
|
1812
|
+
this.logger.debug(`recoverSwaps(escrow): Forcibly updating ${escrowHash} swap: swap already known and in local storage!`);
|
|
1813
|
+
if(await knownSwap._forciblySetOnchainState(state)) {
|
|
1814
|
+
await knownSwap._save();
|
|
1815
|
+
}
|
|
1675
1816
|
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!`);
|
|
1817
|
+
} else {
|
|
1818
|
+
this.logger.debug(`recoverSwaps(escrow): Skipping ${escrowHash} swap: swap already known and in local storage!`);
|
|
1680
1819
|
continue;
|
|
1681
1820
|
}
|
|
1821
|
+
|
|
1682
1822
|
const data = init.data;
|
|
1683
1823
|
|
|
1684
1824
|
//Classify swap
|
|
@@ -1715,7 +1855,50 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1715
1855
|
if(swap!=null) {
|
|
1716
1856
|
recoveredSwaps.push(swap);
|
|
1717
1857
|
} else {
|
|
1718
|
-
if(typeIdentified) this.logger.debug(`recoverSwaps(): Swap data type correctly identified but swap returned is null for swap ${escrowHash}`);
|
|
1858
|
+
if(typeIdentified) this.logger.debug(`recoverSwaps(escrow): Swap data type correctly identified but swap returned is null for swap ${escrowHash}`);
|
|
1859
|
+
}
|
|
1860
|
+
}
|
|
1861
|
+
|
|
1862
|
+
if(spvVaultContract!=null && spvVaultData!=null) {
|
|
1863
|
+
const vaultsData = await spvVaultContract.getMultipleVaultData(
|
|
1864
|
+
Object.keys(spvVaultData.withdrawals)
|
|
1865
|
+
.map(btcTxId => ({
|
|
1866
|
+
owner: spvVaultData.withdrawals[btcTxId].owner,
|
|
1867
|
+
vaultId: spvVaultData.withdrawals[btcTxId].vaultId
|
|
1868
|
+
}))
|
|
1869
|
+
);
|
|
1870
|
+
|
|
1871
|
+
for(let btcTxId in spvVaultData.withdrawals) {
|
|
1872
|
+
const state = spvVaultData.withdrawals[btcTxId];
|
|
1873
|
+
const knownSwap = knownSwaps[btcTxId];
|
|
1874
|
+
|
|
1875
|
+
if(knownSwap!=null) {
|
|
1876
|
+
if(knownSwap instanceof SpvFromBTCSwap) {
|
|
1877
|
+
this.logger.debug(`recoverSwaps(spv_vault): Forcibly updating ${btcTxId} swap: swap already known and in local storage!`);
|
|
1878
|
+
//TODO: Forcibly set on-chain state to the swap
|
|
1879
|
+
// if(await knownSwap._forciblySetOnchainState(state)) {
|
|
1880
|
+
// await knownSwap._save();
|
|
1881
|
+
// }
|
|
1882
|
+
continue;
|
|
1883
|
+
} else {
|
|
1884
|
+
this.logger.debug(`recoverSwaps(spv_vault): Skipping ${btcTxId} swap: swap already known and in local storage!`);
|
|
1885
|
+
continue;
|
|
1886
|
+
}
|
|
1887
|
+
}
|
|
1888
|
+
|
|
1889
|
+
const lp = this.intermediaryDiscovery.intermediaries.find(
|
|
1890
|
+
val => val.supportsChain(chainId) && state.owner.toLowerCase()===val.getAddress(chainId).toLowerCase()
|
|
1891
|
+
);
|
|
1892
|
+
const swap = await wrappers[SwapType.SPV_VAULT_FROM_BTC].recoverFromState(
|
|
1893
|
+
state,
|
|
1894
|
+
vaultsData[state.owner]?.[state.vaultId.toString(10)],
|
|
1895
|
+
lp
|
|
1896
|
+
);
|
|
1897
|
+
if(swap!=null) {
|
|
1898
|
+
recoveredSwaps.push(swap);
|
|
1899
|
+
} else {
|
|
1900
|
+
this.logger.debug(`recoverSwaps(spv_vault): Swap data type correctly identified but swap returned is null for swap ${btcTxId}`);
|
|
1901
|
+
}
|
|
1719
1902
|
}
|
|
1720
1903
|
}
|
|
1721
1904
|
|
|
@@ -1724,7 +1907,15 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1724
1907
|
return recoveredSwaps;
|
|
1725
1908
|
}
|
|
1726
1909
|
|
|
1727
|
-
|
|
1910
|
+
/**
|
|
1911
|
+
* Returns the {@link Token} object for a given token
|
|
1912
|
+
*
|
|
1913
|
+
* @param tickerOrAddress Token to return the object for, can use multiple formats:
|
|
1914
|
+
* - a) token ticker, such as `"BTC"`, `"SOL"`, etc.
|
|
1915
|
+
* - b) token ticker prefixed with smart chain identifier, such as `"SOLANA-SOL"`, `"SOLANA-USDC"`, etc.
|
|
1916
|
+
* - c) token address
|
|
1917
|
+
*/
|
|
1918
|
+
getToken(tickerOrAddress: string): Token<ChainIds<T>> {
|
|
1728
1919
|
//Btc tokens - BTC, BTCLN, BTC-LN
|
|
1729
1920
|
if(tickerOrAddress==="BTC") return BitcoinTokens.BTC;
|
|
1730
1921
|
if(tickerOrAddress==="BTCLN" || tickerOrAddress==="BTC-LN") return BitcoinTokens.BTCLN;
|
|
@@ -1732,21 +1923,21 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1732
1923
|
//Check if the ticker is in format <chainId>-<ticker>, i.e. SOLANA-USDC, STARKNET-WBTC
|
|
1733
1924
|
if(tickerOrAddress.includes("-")) {
|
|
1734
1925
|
const [chainId, ticker] = tickerOrAddress.split("-");
|
|
1735
|
-
const token = this.
|
|
1926
|
+
const token = this._tokensByTicker[chainId]?.[ticker];
|
|
1736
1927
|
if(token==null) throw new UserError(`Not found ticker: ${ticker} for chainId: ${chainId}`);
|
|
1737
1928
|
return token;
|
|
1738
1929
|
}
|
|
1739
1930
|
|
|
1740
1931
|
const possibleTokens: SCToken[] = [];
|
|
1741
|
-
for(let chainId in this.
|
|
1742
|
-
const chain = this.
|
|
1932
|
+
for(let chainId in this._chains) {
|
|
1933
|
+
const chain = this._chains[chainId];
|
|
1743
1934
|
if(chain.chainInterface.isValidToken(tickerOrAddress)) {
|
|
1744
1935
|
//Try to find in known token addresses
|
|
1745
|
-
const token = this.
|
|
1936
|
+
const token = this._tokens[chainId]?.[tickerOrAddress];
|
|
1746
1937
|
if(token!=null) return token;
|
|
1747
1938
|
} else {
|
|
1748
1939
|
//Check in known tickers
|
|
1749
|
-
const token = this.
|
|
1940
|
+
const token = this._tokensByTicker[chainId]?.[tickerOrAddress];
|
|
1750
1941
|
if(token!=null) possibleTokens.push(token);
|
|
1751
1942
|
}
|
|
1752
1943
|
}
|
|
@@ -1760,44 +1951,62 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1760
1951
|
/**
|
|
1761
1952
|
* Creates a child swapper instance with a given smart chain
|
|
1762
1953
|
*
|
|
1763
|
-
* @param chainIdentifier
|
|
1954
|
+
* @param chainIdentifier Smart chain identifier for the created child swapper instance
|
|
1764
1955
|
*/
|
|
1765
1956
|
withChain<ChainIdentifier extends ChainIds<T>>(chainIdentifier: ChainIdentifier): SwapperWithChain<T, ChainIdentifier> {
|
|
1766
|
-
if(this.
|
|
1957
|
+
if(this._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
1767
1958
|
return new SwapperWithChain<T, ChainIdentifier>(this, chainIdentifier as ChainIdentifier);
|
|
1768
1959
|
}
|
|
1769
1960
|
|
|
1770
1961
|
/**
|
|
1771
|
-
* Returns supported smart chains
|
|
1962
|
+
* Returns an array of all the supported smart chains
|
|
1772
1963
|
*/
|
|
1773
1964
|
getSmartChains(): ChainIds<T>[] {
|
|
1774
|
-
return Object.keys(this.
|
|
1965
|
+
return Object.keys(this._chains);
|
|
1775
1966
|
}
|
|
1776
1967
|
|
|
1777
1968
|
/**
|
|
1778
1969
|
* Returns whether the SDK supports a given swap type on a given chain based on currently known LPs
|
|
1779
1970
|
*
|
|
1780
|
-
* @param chainId
|
|
1781
|
-
* @param swapType
|
|
1971
|
+
* @param chainId Smart chain identifier string
|
|
1972
|
+
* @param swapType Swap protocol type
|
|
1782
1973
|
*/
|
|
1783
1974
|
supportsSwapType<
|
|
1784
1975
|
ChainIdentifier extends ChainIds<T>,
|
|
1785
1976
|
Type extends SwapType
|
|
1786
1977
|
>(chainId: ChainIdentifier, swapType: Type): SupportsSwapType<T[ChainIdentifier], Type> {
|
|
1787
|
-
return (this.
|
|
1978
|
+
return (this._chains[chainId]?.wrappers[swapType] != null) as any;
|
|
1788
1979
|
}
|
|
1789
1980
|
|
|
1790
1981
|
/**
|
|
1791
|
-
*
|
|
1792
|
-
*
|
|
1793
|
-
* @param srcToken
|
|
1794
|
-
* @param dstToken
|
|
1982
|
+
* @internal
|
|
1795
1983
|
*/
|
|
1796
1984
|
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);
|
|
1985
|
+
/**
|
|
1986
|
+
* @internal
|
|
1987
|
+
*/
|
|
1797
1988
|
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);
|
|
1989
|
+
/**
|
|
1990
|
+
* @internal
|
|
1991
|
+
*/
|
|
1798
1992
|
getSwapType<C extends ChainIds<T>>(srcToken: SCToken<C>, dstToken: BtcToken<false>): SwapType.TO_BTC;
|
|
1993
|
+
/**
|
|
1994
|
+
* @internal
|
|
1995
|
+
*/
|
|
1799
1996
|
getSwapType<C extends ChainIds<T>>(srcToken: SCToken<C>, dstToken: BtcToken<true>): SwapType.TO_BTCLN;
|
|
1997
|
+
/**
|
|
1998
|
+
* Returns type of the swap based on input and output tokens specified
|
|
1999
|
+
*
|
|
2000
|
+
* @param srcToken Source token
|
|
2001
|
+
* @param dstToken Destination token
|
|
2002
|
+
*/
|
|
1800
2003
|
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;
|
|
2004
|
+
/**
|
|
2005
|
+
* Returns type of the swap based on input and output tokens specified
|
|
2006
|
+
*
|
|
2007
|
+
* @param srcToken Source token
|
|
2008
|
+
* @param dstToken Destination token
|
|
2009
|
+
*/
|
|
1801
2010
|
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
2011
|
if(isSCToken(srcToken)) {
|
|
1803
2012
|
if(!isBtcToken(dstToken)) throw new Error("Swap not supported");
|
|
@@ -1825,54 +2034,29 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1825
2034
|
throw new Error("Swap not supported");
|
|
1826
2035
|
}
|
|
1827
2036
|
|
|
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;
|
|
2037
|
+
/**
|
|
2038
|
+
* Helper information about various swap protocol and their features:
|
|
2039
|
+
* - `requiresInputWallet`: Whether a swap requires a connected wallet on the input chain able to sign
|
|
2040
|
+
* arbitrary transaction
|
|
2041
|
+
* - `requiresOutputWallet`: Whether a swap requires a connected wallet on the output chain able to sign
|
|
2042
|
+
* arbitrary transactions
|
|
2043
|
+
* - `supportsGasDrop`: Whether a swap supports the "gas drop" feature, allowing to user to receive a small
|
|
2044
|
+
* amount of native token as part of the swap when swapping to smart chains
|
|
2045
|
+
*
|
|
2046
|
+
* Uses a `Record` type here, use the {@link SwapProtocolInfo} import for a literal readonly type, with
|
|
2047
|
+
* pre-filled exact values in the type.
|
|
2048
|
+
*/
|
|
2049
|
+
readonly SwapTypeInfo = SwapProtocolInfo as Record<SwapType, {
|
|
2050
|
+
requiresInputWallet: boolean,
|
|
2051
|
+
requiresOutputWallet: boolean,
|
|
2052
|
+
supportsGasDrop: boolean
|
|
2053
|
+
}>;
|
|
1870
2054
|
|
|
1871
2055
|
/**
|
|
1872
2056
|
* Returns minimum/maximum limits for inputs and outputs for a swap between given tokens
|
|
1873
2057
|
*
|
|
1874
|
-
* @param srcToken
|
|
1875
|
-
* @param dstToken
|
|
2058
|
+
* @param srcToken Source token
|
|
2059
|
+
* @param dstToken Destination token
|
|
1876
2060
|
*/
|
|
1877
2061
|
getSwapLimits<C extends ChainIds<T>, A extends Token<C>, B extends Token<C>>(srcToken: A, dstToken: B): {
|
|
1878
2062
|
input: {min: TokenAmount<string, A>, max?: TokenAmount<string, A>},
|
|
@@ -1906,7 +2090,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1906
2090
|
}
|
|
1907
2091
|
|
|
1908
2092
|
/**
|
|
1909
|
-
* Returns supported tokens
|
|
2093
|
+
* Returns an array of supported tokens either on the input or on the output of a swap
|
|
1910
2094
|
*
|
|
1911
2095
|
* @param input Whether to return input tokens or output tokens
|
|
1912
2096
|
*/
|
|
@@ -1955,7 +2139,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1955
2139
|
if(btc) output.push(BitcoinTokens.BTC);
|
|
1956
2140
|
for(let chainId in tokens) {
|
|
1957
2141
|
tokens[chainId].forEach(tokenAddress => {
|
|
1958
|
-
const token = this.
|
|
2142
|
+
const token = this._tokens?.[chainId]?.[tokenAddress];
|
|
1959
2143
|
if(token!=null) output.push(token);
|
|
1960
2144
|
})
|
|
1961
2145
|
}
|
|
@@ -1986,7 +2170,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1986
2170
|
const output: SCToken[] = [];
|
|
1987
2171
|
for(let chainId in tokens) {
|
|
1988
2172
|
tokens[chainId].forEach(tokenAddress => {
|
|
1989
|
-
const token = this.
|
|
2173
|
+
const token = this._tokens?.[chainId]?.[tokenAddress];
|
|
1990
2174
|
if(token!=null) output.push(token);
|
|
1991
2175
|
})
|
|
1992
2176
|
}
|
|
@@ -1996,7 +2180,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1996
2180
|
/**
|
|
1997
2181
|
* Returns the set of supported token addresses by all the intermediaries we know of offering a specific swapType service
|
|
1998
2182
|
*
|
|
1999
|
-
* @param chainIdentifier
|
|
2183
|
+
* @param chainIdentifier Chain identifier string
|
|
2000
2184
|
* @param swapType Specific swap type for which to obtain supported tokens
|
|
2001
2185
|
*/
|
|
2002
2186
|
private getSupportedTokenAddresses<ChainIdentifier extends ChainIds<T>>(chainIdentifier: ChainIdentifier, swapType: SwapType): Set<string> {
|
|
@@ -2053,54 +2237,4 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
2053
2237
|
}
|
|
2054
2238
|
}
|
|
2055
2239
|
|
|
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
2240
|
}
|