@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,721 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ToBTCSwapState = exports.IToBTCSwap = exports.isIToBTCSwapInit = void 0;
|
|
4
|
+
const base_1 = require("@atomiqlabs/base");
|
|
5
|
+
const IntermediaryAPI_1 = require("../../../intermediaries/apis/IntermediaryAPI");
|
|
6
|
+
const IntermediaryError_1 = require("../../../errors/IntermediaryError");
|
|
7
|
+
const Utils_1 = require("../../../utils/Utils");
|
|
8
|
+
const IEscrowSelfInitSwap_1 = require("../IEscrowSelfInitSwap");
|
|
9
|
+
const FeeType_1 = require("../../../enums/FeeType");
|
|
10
|
+
const PercentagePPM_1 = require("../../../types/fees/PercentagePPM");
|
|
11
|
+
const TokenAmount_1 = require("../../../types/TokenAmount");
|
|
12
|
+
const TimeoutUtils_1 = require("../../../utils/TimeoutUtils");
|
|
13
|
+
function isIToBTCSwapInit(obj) {
|
|
14
|
+
return typeof (obj.networkFee) === "bigint" &&
|
|
15
|
+
typeof (obj.networkFeeBtc) === "bigint" &&
|
|
16
|
+
(obj.signatureData == null || (typeof (obj.signatureData) === 'object' &&
|
|
17
|
+
typeof (obj.signatureData.prefix) === "string" &&
|
|
18
|
+
typeof (obj.signatureData.timeout) === "string" &&
|
|
19
|
+
typeof (obj.signatureData.signature) === "string")) &&
|
|
20
|
+
typeof (obj.data) === 'object' &&
|
|
21
|
+
(0, IEscrowSelfInitSwap_1.isIEscrowSelfInitSwapInit)(obj);
|
|
22
|
+
}
|
|
23
|
+
exports.isIToBTCSwapInit = isIToBTCSwapInit;
|
|
24
|
+
class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
|
|
25
|
+
constructor(wrapper, initOrObject) {
|
|
26
|
+
super(wrapper, initOrObject);
|
|
27
|
+
if (isIToBTCSwapInit(initOrObject)) {
|
|
28
|
+
this.state = ToBTCSwapState.CREATED;
|
|
29
|
+
this.networkFee = initOrObject.networkFee;
|
|
30
|
+
this.networkFeeBtc = initOrObject.networkFeeBtc;
|
|
31
|
+
this.data = initOrObject.data;
|
|
32
|
+
this.signatureData = initOrObject.signatureData;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
this.networkFee = (0, Utils_1.toBigInt)(initOrObject.networkFee);
|
|
36
|
+
this.networkFeeBtc = (0, Utils_1.toBigInt)(initOrObject.networkFeeBtc);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
getSwapData() {
|
|
40
|
+
return this.data;
|
|
41
|
+
}
|
|
42
|
+
upgradeVersion() {
|
|
43
|
+
if (this.version == null) {
|
|
44
|
+
switch (this.state) {
|
|
45
|
+
case -2:
|
|
46
|
+
this.state = ToBTCSwapState.REFUNDED;
|
|
47
|
+
break;
|
|
48
|
+
case -1:
|
|
49
|
+
this.state = ToBTCSwapState.QUOTE_EXPIRED;
|
|
50
|
+
break;
|
|
51
|
+
case 0:
|
|
52
|
+
this.state = ToBTCSwapState.CREATED;
|
|
53
|
+
break;
|
|
54
|
+
case 1:
|
|
55
|
+
this.state = ToBTCSwapState.COMMITED;
|
|
56
|
+
break;
|
|
57
|
+
case 2:
|
|
58
|
+
this.state = ToBTCSwapState.CLAIMED;
|
|
59
|
+
break;
|
|
60
|
+
case 3:
|
|
61
|
+
this.state = ToBTCSwapState.REFUNDABLE;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
this.version = 1;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* In case swapFee in BTC is not supplied it recalculates it based on swap price
|
|
69
|
+
* @protected
|
|
70
|
+
*/
|
|
71
|
+
tryRecomputeSwapPrice() {
|
|
72
|
+
const output = this.getOutput();
|
|
73
|
+
if (output != null) {
|
|
74
|
+
if (this.swapFeeBtc == null) {
|
|
75
|
+
this.swapFeeBtc = this.swapFee * output.rawAmount / this.getInputWithoutFee().rawAmount;
|
|
76
|
+
}
|
|
77
|
+
if (this.networkFeeBtc == null) {
|
|
78
|
+
this.networkFeeBtc = this.networkFee * output.rawAmount / this.getInputWithoutFee().rawAmount;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
super.tryRecomputeSwapPrice();
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Returns the payment hash identifier to be sent to the LP for getStatus and getRefund
|
|
85
|
+
* @protected
|
|
86
|
+
*/
|
|
87
|
+
getLpIdentifier() {
|
|
88
|
+
return this.getClaimHash();
|
|
89
|
+
}
|
|
90
|
+
//////////////////////////////
|
|
91
|
+
//// Getters & utils
|
|
92
|
+
getInputAddress() {
|
|
93
|
+
return this._getInitiator();
|
|
94
|
+
}
|
|
95
|
+
getInputTxId() {
|
|
96
|
+
return this.commitTxId ?? null;
|
|
97
|
+
}
|
|
98
|
+
requiresAction() {
|
|
99
|
+
return this.isRefundable();
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Returns whether the swap is finished and in its terminal state (this can mean successful, refunded or failed)
|
|
103
|
+
*/
|
|
104
|
+
isFinished() {
|
|
105
|
+
return this.state === ToBTCSwapState.CLAIMED || this.state === ToBTCSwapState.REFUNDED || this.state === ToBTCSwapState.QUOTE_EXPIRED;
|
|
106
|
+
}
|
|
107
|
+
isRefundable() {
|
|
108
|
+
return this.state === ToBTCSwapState.REFUNDABLE;
|
|
109
|
+
}
|
|
110
|
+
isQuoteExpired() {
|
|
111
|
+
return this.state === ToBTCSwapState.QUOTE_EXPIRED;
|
|
112
|
+
}
|
|
113
|
+
isQuoteSoftExpired() {
|
|
114
|
+
return this.state === ToBTCSwapState.QUOTE_EXPIRED || this.state === ToBTCSwapState.QUOTE_SOFT_EXPIRED;
|
|
115
|
+
}
|
|
116
|
+
isSuccessful() {
|
|
117
|
+
return this.state === ToBTCSwapState.CLAIMED;
|
|
118
|
+
}
|
|
119
|
+
isFailed() {
|
|
120
|
+
return this.state === ToBTCSwapState.REFUNDED;
|
|
121
|
+
}
|
|
122
|
+
_getInitiator() {
|
|
123
|
+
return this.data.getOfferer();
|
|
124
|
+
}
|
|
125
|
+
//////////////////////////////
|
|
126
|
+
//// Amounts & fees
|
|
127
|
+
getSwapFee() {
|
|
128
|
+
if (this.pricingInfo == null)
|
|
129
|
+
throw new Error("No pricing info known, cannot estimate fee!");
|
|
130
|
+
const feeWithoutBaseFee = this.swapFeeBtc - this.pricingInfo.satsBaseFee;
|
|
131
|
+
const output = this.getOutput();
|
|
132
|
+
const swapFeePPM = output?.rawAmount == null ? 0n : feeWithoutBaseFee * 1000000n / output.rawAmount;
|
|
133
|
+
const amountInDstToken = (0, TokenAmount_1.toTokenAmount)(this.swapFeeBtc, this.outputToken, this.wrapper.prices, this.pricingInfo);
|
|
134
|
+
return {
|
|
135
|
+
amountInSrcToken: (0, TokenAmount_1.toTokenAmount)(this.swapFee, this.wrapper.tokens[this.data.getToken()], this.wrapper.prices, this.pricingInfo),
|
|
136
|
+
amountInDstToken,
|
|
137
|
+
currentUsdValue: amountInDstToken.currentUsdValue,
|
|
138
|
+
usdValue: amountInDstToken.usdValue,
|
|
139
|
+
pastUsdValue: amountInDstToken.pastUsdValue,
|
|
140
|
+
composition: {
|
|
141
|
+
base: (0, TokenAmount_1.toTokenAmount)(this.pricingInfo.satsBaseFee, this.outputToken, this.wrapper.prices, this.pricingInfo),
|
|
142
|
+
percentage: (0, PercentagePPM_1.ppmToPercentage)(swapFeePPM)
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Returns network fee for the swap, the fee is represented in source currency & destination currency, but is
|
|
148
|
+
* paid only once
|
|
149
|
+
*/
|
|
150
|
+
getNetworkFee() {
|
|
151
|
+
const amountInDstToken = (0, TokenAmount_1.toTokenAmount)(this.networkFeeBtc, this.outputToken, this.wrapper.prices, this.pricingInfo);
|
|
152
|
+
return {
|
|
153
|
+
amountInSrcToken: (0, TokenAmount_1.toTokenAmount)(this.networkFee, this.wrapper.tokens[this.data.getToken()], this.wrapper.prices, this.pricingInfo),
|
|
154
|
+
amountInDstToken,
|
|
155
|
+
currentUsdValue: amountInDstToken.currentUsdValue,
|
|
156
|
+
usdValue: amountInDstToken.usdValue,
|
|
157
|
+
pastUsdValue: amountInDstToken.pastUsdValue
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
getFee() {
|
|
161
|
+
const amountInDstToken = (0, TokenAmount_1.toTokenAmount)(this.swapFeeBtc + this.networkFeeBtc, this.outputToken, this.wrapper.prices, this.pricingInfo);
|
|
162
|
+
return {
|
|
163
|
+
amountInSrcToken: (0, TokenAmount_1.toTokenAmount)(this.swapFee + this.networkFee, this.wrapper.tokens[this.data.getToken()], this.wrapper.prices, this.pricingInfo),
|
|
164
|
+
amountInDstToken,
|
|
165
|
+
currentUsdValue: amountInDstToken.currentUsdValue,
|
|
166
|
+
usdValue: amountInDstToken.usdValue,
|
|
167
|
+
pastUsdValue: amountInDstToken.pastUsdValue
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
getFeeBreakdown() {
|
|
171
|
+
return [
|
|
172
|
+
{
|
|
173
|
+
type: FeeType_1.FeeType.SWAP,
|
|
174
|
+
fee: this.getSwapFee()
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
type: FeeType_1.FeeType.NETWORK_OUTPUT,
|
|
178
|
+
fee: this.getNetworkFee()
|
|
179
|
+
}
|
|
180
|
+
];
|
|
181
|
+
}
|
|
182
|
+
getInputToken() {
|
|
183
|
+
return this.wrapper.tokens[this.data.getToken()];
|
|
184
|
+
}
|
|
185
|
+
getInput() {
|
|
186
|
+
return (0, TokenAmount_1.toTokenAmount)(this.data.getAmount(), this.wrapper.tokens[this.data.getToken()], this.wrapper.prices, this.pricingInfo);
|
|
187
|
+
}
|
|
188
|
+
getInputWithoutFee() {
|
|
189
|
+
return (0, TokenAmount_1.toTokenAmount)(this.data.getAmount() - (this.swapFee + this.networkFee), this.wrapper.tokens[this.data.getToken()], this.wrapper.prices, this.pricingInfo);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Checks if the intiator/sender has enough balance to go through with the swap
|
|
193
|
+
*/
|
|
194
|
+
async hasEnoughBalance() {
|
|
195
|
+
const [balance, commitFee] = await Promise.all([
|
|
196
|
+
this.wrapper.contract.getBalance(this._getInitiator(), this.data.getToken(), false),
|
|
197
|
+
this.data.getToken() === this.wrapper.chain.getNativeCurrencyAddress() ? this.getCommitFee() : Promise.resolve(null)
|
|
198
|
+
]);
|
|
199
|
+
let required = this.data.getAmount();
|
|
200
|
+
if (commitFee != null)
|
|
201
|
+
required = required + commitFee;
|
|
202
|
+
return {
|
|
203
|
+
enoughBalance: balance >= required,
|
|
204
|
+
balance: (0, TokenAmount_1.toTokenAmount)(balance, this.wrapper.tokens[this.data.getToken()], this.wrapper.prices, this.pricingInfo),
|
|
205
|
+
required: (0, TokenAmount_1.toTokenAmount)(required, this.wrapper.tokens[this.data.getToken()], this.wrapper.prices, this.pricingInfo)
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Check if the initiator/sender has enough balance to cover the transaction fee for processing the swap
|
|
210
|
+
*/
|
|
211
|
+
async hasEnoughForTxFees() {
|
|
212
|
+
const [balance, commitFee] = await Promise.all([
|
|
213
|
+
this.wrapper.contract.getBalance(this._getInitiator(), this.wrapper.chain.getNativeCurrencyAddress(), false),
|
|
214
|
+
this.getCommitFee()
|
|
215
|
+
]);
|
|
216
|
+
return {
|
|
217
|
+
enoughBalance: balance >= commitFee,
|
|
218
|
+
balance: (0, TokenAmount_1.toTokenAmount)(balance, this.wrapper.getNativeToken(), this.wrapper.prices),
|
|
219
|
+
required: (0, TokenAmount_1.toTokenAmount)(commitFee, this.wrapper.getNativeToken(), this.wrapper.prices)
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
//////////////////////////////
|
|
223
|
+
//// Execution
|
|
224
|
+
/**
|
|
225
|
+
* Executes the swap with the provided smart chain wallet/signer
|
|
226
|
+
*
|
|
227
|
+
* @param signer Smart chain wallet/signer to use to sign the transaction on the source chain
|
|
228
|
+
* @param callbacks Callbacks to track the progress of the swap
|
|
229
|
+
* @param options Optional options for the swap like feeRate, AbortSignal, and timeouts/intervals
|
|
230
|
+
*
|
|
231
|
+
* @returns {boolean} Whether the swap was successfully processed by the LP, in case `false` is returned
|
|
232
|
+
* the user can refund their funds back on the source chain by calling `swap.refund()`
|
|
233
|
+
*/
|
|
234
|
+
async execute(signer, callbacks, options) {
|
|
235
|
+
if (this.state === ToBTCSwapState.QUOTE_EXPIRED || this.state === ToBTCSwapState.QUOTE_SOFT_EXPIRED)
|
|
236
|
+
throw new Error("Quote expired");
|
|
237
|
+
if (this.state === ToBTCSwapState.REFUNDED)
|
|
238
|
+
throw new Error("Swap already refunded");
|
|
239
|
+
if (this.state === ToBTCSwapState.REFUNDABLE)
|
|
240
|
+
throw new Error("Swap refundable, refund with swap.refund()");
|
|
241
|
+
if (this.state === ToBTCSwapState.SOFT_CLAIMED || this.state === ToBTCSwapState.CLAIMED)
|
|
242
|
+
throw new Error("Swap already settled!");
|
|
243
|
+
if (this.state === ToBTCSwapState.CREATED) {
|
|
244
|
+
const txId = await this.commit(signer, options?.abortSignal, false, callbacks?.onSourceTransactionSent);
|
|
245
|
+
if (callbacks?.onSourceTransactionConfirmed != null)
|
|
246
|
+
callbacks.onSourceTransactionConfirmed(txId);
|
|
247
|
+
}
|
|
248
|
+
// @ts-ignore
|
|
249
|
+
if (this.state === ToBTCSwapState.CLAIMED || this.state === ToBTCSwapState.SOFT_CLAIMED)
|
|
250
|
+
return true;
|
|
251
|
+
if (this.state === ToBTCSwapState.COMMITED) {
|
|
252
|
+
const success = await this.waitForPayment(options?.maxWaitTillSwapProcessedSeconds ?? 120, options?.paymentCheckIntervalSeconds, options?.abortSignal);
|
|
253
|
+
if (success) {
|
|
254
|
+
if (callbacks?.onSwapSettled != null)
|
|
255
|
+
callbacks.onSwapSettled(this.getOutputTxId());
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
return false;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
throw new Error("Unexpected state reached!");
|
|
263
|
+
}
|
|
264
|
+
async txsExecute(options) {
|
|
265
|
+
if (this.state !== ToBTCSwapState.CREATED)
|
|
266
|
+
throw new Error("Invalid swap state, needs to be CREATED!");
|
|
267
|
+
const txsCommit = await this.txsCommit(options?.skipChecks);
|
|
268
|
+
return [
|
|
269
|
+
{
|
|
270
|
+
name: "Commit",
|
|
271
|
+
description: `Initiates the swap by commiting the funds to the escrow on the ${this.chainIdentifier} side`,
|
|
272
|
+
chain: this.chainIdentifier,
|
|
273
|
+
txs: txsCommit
|
|
274
|
+
}
|
|
275
|
+
];
|
|
276
|
+
}
|
|
277
|
+
//////////////////////////////
|
|
278
|
+
//// Commit
|
|
279
|
+
/**
|
|
280
|
+
* Returns transactions for committing the swap on-chain, initiating the swap
|
|
281
|
+
*
|
|
282
|
+
* @param skipChecks Skip checks like making sure init signature is still valid and swap wasn't commited yet
|
|
283
|
+
* (this is handled on swap creation, if you commit right after quoting, you can use skipChecks=true)
|
|
284
|
+
*
|
|
285
|
+
* @throws {Error} When in invalid state (not PR_CREATED)
|
|
286
|
+
*/
|
|
287
|
+
async txsCommit(skipChecks) {
|
|
288
|
+
if (this.state !== ToBTCSwapState.CREATED)
|
|
289
|
+
throw new Error("Must be in CREATED state!");
|
|
290
|
+
if (this.signatureData == null)
|
|
291
|
+
throw new Error("Init signature data not known, cannot commit!");
|
|
292
|
+
if (!this.initiated) {
|
|
293
|
+
this.initiated = true;
|
|
294
|
+
await this._saveAndEmit();
|
|
295
|
+
}
|
|
296
|
+
return await this.wrapper.contract.txsInit(this._getInitiator(), this.data, this.signatureData, skipChecks, this.feeRate).catch(e => Promise.reject(e instanceof base_1.SignatureVerificationError ? new Error("Request timed out") : e));
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Commits the swap on-chain, initiating the swap
|
|
300
|
+
*
|
|
301
|
+
* @param _signer Signer to sign the transactions with, must be the same as used in the initialization
|
|
302
|
+
* @param abortSignal Abort signal
|
|
303
|
+
* @param skipChecks Skip checks like making sure init signature is still valid and swap wasn't commited yet
|
|
304
|
+
* (this is handled on swap creation, if you commit right after quoting, you can skipChecks)`
|
|
305
|
+
* @param onBeforeTxSent
|
|
306
|
+
* @throws {Error} If invalid signer is provided that doesn't match the swap data
|
|
307
|
+
*/
|
|
308
|
+
async commit(_signer, abortSignal, skipChecks, onBeforeTxSent) {
|
|
309
|
+
const signer = (0, base_1.isAbstractSigner)(_signer) ? _signer : await this.wrapper.chain.wrapSigner(_signer);
|
|
310
|
+
this.checkSigner(signer);
|
|
311
|
+
const txs = await this.txsCommit(skipChecks);
|
|
312
|
+
let txCount = 0;
|
|
313
|
+
const result = await this.wrapper.chain.sendAndConfirm(signer, txs, true, abortSignal, false, (txId, rawTx) => {
|
|
314
|
+
txCount++;
|
|
315
|
+
if (onBeforeTxSent != null && txCount === txs.length)
|
|
316
|
+
onBeforeTxSent(txId);
|
|
317
|
+
return Promise.resolve();
|
|
318
|
+
});
|
|
319
|
+
this.commitTxId = result[result.length - 1];
|
|
320
|
+
if (this.state === ToBTCSwapState.CREATED || this.state === ToBTCSwapState.QUOTE_SOFT_EXPIRED || this.state === ToBTCSwapState.QUOTE_EXPIRED) {
|
|
321
|
+
await this._saveAndEmit(ToBTCSwapState.COMMITED);
|
|
322
|
+
}
|
|
323
|
+
return this.commitTxId;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Waits till a swap is committed, should be called after sending the commit transactions manually
|
|
327
|
+
*
|
|
328
|
+
* @param abortSignal AbortSignal
|
|
329
|
+
* @throws {Error} If swap is not in the correct state (must be CREATED)
|
|
330
|
+
*/
|
|
331
|
+
async waitTillCommited(abortSignal) {
|
|
332
|
+
if (this.state === ToBTCSwapState.COMMITED || this.state === ToBTCSwapState.CLAIMED)
|
|
333
|
+
return Promise.resolve();
|
|
334
|
+
if (this.state !== ToBTCSwapState.CREATED && this.state !== ToBTCSwapState.QUOTE_SOFT_EXPIRED)
|
|
335
|
+
throw new Error("Invalid state (not CREATED)");
|
|
336
|
+
const abortController = (0, Utils_1.extendAbortController)(abortSignal);
|
|
337
|
+
let result;
|
|
338
|
+
try {
|
|
339
|
+
result = await Promise.race([
|
|
340
|
+
this.watchdogWaitTillCommited(undefined, abortController.signal),
|
|
341
|
+
this.waitTillState(ToBTCSwapState.COMMITED, "gte", abortController.signal).then(() => 0)
|
|
342
|
+
]);
|
|
343
|
+
abortController.abort();
|
|
344
|
+
}
|
|
345
|
+
catch (e) {
|
|
346
|
+
abortController.abort();
|
|
347
|
+
throw e;
|
|
348
|
+
}
|
|
349
|
+
if (result === 0)
|
|
350
|
+
this.logger.debug("waitTillCommited(): Resolved from state change");
|
|
351
|
+
if (result === true)
|
|
352
|
+
this.logger.debug("waitTillCommited(): Resolved from watchdog - commited");
|
|
353
|
+
if (result === false) {
|
|
354
|
+
this.logger.debug("waitTillCommited(): Resolved from watchdog - signature expiry");
|
|
355
|
+
if (this.state === ToBTCSwapState.QUOTE_SOFT_EXPIRED || this.state === ToBTCSwapState.CREATED) {
|
|
356
|
+
await this._saveAndEmit(ToBTCSwapState.QUOTE_EXPIRED);
|
|
357
|
+
}
|
|
358
|
+
throw new Error("Quote expired while waiting for transaction confirmation!");
|
|
359
|
+
}
|
|
360
|
+
if (this.state === ToBTCSwapState.QUOTE_SOFT_EXPIRED || this.state === ToBTCSwapState.CREATED || this.state === ToBTCSwapState.QUOTE_EXPIRED) {
|
|
361
|
+
await this._saveAndEmit(ToBTCSwapState.COMMITED);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
//////////////////////////////
|
|
365
|
+
//// Payment
|
|
366
|
+
async waitTillIntermediarySwapProcessed(checkIntervalSeconds, abortSignal) {
|
|
367
|
+
if (this.url == null)
|
|
368
|
+
throw new Error("LP URL not specified!");
|
|
369
|
+
checkIntervalSeconds ??= 5;
|
|
370
|
+
let resp = { code: IntermediaryAPI_1.RefundAuthorizationResponseCodes.PENDING, msg: "" };
|
|
371
|
+
while (!abortSignal?.aborted && (resp.code === IntermediaryAPI_1.RefundAuthorizationResponseCodes.PENDING || resp.code === IntermediaryAPI_1.RefundAuthorizationResponseCodes.NOT_FOUND)) {
|
|
372
|
+
resp = await IntermediaryAPI_1.IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this.data.getSequence());
|
|
373
|
+
if (resp.code === IntermediaryAPI_1.RefundAuthorizationResponseCodes.PAID) {
|
|
374
|
+
const validResponse = await this._setPaymentResult(resp.data, true);
|
|
375
|
+
if (validResponse) {
|
|
376
|
+
if (this.state === ToBTCSwapState.COMMITED || this.state === ToBTCSwapState.REFUNDABLE) {
|
|
377
|
+
await this._saveAndEmit(ToBTCSwapState.SOFT_CLAIMED);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
resp = { code: IntermediaryAPI_1.RefundAuthorizationResponseCodes.PENDING, msg: "" };
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
if (resp.code === IntermediaryAPI_1.RefundAuthorizationResponseCodes.PENDING ||
|
|
385
|
+
resp.code === IntermediaryAPI_1.RefundAuthorizationResponseCodes.NOT_FOUND)
|
|
386
|
+
await (0, TimeoutUtils_1.timeoutPromise)(checkIntervalSeconds * 1000, abortSignal);
|
|
387
|
+
}
|
|
388
|
+
return resp;
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Checks whether the swap was already processed by the LP and is either successful (requires proof which is
|
|
392
|
+
* either a HTLC pre-image for LN swaps or valid txId for on-chain swap) or failed and we can cooperatively
|
|
393
|
+
* refund.
|
|
394
|
+
*
|
|
395
|
+
* @param save whether to save the data
|
|
396
|
+
* @returns true if swap is processed, false if the swap is still ongoing
|
|
397
|
+
* @private
|
|
398
|
+
*/
|
|
399
|
+
async checkIntermediarySwapProcessed(save = true) {
|
|
400
|
+
if (this.state === ToBTCSwapState.CREATED || this.state == ToBTCSwapState.QUOTE_EXPIRED || this.url == null)
|
|
401
|
+
return false;
|
|
402
|
+
if (this.isFinished() || this.isRefundable())
|
|
403
|
+
return true;
|
|
404
|
+
//Check if that maybe already concluded according to the LP
|
|
405
|
+
const resp = await IntermediaryAPI_1.IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this.data.getSequence());
|
|
406
|
+
switch (resp.code) {
|
|
407
|
+
case IntermediaryAPI_1.RefundAuthorizationResponseCodes.PAID:
|
|
408
|
+
const processed = await this._setPaymentResult(resp.data, true);
|
|
409
|
+
if (processed) {
|
|
410
|
+
this.state = ToBTCSwapState.SOFT_CLAIMED;
|
|
411
|
+
if (save)
|
|
412
|
+
await this._saveAndEmit();
|
|
413
|
+
}
|
|
414
|
+
return processed;
|
|
415
|
+
case IntermediaryAPI_1.RefundAuthorizationResponseCodes.REFUND_DATA:
|
|
416
|
+
await this.wrapper.contract.isValidRefundAuthorization(this.data, resp.data);
|
|
417
|
+
this.state = ToBTCSwapState.REFUNDABLE;
|
|
418
|
+
if (save)
|
|
419
|
+
await this._saveAndEmit();
|
|
420
|
+
return true;
|
|
421
|
+
default:
|
|
422
|
+
return false;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* A blocking promise resolving when swap was concluded by the intermediary,
|
|
427
|
+
* rejecting in case of failure
|
|
428
|
+
*
|
|
429
|
+
* @param maxWaitTimeSeconds Maximum time in seconds to wait for the swap to be settled, an error is thrown if the
|
|
430
|
+
* swap is taking too long to claim
|
|
431
|
+
* @param checkIntervalSeconds How often to poll the intermediary for answer
|
|
432
|
+
* @param abortSignal Abort signal
|
|
433
|
+
* @returns {Promise<boolean>} Was the payment successful? If not we can refund.
|
|
434
|
+
* @throws {IntermediaryError} If a swap is determined expired by the intermediary, but it is actually still valid
|
|
435
|
+
* @throws {SignatureVerificationError} If the swap should be cooperatively refundable but the intermediary returned
|
|
436
|
+
* invalid refund signature
|
|
437
|
+
* @throws {Error} When swap expires or if the swap has invalid state (must be COMMITED)
|
|
438
|
+
*/
|
|
439
|
+
async waitForPayment(maxWaitTimeSeconds, checkIntervalSeconds, abortSignal) {
|
|
440
|
+
if (this.state === ToBTCSwapState.CLAIMED)
|
|
441
|
+
return Promise.resolve(true);
|
|
442
|
+
if (this.state !== ToBTCSwapState.COMMITED && this.state !== ToBTCSwapState.SOFT_CLAIMED)
|
|
443
|
+
throw new Error("Invalid state (not COMMITED)");
|
|
444
|
+
const abortController = (0, Utils_1.extendAbortController)(abortSignal);
|
|
445
|
+
let timedOut = false;
|
|
446
|
+
if (maxWaitTimeSeconds != null) {
|
|
447
|
+
const timeout = setTimeout(() => {
|
|
448
|
+
timedOut = true;
|
|
449
|
+
abortController.abort();
|
|
450
|
+
}, maxWaitTimeSeconds * 1000);
|
|
451
|
+
abortController.signal.addEventListener("abort", () => clearTimeout(timeout));
|
|
452
|
+
}
|
|
453
|
+
let result;
|
|
454
|
+
try {
|
|
455
|
+
result = await Promise.race([
|
|
456
|
+
this.waitTillState(ToBTCSwapState.CLAIMED, "gte", abortController.signal),
|
|
457
|
+
this.waitTillIntermediarySwapProcessed(checkIntervalSeconds, abortController.signal)
|
|
458
|
+
]);
|
|
459
|
+
abortController.abort();
|
|
460
|
+
}
|
|
461
|
+
catch (e) {
|
|
462
|
+
abortController.abort();
|
|
463
|
+
if (timedOut) {
|
|
464
|
+
throw new Error("Timed out while waiting for LP to process the swap, the LP might be unresponsive or offline!" +
|
|
465
|
+
` Please check later or wait till ${new Date(Number(this.data.getExpiry()) * 1000).toLocaleString()} to refund unilaterally!`);
|
|
466
|
+
}
|
|
467
|
+
throw e;
|
|
468
|
+
}
|
|
469
|
+
if (typeof result !== "object") {
|
|
470
|
+
if (this.state === ToBTCSwapState.REFUNDABLE)
|
|
471
|
+
throw new Error("Swap expired");
|
|
472
|
+
this.logger.debug("waitTillRefunded(): Resolved from state change");
|
|
473
|
+
return true;
|
|
474
|
+
}
|
|
475
|
+
this.logger.debug("waitTillRefunded(): Resolved from intermediary response");
|
|
476
|
+
switch (result.code) {
|
|
477
|
+
case IntermediaryAPI_1.RefundAuthorizationResponseCodes.PAID:
|
|
478
|
+
return true;
|
|
479
|
+
case IntermediaryAPI_1.RefundAuthorizationResponseCodes.REFUND_DATA:
|
|
480
|
+
const resultData = result.data;
|
|
481
|
+
await this.wrapper.contract.isValidRefundAuthorization(this.data, resultData);
|
|
482
|
+
await this._saveAndEmit(ToBTCSwapState.REFUNDABLE);
|
|
483
|
+
return false;
|
|
484
|
+
case IntermediaryAPI_1.RefundAuthorizationResponseCodes.EXPIRED:
|
|
485
|
+
if (await this.wrapper.contract.isExpired(this._getInitiator(), this.data))
|
|
486
|
+
throw new Error("Swap expired");
|
|
487
|
+
throw new IntermediaryError_1.IntermediaryError("Swap expired");
|
|
488
|
+
case IntermediaryAPI_1.RefundAuthorizationResponseCodes.NOT_FOUND:
|
|
489
|
+
if (this.state === ToBTCSwapState.CLAIMED)
|
|
490
|
+
return true;
|
|
491
|
+
throw new Error("LP swap not found");
|
|
492
|
+
}
|
|
493
|
+
throw new Error("Invalid response code returned by the LP");
|
|
494
|
+
}
|
|
495
|
+
//////////////////////////////
|
|
496
|
+
//// Refund
|
|
497
|
+
/**
|
|
498
|
+
* Get the estimated smart chain transaction fee of the refund transaction
|
|
499
|
+
*/
|
|
500
|
+
getRefundFee() {
|
|
501
|
+
return this.wrapper.contract.getRefundFee(this._getInitiator(), this.data);
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Returns transactions for refunding the swap if the swap is in refundable state, you can check so with isRefundable()
|
|
505
|
+
*
|
|
506
|
+
* @throws {IntermediaryError} If intermediary returns invalid response in case cooperative refund should be used
|
|
507
|
+
* @throws {SignatureVerificationError} If intermediary returned invalid cooperative refund signature
|
|
508
|
+
* @throws {Error} When state is not refundable
|
|
509
|
+
*/
|
|
510
|
+
async txsRefund(signer) {
|
|
511
|
+
if (!this.isRefundable())
|
|
512
|
+
throw new Error("Must be in REFUNDABLE state or expired!");
|
|
513
|
+
signer ??= this._getInitiator();
|
|
514
|
+
if (await this.wrapper.contract.isExpired(this._getInitiator(), this.data)) {
|
|
515
|
+
return await this.wrapper.contract.txsRefund(signer, this.data, true, true);
|
|
516
|
+
}
|
|
517
|
+
else {
|
|
518
|
+
if (this.url == null)
|
|
519
|
+
throw new Error("LP URL not known, cannot get cooperative refund message, wait till expiry to refund!");
|
|
520
|
+
const res = await IntermediaryAPI_1.IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this.data.getSequence());
|
|
521
|
+
if (res.code === IntermediaryAPI_1.RefundAuthorizationResponseCodes.REFUND_DATA) {
|
|
522
|
+
return await this.wrapper.contract.txsRefundWithAuthorization(signer, this.data, res.data, true, true);
|
|
523
|
+
}
|
|
524
|
+
throw new IntermediaryError_1.IntermediaryError("Invalid intermediary cooperative message returned");
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
/**
|
|
528
|
+
* Refunds the swap if the swap is in refundable state, you can check so with isRefundable()
|
|
529
|
+
*
|
|
530
|
+
* @param _signer Signer to sign the transactions with, must be the same as used in the initialization
|
|
531
|
+
* @param abortSignal Abort signal
|
|
532
|
+
* @throws {Error} If invalid signer is provided that doesn't match the swap data
|
|
533
|
+
*/
|
|
534
|
+
async refund(_signer, abortSignal) {
|
|
535
|
+
const signer = (0, base_1.isAbstractSigner)(_signer) ? _signer : await this.wrapper.chain.wrapSigner(_signer);
|
|
536
|
+
const result = await this.wrapper.chain.sendAndConfirm(signer, await this.txsRefund(signer.getAddress()), true, abortSignal);
|
|
537
|
+
this.refundTxId = result[0];
|
|
538
|
+
if (this.state === ToBTCSwapState.COMMITED || this.state === ToBTCSwapState.REFUNDABLE || this.state === ToBTCSwapState.SOFT_CLAIMED) {
|
|
539
|
+
await this._saveAndEmit(ToBTCSwapState.REFUNDED);
|
|
540
|
+
}
|
|
541
|
+
return result[0];
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Waits till a swap is refunded, should be called after sending the refund transactions manually
|
|
545
|
+
*
|
|
546
|
+
* @param abortSignal AbortSignal
|
|
547
|
+
* @throws {Error} When swap is not in a valid state (must be COMMITED)
|
|
548
|
+
* @throws {Error} If we tried to refund but claimer was able to claim first
|
|
549
|
+
*/
|
|
550
|
+
async waitTillRefunded(abortSignal) {
|
|
551
|
+
if (this.state === ToBTCSwapState.REFUNDED)
|
|
552
|
+
return Promise.resolve();
|
|
553
|
+
if (this.state !== ToBTCSwapState.COMMITED && this.state !== ToBTCSwapState.SOFT_CLAIMED)
|
|
554
|
+
throw new Error("Invalid state (not COMMITED)");
|
|
555
|
+
const abortController = new AbortController();
|
|
556
|
+
if (abortSignal != null)
|
|
557
|
+
abortSignal.addEventListener("abort", () => abortController.abort(abortSignal.reason));
|
|
558
|
+
const res = await Promise.race([
|
|
559
|
+
this.watchdogWaitTillResult(undefined, abortController.signal),
|
|
560
|
+
this.waitTillState(ToBTCSwapState.REFUNDED, "eq", abortController.signal).then(() => 0),
|
|
561
|
+
this.waitTillState(ToBTCSwapState.CLAIMED, "eq", abortController.signal).then(() => 1),
|
|
562
|
+
]);
|
|
563
|
+
abortController.abort();
|
|
564
|
+
if (res === 0) {
|
|
565
|
+
this.logger.debug("waitTillRefunded(): Resolved from state change (REFUNDED)");
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
if (res === 1) {
|
|
569
|
+
this.logger.debug("waitTillRefunded(): Resolved from state change (CLAIMED)");
|
|
570
|
+
throw new Error("Tried to refund swap, but claimer claimed it in the meantime!");
|
|
571
|
+
}
|
|
572
|
+
this.logger.debug("waitTillRefunded(): Resolved from watchdog");
|
|
573
|
+
if (res?.type === base_1.SwapCommitStateType.PAID) {
|
|
574
|
+
if (this.claimTxId == null)
|
|
575
|
+
this.claimTxId = await res.getClaimTxId();
|
|
576
|
+
await this._saveAndEmit(ToBTCSwapState.CLAIMED);
|
|
577
|
+
throw new Error("Tried to refund swap, but claimer claimed it in the meantime!");
|
|
578
|
+
}
|
|
579
|
+
if (res?.type === base_1.SwapCommitStateType.NOT_COMMITED) {
|
|
580
|
+
if (this.refundTxId == null && res.getRefundTxId != null)
|
|
581
|
+
this.refundTxId = await res.getRefundTxId();
|
|
582
|
+
await this._saveAndEmit(ToBTCSwapState.REFUNDED);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
//////////////////////////////
|
|
586
|
+
//// Storage
|
|
587
|
+
serialize() {
|
|
588
|
+
const obj = super.serialize();
|
|
589
|
+
return {
|
|
590
|
+
...obj,
|
|
591
|
+
networkFee: this.networkFee == null ? null : this.networkFee.toString(10),
|
|
592
|
+
networkFeeBtc: this.networkFeeBtc == null ? null : this.networkFeeBtc.toString(10)
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
//////////////////////////////
|
|
596
|
+
//// Swap ticks & sync
|
|
597
|
+
/**
|
|
598
|
+
* Checks the swap's state on-chain and compares it to its internal state, updates/changes it according to on-chain
|
|
599
|
+
* data
|
|
600
|
+
*
|
|
601
|
+
* @private
|
|
602
|
+
*/
|
|
603
|
+
async syncStateFromChain(quoteDefinitelyExpired, commitStatus) {
|
|
604
|
+
if (this.state === ToBTCSwapState.CREATED ||
|
|
605
|
+
this.state === ToBTCSwapState.QUOTE_SOFT_EXPIRED ||
|
|
606
|
+
this.state === ToBTCSwapState.COMMITED ||
|
|
607
|
+
this.state === ToBTCSwapState.SOFT_CLAIMED ||
|
|
608
|
+
this.state === ToBTCSwapState.REFUNDABLE) {
|
|
609
|
+
let quoteExpired = false;
|
|
610
|
+
if (this.state === ToBTCSwapState.CREATED || this.state === ToBTCSwapState.QUOTE_SOFT_EXPIRED) {
|
|
611
|
+
//Check if quote is still valid
|
|
612
|
+
quoteExpired = quoteDefinitelyExpired ?? await this._verifyQuoteDefinitelyExpired();
|
|
613
|
+
}
|
|
614
|
+
commitStatus ??= await this.wrapper.contract.getCommitStatus(this._getInitiator(), this.data);
|
|
615
|
+
switch (commitStatus?.type) {
|
|
616
|
+
case base_1.SwapCommitStateType.PAID:
|
|
617
|
+
if (this.claimTxId == null && commitStatus.getClaimTxId)
|
|
618
|
+
this.claimTxId = await commitStatus.getClaimTxId();
|
|
619
|
+
const eventResult = await commitStatus.getClaimResult();
|
|
620
|
+
try {
|
|
621
|
+
await this._setPaymentResult({ secret: eventResult, txId: Buffer.from(eventResult, "hex").reverse().toString("hex") });
|
|
622
|
+
}
|
|
623
|
+
catch (e) {
|
|
624
|
+
this.logger.error(`Failed to set payment result ${eventResult} on the swap!`);
|
|
625
|
+
}
|
|
626
|
+
this.state = ToBTCSwapState.CLAIMED;
|
|
627
|
+
return true;
|
|
628
|
+
case base_1.SwapCommitStateType.REFUNDABLE:
|
|
629
|
+
this.state = ToBTCSwapState.REFUNDABLE;
|
|
630
|
+
return true;
|
|
631
|
+
case base_1.SwapCommitStateType.EXPIRED:
|
|
632
|
+
if (this.refundTxId == null && commitStatus.getRefundTxId)
|
|
633
|
+
this.refundTxId = await commitStatus.getRefundTxId();
|
|
634
|
+
this.state = ToBTCSwapState.QUOTE_EXPIRED;
|
|
635
|
+
return true;
|
|
636
|
+
case base_1.SwapCommitStateType.NOT_COMMITED:
|
|
637
|
+
if (this.refundTxId == null && commitStatus.getRefundTxId)
|
|
638
|
+
this.refundTxId = await commitStatus.getRefundTxId();
|
|
639
|
+
if (this.state === ToBTCSwapState.COMMITED || this.state === ToBTCSwapState.REFUNDABLE) {
|
|
640
|
+
this.state = ToBTCSwapState.REFUNDED;
|
|
641
|
+
return true;
|
|
642
|
+
}
|
|
643
|
+
break;
|
|
644
|
+
case base_1.SwapCommitStateType.COMMITED:
|
|
645
|
+
if (this.state !== ToBTCSwapState.COMMITED && this.state !== ToBTCSwapState.REFUNDABLE) {
|
|
646
|
+
this.state = ToBTCSwapState.COMMITED;
|
|
647
|
+
return true;
|
|
648
|
+
}
|
|
649
|
+
break;
|
|
650
|
+
}
|
|
651
|
+
if ((this.state === ToBTCSwapState.CREATED || this.state === ToBTCSwapState.QUOTE_SOFT_EXPIRED)) {
|
|
652
|
+
if (quoteExpired) {
|
|
653
|
+
this.state = ToBTCSwapState.QUOTE_EXPIRED;
|
|
654
|
+
return true;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
return false;
|
|
659
|
+
}
|
|
660
|
+
_shouldFetchCommitStatus() {
|
|
661
|
+
return this.state === ToBTCSwapState.CREATED ||
|
|
662
|
+
this.state === ToBTCSwapState.QUOTE_SOFT_EXPIRED ||
|
|
663
|
+
this.state === ToBTCSwapState.COMMITED ||
|
|
664
|
+
this.state === ToBTCSwapState.SOFT_CLAIMED ||
|
|
665
|
+
this.state === ToBTCSwapState.REFUNDABLE;
|
|
666
|
+
}
|
|
667
|
+
_shouldFetchExpiryStatus() {
|
|
668
|
+
return this.state === ToBTCSwapState.CREATED || this.state === ToBTCSwapState.QUOTE_SOFT_EXPIRED;
|
|
669
|
+
}
|
|
670
|
+
async _sync(save, quoteDefinitelyExpired, commitStatus) {
|
|
671
|
+
let changed = await this.syncStateFromChain(quoteDefinitelyExpired, commitStatus);
|
|
672
|
+
if (this.state === ToBTCSwapState.COMMITED || this.state === ToBTCSwapState.SOFT_CLAIMED) {
|
|
673
|
+
//Check if that maybe already concluded
|
|
674
|
+
try {
|
|
675
|
+
if (await this.checkIntermediarySwapProcessed(false))
|
|
676
|
+
changed = true;
|
|
677
|
+
}
|
|
678
|
+
catch (e) {
|
|
679
|
+
this.logger.error("_sync(): Failed to synchronize swap, error: ", e);
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
if (save && changed)
|
|
683
|
+
await this._saveAndEmit();
|
|
684
|
+
return changed;
|
|
685
|
+
}
|
|
686
|
+
async _tick(save) {
|
|
687
|
+
switch (this.state) {
|
|
688
|
+
case ToBTCSwapState.CREATED:
|
|
689
|
+
if (this.expiry < Date.now()) {
|
|
690
|
+
this.state = ToBTCSwapState.QUOTE_SOFT_EXPIRED;
|
|
691
|
+
if (save)
|
|
692
|
+
await this._saveAndEmit();
|
|
693
|
+
return true;
|
|
694
|
+
}
|
|
695
|
+
break;
|
|
696
|
+
case ToBTCSwapState.COMMITED:
|
|
697
|
+
case ToBTCSwapState.SOFT_CLAIMED:
|
|
698
|
+
const expired = await this.wrapper.contract.isExpired(this._getInitiator(), this.data);
|
|
699
|
+
if (expired) {
|
|
700
|
+
this.state = ToBTCSwapState.REFUNDABLE;
|
|
701
|
+
if (save)
|
|
702
|
+
await this._saveAndEmit();
|
|
703
|
+
return true;
|
|
704
|
+
}
|
|
705
|
+
break;
|
|
706
|
+
}
|
|
707
|
+
return false;
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
exports.IToBTCSwap = IToBTCSwap;
|
|
711
|
+
var ToBTCSwapState;
|
|
712
|
+
(function (ToBTCSwapState) {
|
|
713
|
+
ToBTCSwapState[ToBTCSwapState["REFUNDED"] = -3] = "REFUNDED";
|
|
714
|
+
ToBTCSwapState[ToBTCSwapState["QUOTE_EXPIRED"] = -2] = "QUOTE_EXPIRED";
|
|
715
|
+
ToBTCSwapState[ToBTCSwapState["QUOTE_SOFT_EXPIRED"] = -1] = "QUOTE_SOFT_EXPIRED";
|
|
716
|
+
ToBTCSwapState[ToBTCSwapState["CREATED"] = 0] = "CREATED";
|
|
717
|
+
ToBTCSwapState[ToBTCSwapState["COMMITED"] = 1] = "COMMITED";
|
|
718
|
+
ToBTCSwapState[ToBTCSwapState["SOFT_CLAIMED"] = 2] = "SOFT_CLAIMED";
|
|
719
|
+
ToBTCSwapState[ToBTCSwapState["CLAIMED"] = 3] = "CLAIMED";
|
|
720
|
+
ToBTCSwapState[ToBTCSwapState["REFUNDABLE"] = 4] = "REFUNDABLE";
|
|
721
|
+
})(ToBTCSwapState = exports.ToBTCSwapState || (exports.ToBTCSwapState = {}));
|