@atomiqlabs/sdk 7.0.11 → 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} +40 -22
- 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} +116 -75
- 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,633 @@
|
|
|
1
|
+
import {SwapType} from "../../../enums/SwapType";
|
|
2
|
+
import {ChainType} from "@atomiqlabs/base";
|
|
3
|
+
import {PaymentAuthError} from "../../../errors/PaymentAuthError";
|
|
4
|
+
import {toBigInt} from "../../../utils/Utils";
|
|
5
|
+
import {parsePsbtTransaction, toOutputScript} from "../../../utils/BitcoinUtils";
|
|
6
|
+
import {isISwapInit, ISwap, ISwapInit} from "../../ISwap";
|
|
7
|
+
import {AddressStatusResponseCodes, TrustedIntermediaryAPI} from "../../../intermediaries/apis/TrustedIntermediaryAPI";
|
|
8
|
+
import {OnchainForGasSwapTypeDefinition, OnchainForGasWrapper} from "./OnchainForGasWrapper";
|
|
9
|
+
import {Fee} from "../../../types/fees/Fee";
|
|
10
|
+
import {IBitcoinWallet, isIBitcoinWallet} from "../../../bitcoin/wallet/IBitcoinWallet";
|
|
11
|
+
import {IAddressSwap} from "../../IAddressSwap";
|
|
12
|
+
import {IBTCWalletSwap} from "../../IBTCWalletSwap";
|
|
13
|
+
import {Transaction} from "@scure/btc-signer";
|
|
14
|
+
import {SingleAddressBitcoinWallet} from "../../../bitcoin/wallet/SingleAddressBitcoinWallet";
|
|
15
|
+
import {Buffer} from "buffer";
|
|
16
|
+
import {
|
|
17
|
+
MinimalBitcoinWalletInterface,
|
|
18
|
+
MinimalBitcoinWalletInterfaceWithSigner
|
|
19
|
+
} from "../../../bitcoin/wallet/MinimalBitcoinWalletInterface";
|
|
20
|
+
import {FeeType} from "../../../enums/FeeType";
|
|
21
|
+
import {ppmToPercentage} from "../../../types/fees/PercentagePPM";
|
|
22
|
+
import {TokenAmount, toTokenAmount} from "../../../types/TokenAmount";
|
|
23
|
+
import {BitcoinTokens, BtcToken, SCToken} from "../../../types/Token";
|
|
24
|
+
import {getLogger, LoggerType} from "../../../utils/Logger";
|
|
25
|
+
import {timeoutPromise} from "../../../utils/TimeoutUtils";
|
|
26
|
+
import {toBitcoinWallet} from "../../../utils/BitcoinWalletUtils";
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* State enum for trusted on-chain gas swaps
|
|
30
|
+
* @category Swaps
|
|
31
|
+
*/
|
|
32
|
+
export enum OnchainForGasSwapState {
|
|
33
|
+
EXPIRED = -3,
|
|
34
|
+
FAILED = -2,
|
|
35
|
+
REFUNDED = -1,
|
|
36
|
+
PR_CREATED = 0,
|
|
37
|
+
FINISHED = 1,
|
|
38
|
+
REFUNDABLE = 2
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type OnchainForGasSwapInit = ISwapInit & {
|
|
42
|
+
paymentHash: string;
|
|
43
|
+
sequence: bigint;
|
|
44
|
+
address: string;
|
|
45
|
+
inputAmount: bigint;
|
|
46
|
+
outputAmount: bigint;
|
|
47
|
+
recipient: string;
|
|
48
|
+
token: string;
|
|
49
|
+
refundAddress?: string;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export function isOnchainForGasSwapInit(obj: any): obj is OnchainForGasSwapInit {
|
|
53
|
+
return typeof(obj.paymentHash)==="string" &&
|
|
54
|
+
typeof(obj.sequence)==="bigint" &&
|
|
55
|
+
typeof(obj.address)==="string" &&
|
|
56
|
+
typeof(obj.inputAmount)==="bigint" &&
|
|
57
|
+
typeof(obj.outputAmount)==="bigint" &&
|
|
58
|
+
typeof(obj.recipient)==="string" &&
|
|
59
|
+
typeof(obj.token)==="string" &&
|
|
60
|
+
(obj.refundAddress==null || typeof(obj.refundAddress)==="string") &&
|
|
61
|
+
isISwapInit(obj);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Trusted on-chain BTC to gas token swap
|
|
66
|
+
* @category Swaps
|
|
67
|
+
*/
|
|
68
|
+
export class OnchainForGasSwap<T extends ChainType = ChainType> extends ISwap<T, OnchainForGasSwapTypeDefinition<T>> implements IAddressSwap, IBTCWalletSwap {
|
|
69
|
+
getSmartChainNetworkFee = null;
|
|
70
|
+
protected readonly TYPE: SwapType = SwapType.TRUSTED_FROM_BTC;
|
|
71
|
+
protected readonly logger: LoggerType;
|
|
72
|
+
|
|
73
|
+
//State: PR_CREATED
|
|
74
|
+
private readonly paymentHash: string;
|
|
75
|
+
private readonly sequence: bigint;
|
|
76
|
+
private readonly address: string;
|
|
77
|
+
private readonly recipient: string;
|
|
78
|
+
private readonly token: string;
|
|
79
|
+
private inputAmount: bigint;
|
|
80
|
+
private outputAmount: bigint;
|
|
81
|
+
private refundAddress?: string;
|
|
82
|
+
|
|
83
|
+
//State: FINISHED
|
|
84
|
+
scTxId?: string;
|
|
85
|
+
txId?: string;
|
|
86
|
+
|
|
87
|
+
//State: REFUNDED
|
|
88
|
+
refundTxId?: string;
|
|
89
|
+
|
|
90
|
+
wrapper: OnchainForGasWrapper<T>;
|
|
91
|
+
|
|
92
|
+
constructor(wrapper: OnchainForGasWrapper<T>, init: OnchainForGasSwapInit);
|
|
93
|
+
constructor(wrapper: OnchainForGasWrapper<T>, obj: any);
|
|
94
|
+
constructor(
|
|
95
|
+
wrapper: OnchainForGasWrapper<T>,
|
|
96
|
+
initOrObj: OnchainForGasSwapInit | any
|
|
97
|
+
) {
|
|
98
|
+
if(isOnchainForGasSwapInit(initOrObj) && initOrObj.url!=null) initOrObj.url += "/frombtc_trusted";
|
|
99
|
+
super(wrapper, initOrObj);
|
|
100
|
+
this.wrapper = wrapper;
|
|
101
|
+
if(isOnchainForGasSwapInit(initOrObj)) {
|
|
102
|
+
this.paymentHash = initOrObj.paymentHash;
|
|
103
|
+
this.sequence = initOrObj.sequence;
|
|
104
|
+
this.address = initOrObj.address;
|
|
105
|
+
this.inputAmount = initOrObj.inputAmount;
|
|
106
|
+
this.outputAmount = initOrObj.outputAmount;
|
|
107
|
+
this.recipient = initOrObj.recipient;
|
|
108
|
+
this.token = initOrObj.token;
|
|
109
|
+
this.refundAddress = initOrObj.refundAddress;
|
|
110
|
+
this.state = OnchainForGasSwapState.PR_CREATED;
|
|
111
|
+
} else {
|
|
112
|
+
this.paymentHash = initOrObj.paymentHash;
|
|
113
|
+
this.sequence = toBigInt(initOrObj.sequence);
|
|
114
|
+
this.address = initOrObj.address;
|
|
115
|
+
this.inputAmount = toBigInt(initOrObj.inputAmount);
|
|
116
|
+
this.outputAmount = toBigInt(initOrObj.outputAmount);
|
|
117
|
+
this.recipient = initOrObj.recipient;
|
|
118
|
+
this.token = initOrObj.token;
|
|
119
|
+
this.refundAddress = initOrObj.refundAddress;
|
|
120
|
+
this.scTxId = initOrObj.scTxId;
|
|
121
|
+
this.txId = initOrObj.txId;
|
|
122
|
+
this.refundTxId = initOrObj.refundTxId;
|
|
123
|
+
}
|
|
124
|
+
this.logger = getLogger("OnchainForGas("+this.getId()+"): ");
|
|
125
|
+
this.tryRecomputeSwapPrice();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
protected upgradeVersion() {
|
|
129
|
+
if(this.version == null) {
|
|
130
|
+
//Noop
|
|
131
|
+
this.version = 1;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* In case swapFee in BTC is not supplied it recalculates it based on swap price
|
|
137
|
+
* @protected
|
|
138
|
+
*/
|
|
139
|
+
protected tryRecomputeSwapPrice() {
|
|
140
|
+
if(this.swapFeeBtc==null && this.swapFee!=null) {
|
|
141
|
+
this.swapFeeBtc = this.swapFee * this.getInput().rawAmount / this.getOutAmountWithoutFee();
|
|
142
|
+
}
|
|
143
|
+
super.tryRecomputeSwapPrice();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
//////////////////////////////
|
|
148
|
+
//// Getters & utils
|
|
149
|
+
|
|
150
|
+
_getEscrowHash(): string {
|
|
151
|
+
return this.paymentHash;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
getOutputAddress(): string | null {
|
|
155
|
+
return this.recipient;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
getInputAddress(): string | null {
|
|
159
|
+
//TODO: Fuck this, it's not used anyway
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
getInputTxId(): string | null {
|
|
164
|
+
return this.txId ?? null;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
getOutputTxId(): string | null {
|
|
168
|
+
return this.scTxId ?? null;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
getId(): string {
|
|
172
|
+
return this.paymentHash;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
getAddress(): string {
|
|
176
|
+
return this.address;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
getHyperlink(): string {
|
|
180
|
+
return "bitcoin:"+this.address+"?amount="+encodeURIComponent((Number(this.inputAmount)/100000000).toString(10));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
requiresAction(): boolean {
|
|
184
|
+
return this.state===OnchainForGasSwapState.REFUNDABLE;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
isFinished(): boolean {
|
|
188
|
+
return this.state===OnchainForGasSwapState.FINISHED || this.state===OnchainForGasSwapState.FAILED || this.state===OnchainForGasSwapState.EXPIRED || this.state===OnchainForGasSwapState.REFUNDED;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
isQuoteExpired(): boolean {
|
|
192
|
+
return this.state===OnchainForGasSwapState.EXPIRED;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
isQuoteSoftExpired(): boolean {
|
|
196
|
+
return this.expiry<Date.now();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
isFailed(): boolean {
|
|
200
|
+
return this.state===OnchainForGasSwapState.FAILED;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
isSuccessful(): boolean {
|
|
204
|
+
return this.state===OnchainForGasSwapState.FINISHED;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
verifyQuoteValid(): Promise<boolean> {
|
|
208
|
+
return Promise.resolve(this.expiry>Date.now());
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
//////////////////////////////
|
|
213
|
+
//// Amounts & fees
|
|
214
|
+
|
|
215
|
+
protected getOutAmountWithoutFee(): bigint {
|
|
216
|
+
return this.outputAmount + (this.swapFee ?? 0n);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
getOutputToken(): SCToken<T["ChainId"]> {
|
|
220
|
+
return this.wrapper.tokens[this.wrapper.chain.getNativeCurrencyAddress()];
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
getOutput(): TokenAmount<T["ChainId"], SCToken<T["ChainId"]>> {
|
|
224
|
+
return toTokenAmount(
|
|
225
|
+
this.outputAmount, this.wrapper.tokens[this.wrapper.chain.getNativeCurrencyAddress()],
|
|
226
|
+
this.wrapper.prices, this.pricingInfo
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
getInputToken(): BtcToken<false> {
|
|
231
|
+
return BitcoinTokens.BTC;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
getInput(): TokenAmount<T["ChainId"], BtcToken<false>> {
|
|
235
|
+
return toTokenAmount(this.inputAmount, BitcoinTokens.BTC, this.wrapper.prices, this.pricingInfo);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
getInputWithoutFee(): TokenAmount<T["ChainId"], BtcToken<false>> {
|
|
239
|
+
return toTokenAmount(
|
|
240
|
+
this.inputAmount - (this.swapFeeBtc ?? 0n), BitcoinTokens.BTC,
|
|
241
|
+
this.wrapper.prices, this.pricingInfo
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
protected getSwapFee(): Fee<T["ChainId"], BtcToken<false>, SCToken<T["ChainId"]>> {
|
|
246
|
+
if(this.pricingInfo==null) throw new Error("No pricing info known!");
|
|
247
|
+
const feeWithoutBaseFee = this.swapFeeBtc==null ? 0n : this.swapFeeBtc - this.pricingInfo.satsBaseFee;
|
|
248
|
+
const swapFeePPM = feeWithoutBaseFee * 1000000n / this.getInputWithoutFee().rawAmount;
|
|
249
|
+
|
|
250
|
+
const amountInSrcToken = toTokenAmount(
|
|
251
|
+
this.swapFeeBtc ?? 0n, BitcoinTokens.BTC, this.wrapper.prices, this.pricingInfo
|
|
252
|
+
);
|
|
253
|
+
return {
|
|
254
|
+
amountInSrcToken,
|
|
255
|
+
amountInDstToken: toTokenAmount(
|
|
256
|
+
this.swapFee ?? 0n, this.wrapper.tokens[this.wrapper.chain.getNativeCurrencyAddress()],
|
|
257
|
+
this.wrapper.prices, this.pricingInfo
|
|
258
|
+
),
|
|
259
|
+
currentUsdValue: amountInSrcToken.currentUsdValue,
|
|
260
|
+
usdValue: amountInSrcToken.usdValue,
|
|
261
|
+
pastUsdValue: amountInSrcToken.pastUsdValue,
|
|
262
|
+
composition: {
|
|
263
|
+
base: toTokenAmount(this.pricingInfo.satsBaseFee, BitcoinTokens.BTC, this.wrapper.prices, this.pricingInfo),
|
|
264
|
+
percentage: ppmToPercentage(swapFeePPM)
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
getFee(): Fee<T["ChainId"], BtcToken<false>, SCToken<T["ChainId"]>> {
|
|
270
|
+
return this.getSwapFee();
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
getFeeBreakdown(): [{type: FeeType.SWAP, fee: Fee<T["ChainId"], BtcToken<false>, SCToken<T["ChainId"]>>}] {
|
|
274
|
+
return [{
|
|
275
|
+
type: FeeType.SWAP,
|
|
276
|
+
fee: this.getSwapFee()
|
|
277
|
+
}];
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
getRequiredConfirmationsCount(): number {
|
|
281
|
+
return 1;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Returns the PSBT that is already funded with wallet's UTXOs (runs a coin-selection algorithm to choose UTXOs to use),
|
|
286
|
+
* also returns inputs indices that need to be signed by the wallet before submitting the PSBT back to the SDK with
|
|
287
|
+
* `swap.submitPsbt()`
|
|
288
|
+
*
|
|
289
|
+
* @param _bitcoinWallet Sender's bitcoin wallet
|
|
290
|
+
* @param feeRate Optional fee rate for the transaction, needs to be at least as big as {minimumBtcFeeRate} field
|
|
291
|
+
* @param additionalOutputs additional outputs to add to the PSBT - can be used to collect fees from users
|
|
292
|
+
*/
|
|
293
|
+
async getFundedPsbt(
|
|
294
|
+
_bitcoinWallet: IBitcoinWallet | MinimalBitcoinWalletInterface,
|
|
295
|
+
feeRate?: number,
|
|
296
|
+
additionalOutputs?: ({amount: bigint, outputScript: Uint8Array} | {amount: bigint, address: string})[]
|
|
297
|
+
): Promise<{psbt: Transaction, psbtHex: string, psbtBase64: string, signInputs: number[]}> {
|
|
298
|
+
if(this.state!==OnchainForGasSwapState.PR_CREATED)
|
|
299
|
+
throw new Error("Swap already paid for!");
|
|
300
|
+
|
|
301
|
+
let bitcoinWallet: IBitcoinWallet;
|
|
302
|
+
if(isIBitcoinWallet(_bitcoinWallet)) {
|
|
303
|
+
bitcoinWallet = _bitcoinWallet;
|
|
304
|
+
} else {
|
|
305
|
+
bitcoinWallet = new SingleAddressBitcoinWallet(this.wrapper.btcRpc, this.wrapper.options.bitcoinNetwork, _bitcoinWallet);
|
|
306
|
+
}
|
|
307
|
+
//TODO: Maybe re-introduce fee rate check here if passed from the user
|
|
308
|
+
if(feeRate==null) {
|
|
309
|
+
feeRate = await bitcoinWallet.getFeeRate();
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const basePsbt = new Transaction({
|
|
313
|
+
allowUnknownOutputs: true,
|
|
314
|
+
allowLegacyWitnessUtxo: true
|
|
315
|
+
});
|
|
316
|
+
basePsbt.addOutput({
|
|
317
|
+
amount: this.outputAmount,
|
|
318
|
+
script: toOutputScript(this.wrapper.options.bitcoinNetwork, this.address)
|
|
319
|
+
});
|
|
320
|
+
if(additionalOutputs!=null) additionalOutputs.forEach(output => {
|
|
321
|
+
basePsbt.addOutput({
|
|
322
|
+
amount: output.amount,
|
|
323
|
+
script: (output as {outputScript: Uint8Array}).outputScript ?? toOutputScript(this.wrapper.options.bitcoinNetwork, (output as {address: string}).address)
|
|
324
|
+
});
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
const psbt = await bitcoinWallet.fundPsbt(basePsbt, feeRate);
|
|
328
|
+
//Sign every input
|
|
329
|
+
const signInputs: number[] = [];
|
|
330
|
+
for(let i=0;i<psbt.inputsLength;i++) {
|
|
331
|
+
signInputs.push(i);
|
|
332
|
+
}
|
|
333
|
+
const serializedPsbt = Buffer.from(psbt.toPSBT());
|
|
334
|
+
return {
|
|
335
|
+
psbt,
|
|
336
|
+
psbtHex: serializedPsbt.toString("hex"),
|
|
337
|
+
psbtBase64: serializedPsbt.toString("base64"),
|
|
338
|
+
signInputs
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Submits a PSBT signed by the wallet back to the SDK
|
|
344
|
+
*
|
|
345
|
+
* @param _psbt A psbt - either a Transaction object or a hex or base64 encoded PSBT string
|
|
346
|
+
*/
|
|
347
|
+
async submitPsbt(_psbt: Transaction | string): Promise<string> {
|
|
348
|
+
const psbt = parsePsbtTransaction(_psbt);
|
|
349
|
+
if(this.state!==OnchainForGasSwapState.PR_CREATED)
|
|
350
|
+
throw new Error("Swap already paid for!");
|
|
351
|
+
|
|
352
|
+
//Ensure not expired
|
|
353
|
+
if(this.expiry<Date.now()) {
|
|
354
|
+
throw new Error("Swap expired!");
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
const output0 = psbt.getOutput(0);
|
|
358
|
+
if(output0.amount!==this.outputAmount)
|
|
359
|
+
throw new Error("PSBT output amount invalid, expected: "+this.outputAmount+" got: "+output0.amount);
|
|
360
|
+
const expectedOutputScript = toOutputScript(this.wrapper.options.bitcoinNetwork, this.address);
|
|
361
|
+
if(output0.script==null || !expectedOutputScript.equals(output0.script))
|
|
362
|
+
throw new Error("PSBT output script invalid!");
|
|
363
|
+
|
|
364
|
+
if(!psbt.isFinal) psbt.finalize();
|
|
365
|
+
|
|
366
|
+
return await this.wrapper.btcRpc.sendRawTransaction(Buffer.from(psbt.toBytes(true, true)).toString("hex"));
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
async estimateBitcoinFee(_bitcoinWallet: IBitcoinWallet | MinimalBitcoinWalletInterface, feeRate?: number): Promise<TokenAmount<any, BtcToken<false>> | null> {
|
|
370
|
+
const bitcoinWallet: IBitcoinWallet = toBitcoinWallet(_bitcoinWallet, this.wrapper.btcRpc, this.wrapper.options.bitcoinNetwork);
|
|
371
|
+
const txFee = await bitcoinWallet.getTransactionFee(this.address, this.inputAmount, feeRate);
|
|
372
|
+
if(txFee==null) return null;
|
|
373
|
+
return toTokenAmount(BigInt(txFee), BitcoinTokens.BTC, this.wrapper.prices, this.pricingInfo);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
async sendBitcoinTransaction(wallet: IBitcoinWallet | MinimalBitcoinWalletInterfaceWithSigner, feeRate?: number): Promise<string> {
|
|
377
|
+
if(this.state!==OnchainForGasSwapState.PR_CREATED)
|
|
378
|
+
throw new Error("Swap already paid for!");
|
|
379
|
+
|
|
380
|
+
//Ensure not expired
|
|
381
|
+
if(this.expiry<Date.now()) {
|
|
382
|
+
throw new Error("Swap expired!");
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if(isIBitcoinWallet(wallet)) {
|
|
386
|
+
return await wallet.sendTransaction(this.address, this.inputAmount, feeRate);
|
|
387
|
+
} else {
|
|
388
|
+
const {psbt, psbtHex, psbtBase64, signInputs} = await this.getFundedPsbt(wallet, feeRate);
|
|
389
|
+
const signedPsbt = await wallet.signPsbt({
|
|
390
|
+
psbt, psbtHex, psbtBase64
|
|
391
|
+
}, signInputs);
|
|
392
|
+
return await this.submitPsbt(signedPsbt);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
async txsExecute(options?: {
|
|
397
|
+
bitcoinWallet?: MinimalBitcoinWalletInterface
|
|
398
|
+
}) {
|
|
399
|
+
if(this.state===OnchainForGasSwapState.PR_CREATED) {
|
|
400
|
+
if(!await this.verifyQuoteValid()) throw new Error("Quote already expired or close to expiry!");
|
|
401
|
+
return [
|
|
402
|
+
{
|
|
403
|
+
name: "Payment" as const,
|
|
404
|
+
description: "Send funds to the bitcoin swap address",
|
|
405
|
+
chain: "BITCOIN",
|
|
406
|
+
txs: [
|
|
407
|
+
options?.bitcoinWallet==null ? {
|
|
408
|
+
address: this.address,
|
|
409
|
+
amount: Number(this.inputAmount),
|
|
410
|
+
hyperlink: this.getHyperlink(),
|
|
411
|
+
type: "ADDRESS"
|
|
412
|
+
} : {
|
|
413
|
+
...await this.getFundedPsbt(options.bitcoinWallet),
|
|
414
|
+
type: "FUNDED_PSBT"
|
|
415
|
+
}
|
|
416
|
+
]
|
|
417
|
+
}
|
|
418
|
+
];
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
throw new Error("Invalid swap state to obtain execution txns, required PR_CREATED or CLAIM_COMMITED");
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
//////////////////////////////
|
|
426
|
+
//// Payment
|
|
427
|
+
|
|
428
|
+
protected async checkAddress(save: boolean = true): Promise<boolean | null> {
|
|
429
|
+
if(
|
|
430
|
+
this.state===OnchainForGasSwapState.FAILED ||
|
|
431
|
+
this.state===OnchainForGasSwapState.EXPIRED ||
|
|
432
|
+
this.state===OnchainForGasSwapState.REFUNDED
|
|
433
|
+
) return false;
|
|
434
|
+
if(this.state===OnchainForGasSwapState.FINISHED) return false;
|
|
435
|
+
if(this.url==null) return false;
|
|
436
|
+
|
|
437
|
+
const response = await TrustedIntermediaryAPI.getAddressStatus(
|
|
438
|
+
this.url, this.paymentHash, this.sequence, this.wrapper.options.getRequestTimeout
|
|
439
|
+
);
|
|
440
|
+
switch(response.code) {
|
|
441
|
+
case AddressStatusResponseCodes.AWAIT_PAYMENT:
|
|
442
|
+
if(this.txId!=null) {
|
|
443
|
+
this.txId = undefined;
|
|
444
|
+
if(save) await this._save();
|
|
445
|
+
return true;
|
|
446
|
+
}
|
|
447
|
+
return false;
|
|
448
|
+
case AddressStatusResponseCodes.AWAIT_CONFIRMATION:
|
|
449
|
+
case AddressStatusResponseCodes.PENDING:
|
|
450
|
+
case AddressStatusResponseCodes.TX_SENT:
|
|
451
|
+
const inputAmount = BigInt(response.data.adjustedAmount);
|
|
452
|
+
const outputAmount = BigInt(response.data.adjustedTotal);
|
|
453
|
+
const adjustedFee = response.data.adjustedFee==null ? null : BigInt(response.data.adjustedFee);
|
|
454
|
+
const adjustedFeeSats = response.data.adjustedFeeSats==null ? null : BigInt(response.data.adjustedFeeSats);
|
|
455
|
+
const txId = response.data.txId;
|
|
456
|
+
if(
|
|
457
|
+
this.txId!=txId ||
|
|
458
|
+
this.inputAmount !== inputAmount ||
|
|
459
|
+
this.outputAmount !== outputAmount
|
|
460
|
+
) {
|
|
461
|
+
this.txId = txId;
|
|
462
|
+
this.inputAmount = inputAmount;
|
|
463
|
+
this.outputAmount = outputAmount;
|
|
464
|
+
if(adjustedFee!=null) this.swapFee = adjustedFee;
|
|
465
|
+
if(adjustedFeeSats!=null) this.swapFeeBtc = adjustedFeeSats;
|
|
466
|
+
if(save) await this._save();
|
|
467
|
+
return true;
|
|
468
|
+
}
|
|
469
|
+
return false;
|
|
470
|
+
case AddressStatusResponseCodes.PAID:
|
|
471
|
+
const txStatus = await this.wrapper.chain.getTxIdStatus(response.data.txId);
|
|
472
|
+
if(txStatus==="success") {
|
|
473
|
+
this.state = OnchainForGasSwapState.FINISHED;
|
|
474
|
+
this.scTxId = response.data.txId;
|
|
475
|
+
if(save) await this._saveAndEmit();
|
|
476
|
+
return true;
|
|
477
|
+
}
|
|
478
|
+
return false;
|
|
479
|
+
case AddressStatusResponseCodes.EXPIRED:
|
|
480
|
+
this.state = OnchainForGasSwapState.EXPIRED;
|
|
481
|
+
if(save) await this._saveAndEmit();
|
|
482
|
+
return true;
|
|
483
|
+
case AddressStatusResponseCodes.REFUNDABLE:
|
|
484
|
+
if(this.state===OnchainForGasSwapState.REFUNDABLE) return null;
|
|
485
|
+
this.state = OnchainForGasSwapState.REFUNDABLE;
|
|
486
|
+
if(save) await this._saveAndEmit();
|
|
487
|
+
return true;
|
|
488
|
+
case AddressStatusResponseCodes.REFUNDED:
|
|
489
|
+
this.state = OnchainForGasSwapState.REFUNDED;
|
|
490
|
+
this.refundTxId = response.data.txId;
|
|
491
|
+
if(save) await this._saveAndEmit();
|
|
492
|
+
return true;
|
|
493
|
+
default:
|
|
494
|
+
this.state = OnchainForGasSwapState.FAILED;
|
|
495
|
+
if(save) await this._saveAndEmit();
|
|
496
|
+
return true;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
protected async setRefundAddress(refundAddress: string): Promise<void> {
|
|
501
|
+
if(this.refundAddress!=null) {
|
|
502
|
+
if(this.refundAddress!==refundAddress) throw new Error("Different refund address already set!");
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
if(this.url==null) throw new Error("LP URL not known, cannot set refund address!");
|
|
506
|
+
await TrustedIntermediaryAPI.setRefundAddress(
|
|
507
|
+
this.url, this.paymentHash, this.sequence, refundAddress, this.wrapper.options.getRequestTimeout
|
|
508
|
+
);
|
|
509
|
+
this.refundAddress = refundAddress;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* A blocking promise resolving when payment was received by the intermediary and client can continue
|
|
514
|
+
* rejecting in case of failure
|
|
515
|
+
*
|
|
516
|
+
* @param abortSignal Abort signal
|
|
517
|
+
* @param checkIntervalSeconds How often to poll the intermediary for answer
|
|
518
|
+
* @param updateCallback Callback called when txId is found, and also called with subsequent confirmations
|
|
519
|
+
* @throws {PaymentAuthError} If swap expired or failed
|
|
520
|
+
* @throws {Error} When in invalid state (not PR_CREATED)
|
|
521
|
+
*/
|
|
522
|
+
async waitForBitcoinTransaction(
|
|
523
|
+
updateCallback?: (txId?: string, confirmations?: number, targetConfirmations?: number, txEtaMs?: number) => void,
|
|
524
|
+
checkIntervalSeconds: number = 5,
|
|
525
|
+
abortSignal?: AbortSignal
|
|
526
|
+
): Promise<string> {
|
|
527
|
+
if(this.state!==OnchainForGasSwapState.PR_CREATED) throw new Error("Must be in PR_CREATED state!");
|
|
528
|
+
|
|
529
|
+
if(!this.initiated) {
|
|
530
|
+
this.initiated = true;
|
|
531
|
+
await this._saveAndEmit();
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
while(
|
|
535
|
+
!abortSignal?.aborted &&
|
|
536
|
+
this.state===OnchainForGasSwapState.PR_CREATED
|
|
537
|
+
) {
|
|
538
|
+
await this.checkAddress(true);
|
|
539
|
+
if(this.txId!=null && updateCallback!=null) {
|
|
540
|
+
const res = await this.wrapper.btcRpc.getTransaction(this.txId);
|
|
541
|
+
if(res==null) {
|
|
542
|
+
updateCallback();
|
|
543
|
+
} else if(res.confirmations!=null && res.confirmations>0) {
|
|
544
|
+
updateCallback(res.txid, res.confirmations, 1, 0);
|
|
545
|
+
} else {
|
|
546
|
+
const delay = await this.wrapper.btcRpc.getConfirmationDelay(res, 1);
|
|
547
|
+
updateCallback(res.txid, 0, 1, delay ?? undefined);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
if(this.state===OnchainForGasSwapState.PR_CREATED)
|
|
551
|
+
await timeoutPromise(checkIntervalSeconds*1000, abortSignal);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
if(
|
|
555
|
+
(this.state as OnchainForGasSwapState)===OnchainForGasSwapState.REFUNDABLE ||
|
|
556
|
+
(this.state as OnchainForGasSwapState)===OnchainForGasSwapState.REFUNDED
|
|
557
|
+
) return this.txId!;
|
|
558
|
+
if(this.isQuoteExpired()) throw new PaymentAuthError("Swap expired");
|
|
559
|
+
if(this.isFailed()) throw new PaymentAuthError("Swap failed");
|
|
560
|
+
return this.txId!;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
async waitTillRefunded(
|
|
564
|
+
checkIntervalSeconds?: number,
|
|
565
|
+
abortSignal?: AbortSignal
|
|
566
|
+
): Promise<void> {
|
|
567
|
+
checkIntervalSeconds ??= 5;
|
|
568
|
+
if(this.state===OnchainForGasSwapState.REFUNDED) return;
|
|
569
|
+
if(this.state!==OnchainForGasSwapState.REFUNDABLE) throw new Error("Must be in REFUNDABLE state!");
|
|
570
|
+
|
|
571
|
+
while(
|
|
572
|
+
!abortSignal?.aborted &&
|
|
573
|
+
this.state===OnchainForGasSwapState.REFUNDABLE
|
|
574
|
+
) {
|
|
575
|
+
await this.checkAddress(true);
|
|
576
|
+
if(this.state===OnchainForGasSwapState.REFUNDABLE)
|
|
577
|
+
await timeoutPromise(checkIntervalSeconds*1000, abortSignal);
|
|
578
|
+
}
|
|
579
|
+
if(this.isQuoteExpired()) throw new PaymentAuthError("Swap expired");
|
|
580
|
+
if(this.isFailed()) throw new PaymentAuthError("Swap failed");
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
async requestRefund(refundAddress?: string, abortSignal?: AbortSignal): Promise<void> {
|
|
584
|
+
if(refundAddress!=null) await this.setRefundAddress(refundAddress);
|
|
585
|
+
await this.waitTillRefunded(undefined, abortSignal);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
//////////////////////////////
|
|
590
|
+
//// Storage
|
|
591
|
+
|
|
592
|
+
serialize(): any{
|
|
593
|
+
return {
|
|
594
|
+
...super.serialize(),
|
|
595
|
+
paymentHash: this.paymentHash,
|
|
596
|
+
sequence: this.sequence==null ? null : this.sequence.toString(10),
|
|
597
|
+
address: this.address,
|
|
598
|
+
inputAmount: this.inputAmount==null ? null : this.inputAmount.toString(10),
|
|
599
|
+
outputAmount: this.outputAmount==null ? null : this.outputAmount.toString(10),
|
|
600
|
+
recipient: this.recipient,
|
|
601
|
+
token: this.token,
|
|
602
|
+
refundAddress: this.refundAddress,
|
|
603
|
+
scTxId: this.scTxId,
|
|
604
|
+
txId: this.txId,
|
|
605
|
+
refundTxId: this.refundTxId,
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
_getInitiator(): string {
|
|
610
|
+
return this.recipient;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
//////////////////////////////
|
|
615
|
+
//// Swap ticks & sync
|
|
616
|
+
|
|
617
|
+
async _sync(save?: boolean): Promise<boolean> {
|
|
618
|
+
if(this.state===OnchainForGasSwapState.PR_CREATED) {
|
|
619
|
+
//Check if it's maybe already paid
|
|
620
|
+
const result = await this.checkAddress(false);
|
|
621
|
+
if(result) {
|
|
622
|
+
if(save) await this._saveAndEmit();
|
|
623
|
+
return true;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
return false;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
_tick(save?: boolean): Promise<boolean> {
|
|
630
|
+
return Promise.resolve(false);
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
}
|