@atomiqlabs/sdk 7.0.12 → 8.0.7
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,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ToBTCSwap = exports.isToBTCSwapInit = void 0;
|
|
4
|
+
const IToBTCSwap_1 = require("../IToBTCSwap");
|
|
5
|
+
const SwapType_1 = require("../../../../enums/SwapType");
|
|
6
|
+
const buffer_1 = require("buffer");
|
|
7
|
+
const IntermediaryError_1 = require("../../../../errors/IntermediaryError");
|
|
8
|
+
const Utils_1 = require("../../../../utils/Utils");
|
|
9
|
+
const TokenAmount_1 = require("../../../../types/TokenAmount");
|
|
10
|
+
const Token_1 = require("../../../../types/Token");
|
|
11
|
+
const Logger_1 = require("../../../../utils/Logger");
|
|
12
|
+
function isToBTCSwapInit(obj) {
|
|
13
|
+
return typeof (obj.address) === "string" &&
|
|
14
|
+
typeof (obj.amount) === "bigint" &&
|
|
15
|
+
typeof (obj.confirmationTarget) === "number" &&
|
|
16
|
+
typeof (obj.satsPerVByte) === "number" &&
|
|
17
|
+
typeof (obj.requiredConfirmations) === "number" &&
|
|
18
|
+
typeof (obj.nonce) === "bigint" &&
|
|
19
|
+
(0, IToBTCSwap_1.isIToBTCSwapInit)(obj);
|
|
20
|
+
}
|
|
21
|
+
exports.isToBTCSwapInit = isToBTCSwapInit;
|
|
22
|
+
/**
|
|
23
|
+
* Smart Chain to on-chain BTC swap
|
|
24
|
+
* @category Swaps
|
|
25
|
+
*/
|
|
26
|
+
class ToBTCSwap extends IToBTCSwap_1.IToBTCSwap {
|
|
27
|
+
constructor(wrapper, initOrObject) {
|
|
28
|
+
if (isToBTCSwapInit(initOrObject) && initOrObject.url != null)
|
|
29
|
+
initOrObject.url += "/tobtc";
|
|
30
|
+
super(wrapper, initOrObject);
|
|
31
|
+
this.outputToken = Token_1.BitcoinTokens.BTC;
|
|
32
|
+
this.TYPE = SwapType_1.SwapType.TO_BTC;
|
|
33
|
+
if (isToBTCSwapInit(initOrObject)) {
|
|
34
|
+
this.address = initOrObject.address;
|
|
35
|
+
this.amount = initOrObject.amount;
|
|
36
|
+
this.confirmationTarget = initOrObject.confirmationTarget;
|
|
37
|
+
this.satsPerVByte = initOrObject.satsPerVByte;
|
|
38
|
+
this.requiredConfirmations = initOrObject.requiredConfirmations;
|
|
39
|
+
this.nonce = initOrObject.nonce;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
this.address = initOrObject.address;
|
|
43
|
+
this.amount = BigInt(initOrObject.amount);
|
|
44
|
+
this.confirmationTarget = initOrObject.confirmationTarget;
|
|
45
|
+
this.satsPerVByte = initOrObject.satsPerVByte;
|
|
46
|
+
this.txId = initOrObject.txId;
|
|
47
|
+
this.requiredConfirmations = initOrObject.requiredConfirmations ?? this.data.getConfirmationsHint();
|
|
48
|
+
this.nonce = (0, Utils_1.toBigInt)(initOrObject.nonce) ?? this.data.getNonceHint();
|
|
49
|
+
}
|
|
50
|
+
this.logger = (0, Logger_1.getLogger)("ToBTC(" + this.getIdentifierHashString() + "): ");
|
|
51
|
+
this.tryRecomputeSwapPrice();
|
|
52
|
+
}
|
|
53
|
+
async _setPaymentResult(result, check = false) {
|
|
54
|
+
if (result == null)
|
|
55
|
+
return false;
|
|
56
|
+
if (result.txId == null)
|
|
57
|
+
throw new IntermediaryError_1.IntermediaryError("No btc txId returned!");
|
|
58
|
+
if (check) {
|
|
59
|
+
const btcTx = await this.wrapper.btcRpc.getTransaction(result.txId);
|
|
60
|
+
if (btcTx == null)
|
|
61
|
+
return false;
|
|
62
|
+
const foundVout = btcTx.outs.find(vout => this.data.getClaimHash() === this.wrapper.contract.getHashForOnchain(buffer_1.Buffer.from(vout.scriptPubKey.hex, "hex"), BigInt(vout.value), this.requiredConfirmations, this.nonce).toString("hex"));
|
|
63
|
+
if (foundVout == null)
|
|
64
|
+
throw new IntermediaryError_1.IntermediaryError("Invalid btc txId returned");
|
|
65
|
+
}
|
|
66
|
+
this.txId = result.txId;
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
//////////////////////////////
|
|
70
|
+
//// Amounts & fees
|
|
71
|
+
getOutputToken() {
|
|
72
|
+
return Token_1.BitcoinTokens.BTC;
|
|
73
|
+
}
|
|
74
|
+
getOutput() {
|
|
75
|
+
return (0, TokenAmount_1.toTokenAmount)(this.amount, this.outputToken, this.wrapper.prices, this.pricingInfo);
|
|
76
|
+
}
|
|
77
|
+
//////////////////////////////
|
|
78
|
+
//// Getters & utils
|
|
79
|
+
/**
|
|
80
|
+
* Returns the bitcoin address where the BTC will be sent to
|
|
81
|
+
*/
|
|
82
|
+
getOutputAddress() {
|
|
83
|
+
return this.address;
|
|
84
|
+
}
|
|
85
|
+
getOutputTxId() {
|
|
86
|
+
return this.txId ?? null;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Returns fee rate of the bitcoin transaction in sats/vB
|
|
90
|
+
*/
|
|
91
|
+
getBitcoinFeeRate() {
|
|
92
|
+
return this.satsPerVByte;
|
|
93
|
+
}
|
|
94
|
+
//////////////////////////////
|
|
95
|
+
//// Storage
|
|
96
|
+
serialize() {
|
|
97
|
+
return {
|
|
98
|
+
...super.serialize(),
|
|
99
|
+
address: this.address,
|
|
100
|
+
amount: this.amount.toString(10),
|
|
101
|
+
confirmationTarget: this.confirmationTarget,
|
|
102
|
+
satsPerVByte: this.satsPerVByte,
|
|
103
|
+
nonce: this.nonce == null ? null : this.nonce.toString(10),
|
|
104
|
+
requiredConfirmations: this.requiredConfirmations,
|
|
105
|
+
txId: this.txId
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
exports.ToBTCSwap = ToBTCSwap;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { ToBTCSwap } from "./ToBTCSwap";
|
|
3
|
+
import { IToBTCDefinition, IToBTCWrapper } from "../IToBTCWrapper";
|
|
4
|
+
import { BitcoinRpc, ChainType } from "@atomiqlabs/base";
|
|
5
|
+
import { Intermediary } from "../../../../intermediaries/Intermediary";
|
|
6
|
+
import { ISwapPrice } from "../../../../prices/abstract/ISwapPrice";
|
|
7
|
+
import { EventEmitter } from "events";
|
|
8
|
+
import { ISwapWrapperOptions, WrapperCtorTokens } from "../../../ISwapWrapper";
|
|
9
|
+
import { SwapType } from "../../../../enums/SwapType";
|
|
10
|
+
import { BTC_NETWORK } from "@scure/btc-signer/utils";
|
|
11
|
+
import { UnifiedSwapEventListener } from "../../../../events/UnifiedSwapEventListener";
|
|
12
|
+
import { UnifiedSwapStorage } from "../../../../storage/UnifiedSwapStorage";
|
|
13
|
+
import { ISwap } from "../../../ISwap";
|
|
14
|
+
import { AmountData } from "../../../../types/AmountData";
|
|
15
|
+
import { AllOptional } from "../../../../utils/TypeUtils";
|
|
16
|
+
export type ToBTCOptions = {
|
|
17
|
+
confirmationTarget?: number;
|
|
18
|
+
confirmations?: number;
|
|
19
|
+
};
|
|
20
|
+
export type ToBTCWrapperOptions = ISwapWrapperOptions & {
|
|
21
|
+
safetyFactor: number;
|
|
22
|
+
maxConfirmations: number;
|
|
23
|
+
bitcoinNetwork: BTC_NETWORK;
|
|
24
|
+
bitcoinBlocktime: number;
|
|
25
|
+
maxExpectedOnchainSendSafetyFactor: number;
|
|
26
|
+
maxExpectedOnchainSendGracePeriodBlocks: number;
|
|
27
|
+
};
|
|
28
|
+
export type ToBTCDefinition<T extends ChainType> = IToBTCDefinition<T, ToBTCWrapper<T>, ToBTCSwap<T>>;
|
|
29
|
+
export declare class ToBTCWrapper<T extends ChainType> extends IToBTCWrapper<T, ToBTCDefinition<T>, ToBTCWrapperOptions> {
|
|
30
|
+
readonly TYPE = SwapType.TO_BTC;
|
|
31
|
+
readonly swapDeserializer: typeof ToBTCSwap;
|
|
32
|
+
readonly btcRpc: BitcoinRpc<any>;
|
|
33
|
+
/**
|
|
34
|
+
* @param chainIdentifier
|
|
35
|
+
* @param unifiedStorage Storage interface for the current environment
|
|
36
|
+
* @param unifiedChainEvents Smart chain on-chain event listener
|
|
37
|
+
* @param chain
|
|
38
|
+
* @param contract Chain specific swap contract
|
|
39
|
+
* @param prices Swap pricing handler
|
|
40
|
+
* @param tokens
|
|
41
|
+
* @param swapDataDeserializer Deserializer for chain specific SwapData
|
|
42
|
+
* @param btcRpc Bitcoin RPC api
|
|
43
|
+
* @param options
|
|
44
|
+
* @param events Instance to use for emitting events
|
|
45
|
+
*/
|
|
46
|
+
constructor(chainIdentifier: string, unifiedStorage: UnifiedSwapStorage<T>, unifiedChainEvents: UnifiedSwapEventListener<T>, chain: T["ChainInterface"], contract: T["Contract"], prices: ISwapPrice, tokens: WrapperCtorTokens, swapDataDeserializer: new (data: any) => T["Data"], btcRpc: BitcoinRpc<any>, options?: AllOptional<ToBTCWrapperOptions>, events?: EventEmitter<{
|
|
47
|
+
swapState: [ISwap];
|
|
48
|
+
}>);
|
|
49
|
+
/**
|
|
50
|
+
* Returns randomly generated random escrow nonce to be used for to BTC on-chain swaps
|
|
51
|
+
* @private
|
|
52
|
+
* @returns Escrow nonce
|
|
53
|
+
*/
|
|
54
|
+
private getRandomNonce;
|
|
55
|
+
/**
|
|
56
|
+
* Converts bitcoin address to its corresponding output script
|
|
57
|
+
*
|
|
58
|
+
* @param addr Bitcoin address to get the output script for
|
|
59
|
+
* @private
|
|
60
|
+
* @returns Output script as Buffer
|
|
61
|
+
* @throws {UserError} if invalid address is specified
|
|
62
|
+
*/
|
|
63
|
+
private btcAddressToOutputScript;
|
|
64
|
+
/**
|
|
65
|
+
* Verifies returned LP data
|
|
66
|
+
*
|
|
67
|
+
* @param signer
|
|
68
|
+
* @param resp LP's response
|
|
69
|
+
* @param amountData
|
|
70
|
+
* @param lp
|
|
71
|
+
* @param options Options as passed to the swap create function
|
|
72
|
+
* @param data LP's returned parsed swap data
|
|
73
|
+
* @param hash Payment hash of the swap
|
|
74
|
+
* @private
|
|
75
|
+
* @throws {IntermediaryError} if returned data are not correct
|
|
76
|
+
*/
|
|
77
|
+
private verifyReturnedData;
|
|
78
|
+
/**
|
|
79
|
+
* Returns quotes fetched from LPs, paying to an 'address' - a bitcoin address
|
|
80
|
+
*
|
|
81
|
+
* @param signer Smart-chain signer address initiating the swap
|
|
82
|
+
* @param address Bitcoin on-chain address you wish to pay to
|
|
83
|
+
* @param amountData Amount of token & amount to swap
|
|
84
|
+
* @param lps LPs (liquidity providers) to get the quotes from
|
|
85
|
+
* @param options Quote options
|
|
86
|
+
* @param additionalParams Additional parameters sent to the LP when creating the swap
|
|
87
|
+
* @param abortSignal Abort signal for aborting the process
|
|
88
|
+
*/
|
|
89
|
+
create(signer: string, address: string, amountData: AmountData, lps: Intermediary[], options?: ToBTCOptions, additionalParams?: Record<string, any>, abortSignal?: AbortSignal): {
|
|
90
|
+
quote: Promise<ToBTCSwap<T>>;
|
|
91
|
+
intermediary: Intermediary;
|
|
92
|
+
}[];
|
|
93
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ToBTCWrapper = void 0;
|
|
4
|
+
const ToBTCSwap_1 = require("./ToBTCSwap");
|
|
5
|
+
const IToBTCWrapper_1 = require("../IToBTCWrapper");
|
|
6
|
+
const base_1 = require("@atomiqlabs/base");
|
|
7
|
+
const UserError_1 = require("../../../../errors/UserError");
|
|
8
|
+
const IntermediaryError_1 = require("../../../../errors/IntermediaryError");
|
|
9
|
+
const SwapType_1 = require("../../../../enums/SwapType");
|
|
10
|
+
const Utils_1 = require("../../../../utils/Utils");
|
|
11
|
+
const BitcoinUtils_1 = require("../../../../utils/BitcoinUtils");
|
|
12
|
+
const IntermediaryAPI_1 = require("../../../../intermediaries/apis/IntermediaryAPI");
|
|
13
|
+
const RequestError_1 = require("../../../../errors/RequestError");
|
|
14
|
+
const utils_1 = require("@scure/btc-signer/utils");
|
|
15
|
+
const RetryUtils_1 = require("../../../../utils/RetryUtils");
|
|
16
|
+
class ToBTCWrapper extends IToBTCWrapper_1.IToBTCWrapper {
|
|
17
|
+
/**
|
|
18
|
+
* @param chainIdentifier
|
|
19
|
+
* @param unifiedStorage Storage interface for the current environment
|
|
20
|
+
* @param unifiedChainEvents Smart chain on-chain event listener
|
|
21
|
+
* @param chain
|
|
22
|
+
* @param contract Chain specific swap contract
|
|
23
|
+
* @param prices Swap pricing handler
|
|
24
|
+
* @param tokens
|
|
25
|
+
* @param swapDataDeserializer Deserializer for chain specific SwapData
|
|
26
|
+
* @param btcRpc Bitcoin RPC api
|
|
27
|
+
* @param options
|
|
28
|
+
* @param events Instance to use for emitting events
|
|
29
|
+
*/
|
|
30
|
+
constructor(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, contract, prices, tokens, swapDataDeserializer, btcRpc, options, events) {
|
|
31
|
+
super(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, contract, prices, tokens, swapDataDeserializer, {
|
|
32
|
+
bitcoinNetwork: options?.bitcoinNetwork ?? utils_1.TEST_NETWORK,
|
|
33
|
+
safetyFactor: options?.safetyFactor ?? 2,
|
|
34
|
+
maxConfirmations: options?.maxConfirmations ?? 6,
|
|
35
|
+
bitcoinBlocktime: options?.bitcoinBlocktime ?? (60 * 10),
|
|
36
|
+
maxExpectedOnchainSendSafetyFactor: options?.maxExpectedOnchainSendSafetyFactor ?? 4,
|
|
37
|
+
maxExpectedOnchainSendGracePeriodBlocks: options?.maxExpectedOnchainSendGracePeriodBlocks ?? 12,
|
|
38
|
+
}, events);
|
|
39
|
+
this.TYPE = SwapType_1.SwapType.TO_BTC;
|
|
40
|
+
this.swapDeserializer = ToBTCSwap_1.ToBTCSwap;
|
|
41
|
+
this.btcRpc = btcRpc;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Returns randomly generated random escrow nonce to be used for to BTC on-chain swaps
|
|
45
|
+
* @private
|
|
46
|
+
* @returns Escrow nonce
|
|
47
|
+
*/
|
|
48
|
+
getRandomNonce() {
|
|
49
|
+
const firstPart = BigInt(Math.floor((Date.now() / 1000)) - 700000000);
|
|
50
|
+
return (firstPart << 24n) | base_1.BigIntBufferUtils.fromBuffer((0, Utils_1.randomBytes)(3));
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Converts bitcoin address to its corresponding output script
|
|
54
|
+
*
|
|
55
|
+
* @param addr Bitcoin address to get the output script for
|
|
56
|
+
* @private
|
|
57
|
+
* @returns Output script as Buffer
|
|
58
|
+
* @throws {UserError} if invalid address is specified
|
|
59
|
+
*/
|
|
60
|
+
btcAddressToOutputScript(addr) {
|
|
61
|
+
try {
|
|
62
|
+
return (0, BitcoinUtils_1.toOutputScript)(this.options.bitcoinNetwork, addr);
|
|
63
|
+
}
|
|
64
|
+
catch (e) {
|
|
65
|
+
throw new UserError_1.UserError("Invalid address specified");
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Verifies returned LP data
|
|
70
|
+
*
|
|
71
|
+
* @param signer
|
|
72
|
+
* @param resp LP's response
|
|
73
|
+
* @param amountData
|
|
74
|
+
* @param lp
|
|
75
|
+
* @param options Options as passed to the swap create function
|
|
76
|
+
* @param data LP's returned parsed swap data
|
|
77
|
+
* @param hash Payment hash of the swap
|
|
78
|
+
* @private
|
|
79
|
+
* @throws {IntermediaryError} if returned data are not correct
|
|
80
|
+
*/
|
|
81
|
+
verifyReturnedData(signer, resp, amountData, lp, options, data, hash) {
|
|
82
|
+
if (resp.totalFee !== (resp.swapFee + resp.networkFee))
|
|
83
|
+
throw new IntermediaryError_1.IntermediaryError("Invalid totalFee returned");
|
|
84
|
+
if (amountData.exactIn) {
|
|
85
|
+
if (resp.total !== amountData.amount)
|
|
86
|
+
throw new IntermediaryError_1.IntermediaryError("Invalid total returned");
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
if (resp.amount !== amountData.amount)
|
|
90
|
+
throw new IntermediaryError_1.IntermediaryError("Invalid amount returned");
|
|
91
|
+
}
|
|
92
|
+
const maxAllowedBlockDelta = BigInt(options.confirmations +
|
|
93
|
+
options.confirmationTarget +
|
|
94
|
+
this.options.maxExpectedOnchainSendGracePeriodBlocks);
|
|
95
|
+
const maxAllowedExpiryDelta = maxAllowedBlockDelta
|
|
96
|
+
* BigInt(this.options.maxExpectedOnchainSendSafetyFactor)
|
|
97
|
+
* BigInt(this.options.bitcoinBlocktime);
|
|
98
|
+
const currentTimestamp = BigInt(Math.floor(Date.now() / 1000));
|
|
99
|
+
const maxAllowedExpiryTimestamp = currentTimestamp + maxAllowedExpiryDelta;
|
|
100
|
+
if (data.getExpiry() > maxAllowedExpiryTimestamp) {
|
|
101
|
+
throw new IntermediaryError_1.IntermediaryError("Expiry time returned too high!");
|
|
102
|
+
}
|
|
103
|
+
if (data.getAmount() !== resp.total ||
|
|
104
|
+
data.getClaimHash() !== hash ||
|
|
105
|
+
data.getType() !== base_1.ChainSwapType.CHAIN_NONCED ||
|
|
106
|
+
!data.isPayIn() ||
|
|
107
|
+
!data.isToken(amountData.token) ||
|
|
108
|
+
!data.isClaimer(lp.getAddress(this.chainIdentifier)) ||
|
|
109
|
+
!data.isOfferer(signer) ||
|
|
110
|
+
data.getTotalDeposit() !== 0n) {
|
|
111
|
+
throw new IntermediaryError_1.IntermediaryError("Invalid data returned");
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Returns quotes fetched from LPs, paying to an 'address' - a bitcoin address
|
|
116
|
+
*
|
|
117
|
+
* @param signer Smart-chain signer address initiating the swap
|
|
118
|
+
* @param address Bitcoin on-chain address you wish to pay to
|
|
119
|
+
* @param amountData Amount of token & amount to swap
|
|
120
|
+
* @param lps LPs (liquidity providers) to get the quotes from
|
|
121
|
+
* @param options Quote options
|
|
122
|
+
* @param additionalParams Additional parameters sent to the LP when creating the swap
|
|
123
|
+
* @param abortSignal Abort signal for aborting the process
|
|
124
|
+
*/
|
|
125
|
+
create(signer, address, amountData, lps, options, additionalParams, abortSignal) {
|
|
126
|
+
if (!this.isInitialized)
|
|
127
|
+
throw new Error("Not initialized, call init() first!");
|
|
128
|
+
const _options = {
|
|
129
|
+
confirmationTarget: options?.confirmationTarget ?? 3,
|
|
130
|
+
confirmations: options?.confirmations ?? 2
|
|
131
|
+
};
|
|
132
|
+
const nonce = this.getRandomNonce();
|
|
133
|
+
const outputScript = this.btcAddressToOutputScript(address);
|
|
134
|
+
const _hash = !amountData.exactIn ?
|
|
135
|
+
this.contract.getHashForOnchain(outputScript, amountData.amount, _options.confirmations, nonce).toString("hex") :
|
|
136
|
+
undefined;
|
|
137
|
+
const _abortController = (0, Utils_1.extendAbortController)(abortSignal);
|
|
138
|
+
const pricePreFetchPromise = this.preFetchPrice(amountData, _abortController.signal);
|
|
139
|
+
const usdPricePrefetchPromise = this.preFetchUsdPrice(_abortController.signal);
|
|
140
|
+
const feeRatePromise = this.preFetchFeeRate(signer, amountData, _hash, _abortController);
|
|
141
|
+
const _signDataPromise = this.contract.preFetchBlockDataForSignatures == null ?
|
|
142
|
+
this.preFetchSignData(Promise.resolve(true)) :
|
|
143
|
+
undefined;
|
|
144
|
+
return lps.map(lp => {
|
|
145
|
+
return {
|
|
146
|
+
intermediary: lp,
|
|
147
|
+
quote: (async () => {
|
|
148
|
+
if (lp.services[SwapType_1.SwapType.TO_BTC] == null)
|
|
149
|
+
throw new Error("LP service for processing to btc swaps not found!");
|
|
150
|
+
const abortController = (0, Utils_1.extendAbortController)(_abortController.signal);
|
|
151
|
+
const reputationPromise = this.preFetchIntermediaryReputation(amountData, lp, abortController);
|
|
152
|
+
try {
|
|
153
|
+
const { signDataPromise, resp } = await (0, RetryUtils_1.tryWithRetries)(async (retryCount) => {
|
|
154
|
+
const { signDataPrefetch, response } = IntermediaryAPI_1.IntermediaryAPI.initToBTC(this.chainIdentifier, lp.url, {
|
|
155
|
+
btcAddress: address,
|
|
156
|
+
amount: amountData.amount,
|
|
157
|
+
confirmationTarget: _options.confirmationTarget,
|
|
158
|
+
confirmations: _options.confirmations,
|
|
159
|
+
nonce: nonce,
|
|
160
|
+
token: amountData.token,
|
|
161
|
+
offerer: signer,
|
|
162
|
+
exactIn: amountData.exactIn,
|
|
163
|
+
feeRate: (0, Utils_1.throwIfUndefined)(feeRatePromise),
|
|
164
|
+
additionalParams
|
|
165
|
+
}, this.options.postRequestTimeout, abortController.signal, retryCount > 0 ? false : undefined);
|
|
166
|
+
return {
|
|
167
|
+
signDataPromise: _signDataPromise ?? this.preFetchSignData(signDataPrefetch),
|
|
168
|
+
resp: await response
|
|
169
|
+
};
|
|
170
|
+
}, undefined, RequestError_1.RequestError, abortController.signal);
|
|
171
|
+
let hash = _hash ?? this.contract.getHashForOnchain(outputScript, resp.amount, _options.confirmations, nonce).toString("hex");
|
|
172
|
+
const data = new this.swapDataDeserializer(resp.data);
|
|
173
|
+
data.setOfferer(signer);
|
|
174
|
+
this.verifyReturnedData(signer, resp, amountData, lp, _options, data, hash);
|
|
175
|
+
const [pricingInfo, signatureExpiry, reputation] = await Promise.all([
|
|
176
|
+
this.verifyReturnedPrice(lp.services[SwapType_1.SwapType.TO_BTC], true, resp.amount, data.getAmount(), amountData.token, resp, pricePreFetchPromise, usdPricePrefetchPromise, abortController.signal),
|
|
177
|
+
this.verifyReturnedSignature(signer, data, resp, feeRatePromise, signDataPromise, abortController.signal),
|
|
178
|
+
reputationPromise
|
|
179
|
+
]);
|
|
180
|
+
abortController.signal.throwIfAborted();
|
|
181
|
+
if (reputation != null)
|
|
182
|
+
lp.reputation[amountData.token.toString()] = reputation;
|
|
183
|
+
const inputWithoutFees = data.getAmount() - resp.swapFee - resp.networkFee;
|
|
184
|
+
const swapFeeBtc = resp.swapFee * resp.amount / inputWithoutFees;
|
|
185
|
+
const networkFeeBtc = resp.networkFee * resp.amount / inputWithoutFees;
|
|
186
|
+
const quote = new ToBTCSwap_1.ToBTCSwap(this, {
|
|
187
|
+
pricingInfo,
|
|
188
|
+
url: lp.url,
|
|
189
|
+
expiry: signatureExpiry,
|
|
190
|
+
swapFee: resp.swapFee,
|
|
191
|
+
swapFeeBtc,
|
|
192
|
+
feeRate: (await feeRatePromise),
|
|
193
|
+
signatureData: resp,
|
|
194
|
+
data,
|
|
195
|
+
networkFee: resp.networkFee,
|
|
196
|
+
networkFeeBtc,
|
|
197
|
+
address,
|
|
198
|
+
amount: resp.amount,
|
|
199
|
+
confirmationTarget: _options.confirmationTarget,
|
|
200
|
+
satsPerVByte: Number(resp.satsPervByte),
|
|
201
|
+
exactIn: amountData.exactIn,
|
|
202
|
+
requiredConfirmations: _options.confirmations,
|
|
203
|
+
nonce
|
|
204
|
+
});
|
|
205
|
+
await quote._save();
|
|
206
|
+
return quote;
|
|
207
|
+
}
|
|
208
|
+
catch (e) {
|
|
209
|
+
abortController.abort(e);
|
|
210
|
+
throw e;
|
|
211
|
+
}
|
|
212
|
+
})()
|
|
213
|
+
};
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
exports.ToBTCWrapper = ToBTCWrapper;
|