@atomiqlabs/sdk 8.6.6 → 8.6.8
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/SmartChainAssets.d.ts +181 -181
- package/dist/SmartChainAssets.js +181 -181
- package/dist/bitcoin/coinselect2/accumulative.d.ts +6 -6
- package/dist/bitcoin/coinselect2/accumulative.js +52 -51
- package/dist/bitcoin/coinselect2/blackjack.d.ts +6 -6
- package/dist/bitcoin/coinselect2/blackjack.js +38 -37
- package/dist/bitcoin/coinselect2/index.d.ts +19 -17
- package/dist/bitcoin/coinselect2/index.js +69 -69
- package/dist/bitcoin/coinselect2/utils.d.ts +77 -75
- package/dist/bitcoin/coinselect2/utils.js +123 -123
- package/dist/bitcoin/wallet/BitcoinWallet.d.ts +130 -128
- package/dist/bitcoin/wallet/BitcoinWallet.js +322 -322
- package/dist/bitcoin/wallet/IBitcoinWallet.d.ts +78 -78
- package/dist/bitcoin/wallet/IBitcoinWallet.js +21 -20
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.d.ts +101 -99
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.js +190 -176
- package/dist/enums/FeeType.d.ts +15 -15
- package/dist/enums/FeeType.js +19 -19
- package/dist/enums/SwapAmountType.d.ts +15 -15
- package/dist/enums/SwapAmountType.js +19 -19
- package/dist/enums/SwapDirection.d.ts +15 -15
- package/dist/enums/SwapDirection.js +19 -19
- package/dist/enums/SwapSide.d.ts +15 -15
- package/dist/enums/SwapSide.js +19 -19
- package/dist/enums/SwapType.d.ts +75 -75
- package/dist/enums/SwapType.js +79 -79
- package/dist/errors/IntermediaryError.d.ts +13 -13
- package/dist/errors/IntermediaryError.js +27 -27
- package/dist/errors/RequestError.d.ts +32 -32
- package/dist/errors/RequestError.js +54 -54
- package/dist/errors/UserError.d.ts +8 -8
- package/dist/errors/UserError.js +16 -16
- package/dist/events/UnifiedSwapEventListener.d.ts +23 -23
- package/dist/events/UnifiedSwapEventListener.js +130 -130
- package/dist/http/HttpUtils.d.ts +27 -27
- package/dist/http/HttpUtils.js +91 -90
- package/dist/http/paramcoders/IParamReader.d.ts +8 -8
- package/dist/http/paramcoders/IParamReader.js +2 -2
- package/dist/http/paramcoders/ParamDecoder.d.ts +44 -42
- package/dist/http/paramcoders/ParamDecoder.js +137 -137
- package/dist/http/paramcoders/ParamEncoder.d.ts +20 -18
- package/dist/http/paramcoders/ParamEncoder.js +36 -36
- package/dist/http/paramcoders/SchemaVerifier.d.ts +26 -26
- package/dist/http/paramcoders/SchemaVerifier.js +145 -145
- package/dist/http/paramcoders/client/ResponseParamDecoder.d.ts +11 -11
- package/dist/http/paramcoders/client/ResponseParamDecoder.js +57 -57
- package/dist/http/paramcoders/client/StreamParamEncoder.d.ts +13 -11
- package/dist/http/paramcoders/client/StreamParamEncoder.js +26 -26
- package/dist/http/paramcoders/client/StreamingFetchPromise.d.ts +16 -16
- package/dist/http/paramcoders/client/StreamingFetchPromise.js +174 -173
- package/dist/index.d.ts +85 -85
- package/dist/index.js +158 -158
- package/dist/intermediaries/Intermediary.d.ts +157 -157
- package/dist/intermediaries/Intermediary.js +142 -142
- package/dist/intermediaries/IntermediaryDiscovery.d.ts +199 -198
- package/dist/intermediaries/IntermediaryDiscovery.js +406 -406
- package/dist/intermediaries/apis/IntermediaryAPI.d.ts +439 -437
- package/dist/intermediaries/apis/IntermediaryAPI.js +603 -603
- package/dist/intermediaries/apis/TrustedIntermediaryAPI.d.ts +155 -155
- package/dist/intermediaries/apis/TrustedIntermediaryAPI.js +137 -137
- package/dist/lnurl/LNURL.d.ts +102 -102
- package/dist/lnurl/LNURL.js +321 -321
- package/dist/prices/RedundantSwapPrice.d.ts +110 -110
- package/dist/prices/RedundantSwapPrice.js +222 -222
- package/dist/prices/SingleSwapPrice.d.ts +34 -34
- package/dist/prices/SingleSwapPrice.js +44 -44
- package/dist/prices/SwapPriceWithChain.d.ts +107 -107
- package/dist/prices/SwapPriceWithChain.js +128 -128
- package/dist/prices/abstract/ICachedSwapPrice.d.ts +28 -28
- package/dist/prices/abstract/ICachedSwapPrice.js +62 -62
- package/dist/prices/abstract/IPriceProvider.d.ts +81 -81
- package/dist/prices/abstract/IPriceProvider.js +74 -74
- package/dist/prices/abstract/ISwapPrice.d.ts +168 -168
- package/dist/prices/abstract/ISwapPrice.js +279 -279
- package/dist/prices/providers/BinancePriceProvider.d.ts +23 -23
- package/dist/prices/providers/BinancePriceProvider.js +30 -30
- package/dist/prices/providers/CoinGeckoPriceProvider.d.ts +23 -23
- package/dist/prices/providers/CoinGeckoPriceProvider.js +29 -29
- package/dist/prices/providers/CoinPaprikaPriceProvider.d.ts +25 -25
- package/dist/prices/providers/CoinPaprikaPriceProvider.js +29 -29
- package/dist/prices/providers/CustomPriceProvider.d.ts +24 -24
- package/dist/prices/providers/CustomPriceProvider.js +35 -35
- package/dist/prices/providers/KrakenPriceProvider.d.ts +38 -38
- package/dist/prices/providers/KrakenPriceProvider.js +45 -45
- package/dist/prices/providers/OKXPriceProvider.d.ts +34 -34
- package/dist/prices/providers/OKXPriceProvider.js +29 -29
- package/dist/prices/providers/abstract/ExchangePriceProvider.d.ts +17 -17
- package/dist/prices/providers/abstract/ExchangePriceProvider.js +21 -21
- package/dist/prices/providers/abstract/HttpPriceProvider.d.ts +7 -7
- package/dist/prices/providers/abstract/HttpPriceProvider.js +12 -12
- package/dist/storage/IUnifiedStorage.d.ts +85 -85
- package/dist/storage/IUnifiedStorage.js +2 -2
- package/dist/storage/UnifiedSwapStorage.d.ts +114 -114
- package/dist/storage/UnifiedSwapStorage.js +116 -116
- package/dist/storage-browser/IndexedDBUnifiedStorage.d.ts +63 -63
- package/dist/storage-browser/IndexedDBUnifiedStorage.js +298 -298
- package/dist/storage-browser/LocalStorageManager.d.ts +49 -49
- package/dist/storage-browser/LocalStorageManager.js +93 -93
- package/dist/swapper/Swapper.d.ts +687 -686
- package/dist/swapper/Swapper.js +1603 -1603
- package/dist/swapper/SwapperFactory.d.ts +135 -135
- package/dist/swapper/SwapperFactory.js +162 -162
- package/dist/swapper/SwapperUtils.d.ts +200 -200
- package/dist/swapper/SwapperUtils.js +467 -467
- package/dist/swapper/SwapperWithChain.d.ts +404 -404
- package/dist/swapper/SwapperWithChain.js +469 -469
- package/dist/swapper/SwapperWithSigner.d.ts +322 -322
- package/dist/swapper/SwapperWithSigner.js +318 -318
- package/dist/swaps/IAddressSwap.d.ts +22 -22
- package/dist/swaps/IAddressSwap.js +14 -13
- package/dist/swaps/IBTCWalletSwap.d.ts +73 -73
- package/dist/swaps/IBTCWalletSwap.js +18 -17
- package/dist/swaps/IClaimableSwap.d.ts +49 -49
- package/dist/swaps/IClaimableSwap.js +15 -14
- package/dist/swaps/IClaimableSwapWrapper.d.ts +15 -15
- package/dist/swaps/IClaimableSwapWrapper.js +2 -2
- package/dist/swaps/IRefundableSwap.d.ts +43 -43
- package/dist/swaps/IRefundableSwap.js +14 -13
- package/dist/swaps/ISwap.d.ts +387 -386
- package/dist/swaps/ISwap.js +346 -346
- package/dist/swaps/ISwapWithGasDrop.d.ts +21 -21
- package/dist/swaps/ISwapWithGasDrop.js +12 -11
- package/dist/swaps/ISwapWrapper.d.ts +285 -283
- package/dist/swaps/ISwapWrapper.js +353 -353
- package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.d.ts +98 -98
- package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.js +126 -126
- package/dist/swaps/escrow_swaps/IEscrowSwap.d.ts +135 -133
- package/dist/swaps/escrow_swaps/IEscrowSwap.js +169 -169
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.d.ts +115 -114
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.js +134 -134
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.d.ts +101 -98
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.js +130 -130
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.d.ts +162 -162
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.js +190 -190
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.d.ts +58 -58
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.js +78 -78
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.d.ts +531 -529
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +1285 -1285
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +184 -181
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +418 -418
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.d.ts +583 -581
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +1371 -1371
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +228 -225
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +506 -506
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.d.ts +458 -458
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +1126 -1126
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.d.ts +191 -190
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.js +378 -378
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.d.ts +403 -403
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +924 -924
- package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.d.ts +62 -62
- package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.js +112 -112
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.d.ts +127 -125
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.js +256 -256
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.d.ts +242 -241
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.js +520 -520
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.d.ts +73 -73
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.js +155 -155
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.d.ts +128 -127
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.js +278 -278
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +630 -630
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +1443 -1443
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.d.ts +214 -213
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.js +756 -756
- package/dist/swaps/trusted/ln/LnForGasSwap.d.ts +261 -261
- package/dist/swaps/trusted/ln/LnForGasSwap.js +511 -511
- package/dist/swaps/trusted/ln/LnForGasWrapper.d.ts +40 -40
- package/dist/swaps/trusted/ln/LnForGasWrapper.js +82 -82
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.d.ts +342 -342
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.js +715 -715
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.d.ts +69 -68
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.js +92 -92
- package/dist/types/AmountData.d.ts +10 -10
- package/dist/types/AmountData.js +2 -2
- package/dist/types/CustomPriceFunction.d.ts +11 -11
- package/dist/types/CustomPriceFunction.js +2 -2
- package/dist/types/PriceInfoType.d.ts +28 -28
- package/dist/types/PriceInfoType.js +57 -56
- package/dist/types/SwapExecutionAction.d.ts +88 -88
- package/dist/types/SwapExecutionAction.js +2 -2
- package/dist/types/SwapStateInfo.d.ts +5 -5
- package/dist/types/SwapStateInfo.js +2 -2
- package/dist/types/SwapWithSigner.d.ts +17 -17
- package/dist/types/SwapWithSigner.js +43 -42
- package/dist/types/Token.d.ts +99 -99
- package/dist/types/Token.js +76 -76
- package/dist/types/TokenAmount.d.ts +69 -69
- package/dist/types/TokenAmount.js +60 -59
- package/dist/types/fees/Fee.d.ts +50 -50
- package/dist/types/fees/Fee.js +2 -2
- package/dist/types/fees/FeeBreakdown.d.ts +11 -11
- package/dist/types/fees/FeeBreakdown.js +2 -2
- package/dist/types/fees/PercentagePPM.d.ts +17 -17
- package/dist/types/fees/PercentagePPM.js +18 -17
- package/dist/types/lnurl/LNURLPay.d.ts +61 -61
- package/dist/types/lnurl/LNURLPay.js +31 -30
- package/dist/types/lnurl/LNURLWithdraw.d.ts +48 -48
- package/dist/types/lnurl/LNURLWithdraw.js +27 -26
- package/dist/types/wallets/LightningInvoiceCreateService.d.ts +24 -24
- package/dist/types/wallets/LightningInvoiceCreateService.js +15 -14
- package/dist/types/wallets/MinimalBitcoinWalletInterface.d.ts +23 -23
- package/dist/types/wallets/MinimalBitcoinWalletInterface.js +2 -2
- package/dist/types/wallets/MinimalLightningNetworkWalletInterface.d.ts +9 -9
- package/dist/types/wallets/MinimalLightningNetworkWalletInterface.js +2 -2
- package/dist/utils/AutomaticClockDriftCorrection.d.ts +1 -1
- package/dist/utils/AutomaticClockDriftCorrection.js +70 -69
- package/dist/utils/BitcoinUtils.d.ts +14 -12
- package/dist/utils/BitcoinUtils.js +102 -101
- package/dist/utils/BitcoinWalletUtils.d.ts +7 -7
- package/dist/utils/BitcoinWalletUtils.js +14 -13
- package/dist/utils/Logger.d.ts +7 -7
- package/dist/utils/Logger.js +12 -11
- package/dist/utils/RetryUtils.d.ts +22 -22
- package/dist/utils/RetryUtils.js +67 -66
- package/dist/utils/SwapUtils.d.ts +88 -88
- package/dist/utils/SwapUtils.js +72 -72
- package/dist/utils/TimeoutUtils.d.ts +17 -17
- package/dist/utils/TimeoutUtils.js +55 -54
- package/dist/utils/TokenUtils.d.ts +19 -19
- package/dist/utils/TokenUtils.js +37 -36
- package/dist/utils/TypeUtils.d.ts +7 -7
- package/dist/utils/TypeUtils.js +2 -2
- package/dist/utils/Utils.d.ts +58 -56
- package/dist/utils/Utils.js +194 -193
- package/package.json +1 -1
- package/src/bitcoin/wallet/SingleAddressBitcoinWallet.ts +14 -2
|
@@ -1,520 +1,520 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ToBTCLNWrapper = void 0;
|
|
4
|
-
const bolt11_1 = require("@atomiqlabs/bolt11");
|
|
5
|
-
const ToBTCLNSwap_1 = require("./ToBTCLNSwap");
|
|
6
|
-
const IToBTCWrapper_1 = require("../IToBTCWrapper");
|
|
7
|
-
const UserError_1 = require("../../../../errors/UserError");
|
|
8
|
-
const base_1 = require("@atomiqlabs/base");
|
|
9
|
-
const IntermediaryError_1 = require("../../../../errors/IntermediaryError");
|
|
10
|
-
const SwapType_1 = require("../../../../enums/SwapType");
|
|
11
|
-
const Utils_1 = require("../../../../utils/Utils");
|
|
12
|
-
const IntermediaryAPI_1 = require("../../../../intermediaries/apis/IntermediaryAPI");
|
|
13
|
-
const RequestError_1 = require("../../../../errors/RequestError");
|
|
14
|
-
const LNURL_1 = require("../../../../lnurl/LNURL");
|
|
15
|
-
const IToBTCSwap_1 = require("../IToBTCSwap");
|
|
16
|
-
const sha2_1 = require("@noble/hashes/sha2");
|
|
17
|
-
const RetryUtils_1 = require("../../../../utils/RetryUtils");
|
|
18
|
-
/**
|
|
19
|
-
* Escrow based (HTLC) swap for Smart chains -> Bitcoin lightning
|
|
20
|
-
*
|
|
21
|
-
* @category Swaps/Smart chain → Lightning
|
|
22
|
-
*/
|
|
23
|
-
class ToBTCLNWrapper extends IToBTCWrapper_1.IToBTCWrapper {
|
|
24
|
-
constructor(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, contract, prices, tokens, swapDataDeserializer, options, events) {
|
|
25
|
-
super(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, contract, prices, tokens, swapDataDeserializer, {
|
|
26
|
-
...options,
|
|
27
|
-
paymentTimeoutSeconds: options?.paymentTimeoutSeconds ?? 5 * 24 * 60 * 60,
|
|
28
|
-
lightningBaseFee: options?.lightningBaseFee ?? 10,
|
|
29
|
-
lightningFeePPM: options?.lightningFeePPM ?? 2000
|
|
30
|
-
}, events);
|
|
31
|
-
this.TYPE = SwapType_1.SwapType.TO_BTCLN;
|
|
32
|
-
/**
|
|
33
|
-
* @internal
|
|
34
|
-
*/
|
|
35
|
-
this._swapDeserializer = ToBTCLNSwap_1.ToBTCLNSwap;
|
|
36
|
-
}
|
|
37
|
-
toRequiredSwapOptions(amountData, options, pricePreFetchPromise, abortSignal) {
|
|
38
|
-
const expirySeconds = options?.expirySeconds ?? this._options.paymentTimeoutSeconds;
|
|
39
|
-
const maxRoutingBaseFee = options?.maxRoutingBaseFee ?? BigInt(this._options.lightningBaseFee);
|
|
40
|
-
const maxRoutingPPM = options?.maxRoutingFeePercentage != null
|
|
41
|
-
? BigInt(Math.floor(options.maxRoutingFeePercentage *
|
|
42
|
-
: options?.maxRoutingPPM ?? BigInt(this._options.lightningFeePPM);
|
|
43
|
-
let maxFee;
|
|
44
|
-
if (options?.maxFee != null) {
|
|
45
|
-
maxFee = options.maxFee;
|
|
46
|
-
}
|
|
47
|
-
else if (amountData.exactIn) {
|
|
48
|
-
if (pricePreFetchPromise != null) {
|
|
49
|
-
maxFee = pricePreFetchPromise
|
|
50
|
-
.then(val => this._prices.getFromBtcSwapAmount(this.chainIdentifier, maxRoutingBaseFee, amountData.token, abortSignal, val))
|
|
51
|
-
.then(_maxBaseFee => this.calculateFeeForAmount(amountData.amount, _maxBaseFee, maxRoutingPPM));
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
maxFee = this._prices.getFromBtcSwapAmount(this.chainIdentifier, maxRoutingBaseFee, amountData.token, abortSignal)
|
|
55
|
-
.then(_maxBaseFee => this.calculateFeeForAmount(amountData.amount, _maxBaseFee, maxRoutingPPM));
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
maxFee = this.calculateFeeForAmount(amountData.amount, maxRoutingBaseFee, maxRoutingPPM);
|
|
60
|
-
}
|
|
61
|
-
return {
|
|
62
|
-
expiryTimestamp: options?.expiryTimestamp ?? BigInt(Math.floor(Date.now() / 1000) + expirySeconds),
|
|
63
|
-
maxFee
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Verifies whether a given payment hash was already paid by checking the local
|
|
68
|
-
* storage of known swaps
|
|
69
|
-
*
|
|
70
|
-
* @param paymentHash Payment hash to check
|
|
71
|
-
*
|
|
72
|
-
* @private
|
|
73
|
-
*/
|
|
74
|
-
async checkPaymentHashWasPaid(paymentHash) {
|
|
75
|
-
const swaps = await this.unifiedStorage.query([[{ key: "type", value: this.TYPE }, { key: "paymentHash", value: paymentHash }]], (obj) => new this._swapDeserializer(this, obj));
|
|
76
|
-
for (let value of swaps) {
|
|
77
|
-
if (value._state === IToBTCSwap_1.ToBTCSwapState.CLAIMED || value._state === IToBTCSwap_1.ToBTCSwapState.SOFT_CLAIMED)
|
|
78
|
-
throw new UserError_1.UserError("Lightning invoice was already paid!");
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Calculates maximum lightning network routing fee based on amount
|
|
83
|
-
*
|
|
84
|
-
* @param amount BTC amount of the swap in satoshis
|
|
85
|
-
* @param overrideBaseFee Override wrapper's default base fee
|
|
86
|
-
* @param overrideFeePPM Override wrapper's default PPM
|
|
87
|
-
*
|
|
88
|
-
* @returns Maximum lightning routing fee in sats
|
|
89
|
-
*
|
|
90
|
-
* @private
|
|
91
|
-
*/
|
|
92
|
-
calculateFeeForAmount(amount, overrideBaseFee, overrideFeePPM) {
|
|
93
|
-
return BigInt(overrideBaseFee ?? this._options.lightningBaseFee)
|
|
94
|
-
+ (amount * BigInt(overrideFeePPM ?? this._options.lightningFeePPM) / 1000000n);
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Verifies returned LP data
|
|
98
|
-
*
|
|
99
|
-
* @param signer
|
|
100
|
-
* @param resp Response as returned by the LP
|
|
101
|
-
* @param parsedPr Parsed bolt11 lightning invoice
|
|
102
|
-
* @param token Smart chain token to be used in the swap
|
|
103
|
-
* @param lp
|
|
104
|
-
* @param calculatedOptions Swap options computed from the swap create options
|
|
105
|
-
* @param data Parsed swap data returned by the LP
|
|
106
|
-
* @param requiredTotal Required total to be paid on the input (for exactIn swaps)
|
|
107
|
-
*
|
|
108
|
-
* @throws {IntermediaryError} In case the response is not valid
|
|
109
|
-
*
|
|
110
|
-
* @private
|
|
111
|
-
*/
|
|
112
|
-
async verifyReturnedData(signer, resp, parsedPr, token, lp, calculatedOptions, data, requiredTotal) {
|
|
113
|
-
if (resp.routingFeeSats > await calculatedOptions.maxFee)
|
|
114
|
-
throw new IntermediaryError_1.IntermediaryError("Invalid max fee sats returned");
|
|
115
|
-
if (requiredTotal != null && resp.total !== requiredTotal)
|
|
116
|
-
throw new IntermediaryError_1.IntermediaryError("Invalid data returned - total amount");
|
|
117
|
-
if (parsedPr.tagsObject.payment_hash == null)
|
|
118
|
-
throw new Error("Swap invoice doesn't contain payment hash field!");
|
|
119
|
-
const claimHash = this._contract.getHashForHtlc(Buffer.from(parsedPr.tagsObject.payment_hash, "hex"));
|
|
120
|
-
if (data.getAmount() !== resp.total ||
|
|
121
|
-
!Buffer.from(data.getClaimHash(), "hex").equals(claimHash) ||
|
|
122
|
-
data.getExpiry() !== calculatedOptions.expiryTimestamp ||
|
|
123
|
-
data.getType() !== base_1.ChainSwapType.HTLC ||
|
|
124
|
-
!data.isPayIn() ||
|
|
125
|
-
!data.isToken(token) ||
|
|
126
|
-
!data.isClaimer(lp.getAddress(this.chainIdentifier)) ||
|
|
127
|
-
!data.isOfferer(signer) ||
|
|
128
|
-
data.getTotalDeposit() !== 0n) {
|
|
129
|
-
throw new IntermediaryError_1.IntermediaryError("Invalid data returned");
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Returns the quote/swap from a given intermediary
|
|
134
|
-
*
|
|
135
|
-
* @param signer Smartchain signer initiating the swap
|
|
136
|
-
* @param amountData
|
|
137
|
-
* @param lp Intermediary
|
|
138
|
-
* @param pr bolt11 lightning network invoice
|
|
139
|
-
* @param parsedPr Parsed bolt11 lightning network invoice
|
|
140
|
-
* @param calculatedOptions Swap options computed from the swap create options
|
|
141
|
-
* @param preFetches
|
|
142
|
-
* @param abort Abort signal or controller, if AbortController is passed it is used as-is, when AbortSignal is passed
|
|
143
|
-
* it is extended with extendAbortController and then used
|
|
144
|
-
* @param additionalParams Additional params that should be sent to the LP
|
|
145
|
-
*
|
|
146
|
-
* @private
|
|
147
|
-
*/
|
|
148
|
-
async getIntermediaryQuote(signer, amountData, lp, pr, parsedPr, calculatedOptions, preFetches, abort, additionalParams) {
|
|
149
|
-
if (lp.services[SwapType_1.SwapType.TO_BTCLN] == null)
|
|
150
|
-
throw new Error("LP service for processing to btcln swaps not found!");
|
|
151
|
-
const abortController = abort instanceof AbortController ? abort : (0, Utils_1.extendAbortController)(abort);
|
|
152
|
-
const reputationPromise = this.preFetchIntermediaryReputation(amountData, lp, abortController);
|
|
153
|
-
try {
|
|
154
|
-
const { signDataPromise, resp } = await (0, RetryUtils_1.tryWithRetries)(async (retryCount) => {
|
|
155
|
-
const { signDataPrefetch, response } = IntermediaryAPI_1.IntermediaryAPI.initToBTCLN(this.chainIdentifier, lp.url, {
|
|
156
|
-
offerer: signer,
|
|
157
|
-
pr,
|
|
158
|
-
maxFee: await calculatedOptions.maxFee,
|
|
159
|
-
expiryTimestamp: calculatedOptions.expiryTimestamp,
|
|
160
|
-
token: amountData.token,
|
|
161
|
-
feeRate: (0, Utils_1.throwIfUndefined)(preFetches.feeRatePromise),
|
|
162
|
-
additionalParams
|
|
163
|
-
}, this._options.postRequestTimeout, abortController.signal, retryCount > 0 ? false : undefined);
|
|
164
|
-
let signDataPromise = preFetches.signDataPrefetchPromise;
|
|
165
|
-
if (signDataPromise == null) {
|
|
166
|
-
signDataPromise = this.preFetchSignData(signDataPrefetch);
|
|
167
|
-
}
|
|
168
|
-
else
|
|
169
|
-
signDataPrefetch.catch(() => { });
|
|
170
|
-
return {
|
|
171
|
-
signDataPromise,
|
|
172
|
-
resp: await response
|
|
173
|
-
};
|
|
174
|
-
}, undefined, e => e instanceof RequestError_1.RequestError, abortController.signal);
|
|
175
|
-
if (parsedPr.millisatoshis == null)
|
|
176
|
-
throw new Error("Swap invoice doesn't have msat amount field!");
|
|
177
|
-
const amountOut = (BigInt(parsedPr.millisatoshis) + 999n) / 1000n;
|
|
178
|
-
const totalFee = resp.swapFee + resp.maxFee;
|
|
179
|
-
const data = new this._swapDataDeserializer(resp.data);
|
|
180
|
-
data.setOfferer(signer);
|
|
181
|
-
await this.verifyReturnedData(signer, resp, parsedPr, amountData.token, lp, calculatedOptions, data);
|
|
182
|
-
const swapFeeBtc = resp.swapFee * amountOut / (data.getAmount() - totalFee);
|
|
183
|
-
const [pricingInfo, signatureExpiry, reputation] = await Promise.all([
|
|
184
|
-
this.verifyReturnedPrice(lp.services[SwapType_1.SwapType.TO_BTCLN], true, amountOut, data.getAmount(), amountData.token, { networkFee: resp.maxFee, swapFeeBtc }, preFetches.pricePreFetchPromise, preFetches.usdPricePrefetchPromise, abortController.signal),
|
|
185
|
-
this.verifyReturnedSignature(signer, data, resp, preFetches.feeRatePromise, signDataPromise, abortController.signal),
|
|
186
|
-
reputationPromise
|
|
187
|
-
]);
|
|
188
|
-
abortController.signal.throwIfAborted();
|
|
189
|
-
if (reputation != null)
|
|
190
|
-
lp.reputation[amountData.token.toString()] = reputation;
|
|
191
|
-
const quote = new ToBTCLNSwap_1.ToBTCLNSwap(this, {
|
|
192
|
-
pricingInfo,
|
|
193
|
-
url: lp.url,
|
|
194
|
-
expiry: signatureExpiry,
|
|
195
|
-
swapFee: resp.swapFee,
|
|
196
|
-
swapFeeBtc,
|
|
197
|
-
feeRate: (await preFetches.feeRatePromise),
|
|
198
|
-
signatureData: resp,
|
|
199
|
-
data,
|
|
200
|
-
networkFee: resp.maxFee,
|
|
201
|
-
networkFeeBtc: resp.routingFeeSats,
|
|
202
|
-
confidence: resp.confidence,
|
|
203
|
-
pr,
|
|
204
|
-
exactIn: false
|
|
205
|
-
});
|
|
206
|
-
return quote;
|
|
207
|
-
}
|
|
208
|
-
catch (e) {
|
|
209
|
-
abortController.abort(e);
|
|
210
|
-
throw e;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
/**
|
|
214
|
-
* Returns a newly created Smart chain -> Lightning swap using the HTLC based escrow swap protocol,
|
|
215
|
-
* the amount is parsed from the provided lightning network payment request (bolt11 invoice)
|
|
216
|
-
*
|
|
217
|
-
* @param signer Source chain signer address initiating the swap
|
|
218
|
-
* @param recipient BOLT11 payment request (bitcoin lightning invoice) you wish to pay
|
|
219
|
-
* @param amountData Token to swap
|
|
220
|
-
* @param lps An array of intermediaries (LPs) to get the quotes from
|
|
221
|
-
* @param options Optional additional quote options
|
|
222
|
-
* @param additionalParams Optional additional parameters sent to the LP when creating the swap
|
|
223
|
-
* @param abortSignal Abort signal
|
|
224
|
-
* @param preFetches Optional existing pre-fetch promises for the swap (only used internally for LNURL swaps)
|
|
225
|
-
*/
|
|
226
|
-
async create(signer, recipient, amountData, lps, options, additionalParams, abortSignal, preFetches) {
|
|
227
|
-
const parsedPr = (0, bolt11_1.decode)(recipient);
|
|
228
|
-
if (parsedPr.millisatoshis == null)
|
|
229
|
-
throw new UserError_1.UserError("Must be an invoice with amount");
|
|
230
|
-
const amountOut = (BigInt(parsedPr.millisatoshis) + 999n) / 1000n;
|
|
231
|
-
const _options = this.toRequiredSwapOptions({ ...amountData, amount: amountOut }, options);
|
|
232
|
-
if (parsedPr.tagsObject.payment_hash == null)
|
|
233
|
-
throw new Error("Provided lightning invoice doesn't contain payment hash field!");
|
|
234
|
-
await this.checkPaymentHashWasPaid(parsedPr.tagsObject.payment_hash);
|
|
235
|
-
const claimHash = this._contract.getHashForHtlc(Buffer.from(parsedPr.tagsObject.payment_hash, "hex"));
|
|
236
|
-
const _abortController = (0, Utils_1.extendAbortController)(abortSignal);
|
|
237
|
-
const _preFetches = preFetches ?? {
|
|
238
|
-
pricePreFetchPromise: this.preFetchPrice(amountData, _abortController.signal),
|
|
239
|
-
feeRatePromise: this.preFetchFeeRate(signer, amountData, claimHash.toString("hex"), _abortController),
|
|
240
|
-
usdPricePrefetchPromise: this.preFetchUsdPrice(_abortController.signal),
|
|
241
|
-
signDataPrefetchPromise: this._contract.preFetchBlockDataForSignatures == null ? this.preFetchSignData(Promise.resolve(true)) : undefined
|
|
242
|
-
};
|
|
243
|
-
return lps.map(lp => {
|
|
244
|
-
return {
|
|
245
|
-
intermediary: lp,
|
|
246
|
-
quote: this.getIntermediaryQuote(signer, amountData, lp, recipient, parsedPr, _options, _preFetches, _abortController.signal, additionalParams)
|
|
247
|
-
};
|
|
248
|
-
});
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* Parses and fetches lnurl pay params from the specified lnurl
|
|
252
|
-
*
|
|
253
|
-
* @param lnurl LNURL to be parsed and fetched
|
|
254
|
-
* @param abortSignal Abort signal
|
|
255
|
-
* @throws {UserError} if the LNURL is invalid or if it's not a LNURL-pay
|
|
256
|
-
*
|
|
257
|
-
* @private
|
|
258
|
-
*/
|
|
259
|
-
async getLNURLPay(lnurl, abortSignal) {
|
|
260
|
-
if (typeof (lnurl) !== "string")
|
|
261
|
-
return lnurl;
|
|
262
|
-
const res = await LNURL_1.LNURL.getLNURL(lnurl, true, this._options.getRequestTimeout, abortSignal);
|
|
263
|
-
if (res == null)
|
|
264
|
-
throw new UserError_1.UserError("Invalid LNURL");
|
|
265
|
-
if (res.tag !== "payRequest")
|
|
266
|
-
throw new UserError_1.UserError("Not a LNURL-pay");
|
|
267
|
-
return res;
|
|
268
|
-
}
|
|
269
|
-
/**
|
|
270
|
-
* Returns the quote/swap from the given LP
|
|
271
|
-
*
|
|
272
|
-
* @param signer Source chain signer address initiating the swap
|
|
273
|
-
* @param amountData Token to swap
|
|
274
|
-
* @param invoiceCreateService Service for creating fixed amount invoices
|
|
275
|
-
* @param lp Intermediary (LPs) to get the quote from
|
|
276
|
-
* @param dummyPr Dummy minimum value bolt11 lightning invoice returned from the LNURL-pay, used to estimate
|
|
277
|
-
* network fees for an actual invoice
|
|
278
|
-
* @param calculatedOptions Swap options computed from the swap create options
|
|
279
|
-
* @param preFetches Optional existing pre-fetch promises for the swap (only used internally for LNURL swaps)
|
|
280
|
-
* @param abortSignal Abort signal
|
|
281
|
-
* @param additionalParams Additional params to be sent to the intermediary
|
|
282
|
-
*
|
|
283
|
-
* @private
|
|
284
|
-
*/
|
|
285
|
-
async getIntermediaryQuoteExactIn(signer, amountData, invoiceCreateService, lp, dummyPr, calculatedOptions, preFetches, abortSignal, additionalParams) {
|
|
286
|
-
if (lp.services[SwapType_1.SwapType.TO_BTCLN] == null)
|
|
287
|
-
throw new Error("LP service for processing to btcln swaps not found!");
|
|
288
|
-
const abortController = (0, Utils_1.extendAbortController)(abortSignal);
|
|
289
|
-
const reputationPromise = this.preFetchIntermediaryReputation(amountData, lp, abortController);
|
|
290
|
-
try {
|
|
291
|
-
const { signDataPromise, prepareResp } = await (0, RetryUtils_1.tryWithRetries)(async (retryCount) => {
|
|
292
|
-
const { signDataPrefetch, response } = IntermediaryAPI_1.IntermediaryAPI.prepareToBTCLNExactIn(this.chainIdentifier, lp.url, {
|
|
293
|
-
token: amountData.token,
|
|
294
|
-
offerer: signer,
|
|
295
|
-
pr: dummyPr,
|
|
296
|
-
amount: amountData.amount,
|
|
297
|
-
maxFee: await calculatedOptions.maxFee,
|
|
298
|
-
expiryTimestamp: calculatedOptions.expiryTimestamp,
|
|
299
|
-
additionalParams
|
|
300
|
-
}, this._options.postRequestTimeout, abortController.signal, retryCount > 0 ? false : undefined);
|
|
301
|
-
return {
|
|
302
|
-
signDataPromise: this.preFetchSignData(signDataPrefetch),
|
|
303
|
-
prepareResp: await response
|
|
304
|
-
};
|
|
305
|
-
}, undefined, e => e instanceof RequestError_1.RequestError, abortController.signal);
|
|
306
|
-
if (prepareResp.amount <= 0n)
|
|
307
|
-
throw new IntermediaryError_1.IntermediaryError("Invalid amount returned (zero or negative)");
|
|
308
|
-
if (invoiceCreateService.minMsats != null) {
|
|
309
|
-
if (prepareResp.amount < invoiceCreateService.minMsats / 1000n)
|
|
310
|
-
throw new UserError_1.UserError("Amount less than minimum");
|
|
311
|
-
}
|
|
312
|
-
if (invoiceCreateService.maxMSats != null) {
|
|
313
|
-
if (prepareResp.amount > invoiceCreateService.maxMSats / 1000n)
|
|
314
|
-
throw new UserError_1.UserError("Amount more than maximum");
|
|
315
|
-
}
|
|
316
|
-
const invoice = await invoiceCreateService.getInvoice(Number(prepareResp.amount), abortController.signal);
|
|
317
|
-
const parsedInvoice = (0, bolt11_1.decode)(invoice);
|
|
318
|
-
const resp = await (0, RetryUtils_1.tryWithRetries)((retryCount) => IntermediaryAPI_1.IntermediaryAPI.initToBTCLNExactIn(lp.url, {
|
|
319
|
-
pr: invoice,
|
|
320
|
-
reqId: prepareResp.reqId,
|
|
321
|
-
feeRate: (0, Utils_1.throwIfUndefined)(preFetches.feeRatePromise),
|
|
322
|
-
additionalParams
|
|
323
|
-
}, this._options.postRequestTimeout, abortController.signal, retryCount > 0 ? false : undefined), undefined, RequestError_1.RequestError, abortController.signal);
|
|
324
|
-
if (parsedInvoice.millisatoshis == null)
|
|
325
|
-
throw new Error("Swap invoice doesn't have msat amount field!");
|
|
326
|
-
const amountOut = (BigInt(parsedInvoice.millisatoshis) + 999n) / 1000n;
|
|
327
|
-
const totalFee = resp.swapFee + resp.maxFee;
|
|
328
|
-
const data = new this._swapDataDeserializer(resp.data);
|
|
329
|
-
data.setOfferer(signer);
|
|
330
|
-
await this.verifyReturnedData(signer, resp, parsedInvoice, amountData.token, lp, calculatedOptions, data, amountData.amount);
|
|
331
|
-
const swapFeeBtc = resp.swapFee * amountOut / (data.getAmount() - totalFee);
|
|
332
|
-
const [pricingInfo, signatureExpiry, reputation] = await Promise.all([
|
|
333
|
-
this.verifyReturnedPrice(lp.services[SwapType_1.SwapType.TO_BTCLN], true, prepareResp.amount, data.getAmount(), amountData.token, { networkFee: resp.maxFee, swapFeeBtc }, preFetches.pricePreFetchPromise, preFetches.usdPricePrefetchPromise, abortSignal),
|
|
334
|
-
this.verifyReturnedSignature(signer, data, resp, preFetches.feeRatePromise, signDataPromise, abortController.signal),
|
|
335
|
-
reputationPromise
|
|
336
|
-
]);
|
|
337
|
-
abortController.signal.throwIfAborted();
|
|
338
|
-
if (reputation != null)
|
|
339
|
-
lp.reputation[amountData.token.toString()] = reputation;
|
|
340
|
-
const quote = new ToBTCLNSwap_1.ToBTCLNSwap(this, {
|
|
341
|
-
pricingInfo,
|
|
342
|
-
url: lp.url,
|
|
343
|
-
expiry: signatureExpiry,
|
|
344
|
-
swapFee: resp.swapFee,
|
|
345
|
-
swapFeeBtc,
|
|
346
|
-
feeRate: (await preFetches.feeRatePromise),
|
|
347
|
-
signatureData: resp,
|
|
348
|
-
data,
|
|
349
|
-
networkFee: resp.maxFee,
|
|
350
|
-
networkFeeBtc: resp.routingFeeSats,
|
|
351
|
-
confidence: resp.confidence,
|
|
352
|
-
pr: invoice,
|
|
353
|
-
exactIn: true
|
|
354
|
-
});
|
|
355
|
-
return quote;
|
|
356
|
-
}
|
|
357
|
-
catch (e) {
|
|
358
|
-
abortController.abort(e);
|
|
359
|
-
throw e;
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
/**
|
|
363
|
-
* Returns a newly created Smart chain -> Lightning swap using the HTLC based escrow swap protocol via
|
|
364
|
-
* invoice creation service. This allows exactIn swaps by requesting the desired fixed amount lightning
|
|
365
|
-
* network invoice from the service.
|
|
366
|
-
*
|
|
367
|
-
* @param signer Source chain signer address initiating the swap
|
|
368
|
-
* @param invoiceCreateServicePromise Service to request destination lightning network invoices from
|
|
369
|
-
* @param amountData Amount, token and exact input/output data for to swap
|
|
370
|
-
* @param lps An array of intermediaries (LPs) to get the quotes from
|
|
371
|
-
* @param options Optional additional quote options
|
|
372
|
-
* @param additionalParams Optional additional parameters sent to the LP when creating the swap
|
|
373
|
-
* @param abortSignal Abort signal
|
|
374
|
-
*/
|
|
375
|
-
async createViaInvoiceCreateService(signer, invoiceCreateServicePromise, amountData, lps, options, additionalParams, abortSignal) {
|
|
376
|
-
if (!this.isInitialized)
|
|
377
|
-
throw new Error("Not initialized, call init() first!");
|
|
378
|
-
const _abortController = (0, Utils_1.extendAbortController)(abortSignal);
|
|
379
|
-
const pricePreFetchPromise = this.preFetchPrice(amountData, _abortController.signal);
|
|
380
|
-
const usdPricePrefetchPromise = this.preFetchUsdPrice(_abortController.signal);
|
|
381
|
-
const feeRatePromise = this.preFetchFeeRate(signer, amountData, undefined, _abortController);
|
|
382
|
-
const signDataPrefetchPromise = this._contract.preFetchBlockDataForSignatures == null ?
|
|
383
|
-
this.preFetchSignData(Promise.resolve(true)) :
|
|
384
|
-
undefined;
|
|
385
|
-
const _options = this.toRequiredSwapOptions(amountData, options, pricePreFetchPromise, _abortController.signal);
|
|
386
|
-
try {
|
|
387
|
-
const invoiceCreateService = await invoiceCreateServicePromise;
|
|
388
|
-
if (amountData.exactIn) {
|
|
389
|
-
const dummyInvoice = await invoiceCreateService.getInvoice(invoiceCreateService.minMsats == null ? 1 : Number(invoiceCreateService.minMsats / 1000n), _abortController.signal);
|
|
390
|
-
return lps.map(lp => {
|
|
391
|
-
return {
|
|
392
|
-
quote: this.getIntermediaryQuoteExactIn(signer, amountData, invoiceCreateService, lp, dummyInvoice, _options, {
|
|
393
|
-
pricePreFetchPromise,
|
|
394
|
-
usdPricePrefetchPromise,
|
|
395
|
-
feeRatePromise
|
|
396
|
-
}, _abortController.signal, additionalParams),
|
|
397
|
-
intermediary: lp
|
|
398
|
-
};
|
|
399
|
-
});
|
|
400
|
-
}
|
|
401
|
-
else {
|
|
402
|
-
if (invoiceCreateService.minMsats != null) {
|
|
403
|
-
if (amountData.amount < invoiceCreateService.minMsats / 1000n)
|
|
404
|
-
throw new UserError_1.UserError("Amount less than minimum");
|
|
405
|
-
}
|
|
406
|
-
if (invoiceCreateService.maxMSats != null) {
|
|
407
|
-
if (amountData.amount > invoiceCreateService.maxMSats / 1000n)
|
|
408
|
-
throw new UserError_1.UserError("Amount more than maximum");
|
|
409
|
-
}
|
|
410
|
-
const invoice = await invoiceCreateService.getInvoice(Number(amountData.amount), _abortController.signal);
|
|
411
|
-
return (await this.create(signer, invoice, { ...amountData, exactIn: false }, lps, options, additionalParams, _abortController.signal, {
|
|
412
|
-
feeRatePromise,
|
|
413
|
-
pricePreFetchPromise,
|
|
414
|
-
usdPricePrefetchPromise,
|
|
415
|
-
signDataPrefetchPromise
|
|
416
|
-
}));
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
catch (e) {
|
|
420
|
-
_abortController.abort(e);
|
|
421
|
-
throw e;
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
/**
|
|
425
|
-
* Returns a newly created Smart chain -> Lightning swap using the HTLC based escrow swap protocol. Pays to
|
|
426
|
-
* an LNURL-pay link. This allows exactIn swaps by requesting the desired fixed amount lightning
|
|
427
|
-
* network invoice from the LNURL service.
|
|
428
|
-
*
|
|
429
|
-
* @param signer Source chain signer address initiating the swap
|
|
430
|
-
* @param lnurl LNURL-pay link of the recipient
|
|
431
|
-
* @param amountData Amount, token and exact input/output data for to swap
|
|
432
|
-
* @param lps An array of intermediaries (LPs) to get the quotes from
|
|
433
|
-
* @param options Optional additional quote options
|
|
434
|
-
* @param additionalParams Optional additional parameters sent to the LP when creating the swap
|
|
435
|
-
* @param abortSignal Abort signal
|
|
436
|
-
*/
|
|
437
|
-
async createViaLNURL(signer, lnurl, amountData, lps, options, additionalParams, abortSignal) {
|
|
438
|
-
let successActions = {};
|
|
439
|
-
const _abortController = (0, Utils_1.extendAbortController)(abortSignal);
|
|
440
|
-
const invoiceCreateService = (async () => {
|
|
441
|
-
let payRequest = await this.getLNURLPay(lnurl, _abortController.signal);
|
|
442
|
-
if (options?.comment != null &&
|
|
443
|
-
(payRequest.commentAllowed == null || options.comment.length > payRequest.commentAllowed))
|
|
444
|
-
throw new UserError_1.UserError("Comment not allowed or too long");
|
|
445
|
-
return {
|
|
446
|
-
getInvoice: async (amountSats, abortSignal) => {
|
|
447
|
-
const { invoice, successAction } = await LNURL_1.LNURL.useLNURLPay(payRequest, BigInt(amountSats), options?.comment, this._options.getRequestTimeout, abortSignal);
|
|
448
|
-
if (successAction != null)
|
|
449
|
-
successActions[invoice] = successAction;
|
|
450
|
-
return invoice;
|
|
451
|
-
},
|
|
452
|
-
minMsats: BigInt(payRequest.minSendable),
|
|
453
|
-
maxMsats: BigInt(payRequest.maxSendable),
|
|
454
|
-
url: payRequest.url
|
|
455
|
-
};
|
|
456
|
-
})();
|
|
457
|
-
const quotes = await this.createViaInvoiceCreateService(signer, invoiceCreateService, amountData, lps, options, additionalParams, _abortController.signal);
|
|
458
|
-
_abortController.signal.throwIfAborted();
|
|
459
|
-
const resolved = await invoiceCreateService;
|
|
460
|
-
_abortController.signal.throwIfAborted();
|
|
461
|
-
return quotes.map(value => ({
|
|
462
|
-
quote: value.quote.then(quote => {
|
|
463
|
-
let _successAction;
|
|
464
|
-
const quoteAddress = quote.getOutputAddress();
|
|
465
|
-
if (quoteAddress != null) {
|
|
466
|
-
const successAction = successActions[quoteAddress];
|
|
467
|
-
if (successAction != null)
|
|
468
|
-
_successAction = successAction;
|
|
469
|
-
}
|
|
470
|
-
quote._setLNURLData(resolved.url, _successAction);
|
|
471
|
-
return quote;
|
|
472
|
-
}),
|
|
473
|
-
intermediary: value.intermediary
|
|
474
|
-
}));
|
|
475
|
-
}
|
|
476
|
-
/**
|
|
477
|
-
* @inheritDoc
|
|
478
|
-
*/
|
|
479
|
-
async recoverFromSwapDataAndState(init, state, lp) {
|
|
480
|
-
const data = init.data;
|
|
481
|
-
let paymentHash = data.getHTLCHashHint();
|
|
482
|
-
let secret;
|
|
483
|
-
if (state.type === base_1.SwapCommitStateType.PAID) {
|
|
484
|
-
secret = await state.getClaimResult();
|
|
485
|
-
paymentHash = Buffer.from((0, sha2_1.sha256)(Buffer.from(secret, "hex"))).toString("hex");
|
|
486
|
-
}
|
|
487
|
-
const swapInit = {
|
|
488
|
-
pricingInfo: {
|
|
489
|
-
isValid: true,
|
|
490
|
-
satsBaseFee: 0n,
|
|
491
|
-
swapPriceUSatPerToken: 100000000000000n,
|
|
492
|
-
realPriceUSatPerToken: 100000000000000n,
|
|
493
|
-
differencePPM: 0n,
|
|
494
|
-
feePPM: 0n,
|
|
495
|
-
},
|
|
496
|
-
url: lp?.url,
|
|
497
|
-
expiry: 0,
|
|
498
|
-
swapFee: 0n,
|
|
499
|
-
swapFeeBtc: 0n,
|
|
500
|
-
feeRate: "",
|
|
501
|
-
signatureData: undefined,
|
|
502
|
-
data,
|
|
503
|
-
networkFee: 0n,
|
|
504
|
-
networkFeeBtc: 0n,
|
|
505
|
-
confidence: 0,
|
|
506
|
-
pr: paymentHash ?? undefined,
|
|
507
|
-
exactIn: false
|
|
508
|
-
};
|
|
509
|
-
const swap = new ToBTCLNSwap_1.ToBTCLNSwap(this, swapInit);
|
|
510
|
-
swap._commitTxId = await init.getInitTxId();
|
|
511
|
-
const blockData = await init.getTxBlock();
|
|
512
|
-
swap.createdAt = blockData.blockTime * 1000;
|
|
513
|
-
swap._setInitiated();
|
|
514
|
-
swap._state = IToBTCSwap_1.ToBTCSwapState.COMMITED;
|
|
515
|
-
await swap._sync(false, false, state);
|
|
516
|
-
await swap._save();
|
|
517
|
-
return swap;
|
|
518
|
-
}
|
|
519
|
-
}
|
|
520
|
-
exports.ToBTCLNWrapper = ToBTCLNWrapper;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ToBTCLNWrapper = void 0;
|
|
4
|
+
const bolt11_1 = require("@atomiqlabs/bolt11");
|
|
5
|
+
const ToBTCLNSwap_1 = require("./ToBTCLNSwap");
|
|
6
|
+
const IToBTCWrapper_1 = require("../IToBTCWrapper");
|
|
7
|
+
const UserError_1 = require("../../../../errors/UserError");
|
|
8
|
+
const base_1 = require("@atomiqlabs/base");
|
|
9
|
+
const IntermediaryError_1 = require("../../../../errors/IntermediaryError");
|
|
10
|
+
const SwapType_1 = require("../../../../enums/SwapType");
|
|
11
|
+
const Utils_1 = require("../../../../utils/Utils");
|
|
12
|
+
const IntermediaryAPI_1 = require("../../../../intermediaries/apis/IntermediaryAPI");
|
|
13
|
+
const RequestError_1 = require("../../../../errors/RequestError");
|
|
14
|
+
const LNURL_1 = require("../../../../lnurl/LNURL");
|
|
15
|
+
const IToBTCSwap_1 = require("../IToBTCSwap");
|
|
16
|
+
const sha2_1 = require("@noble/hashes/sha2");
|
|
17
|
+
const RetryUtils_1 = require("../../../../utils/RetryUtils");
|
|
18
|
+
/**
|
|
19
|
+
* Escrow based (HTLC) swap for Smart chains -> Bitcoin lightning
|
|
20
|
+
*
|
|
21
|
+
* @category Swaps/Smart chain → Lightning
|
|
22
|
+
*/
|
|
23
|
+
class ToBTCLNWrapper extends IToBTCWrapper_1.IToBTCWrapper {
|
|
24
|
+
constructor(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, contract, prices, tokens, swapDataDeserializer, options, events) {
|
|
25
|
+
super(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, contract, prices, tokens, swapDataDeserializer, {
|
|
26
|
+
...options,
|
|
27
|
+
paymentTimeoutSeconds: options?.paymentTimeoutSeconds ?? 5 * 24 * 60 * 60,
|
|
28
|
+
lightningBaseFee: options?.lightningBaseFee ?? 10,
|
|
29
|
+
lightningFeePPM: options?.lightningFeePPM ?? 2000
|
|
30
|
+
}, events);
|
|
31
|
+
this.TYPE = SwapType_1.SwapType.TO_BTCLN;
|
|
32
|
+
/**
|
|
33
|
+
* @internal
|
|
34
|
+
*/
|
|
35
|
+
this._swapDeserializer = ToBTCLNSwap_1.ToBTCLNSwap;
|
|
36
|
+
}
|
|
37
|
+
toRequiredSwapOptions(amountData, options, pricePreFetchPromise, abortSignal) {
|
|
38
|
+
const expirySeconds = options?.expirySeconds ?? this._options.paymentTimeoutSeconds;
|
|
39
|
+
const maxRoutingBaseFee = options?.maxRoutingBaseFee ?? BigInt(this._options.lightningBaseFee);
|
|
40
|
+
const maxRoutingPPM = options?.maxRoutingFeePercentage != null
|
|
41
|
+
? BigInt(Math.floor(options.maxRoutingFeePercentage * 10000))
|
|
42
|
+
: options?.maxRoutingPPM ?? BigInt(this._options.lightningFeePPM);
|
|
43
|
+
let maxFee;
|
|
44
|
+
if (options?.maxFee != null) {
|
|
45
|
+
maxFee = options.maxFee;
|
|
46
|
+
}
|
|
47
|
+
else if (amountData.exactIn) {
|
|
48
|
+
if (pricePreFetchPromise != null) {
|
|
49
|
+
maxFee = pricePreFetchPromise
|
|
50
|
+
.then(val => this._prices.getFromBtcSwapAmount(this.chainIdentifier, maxRoutingBaseFee, amountData.token, abortSignal, val))
|
|
51
|
+
.then(_maxBaseFee => this.calculateFeeForAmount(amountData.amount, _maxBaseFee, maxRoutingPPM));
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
maxFee = this._prices.getFromBtcSwapAmount(this.chainIdentifier, maxRoutingBaseFee, amountData.token, abortSignal)
|
|
55
|
+
.then(_maxBaseFee => this.calculateFeeForAmount(amountData.amount, _maxBaseFee, maxRoutingPPM));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
maxFee = this.calculateFeeForAmount(amountData.amount, maxRoutingBaseFee, maxRoutingPPM);
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
expiryTimestamp: options?.expiryTimestamp ?? BigInt(Math.floor(Date.now() / 1000) + expirySeconds),
|
|
63
|
+
maxFee
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Verifies whether a given payment hash was already paid by checking the local
|
|
68
|
+
* storage of known swaps
|
|
69
|
+
*
|
|
70
|
+
* @param paymentHash Payment hash to check
|
|
71
|
+
*
|
|
72
|
+
* @private
|
|
73
|
+
*/
|
|
74
|
+
async checkPaymentHashWasPaid(paymentHash) {
|
|
75
|
+
const swaps = await this.unifiedStorage.query([[{ key: "type", value: this.TYPE }, { key: "paymentHash", value: paymentHash }]], (obj) => new this._swapDeserializer(this, obj));
|
|
76
|
+
for (let value of swaps) {
|
|
77
|
+
if (value._state === IToBTCSwap_1.ToBTCSwapState.CLAIMED || value._state === IToBTCSwap_1.ToBTCSwapState.SOFT_CLAIMED)
|
|
78
|
+
throw new UserError_1.UserError("Lightning invoice was already paid!");
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Calculates maximum lightning network routing fee based on amount
|
|
83
|
+
*
|
|
84
|
+
* @param amount BTC amount of the swap in satoshis
|
|
85
|
+
* @param overrideBaseFee Override wrapper's default base fee
|
|
86
|
+
* @param overrideFeePPM Override wrapper's default PPM
|
|
87
|
+
*
|
|
88
|
+
* @returns Maximum lightning routing fee in sats
|
|
89
|
+
*
|
|
90
|
+
* @private
|
|
91
|
+
*/
|
|
92
|
+
calculateFeeForAmount(amount, overrideBaseFee, overrideFeePPM) {
|
|
93
|
+
return BigInt(overrideBaseFee ?? this._options.lightningBaseFee)
|
|
94
|
+
+ (amount * BigInt(overrideFeePPM ?? this._options.lightningFeePPM) / 1000000n);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Verifies returned LP data
|
|
98
|
+
*
|
|
99
|
+
* @param signer
|
|
100
|
+
* @param resp Response as returned by the LP
|
|
101
|
+
* @param parsedPr Parsed bolt11 lightning invoice
|
|
102
|
+
* @param token Smart chain token to be used in the swap
|
|
103
|
+
* @param lp
|
|
104
|
+
* @param calculatedOptions Swap options computed from the swap create options
|
|
105
|
+
* @param data Parsed swap data returned by the LP
|
|
106
|
+
* @param requiredTotal Required total to be paid on the input (for exactIn swaps)
|
|
107
|
+
*
|
|
108
|
+
* @throws {IntermediaryError} In case the response is not valid
|
|
109
|
+
*
|
|
110
|
+
* @private
|
|
111
|
+
*/
|
|
112
|
+
async verifyReturnedData(signer, resp, parsedPr, token, lp, calculatedOptions, data, requiredTotal) {
|
|
113
|
+
if (resp.routingFeeSats > await calculatedOptions.maxFee)
|
|
114
|
+
throw new IntermediaryError_1.IntermediaryError("Invalid max fee sats returned");
|
|
115
|
+
if (requiredTotal != null && resp.total !== requiredTotal)
|
|
116
|
+
throw new IntermediaryError_1.IntermediaryError("Invalid data returned - total amount");
|
|
117
|
+
if (parsedPr.tagsObject.payment_hash == null)
|
|
118
|
+
throw new Error("Swap invoice doesn't contain payment hash field!");
|
|
119
|
+
const claimHash = this._contract.getHashForHtlc(Buffer.from(parsedPr.tagsObject.payment_hash, "hex"));
|
|
120
|
+
if (data.getAmount() !== resp.total ||
|
|
121
|
+
!Buffer.from(data.getClaimHash(), "hex").equals(claimHash) ||
|
|
122
|
+
data.getExpiry() !== calculatedOptions.expiryTimestamp ||
|
|
123
|
+
data.getType() !== base_1.ChainSwapType.HTLC ||
|
|
124
|
+
!data.isPayIn() ||
|
|
125
|
+
!data.isToken(token) ||
|
|
126
|
+
!data.isClaimer(lp.getAddress(this.chainIdentifier)) ||
|
|
127
|
+
!data.isOfferer(signer) ||
|
|
128
|
+
data.getTotalDeposit() !== 0n) {
|
|
129
|
+
throw new IntermediaryError_1.IntermediaryError("Invalid data returned");
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Returns the quote/swap from a given intermediary
|
|
134
|
+
*
|
|
135
|
+
* @param signer Smartchain signer initiating the swap
|
|
136
|
+
* @param amountData
|
|
137
|
+
* @param lp Intermediary
|
|
138
|
+
* @param pr bolt11 lightning network invoice
|
|
139
|
+
* @param parsedPr Parsed bolt11 lightning network invoice
|
|
140
|
+
* @param calculatedOptions Swap options computed from the swap create options
|
|
141
|
+
* @param preFetches
|
|
142
|
+
* @param abort Abort signal or controller, if AbortController is passed it is used as-is, when AbortSignal is passed
|
|
143
|
+
* it is extended with extendAbortController and then used
|
|
144
|
+
* @param additionalParams Additional params that should be sent to the LP
|
|
145
|
+
*
|
|
146
|
+
* @private
|
|
147
|
+
*/
|
|
148
|
+
async getIntermediaryQuote(signer, amountData, lp, pr, parsedPr, calculatedOptions, preFetches, abort, additionalParams) {
|
|
149
|
+
if (lp.services[SwapType_1.SwapType.TO_BTCLN] == null)
|
|
150
|
+
throw new Error("LP service for processing to btcln swaps not found!");
|
|
151
|
+
const abortController = abort instanceof AbortController ? abort : (0, Utils_1.extendAbortController)(abort);
|
|
152
|
+
const reputationPromise = this.preFetchIntermediaryReputation(amountData, lp, abortController);
|
|
153
|
+
try {
|
|
154
|
+
const { signDataPromise, resp } = await (0, RetryUtils_1.tryWithRetries)(async (retryCount) => {
|
|
155
|
+
const { signDataPrefetch, response } = IntermediaryAPI_1.IntermediaryAPI.initToBTCLN(this.chainIdentifier, lp.url, {
|
|
156
|
+
offerer: signer,
|
|
157
|
+
pr,
|
|
158
|
+
maxFee: await calculatedOptions.maxFee,
|
|
159
|
+
expiryTimestamp: calculatedOptions.expiryTimestamp,
|
|
160
|
+
token: amountData.token,
|
|
161
|
+
feeRate: (0, Utils_1.throwIfUndefined)(preFetches.feeRatePromise),
|
|
162
|
+
additionalParams
|
|
163
|
+
}, this._options.postRequestTimeout, abortController.signal, retryCount > 0 ? false : undefined);
|
|
164
|
+
let signDataPromise = preFetches.signDataPrefetchPromise;
|
|
165
|
+
if (signDataPromise == null) {
|
|
166
|
+
signDataPromise = this.preFetchSignData(signDataPrefetch);
|
|
167
|
+
}
|
|
168
|
+
else
|
|
169
|
+
signDataPrefetch.catch(() => { });
|
|
170
|
+
return {
|
|
171
|
+
signDataPromise,
|
|
172
|
+
resp: await response
|
|
173
|
+
};
|
|
174
|
+
}, undefined, e => e instanceof RequestError_1.RequestError, abortController.signal);
|
|
175
|
+
if (parsedPr.millisatoshis == null)
|
|
176
|
+
throw new Error("Swap invoice doesn't have msat amount field!");
|
|
177
|
+
const amountOut = (BigInt(parsedPr.millisatoshis) + 999n) / 1000n;
|
|
178
|
+
const totalFee = resp.swapFee + resp.maxFee;
|
|
179
|
+
const data = new this._swapDataDeserializer(resp.data);
|
|
180
|
+
data.setOfferer(signer);
|
|
181
|
+
await this.verifyReturnedData(signer, resp, parsedPr, amountData.token, lp, calculatedOptions, data);
|
|
182
|
+
const swapFeeBtc = resp.swapFee * amountOut / (data.getAmount() - totalFee);
|
|
183
|
+
const [pricingInfo, signatureExpiry, reputation] = await Promise.all([
|
|
184
|
+
this.verifyReturnedPrice(lp.services[SwapType_1.SwapType.TO_BTCLN], true, amountOut, data.getAmount(), amountData.token, { networkFee: resp.maxFee, swapFeeBtc }, preFetches.pricePreFetchPromise, preFetches.usdPricePrefetchPromise, abortController.signal),
|
|
185
|
+
this.verifyReturnedSignature(signer, data, resp, preFetches.feeRatePromise, signDataPromise, abortController.signal),
|
|
186
|
+
reputationPromise
|
|
187
|
+
]);
|
|
188
|
+
abortController.signal.throwIfAborted();
|
|
189
|
+
if (reputation != null)
|
|
190
|
+
lp.reputation[amountData.token.toString()] = reputation;
|
|
191
|
+
const quote = new ToBTCLNSwap_1.ToBTCLNSwap(this, {
|
|
192
|
+
pricingInfo,
|
|
193
|
+
url: lp.url,
|
|
194
|
+
expiry: signatureExpiry,
|
|
195
|
+
swapFee: resp.swapFee,
|
|
196
|
+
swapFeeBtc,
|
|
197
|
+
feeRate: (await preFetches.feeRatePromise),
|
|
198
|
+
signatureData: resp,
|
|
199
|
+
data,
|
|
200
|
+
networkFee: resp.maxFee,
|
|
201
|
+
networkFeeBtc: resp.routingFeeSats,
|
|
202
|
+
confidence: resp.confidence,
|
|
203
|
+
pr,
|
|
204
|
+
exactIn: false
|
|
205
|
+
});
|
|
206
|
+
return quote;
|
|
207
|
+
}
|
|
208
|
+
catch (e) {
|
|
209
|
+
abortController.abort(e);
|
|
210
|
+
throw e;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Returns a newly created Smart chain -> Lightning swap using the HTLC based escrow swap protocol,
|
|
215
|
+
* the amount is parsed from the provided lightning network payment request (bolt11 invoice)
|
|
216
|
+
*
|
|
217
|
+
* @param signer Source chain signer address initiating the swap
|
|
218
|
+
* @param recipient BOLT11 payment request (bitcoin lightning invoice) you wish to pay
|
|
219
|
+
* @param amountData Token to swap
|
|
220
|
+
* @param lps An array of intermediaries (LPs) to get the quotes from
|
|
221
|
+
* @param options Optional additional quote options
|
|
222
|
+
* @param additionalParams Optional additional parameters sent to the LP when creating the swap
|
|
223
|
+
* @param abortSignal Abort signal
|
|
224
|
+
* @param preFetches Optional existing pre-fetch promises for the swap (only used internally for LNURL swaps)
|
|
225
|
+
*/
|
|
226
|
+
async create(signer, recipient, amountData, lps, options, additionalParams, abortSignal, preFetches) {
|
|
227
|
+
const parsedPr = (0, bolt11_1.decode)(recipient);
|
|
228
|
+
if (parsedPr.millisatoshis == null)
|
|
229
|
+
throw new UserError_1.UserError("Must be an invoice with amount");
|
|
230
|
+
const amountOut = (BigInt(parsedPr.millisatoshis) + 999n) / 1000n;
|
|
231
|
+
const _options = this.toRequiredSwapOptions({ ...amountData, amount: amountOut }, options);
|
|
232
|
+
if (parsedPr.tagsObject.payment_hash == null)
|
|
233
|
+
throw new Error("Provided lightning invoice doesn't contain payment hash field!");
|
|
234
|
+
await this.checkPaymentHashWasPaid(parsedPr.tagsObject.payment_hash);
|
|
235
|
+
const claimHash = this._contract.getHashForHtlc(Buffer.from(parsedPr.tagsObject.payment_hash, "hex"));
|
|
236
|
+
const _abortController = (0, Utils_1.extendAbortController)(abortSignal);
|
|
237
|
+
const _preFetches = preFetches ?? {
|
|
238
|
+
pricePreFetchPromise: this.preFetchPrice(amountData, _abortController.signal),
|
|
239
|
+
feeRatePromise: this.preFetchFeeRate(signer, amountData, claimHash.toString("hex"), _abortController),
|
|
240
|
+
usdPricePrefetchPromise: this.preFetchUsdPrice(_abortController.signal),
|
|
241
|
+
signDataPrefetchPromise: this._contract.preFetchBlockDataForSignatures == null ? this.preFetchSignData(Promise.resolve(true)) : undefined
|
|
242
|
+
};
|
|
243
|
+
return lps.map(lp => {
|
|
244
|
+
return {
|
|
245
|
+
intermediary: lp,
|
|
246
|
+
quote: this.getIntermediaryQuote(signer, amountData, lp, recipient, parsedPr, _options, _preFetches, _abortController.signal, additionalParams)
|
|
247
|
+
};
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Parses and fetches lnurl pay params from the specified lnurl
|
|
252
|
+
*
|
|
253
|
+
* @param lnurl LNURL to be parsed and fetched
|
|
254
|
+
* @param abortSignal Abort signal
|
|
255
|
+
* @throws {UserError} if the LNURL is invalid or if it's not a LNURL-pay
|
|
256
|
+
*
|
|
257
|
+
* @private
|
|
258
|
+
*/
|
|
259
|
+
async getLNURLPay(lnurl, abortSignal) {
|
|
260
|
+
if (typeof (lnurl) !== "string")
|
|
261
|
+
return lnurl;
|
|
262
|
+
const res = await LNURL_1.LNURL.getLNURL(lnurl, true, this._options.getRequestTimeout, abortSignal);
|
|
263
|
+
if (res == null)
|
|
264
|
+
throw new UserError_1.UserError("Invalid LNURL");
|
|
265
|
+
if (res.tag !== "payRequest")
|
|
266
|
+
throw new UserError_1.UserError("Not a LNURL-pay");
|
|
267
|
+
return res;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Returns the quote/swap from the given LP
|
|
271
|
+
*
|
|
272
|
+
* @param signer Source chain signer address initiating the swap
|
|
273
|
+
* @param amountData Token to swap
|
|
274
|
+
* @param invoiceCreateService Service for creating fixed amount invoices
|
|
275
|
+
* @param lp Intermediary (LPs) to get the quote from
|
|
276
|
+
* @param dummyPr Dummy minimum value bolt11 lightning invoice returned from the LNURL-pay, used to estimate
|
|
277
|
+
* network fees for an actual invoice
|
|
278
|
+
* @param calculatedOptions Swap options computed from the swap create options
|
|
279
|
+
* @param preFetches Optional existing pre-fetch promises for the swap (only used internally for LNURL swaps)
|
|
280
|
+
* @param abortSignal Abort signal
|
|
281
|
+
* @param additionalParams Additional params to be sent to the intermediary
|
|
282
|
+
*
|
|
283
|
+
* @private
|
|
284
|
+
*/
|
|
285
|
+
async getIntermediaryQuoteExactIn(signer, amountData, invoiceCreateService, lp, dummyPr, calculatedOptions, preFetches, abortSignal, additionalParams) {
|
|
286
|
+
if (lp.services[SwapType_1.SwapType.TO_BTCLN] == null)
|
|
287
|
+
throw new Error("LP service for processing to btcln swaps not found!");
|
|
288
|
+
const abortController = (0, Utils_1.extendAbortController)(abortSignal);
|
|
289
|
+
const reputationPromise = this.preFetchIntermediaryReputation(amountData, lp, abortController);
|
|
290
|
+
try {
|
|
291
|
+
const { signDataPromise, prepareResp } = await (0, RetryUtils_1.tryWithRetries)(async (retryCount) => {
|
|
292
|
+
const { signDataPrefetch, response } = IntermediaryAPI_1.IntermediaryAPI.prepareToBTCLNExactIn(this.chainIdentifier, lp.url, {
|
|
293
|
+
token: amountData.token,
|
|
294
|
+
offerer: signer,
|
|
295
|
+
pr: dummyPr,
|
|
296
|
+
amount: amountData.amount,
|
|
297
|
+
maxFee: await calculatedOptions.maxFee,
|
|
298
|
+
expiryTimestamp: calculatedOptions.expiryTimestamp,
|
|
299
|
+
additionalParams
|
|
300
|
+
}, this._options.postRequestTimeout, abortController.signal, retryCount > 0 ? false : undefined);
|
|
301
|
+
return {
|
|
302
|
+
signDataPromise: this.preFetchSignData(signDataPrefetch),
|
|
303
|
+
prepareResp: await response
|
|
304
|
+
};
|
|
305
|
+
}, undefined, e => e instanceof RequestError_1.RequestError, abortController.signal);
|
|
306
|
+
if (prepareResp.amount <= 0n)
|
|
307
|
+
throw new IntermediaryError_1.IntermediaryError("Invalid amount returned (zero or negative)");
|
|
308
|
+
if (invoiceCreateService.minMsats != null) {
|
|
309
|
+
if (prepareResp.amount < invoiceCreateService.minMsats / 1000n)
|
|
310
|
+
throw new UserError_1.UserError("Amount less than minimum");
|
|
311
|
+
}
|
|
312
|
+
if (invoiceCreateService.maxMSats != null) {
|
|
313
|
+
if (prepareResp.amount > invoiceCreateService.maxMSats / 1000n)
|
|
314
|
+
throw new UserError_1.UserError("Amount more than maximum");
|
|
315
|
+
}
|
|
316
|
+
const invoice = await invoiceCreateService.getInvoice(Number(prepareResp.amount), abortController.signal);
|
|
317
|
+
const parsedInvoice = (0, bolt11_1.decode)(invoice);
|
|
318
|
+
const resp = await (0, RetryUtils_1.tryWithRetries)((retryCount) => IntermediaryAPI_1.IntermediaryAPI.initToBTCLNExactIn(lp.url, {
|
|
319
|
+
pr: invoice,
|
|
320
|
+
reqId: prepareResp.reqId,
|
|
321
|
+
feeRate: (0, Utils_1.throwIfUndefined)(preFetches.feeRatePromise),
|
|
322
|
+
additionalParams
|
|
323
|
+
}, this._options.postRequestTimeout, abortController.signal, retryCount > 0 ? false : undefined), undefined, RequestError_1.RequestError, abortController.signal);
|
|
324
|
+
if (parsedInvoice.millisatoshis == null)
|
|
325
|
+
throw new Error("Swap invoice doesn't have msat amount field!");
|
|
326
|
+
const amountOut = (BigInt(parsedInvoice.millisatoshis) + 999n) / 1000n;
|
|
327
|
+
const totalFee = resp.swapFee + resp.maxFee;
|
|
328
|
+
const data = new this._swapDataDeserializer(resp.data);
|
|
329
|
+
data.setOfferer(signer);
|
|
330
|
+
await this.verifyReturnedData(signer, resp, parsedInvoice, amountData.token, lp, calculatedOptions, data, amountData.amount);
|
|
331
|
+
const swapFeeBtc = resp.swapFee * amountOut / (data.getAmount() - totalFee);
|
|
332
|
+
const [pricingInfo, signatureExpiry, reputation] = await Promise.all([
|
|
333
|
+
this.verifyReturnedPrice(lp.services[SwapType_1.SwapType.TO_BTCLN], true, prepareResp.amount, data.getAmount(), amountData.token, { networkFee: resp.maxFee, swapFeeBtc }, preFetches.pricePreFetchPromise, preFetches.usdPricePrefetchPromise, abortSignal),
|
|
334
|
+
this.verifyReturnedSignature(signer, data, resp, preFetches.feeRatePromise, signDataPromise, abortController.signal),
|
|
335
|
+
reputationPromise
|
|
336
|
+
]);
|
|
337
|
+
abortController.signal.throwIfAborted();
|
|
338
|
+
if (reputation != null)
|
|
339
|
+
lp.reputation[amountData.token.toString()] = reputation;
|
|
340
|
+
const quote = new ToBTCLNSwap_1.ToBTCLNSwap(this, {
|
|
341
|
+
pricingInfo,
|
|
342
|
+
url: lp.url,
|
|
343
|
+
expiry: signatureExpiry,
|
|
344
|
+
swapFee: resp.swapFee,
|
|
345
|
+
swapFeeBtc,
|
|
346
|
+
feeRate: (await preFetches.feeRatePromise),
|
|
347
|
+
signatureData: resp,
|
|
348
|
+
data,
|
|
349
|
+
networkFee: resp.maxFee,
|
|
350
|
+
networkFeeBtc: resp.routingFeeSats,
|
|
351
|
+
confidence: resp.confidence,
|
|
352
|
+
pr: invoice,
|
|
353
|
+
exactIn: true
|
|
354
|
+
});
|
|
355
|
+
return quote;
|
|
356
|
+
}
|
|
357
|
+
catch (e) {
|
|
358
|
+
abortController.abort(e);
|
|
359
|
+
throw e;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Returns a newly created Smart chain -> Lightning swap using the HTLC based escrow swap protocol via
|
|
364
|
+
* invoice creation service. This allows exactIn swaps by requesting the desired fixed amount lightning
|
|
365
|
+
* network invoice from the service.
|
|
366
|
+
*
|
|
367
|
+
* @param signer Source chain signer address initiating the swap
|
|
368
|
+
* @param invoiceCreateServicePromise Service to request destination lightning network invoices from
|
|
369
|
+
* @param amountData Amount, token and exact input/output data for to swap
|
|
370
|
+
* @param lps An array of intermediaries (LPs) to get the quotes from
|
|
371
|
+
* @param options Optional additional quote options
|
|
372
|
+
* @param additionalParams Optional additional parameters sent to the LP when creating the swap
|
|
373
|
+
* @param abortSignal Abort signal
|
|
374
|
+
*/
|
|
375
|
+
async createViaInvoiceCreateService(signer, invoiceCreateServicePromise, amountData, lps, options, additionalParams, abortSignal) {
|
|
376
|
+
if (!this.isInitialized)
|
|
377
|
+
throw new Error("Not initialized, call init() first!");
|
|
378
|
+
const _abortController = (0, Utils_1.extendAbortController)(abortSignal);
|
|
379
|
+
const pricePreFetchPromise = this.preFetchPrice(amountData, _abortController.signal);
|
|
380
|
+
const usdPricePrefetchPromise = this.preFetchUsdPrice(_abortController.signal);
|
|
381
|
+
const feeRatePromise = this.preFetchFeeRate(signer, amountData, undefined, _abortController);
|
|
382
|
+
const signDataPrefetchPromise = this._contract.preFetchBlockDataForSignatures == null ?
|
|
383
|
+
this.preFetchSignData(Promise.resolve(true)) :
|
|
384
|
+
undefined;
|
|
385
|
+
const _options = this.toRequiredSwapOptions(amountData, options, pricePreFetchPromise, _abortController.signal);
|
|
386
|
+
try {
|
|
387
|
+
const invoiceCreateService = await invoiceCreateServicePromise;
|
|
388
|
+
if (amountData.exactIn) {
|
|
389
|
+
const dummyInvoice = await invoiceCreateService.getInvoice(invoiceCreateService.minMsats == null ? 1 : Number(invoiceCreateService.minMsats / 1000n), _abortController.signal);
|
|
390
|
+
return lps.map(lp => {
|
|
391
|
+
return {
|
|
392
|
+
quote: this.getIntermediaryQuoteExactIn(signer, amountData, invoiceCreateService, lp, dummyInvoice, _options, {
|
|
393
|
+
pricePreFetchPromise,
|
|
394
|
+
usdPricePrefetchPromise,
|
|
395
|
+
feeRatePromise
|
|
396
|
+
}, _abortController.signal, additionalParams),
|
|
397
|
+
intermediary: lp
|
|
398
|
+
};
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
else {
|
|
402
|
+
if (invoiceCreateService.minMsats != null) {
|
|
403
|
+
if (amountData.amount < invoiceCreateService.minMsats / 1000n)
|
|
404
|
+
throw new UserError_1.UserError("Amount less than minimum");
|
|
405
|
+
}
|
|
406
|
+
if (invoiceCreateService.maxMSats != null) {
|
|
407
|
+
if (amountData.amount > invoiceCreateService.maxMSats / 1000n)
|
|
408
|
+
throw new UserError_1.UserError("Amount more than maximum");
|
|
409
|
+
}
|
|
410
|
+
const invoice = await invoiceCreateService.getInvoice(Number(amountData.amount), _abortController.signal);
|
|
411
|
+
return (await this.create(signer, invoice, { ...amountData, exactIn: false }, lps, options, additionalParams, _abortController.signal, {
|
|
412
|
+
feeRatePromise,
|
|
413
|
+
pricePreFetchPromise,
|
|
414
|
+
usdPricePrefetchPromise,
|
|
415
|
+
signDataPrefetchPromise
|
|
416
|
+
}));
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
catch (e) {
|
|
420
|
+
_abortController.abort(e);
|
|
421
|
+
throw e;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* Returns a newly created Smart chain -> Lightning swap using the HTLC based escrow swap protocol. Pays to
|
|
426
|
+
* an LNURL-pay link. This allows exactIn swaps by requesting the desired fixed amount lightning
|
|
427
|
+
* network invoice from the LNURL service.
|
|
428
|
+
*
|
|
429
|
+
* @param signer Source chain signer address initiating the swap
|
|
430
|
+
* @param lnurl LNURL-pay link of the recipient
|
|
431
|
+
* @param amountData Amount, token and exact input/output data for to swap
|
|
432
|
+
* @param lps An array of intermediaries (LPs) to get the quotes from
|
|
433
|
+
* @param options Optional additional quote options
|
|
434
|
+
* @param additionalParams Optional additional parameters sent to the LP when creating the swap
|
|
435
|
+
* @param abortSignal Abort signal
|
|
436
|
+
*/
|
|
437
|
+
async createViaLNURL(signer, lnurl, amountData, lps, options, additionalParams, abortSignal) {
|
|
438
|
+
let successActions = {};
|
|
439
|
+
const _abortController = (0, Utils_1.extendAbortController)(abortSignal);
|
|
440
|
+
const invoiceCreateService = (async () => {
|
|
441
|
+
let payRequest = await this.getLNURLPay(lnurl, _abortController.signal);
|
|
442
|
+
if (options?.comment != null &&
|
|
443
|
+
(payRequest.commentAllowed == null || options.comment.length > payRequest.commentAllowed))
|
|
444
|
+
throw new UserError_1.UserError("Comment not allowed or too long");
|
|
445
|
+
return {
|
|
446
|
+
getInvoice: async (amountSats, abortSignal) => {
|
|
447
|
+
const { invoice, successAction } = await LNURL_1.LNURL.useLNURLPay(payRequest, BigInt(amountSats), options?.comment, this._options.getRequestTimeout, abortSignal);
|
|
448
|
+
if (successAction != null)
|
|
449
|
+
successActions[invoice] = successAction;
|
|
450
|
+
return invoice;
|
|
451
|
+
},
|
|
452
|
+
minMsats: BigInt(payRequest.minSendable),
|
|
453
|
+
maxMsats: BigInt(payRequest.maxSendable),
|
|
454
|
+
url: payRequest.url
|
|
455
|
+
};
|
|
456
|
+
})();
|
|
457
|
+
const quotes = await this.createViaInvoiceCreateService(signer, invoiceCreateService, amountData, lps, options, additionalParams, _abortController.signal);
|
|
458
|
+
_abortController.signal.throwIfAborted();
|
|
459
|
+
const resolved = await invoiceCreateService;
|
|
460
|
+
_abortController.signal.throwIfAborted();
|
|
461
|
+
return quotes.map(value => ({
|
|
462
|
+
quote: value.quote.then(quote => {
|
|
463
|
+
let _successAction;
|
|
464
|
+
const quoteAddress = quote.getOutputAddress();
|
|
465
|
+
if (quoteAddress != null) {
|
|
466
|
+
const successAction = successActions[quoteAddress];
|
|
467
|
+
if (successAction != null)
|
|
468
|
+
_successAction = successAction;
|
|
469
|
+
}
|
|
470
|
+
quote._setLNURLData(resolved.url, _successAction);
|
|
471
|
+
return quote;
|
|
472
|
+
}),
|
|
473
|
+
intermediary: value.intermediary
|
|
474
|
+
}));
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* @inheritDoc
|
|
478
|
+
*/
|
|
479
|
+
async recoverFromSwapDataAndState(init, state, lp) {
|
|
480
|
+
const data = init.data;
|
|
481
|
+
let paymentHash = data.getHTLCHashHint();
|
|
482
|
+
let secret;
|
|
483
|
+
if (state.type === base_1.SwapCommitStateType.PAID) {
|
|
484
|
+
secret = await state.getClaimResult();
|
|
485
|
+
paymentHash = Buffer.from((0, sha2_1.sha256)(Buffer.from(secret, "hex"))).toString("hex");
|
|
486
|
+
}
|
|
487
|
+
const swapInit = {
|
|
488
|
+
pricingInfo: {
|
|
489
|
+
isValid: true,
|
|
490
|
+
satsBaseFee: 0n,
|
|
491
|
+
swapPriceUSatPerToken: 100000000000000n,
|
|
492
|
+
realPriceUSatPerToken: 100000000000000n,
|
|
493
|
+
differencePPM: 0n,
|
|
494
|
+
feePPM: 0n,
|
|
495
|
+
},
|
|
496
|
+
url: lp?.url,
|
|
497
|
+
expiry: 0,
|
|
498
|
+
swapFee: 0n,
|
|
499
|
+
swapFeeBtc: 0n,
|
|
500
|
+
feeRate: "",
|
|
501
|
+
signatureData: undefined,
|
|
502
|
+
data,
|
|
503
|
+
networkFee: 0n,
|
|
504
|
+
networkFeeBtc: 0n,
|
|
505
|
+
confidence: 0,
|
|
506
|
+
pr: paymentHash ?? undefined,
|
|
507
|
+
exactIn: false
|
|
508
|
+
};
|
|
509
|
+
const swap = new ToBTCLNSwap_1.ToBTCLNSwap(this, swapInit);
|
|
510
|
+
swap._commitTxId = await init.getInitTxId();
|
|
511
|
+
const blockData = await init.getTxBlock();
|
|
512
|
+
swap.createdAt = blockData.blockTime * 1000;
|
|
513
|
+
swap._setInitiated();
|
|
514
|
+
swap._state = IToBTCSwap_1.ToBTCSwapState.COMMITED;
|
|
515
|
+
await swap._sync(false, false, state);
|
|
516
|
+
await swap._save();
|
|
517
|
+
return swap;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
exports.ToBTCLNWrapper = ToBTCLNWrapper;
|