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