@atomiqlabs/sdk 8.1.8 → 8.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bitcoin/coinselect2/utils.d.ts +6 -0
- package/dist/bitcoin/wallet/BitcoinWallet.d.ts +41 -5
- package/dist/bitcoin/wallet/BitcoinWallet.js +36 -1
- package/dist/bitcoin/wallet/IBitcoinWallet.d.ts +52 -2
- package/dist/bitcoin/wallet/IBitcoinWallet.js +2 -1
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.d.ts +42 -7
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.js +36 -1
- package/dist/enums/FeeType.d.ts +8 -1
- package/dist/enums/FeeType.js +8 -1
- package/dist/enums/SwapAmountType.d.ts +7 -0
- package/dist/enums/SwapAmountType.js +7 -0
- package/dist/enums/SwapDirection.d.ts +7 -0
- package/dist/enums/SwapDirection.js +7 -0
- package/dist/enums/SwapType.d.ts +62 -1
- package/dist/enums/SwapType.js +62 -1
- package/dist/errors/IntermediaryError.d.ts +4 -0
- package/dist/errors/IntermediaryError.js +1 -0
- package/dist/errors/RequestError.d.ts +15 -1
- package/dist/errors/RequestError.js +8 -0
- package/dist/errors/UserError.d.ts +1 -0
- package/dist/errors/UserError.js +1 -0
- package/dist/index.d.ts +5 -5
- package/dist/index.js +7 -6
- package/dist/intermediaries/Intermediary.d.ts +61 -14
- package/dist/intermediaries/Intermediary.js +38 -11
- package/dist/intermediaries/IntermediaryDiscovery.d.ts +62 -29
- package/dist/intermediaries/IntermediaryDiscovery.js +39 -24
- package/dist/prices/RedundantSwapPrice.d.ts +26 -5
- package/dist/prices/RedundantSwapPrice.js +22 -2
- package/dist/prices/SingleSwapPrice.d.ts +10 -7
- package/dist/prices/SingleSwapPrice.js +11 -8
- package/dist/prices/SwapPriceWithChain.d.ts +56 -19
- package/dist/prices/SwapPriceWithChain.js +62 -25
- package/dist/prices/abstract/IPriceProvider.d.ts +4 -4
- package/dist/prices/abstract/IPriceProvider.js +1 -1
- package/dist/prices/abstract/ISwapPrice.d.ts +95 -46
- package/dist/prices/abstract/ISwapPrice.js +104 -56
- package/dist/prices/providers/BinancePriceProvider.d.ts +8 -1
- package/dist/prices/providers/BinancePriceProvider.js +8 -1
- package/dist/prices/providers/CoinGeckoPriceProvider.d.ts +7 -1
- package/dist/prices/providers/CoinGeckoPriceProvider.js +7 -1
- package/dist/prices/providers/CoinPaprikaPriceProvider.d.ts +7 -1
- package/dist/prices/providers/CoinPaprikaPriceProvider.js +7 -1
- package/dist/prices/providers/CustomPriceProvider.d.ts +12 -1
- package/dist/prices/providers/CustomPriceProvider.js +12 -1
- package/dist/prices/providers/KrakenPriceProvider.d.ts +10 -1
- package/dist/prices/providers/KrakenPriceProvider.js +10 -1
- package/dist/prices/providers/OKXPriceProvider.d.ts +7 -1
- package/dist/prices/providers/OKXPriceProvider.js +7 -1
- package/dist/prices/providers/abstract/ExchangePriceProvider.d.ts +3 -0
- package/dist/prices/providers/abstract/ExchangePriceProvider.js +3 -0
- package/dist/storage/IUnifiedStorage.d.ts +19 -7
- package/dist/storage/UnifiedSwapStorage.d.ts +33 -3
- package/dist/storage/UnifiedSwapStorage.js +29 -1
- package/dist/storage-browser/IndexedDBUnifiedStorage.d.ts +31 -7
- package/dist/storage-browser/IndexedDBUnifiedStorage.js +29 -6
- package/dist/storage-browser/LocalStorageManager.d.ts +25 -1
- package/dist/storage-browser/LocalStorageManager.js +25 -1
- package/dist/swapper/Swapper.d.ts +380 -226
- package/dist/swapper/Swapper.js +383 -349
- package/dist/swapper/SwapperFactory.d.ts +66 -18
- package/dist/swapper/SwapperFactory.js +24 -3
- package/dist/swapper/SwapperUtils.d.ts +75 -28
- package/dist/swapper/SwapperUtils.js +107 -60
- package/dist/swapper/SwapperWithChain.d.ts +286 -91
- package/dist/swapper/SwapperWithChain.js +218 -64
- package/dist/swapper/SwapperWithSigner.d.ts +229 -80
- package/dist/swapper/SwapperWithSigner.js +190 -44
- package/dist/swaps/IAddressSwap.d.ts +12 -3
- package/dist/swaps/IAddressSwap.js +3 -2
- package/dist/swaps/IBTCWalletSwap.d.ts +26 -8
- package/dist/swaps/IBTCWalletSwap.js +3 -2
- package/dist/swaps/IClaimableSwap.d.ts +38 -6
- package/dist/swaps/IClaimableSwap.js +3 -2
- package/dist/swaps/IClaimableSwapWrapper.d.ts +11 -1
- package/dist/swaps/IRefundableSwap.d.ts +31 -5
- package/dist/swaps/IRefundableSwap.js +3 -2
- package/dist/swaps/ISwap.d.ts +162 -24
- package/dist/swaps/ISwap.js +92 -35
- package/dist/swaps/ISwapWithGasDrop.d.ts +8 -2
- package/dist/swaps/ISwapWithGasDrop.js +2 -1
- package/dist/swaps/ISwapWrapper.d.ts +161 -52
- package/dist/swaps/ISwapWrapper.js +131 -73
- package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.d.ts +51 -6
- package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.js +22 -12
- package/dist/swaps/escrow_swaps/IEscrowSwap.d.ts +65 -12
- package/dist/swaps/escrow_swaps/IEscrowSwap.js +38 -19
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.d.ts +39 -9
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.js +30 -21
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.d.ts +31 -15
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.js +33 -18
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.d.ts +97 -28
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.js +91 -27
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.d.ts +22 -9
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.js +24 -11
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.d.ts +278 -60
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +519 -241
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +77 -26
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +132 -50
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.d.ts +313 -52
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +544 -194
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +87 -26
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +147 -58
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.d.ts +222 -55
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +462 -244
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.d.ts +77 -23
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.js +116 -46
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.d.ts +195 -58
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +324 -191
- package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.d.ts +30 -5
- package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.js +44 -19
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.d.ts +61 -20
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.js +75 -32
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.d.ts +76 -50
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.js +106 -101
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.d.ts +37 -14
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.js +66 -20
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.d.ts +46 -17
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.js +82 -27
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +350 -88
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +482 -215
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.d.ts +76 -24
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.js +247 -124
- package/dist/swaps/trusted/ln/LnForGasSwap.d.ts +148 -20
- package/dist/swaps/trusted/ln/LnForGasSwap.js +175 -45
- package/dist/swaps/trusted/ln/LnForGasWrapper.d.ts +29 -10
- package/dist/swaps/trusted/ln/LnForGasWrapper.js +30 -11
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.d.ts +202 -49
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.js +232 -80
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.d.ts +34 -12
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.js +33 -14
- package/dist/types/AmountData.d.ts +2 -1
- package/dist/types/CustomPriceFunction.d.ts +8 -2
- package/dist/types/PriceInfoType.d.ts +4 -4
- package/dist/types/PriceInfoType.js +3 -3
- package/dist/types/SwapExecutionAction.d.ts +85 -4
- package/dist/types/SwapWithSigner.d.ts +5 -2
- package/dist/types/SwapWithSigner.js +5 -2
- package/dist/types/Token.d.ts +11 -5
- package/dist/types/Token.js +6 -3
- package/dist/types/TokenAmount.d.ts +3 -0
- package/dist/types/TokenAmount.js +2 -0
- package/dist/types/fees/Fee.d.ts +3 -2
- package/dist/types/fees/FeeBreakdown.d.ts +3 -2
- package/dist/types/fees/PercentagePPM.d.ts +4 -2
- package/dist/types/fees/PercentagePPM.js +2 -1
- package/dist/types/lnurl/LNURLPay.d.ts +20 -12
- package/dist/types/lnurl/LNURLPay.js +8 -4
- package/dist/types/lnurl/LNURLWithdraw.d.ts +17 -10
- package/dist/types/lnurl/LNURLWithdraw.js +8 -4
- package/dist/types/wallets/LightningInvoiceCreateService.d.ts +24 -0
- package/dist/types/wallets/LightningInvoiceCreateService.js +15 -0
- package/dist/types/wallets/MinimalBitcoinWalletInterface.d.ts +3 -1
- package/dist/types/wallets/MinimalLightningNetworkWalletInterface.d.ts +4 -2
- package/dist/utils/BitcoinUtils.d.ts +1 -0
- package/dist/utils/BitcoinUtils.js +5 -1
- package/dist/utils/SwapUtils.d.ts +58 -1
- package/dist/utils/SwapUtils.js +55 -1
- package/dist/utils/TokenUtils.d.ts +10 -2
- package/dist/utils/TokenUtils.js +12 -4
- package/package.json +3 -3
- package/src/bitcoin/coinselect2/utils.ts +6 -0
- package/src/bitcoin/wallet/BitcoinWallet.ts +41 -5
- package/src/bitcoin/wallet/IBitcoinWallet.ts +57 -2
- package/src/bitcoin/wallet/SingleAddressBitcoinWallet.ts +42 -6
- package/src/enums/FeeType.ts +8 -1
- package/src/enums/SwapAmountType.ts +7 -0
- package/src/enums/SwapDirection.ts +7 -0
- package/src/enums/SwapType.ts +62 -2
- package/src/errors/IntermediaryError.ts +4 -0
- package/src/errors/RequestError.ts +15 -1
- package/src/errors/UserError.ts +1 -0
- package/src/index.ts +12 -5
- package/src/intermediaries/Intermediary.ts +61 -14
- package/src/intermediaries/IntermediaryDiscovery.ts +69 -34
- package/src/prices/RedundantSwapPrice.ts +26 -6
- package/src/prices/SingleSwapPrice.ts +11 -8
- package/src/prices/SwapPriceWithChain.ts +63 -26
- package/src/prices/abstract/IPriceProvider.ts +4 -4
- package/src/prices/abstract/ISwapPrice.ts +115 -66
- package/src/prices/providers/BinancePriceProvider.ts +8 -1
- package/src/prices/providers/CoinGeckoPriceProvider.ts +7 -1
- package/src/prices/providers/CoinPaprikaPriceProvider.ts +7 -1
- package/src/prices/providers/CustomPriceProvider.ts +12 -1
- package/src/prices/providers/KrakenPriceProvider.ts +10 -1
- package/src/prices/providers/OKXPriceProvider.ts +7 -1
- package/src/prices/providers/abstract/ExchangePriceProvider.ts +3 -0
- package/src/storage/IUnifiedStorage.ts +19 -7
- package/src/storage/UnifiedSwapStorage.ts +33 -3
- package/src/storage-browser/IndexedDBUnifiedStorage.ts +31 -8
- package/src/storage-browser/LocalStorageManager.ts +25 -1
- package/src/swapper/Swapper.ts +599 -390
- package/src/swapper/SwapperFactory.ts +73 -24
- package/src/swapper/SwapperUtils.ts +107 -60
- package/src/swapper/SwapperWithChain.ts +320 -81
- package/src/swapper/SwapperWithSigner.ts +263 -56
- package/src/swaps/IAddressSwap.ts +13 -3
- package/src/swaps/IBTCWalletSwap.ts +26 -10
- package/src/swaps/IClaimableSwap.ts +41 -6
- package/src/swaps/IClaimableSwapWrapper.ts +11 -2
- package/src/swaps/IRefundableSwap.ts +34 -5
- package/src/swaps/ISwap.ts +224 -85
- package/src/swaps/ISwapWithGasDrop.ts +8 -2
- package/src/swaps/ISwapWrapper.ts +216 -98
- package/src/swaps/escrow_swaps/IEscrowSelfInitSwap.ts +64 -18
- package/src/swaps/escrow_swaps/IEscrowSwap.ts +83 -37
- package/src/swaps/escrow_swaps/IEscrowSwapWrapper.ts +61 -30
- package/src/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.ts +37 -19
- package/src/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.ts +123 -50
- package/src/swaps/escrow_swaps/frombtc/IFromBTCWrapper.ts +24 -11
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.ts +562 -258
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +156 -62
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +592 -227
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +177 -74
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.ts +483 -245
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.ts +141 -59
- package/src/swaps/escrow_swaps/tobtc/IToBTCSwap.ts +350 -195
- package/src/swaps/escrow_swaps/tobtc/IToBTCWrapper.ts +48 -23
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.ts +87 -40
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.ts +110 -110
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.ts +89 -34
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.ts +101 -31
- package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +556 -259
- package/src/swaps/spv_swaps/SpvFromBTCWrapper.ts +292 -148
- package/src/swaps/trusted/ln/LnForGasSwap.ts +186 -47
- package/src/swaps/trusted/ln/LnForGasWrapper.ts +34 -15
- package/src/swaps/trusted/onchain/OnchainForGasSwap.ts +262 -88
- package/src/swaps/trusted/onchain/OnchainForGasWrapper.ts +41 -19
- package/src/types/AmountData.ts +2 -1
- package/src/types/CustomPriceFunction.ts +8 -2
- package/src/types/PriceInfoType.ts +4 -4
- package/src/types/SwapExecutionAction.ts +97 -5
- package/src/types/SwapWithSigner.ts +8 -4
- package/src/types/Token.ts +12 -5
- package/src/types/TokenAmount.ts +3 -0
- package/src/types/fees/Fee.ts +3 -2
- package/src/types/fees/FeeBreakdown.ts +3 -2
- package/src/types/fees/PercentagePPM.ts +4 -2
- package/src/types/lnurl/LNURLPay.ts +20 -12
- package/src/types/lnurl/LNURLWithdraw.ts +17 -10
- package/src/types/wallets/LightningInvoiceCreateService.ts +30 -0
- package/src/types/wallets/MinimalBitcoinWalletInterface.ts +3 -1
- package/src/types/wallets/MinimalLightningNetworkWalletInterface.ts +4 -2
- package/src/utils/BitcoinUtils.ts +5 -0
- package/src/utils/SwapUtils.ts +63 -1
- package/src/utils/TokenUtils.ts +12 -4
- package/dist/bitcoin/BitcoinRpcWithAddressIndex.d.ts +0 -68
- package/dist/bitcoin/BitcoinRpcWithAddressIndex.js +0 -2
- package/dist/bitcoin/LightningNetworkApi.d.ts +0 -12
- package/dist/bitcoin/LightningNetworkApi.js +0 -2
- package/dist/bitcoin/mempool/MempoolApi.d.ts +0 -350
- package/dist/bitcoin/mempool/MempoolApi.js +0 -311
- package/dist/bitcoin/mempool/MempoolBitcoinBlock.d.ts +0 -44
- package/dist/bitcoin/mempool/MempoolBitcoinBlock.js +0 -48
- package/dist/bitcoin/mempool/MempoolBitcoinRpc.d.ts +0 -119
- package/dist/bitcoin/mempool/MempoolBitcoinRpc.js +0 -361
- package/dist/bitcoin/mempool/synchronizer/MempoolBtcRelaySynchronizer.d.ts +0 -22
- package/dist/bitcoin/mempool/synchronizer/MempoolBtcRelaySynchronizer.js +0 -105
- package/dist/errors/PaymentAuthError.d.ts +0 -11
- package/dist/errors/PaymentAuthError.js +0 -23
- package/src/errors/PaymentAuthError.ts +0 -26
|
@@ -8,11 +8,35 @@ import {AmountData} from "../../../types/AmountData";
|
|
|
8
8
|
|
|
9
9
|
export type IToBTCDefinition<T extends ChainType, W extends IToBTCWrapper<T, any>, S extends IToBTCSwap<T>> = SwapTypeDefinition<T, W, S>;
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* Base class for wrappers of escrow-based Smart chain -> Bitcoin (on-chain & lightning) swaps
|
|
13
|
+
*
|
|
14
|
+
* @category Swaps/Smart chain → Bitcoin
|
|
15
|
+
*/
|
|
11
16
|
export abstract class IToBTCWrapper<
|
|
12
17
|
T extends ChainType,
|
|
13
18
|
D extends IToBTCDefinition<T, IToBTCWrapper<T, D>, IToBTCSwap<T, D>>,
|
|
14
19
|
O extends ISwapWrapperOptions = ISwapWrapperOptions
|
|
15
20
|
> extends IEscrowSwapWrapper<T, D, O> {
|
|
21
|
+
/**
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
protected readonly tickSwapState = [ToBTCSwapState.CREATED, ToBTCSwapState.COMMITED, ToBTCSwapState.SOFT_CLAIMED];
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
readonly _pendingSwapStates = [
|
|
30
|
+
ToBTCSwapState.CREATED,
|
|
31
|
+
ToBTCSwapState.QUOTE_SOFT_EXPIRED,
|
|
32
|
+
ToBTCSwapState.COMMITED,
|
|
33
|
+
ToBTCSwapState.SOFT_CLAIMED,
|
|
34
|
+
ToBTCSwapState.REFUNDABLE
|
|
35
|
+
];
|
|
36
|
+
/**
|
|
37
|
+
* @internal
|
|
38
|
+
*/
|
|
39
|
+
readonly _refundableSwapStates = [ToBTCSwapState.REFUNDABLE];
|
|
16
40
|
|
|
17
41
|
/**
|
|
18
42
|
* Pre-fetches intermediary's reputation, doesn't throw, instead aborts via abortController and returns null
|
|
@@ -20,16 +44,17 @@ export abstract class IToBTCWrapper<
|
|
|
20
44
|
* @param amountData
|
|
21
45
|
* @param lp Intermediary
|
|
22
46
|
* @param abortController
|
|
23
|
-
* @protected
|
|
24
47
|
* @returns Intermediary's reputation or null if failed
|
|
25
48
|
* @throws {IntermediaryError} If the intermediary vault doesn't exist
|
|
49
|
+
*
|
|
50
|
+
* @internal
|
|
26
51
|
*/
|
|
27
52
|
protected preFetchIntermediaryReputation(
|
|
28
53
|
amountData: Omit<AmountData, "amount">,
|
|
29
54
|
lp: Intermediary,
|
|
30
55
|
abortController: AbortController
|
|
31
56
|
): Promise<SingleChainReputationType | undefined> {
|
|
32
|
-
return lp.getReputation(this.chainIdentifier, this.
|
|
57
|
+
return lp.getReputation(this.chainIdentifier, this._contract, [amountData.token.toString()], abortController.signal).then(res => {
|
|
33
58
|
if(res==null) throw new IntermediaryError("Invalid data returned - invalid LP vault");
|
|
34
59
|
return res;
|
|
35
60
|
}).catch(e => {
|
|
@@ -46,11 +71,12 @@ export abstract class IToBTCWrapper<
|
|
|
46
71
|
* @param amountData
|
|
47
72
|
* @param claimHash optional hash of the swap or null
|
|
48
73
|
* @param abortController
|
|
49
|
-
* @protected
|
|
50
74
|
* @returns Fee rate
|
|
75
|
+
*
|
|
76
|
+
* @internal
|
|
51
77
|
*/
|
|
52
78
|
protected preFetchFeeRate(signer: string, amountData: Omit<AmountData, "amount">, claimHash: string | undefined, abortController: AbortController): Promise<string | undefined> {
|
|
53
|
-
return this.
|
|
79
|
+
return this._contract.getInitPayInFeeRate(signer, this._chain.randomAddress(), amountData.token, claimHash)
|
|
54
80
|
.catch(e => {
|
|
55
81
|
this.logger.warn("preFetchFeeRate(): Error: ", e);
|
|
56
82
|
abortController.abort(e);
|
|
@@ -58,44 +84,43 @@ export abstract class IToBTCWrapper<
|
|
|
58
84
|
});
|
|
59
85
|
}
|
|
60
86
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
ToBTCSwapState.COMMITED,
|
|
65
|
-
ToBTCSwapState.SOFT_CLAIMED,
|
|
66
|
-
ToBTCSwapState.REFUNDABLE
|
|
67
|
-
];
|
|
68
|
-
public readonly tickSwapState = [ToBTCSwapState.CREATED, ToBTCSwapState.COMMITED, ToBTCSwapState.SOFT_CLAIMED];
|
|
69
|
-
public readonly refundableSwapStates = [ToBTCSwapState.REFUNDABLE];
|
|
70
|
-
|
|
87
|
+
/**
|
|
88
|
+
* @internal
|
|
89
|
+
*/
|
|
71
90
|
protected async processEventInitialize(swap: D["Swap"], event: InitializeEvent<T["Data"]>): Promise<boolean> {
|
|
72
|
-
if(swap.
|
|
73
|
-
swap.
|
|
74
|
-
if(swap.
|
|
91
|
+
if(swap._state===ToBTCSwapState.CREATED || swap._state===ToBTCSwapState.QUOTE_SOFT_EXPIRED) {
|
|
92
|
+
swap._state = ToBTCSwapState.COMMITED;
|
|
93
|
+
if(swap._commitTxId==null) swap._commitTxId = event.meta?.txId;
|
|
75
94
|
return true;
|
|
76
95
|
}
|
|
77
96
|
return false;
|
|
78
97
|
}
|
|
79
98
|
|
|
99
|
+
/**
|
|
100
|
+
* @internal
|
|
101
|
+
*/
|
|
80
102
|
protected async processEventClaim(swap: D["Swap"], event: ClaimEvent<T["Data"]>): Promise<boolean> {
|
|
81
|
-
if(swap.
|
|
103
|
+
if(swap._state!==ToBTCSwapState.REFUNDED && swap._state!==ToBTCSwapState.CLAIMED) {
|
|
82
104
|
await swap._setPaymentResult({
|
|
83
105
|
secret: event.result,
|
|
84
106
|
txId: Buffer.from(event.result, "hex").reverse().toString("hex")
|
|
85
107
|
}).catch(e => {
|
|
86
108
|
this.logger.warn(`processEventClaim(): Failed to set payment result ${event.result}: `, e);
|
|
87
109
|
});
|
|
88
|
-
swap.
|
|
89
|
-
if(swap.
|
|
110
|
+
swap._state = ToBTCSwapState.CLAIMED;
|
|
111
|
+
if(swap._claimTxId==null) swap._claimTxId = event.meta?.txId;
|
|
90
112
|
return true;
|
|
91
113
|
}
|
|
92
114
|
return false;
|
|
93
115
|
}
|
|
94
116
|
|
|
117
|
+
/**
|
|
118
|
+
* @internal
|
|
119
|
+
*/
|
|
95
120
|
protected processEventRefund(swap: D["Swap"], event: RefundEvent<T["Data"]>): Promise<boolean> {
|
|
96
|
-
if(swap.
|
|
97
|
-
swap.
|
|
98
|
-
if(swap.
|
|
121
|
+
if(swap._state!==ToBTCSwapState.CLAIMED && swap._state!==ToBTCSwapState.REFUNDED) {
|
|
122
|
+
swap._state = ToBTCSwapState.REFUNDED;
|
|
123
|
+
if(swap._refundTxId==null) swap._refundTxId = event.meta?.txId;
|
|
99
124
|
return Promise.resolve(true);
|
|
100
125
|
}
|
|
101
126
|
return Promise.resolve(false);
|
|
@@ -34,25 +34,38 @@ const SNOWFLAKE_LIST: Set<string> = new Set([
|
|
|
34
34
|
]);
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
37
|
+
* Escrow based (HTLC) swap for Smart chains -> Bitcoin lightning
|
|
38
|
+
*
|
|
39
|
+
* @category Swaps/Smart chain → Lightning
|
|
39
40
|
*/
|
|
40
41
|
export class ToBTCLNSwap<T extends ChainType = ChainType> extends IToBTCSwap<T, ToBTCLNDefinition<T>> {
|
|
41
|
-
private readonly usesClaimHashAsId: boolean;
|
|
42
|
-
|
|
43
|
-
protected outputToken: BtcToken<true> = BitcoinTokens.BTCLN;
|
|
44
42
|
protected readonly TYPE = SwapType.TO_BTCLN;
|
|
43
|
+
/**
|
|
44
|
+
* @internal
|
|
45
|
+
*/
|
|
46
|
+
protected readonly outputToken: BtcToken<true> = BitcoinTokens.BTCLN;
|
|
47
|
+
/**
|
|
48
|
+
* @internal
|
|
49
|
+
*/
|
|
45
50
|
protected readonly logger: LoggerType;
|
|
46
51
|
|
|
52
|
+
private readonly usesClaimHashAsId: boolean;
|
|
47
53
|
private readonly confidence: number;
|
|
48
|
-
private
|
|
49
|
-
|
|
50
|
-
readonly paymentHash?: string;
|
|
54
|
+
private pr?: string;
|
|
55
|
+
private secret?: string;
|
|
51
56
|
|
|
52
|
-
lnurl?: string;
|
|
53
|
-
successAction?: LNURLPaySuccessAction;
|
|
57
|
+
private lnurl?: string;
|
|
58
|
+
private successAction?: LNURLPaySuccessAction;
|
|
54
59
|
|
|
55
|
-
|
|
60
|
+
/**
|
|
61
|
+
* Sets the LNURL data for the swap
|
|
62
|
+
*
|
|
63
|
+
* @internal
|
|
64
|
+
*/
|
|
65
|
+
_setLNURLData(lnurl: string, successAction?: LNURLPaySuccessAction) {
|
|
66
|
+
this.lnurl = lnurl;
|
|
67
|
+
this.successAction = successAction;
|
|
68
|
+
}
|
|
56
69
|
|
|
57
70
|
constructor(wrapper: ToBTCLNWrapper<T>, init: ToBTCLNSwapInit<T["Data"]>);
|
|
58
71
|
constructor(wrapper: ToBTCLNWrapper<T>, obj: any);
|
|
@@ -75,23 +88,30 @@ export class ToBTCLNSwap<T extends ChainType = ChainType> extends IToBTCSwap<T,
|
|
|
75
88
|
this.usesClaimHashAsId = initOrObj.usesClaimHashAsId ?? false;
|
|
76
89
|
}
|
|
77
90
|
|
|
78
|
-
const paymentHash = this.getPaymentHash();
|
|
79
|
-
if(paymentHash!=null) this.paymentHash = paymentHash.toString("hex");
|
|
80
91
|
this.logger = getLogger("ToBTCLN("+this.getIdentifierHashString()+"): ");
|
|
81
92
|
this.tryRecomputeSwapPrice();
|
|
82
93
|
}
|
|
83
94
|
|
|
95
|
+
/**
|
|
96
|
+
* @inheritDoc
|
|
97
|
+
* @internal
|
|
98
|
+
*/
|
|
84
99
|
_setPaymentResult(result: { secret?: string; txId?: string }, check: boolean = false): Promise<boolean> {
|
|
85
100
|
if(result==null) return Promise.resolve(false);
|
|
86
101
|
if(result.secret==null) throw new IntermediaryError("No payment secret returned!");
|
|
102
|
+
|
|
103
|
+
const secretBuffer = Buffer.from(result.secret, "hex");
|
|
104
|
+
const hash = Buffer.from(sha256(secretBuffer));
|
|
105
|
+
|
|
87
106
|
if(check) {
|
|
88
|
-
const
|
|
89
|
-
const hash = Buffer.from(sha256(secretBuffer));
|
|
90
|
-
const claimHash = this.wrapper.contract.getHashForHtlc(hash);
|
|
107
|
+
const claimHash = this.wrapper._contract.getHashForHtlc(hash);
|
|
91
108
|
|
|
92
109
|
const expectedClaimHash = Buffer.from(this.getClaimHash(), "hex");
|
|
93
110
|
if(!claimHash.equals(expectedClaimHash)) throw new IntermediaryError("Invalid payment secret returned");
|
|
94
111
|
}
|
|
112
|
+
|
|
113
|
+
this.pr ??= hash.toString("hex");
|
|
114
|
+
|
|
95
115
|
this.secret = result.secret;
|
|
96
116
|
return Promise.resolve(true);
|
|
97
117
|
}
|
|
@@ -100,29 +120,40 @@ export class ToBTCLNSwap<T extends ChainType = ChainType> extends IToBTCSwap<T,
|
|
|
100
120
|
//////////////////////////////
|
|
101
121
|
//// Amounts & fees
|
|
102
122
|
|
|
123
|
+
/**
|
|
124
|
+
* @inheritDoc
|
|
125
|
+
*/
|
|
103
126
|
getOutputToken(): BtcToken<true> {
|
|
104
127
|
return BitcoinTokens.BTCLN;
|
|
105
128
|
}
|
|
106
129
|
|
|
130
|
+
/**
|
|
131
|
+
* @inheritDoc
|
|
132
|
+
*/
|
|
107
133
|
getOutput(): TokenAmount<T["ChainId"], BtcToken<true>> {
|
|
108
134
|
if(this.pr==null || !this.pr.toLowerCase().startsWith("ln"))
|
|
109
|
-
return toTokenAmount(null, this.outputToken, this.wrapper.
|
|
135
|
+
return toTokenAmount(null, this.outputToken, this.wrapper._prices, this.pricingInfo);
|
|
110
136
|
const parsedPR = bolt11Decode(this.pr);
|
|
111
137
|
if(parsedPR.millisatoshis==null) throw new Error("Swap invoice has no msat amount field!");
|
|
112
138
|
const amount = (BigInt(parsedPR.millisatoshis) + 999n) / 1000n;
|
|
113
|
-
return toTokenAmount(amount, this.outputToken, this.wrapper.
|
|
139
|
+
return toTokenAmount(amount, this.outputToken, this.wrapper._prices, this.pricingInfo);
|
|
114
140
|
}
|
|
115
141
|
|
|
116
142
|
|
|
117
143
|
//////////////////////////////
|
|
118
144
|
//// Getters & utils
|
|
119
145
|
|
|
146
|
+
/**
|
|
147
|
+
* @inheritDoc
|
|
148
|
+
*/
|
|
120
149
|
getOutputTxId(): string | null {
|
|
121
|
-
|
|
150
|
+
const paymentHash = this.getPaymentHash();
|
|
151
|
+
if(paymentHash==null) return null;
|
|
152
|
+
return paymentHash.toString("hex");
|
|
122
153
|
}
|
|
123
154
|
|
|
124
155
|
/**
|
|
125
|
-
*
|
|
156
|
+
* @inheritDoc
|
|
126
157
|
*/
|
|
127
158
|
getOutputAddress(): string | null {
|
|
128
159
|
return this.lnurl ?? this.pr ?? null;
|
|
@@ -136,15 +167,16 @@ export class ToBTCLNSwap<T extends ChainType = ChainType> extends IToBTCSwap<T,
|
|
|
136
167
|
}
|
|
137
168
|
|
|
138
169
|
/**
|
|
139
|
-
* Returns the confidence of the intermediary that this payment will succeed
|
|
140
|
-
*
|
|
170
|
+
* Returns the confidence of the intermediary that this payment will succeed.
|
|
171
|
+
*
|
|
172
|
+
* @returns Decimal value between 0 and 1, where 0 is not likely and 1 is very likely
|
|
141
173
|
*/
|
|
142
174
|
getConfidence(): number {
|
|
143
175
|
return this.confidence;
|
|
144
176
|
}
|
|
145
177
|
|
|
146
178
|
/**
|
|
147
|
-
* Checks whether a swap is likely to fail, based on the confidence as reported by the LP
|
|
179
|
+
* Checks whether a swap is likely to fail, based on the confidence as reported by the intermediary (LP)
|
|
148
180
|
*/
|
|
149
181
|
willLikelyFail(): boolean {
|
|
150
182
|
if(this.pr==null || !this.pr.toLowerCase().startsWith("ln")) return false;
|
|
@@ -163,8 +195,8 @@ export class ToBTCLNSwap<T extends ChainType = ChainType> extends IToBTCSwap<T,
|
|
|
163
195
|
}
|
|
164
196
|
|
|
165
197
|
/**
|
|
166
|
-
* Tries to detect if the target lightning invoice is a non-custodial mobile wallet, care must be taken
|
|
167
|
-
* for such a wallet to be online when attempting to make a swap
|
|
198
|
+
* Tries to detect if the target lightning invoice is a non-custodial mobile wallet, extract care must be taken
|
|
199
|
+
* for such a wallet **to be online** when attempting to make a swap sending to such a wallet
|
|
168
200
|
*/
|
|
169
201
|
isPayingToNonCustodialWallet(): boolean {
|
|
170
202
|
if(this.pr==null || !this.pr.toLowerCase().startsWith("ln")) return false;
|
|
@@ -177,32 +209,44 @@ export class ToBTCLNSwap<T extends ChainType = ChainType> extends IToBTCSwap<T,
|
|
|
177
209
|
return false;
|
|
178
210
|
}
|
|
179
211
|
|
|
180
|
-
|
|
212
|
+
/**
|
|
213
|
+
* @inheritDoc
|
|
214
|
+
* @internal
|
|
215
|
+
*/
|
|
216
|
+
protected getIdentifierHash(): Buffer {
|
|
181
217
|
const idBuffer: Buffer = this.usesClaimHashAsId
|
|
182
218
|
? Buffer.from(this.getClaimHash(), "hex")
|
|
183
219
|
: this.getPaymentHash()!;
|
|
184
|
-
if(this.
|
|
185
|
-
return Buffer.concat([idBuffer, Buffer.from(this.
|
|
220
|
+
if(this._randomNonce==null) return idBuffer;
|
|
221
|
+
return Buffer.concat([idBuffer, Buffer.from(this._randomNonce, "hex")]);
|
|
186
222
|
}
|
|
187
223
|
|
|
188
|
-
|
|
189
|
-
|
|
224
|
+
/**
|
|
225
|
+
* @inheritDoc
|
|
226
|
+
* @internal
|
|
227
|
+
*/
|
|
228
|
+
protected getLpIdentifier(): string {
|
|
229
|
+
if(this.pr==null) return this._data.getEscrowHash();
|
|
190
230
|
if(this.pr.toLowerCase().startsWith("ln")) {
|
|
191
231
|
const parsed = bolt11Decode(this.pr);
|
|
192
232
|
if(parsed.tagsObject.payment_hash==null) throw new Error("Swap invoice has no payment hash field!");
|
|
193
|
-
return
|
|
233
|
+
return parsed.tagsObject.payment_hash;
|
|
194
234
|
}
|
|
195
|
-
return
|
|
235
|
+
return this.pr;
|
|
196
236
|
}
|
|
197
237
|
|
|
198
|
-
|
|
199
|
-
|
|
238
|
+
/**
|
|
239
|
+
* Returns the payment hash of the swap, i.e. a payment hash of the lightning network invoice that
|
|
240
|
+
* is about to be paid
|
|
241
|
+
*/
|
|
242
|
+
getPaymentHash(): Buffer | null {
|
|
243
|
+
if(this.pr==null) return null;
|
|
200
244
|
if(this.pr.toLowerCase().startsWith("ln")) {
|
|
201
245
|
const parsed = bolt11Decode(this.pr);
|
|
202
246
|
if(parsed.tagsObject.payment_hash==null) throw new Error("Swap invoice has no payment hash field!");
|
|
203
|
-
return parsed.tagsObject.payment_hash;
|
|
247
|
+
return Buffer.from(parsed.tagsObject.payment_hash, "hex");
|
|
204
248
|
}
|
|
205
|
-
return this.pr;
|
|
249
|
+
return Buffer.from(this.pr, "hex");
|
|
206
250
|
}
|
|
207
251
|
|
|
208
252
|
|
|
@@ -210,28 +254,28 @@ export class ToBTCLNSwap<T extends ChainType = ChainType> extends IToBTCSwap<T,
|
|
|
210
254
|
//// LNURL-pay
|
|
211
255
|
|
|
212
256
|
/**
|
|
213
|
-
*
|
|
257
|
+
* Whether this is an LNURL-pay swap
|
|
214
258
|
*/
|
|
215
259
|
isLNURL(): boolean {
|
|
216
260
|
return this.lnurl!=null;
|
|
217
261
|
}
|
|
218
262
|
|
|
219
263
|
/**
|
|
220
|
-
* Gets the used LNURL or null if this is not an LNURL-pay swap
|
|
264
|
+
* Gets the used LNURL-pay link or `null` if this is not an LNURL-pay swap
|
|
221
265
|
*/
|
|
222
266
|
getLNURL(): string | null {
|
|
223
267
|
return this.lnurl ?? null;
|
|
224
268
|
}
|
|
225
269
|
|
|
226
270
|
/**
|
|
227
|
-
* Checks whether this LNURL payment contains a success
|
|
271
|
+
* Checks whether this LNURL-pay payment contains a success action
|
|
228
272
|
*/
|
|
229
273
|
hasSuccessAction(): boolean {
|
|
230
274
|
return this.successAction!=null;
|
|
231
275
|
}
|
|
232
276
|
|
|
233
277
|
/**
|
|
234
|
-
* Returns the success action after a successful payment, else null
|
|
278
|
+
* Returns the success action after a successful payment, else `null`
|
|
235
279
|
*/
|
|
236
280
|
getSuccessAction(): LNURLDecodedSuccessAction | null {
|
|
237
281
|
return LNURL.decodeSuccessAction(this.successAction, this.secret);
|
|
@@ -241,6 +285,9 @@ export class ToBTCLNSwap<T extends ChainType = ChainType> extends IToBTCSwap<T,
|
|
|
241
285
|
//////////////////////////////
|
|
242
286
|
//// Storage
|
|
243
287
|
|
|
288
|
+
/**
|
|
289
|
+
* @inheritDoc
|
|
290
|
+
*/
|
|
244
291
|
serialize(): any {
|
|
245
292
|
return {
|
|
246
293
|
...super.serialize(),
|