@atomiqlabs/sdk 7.0.12 → 8.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -29
- package/dist/SmartChainAssets.d.ts +11 -3
- package/dist/SmartChainAssets.js +7 -3
- package/dist/bitcoin/BitcoinRpcWithAddressIndex.d.ts +68 -0
- package/dist/bitcoin/BitcoinRpcWithAddressIndex.js +2 -0
- package/dist/bitcoin/LightningNetworkApi.d.ts +12 -0
- package/dist/bitcoin/LightningNetworkApi.js +2 -0
- package/dist/bitcoin/coinselect2/accumulative.d.ts +6 -0
- package/dist/bitcoin/coinselect2/accumulative.js +52 -0
- package/dist/bitcoin/coinselect2/blackjack.d.ts +6 -0
- package/dist/bitcoin/coinselect2/blackjack.js +38 -0
- package/dist/bitcoin/coinselect2/index.d.ts +19 -0
- package/dist/bitcoin/coinselect2/index.js +69 -0
- package/dist/bitcoin/coinselect2/utils.d.ts +71 -0
- package/dist/bitcoin/coinselect2/utils.js +123 -0
- package/dist/bitcoin/mempool/MempoolApi.d.ts +350 -0
- package/dist/bitcoin/mempool/MempoolApi.js +311 -0
- package/dist/bitcoin/mempool/MempoolBitcoinBlock.d.ts +44 -0
- package/dist/bitcoin/mempool/MempoolBitcoinBlock.js +48 -0
- package/dist/bitcoin/mempool/MempoolBitcoinRpc.d.ts +119 -0
- package/dist/bitcoin/mempool/MempoolBitcoinRpc.js +361 -0
- package/dist/bitcoin/mempool/synchronizer/MempoolBtcRelaySynchronizer.d.ts +22 -0
- package/dist/bitcoin/mempool/synchronizer/MempoolBtcRelaySynchronizer.js +105 -0
- package/dist/bitcoin/wallet/BitcoinWallet.d.ts +93 -0
- package/dist/bitcoin/wallet/BitcoinWallet.js +273 -0
- package/dist/bitcoin/wallet/IBitcoinWallet.d.ts +28 -0
- package/dist/bitcoin/wallet/IBitcoinWallet.js +20 -0
- package/dist/bitcoin/wallet/MinimalBitcoinWalletInterface.d.ts +21 -0
- package/dist/bitcoin/wallet/MinimalBitcoinWalletInterface.js +2 -0
- package/dist/bitcoin/wallet/MinimalLightningNetworkWalletInterface.d.ts +7 -0
- package/dist/bitcoin/wallet/MinimalLightningNetworkWalletInterface.js +2 -0
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.d.ts +40 -0
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.js +86 -0
- package/dist/enums/FeeType.d.ts +8 -0
- package/dist/enums/FeeType.js +12 -0
- package/dist/enums/SwapAmountType.d.ts +8 -0
- package/dist/enums/SwapAmountType.js +12 -0
- package/dist/enums/SwapDirection.d.ts +8 -0
- package/dist/enums/SwapDirection.js +12 -0
- package/dist/enums/SwapType.d.ts +14 -0
- package/dist/enums/SwapType.js +18 -0
- package/dist/errors/IntermediaryError.d.ts +9 -0
- package/dist/errors/IntermediaryError.js +26 -0
- package/dist/errors/PaymentAuthError.d.ts +11 -0
- package/dist/errors/PaymentAuthError.js +23 -0
- package/dist/errors/RequestError.d.ts +18 -0
- package/dist/errors/RequestError.js +46 -0
- package/dist/errors/UserError.d.ts +7 -0
- package/dist/errors/UserError.js +15 -0
- package/dist/events/UnifiedSwapEventListener.d.ts +23 -0
- package/dist/events/UnifiedSwapEventListener.js +130 -0
- package/dist/http/HttpUtils.d.ts +27 -0
- package/dist/http/HttpUtils.js +91 -0
- package/dist/http/paramcoders/IParamReader.d.ts +8 -0
- package/dist/http/paramcoders/IParamReader.js +2 -0
- package/dist/http/paramcoders/ParamDecoder.d.ts +44 -0
- package/dist/http/paramcoders/ParamDecoder.js +132 -0
- package/dist/http/paramcoders/ParamEncoder.d.ts +20 -0
- package/dist/http/paramcoders/ParamEncoder.js +31 -0
- package/dist/http/paramcoders/SchemaVerifier.d.ts +26 -0
- package/dist/http/paramcoders/SchemaVerifier.js +145 -0
- package/dist/http/paramcoders/client/ResponseParamDecoder.d.ts +11 -0
- package/dist/http/paramcoders/client/ResponseParamDecoder.js +57 -0
- package/dist/http/paramcoders/client/StreamParamEncoder.d.ts +13 -0
- package/dist/http/paramcoders/client/StreamParamEncoder.js +26 -0
- package/dist/http/paramcoders/client/StreamingFetchPromise.d.ts +16 -0
- package/dist/http/paramcoders/client/StreamingFetchPromise.js +174 -0
- package/dist/index.d.ts +82 -4
- package/dist/index.js +128 -4
- package/dist/intermediaries/Intermediary.d.ts +111 -0
- package/dist/intermediaries/Intermediary.js +115 -0
- package/dist/intermediaries/IntermediaryDiscovery.d.ts +166 -0
- package/dist/intermediaries/IntermediaryDiscovery.js +390 -0
- package/dist/intermediaries/apis/IntermediaryAPI.d.ts +436 -0
- package/dist/intermediaries/apis/IntermediaryAPI.js +600 -0
- package/dist/intermediaries/apis/TrustedIntermediaryAPI.d.ts +154 -0
- package/dist/intermediaries/apis/TrustedIntermediaryAPI.js +136 -0
- package/dist/lnurl/LNURL.d.ts +102 -0
- package/dist/lnurl/LNURL.js +321 -0
- package/dist/prices/RedundantSwapPrice.d.ts +89 -0
- package/dist/prices/RedundantSwapPrice.js +202 -0
- package/dist/prices/SingleSwapPrice.d.ts +31 -0
- package/dist/prices/SingleSwapPrice.js +41 -0
- package/dist/prices/SwapPriceWithChain.d.ts +70 -0
- package/dist/prices/SwapPriceWithChain.js +91 -0
- package/dist/prices/abstract/ICachedSwapPrice.d.ts +28 -0
- package/dist/prices/abstract/ICachedSwapPrice.js +62 -0
- package/dist/prices/abstract/IPriceProvider.d.ts +81 -0
- package/dist/prices/abstract/IPriceProvider.js +74 -0
- package/dist/prices/abstract/ISwapPrice.d.ts +117 -0
- package/dist/prices/abstract/ISwapPrice.js +219 -0
- package/dist/prices/providers/BinancePriceProvider.d.ts +16 -0
- package/dist/prices/providers/BinancePriceProvider.js +23 -0
- package/dist/prices/providers/CoinGeckoPriceProvider.d.ts +17 -0
- package/dist/prices/providers/CoinGeckoPriceProvider.js +23 -0
- package/dist/prices/providers/CoinPaprikaPriceProvider.d.ts +19 -0
- package/dist/prices/providers/CoinPaprikaPriceProvider.js +23 -0
- package/dist/prices/providers/CustomPriceProvider.d.ts +13 -0
- package/dist/prices/providers/CustomPriceProvider.js +24 -0
- package/dist/prices/providers/KrakenPriceProvider.d.ts +29 -0
- package/dist/prices/providers/KrakenPriceProvider.js +36 -0
- package/dist/prices/providers/OKXPriceProvider.d.ts +28 -0
- package/dist/prices/providers/OKXPriceProvider.js +23 -0
- package/dist/prices/providers/abstract/ExchangePriceProvider.d.ts +14 -0
- package/dist/prices/providers/abstract/ExchangePriceProvider.js +18 -0
- package/dist/prices/providers/abstract/HttpPriceProvider.d.ts +7 -0
- package/dist/prices/providers/abstract/HttpPriceProvider.js +12 -0
- package/dist/storage/IUnifiedStorage.d.ts +73 -0
- package/dist/storage/IUnifiedStorage.js +2 -0
- package/dist/storage/UnifiedSwapStorage.d.ts +82 -0
- package/dist/storage/UnifiedSwapStorage.js +83 -0
- package/dist/storage-browser/IndexedDBUnifiedStorage.d.ts +39 -0
- package/dist/storage-browser/IndexedDBUnifiedStorage.js +275 -0
- package/dist/{storage → storage-browser}/LocalStorageManager.d.ts +1 -0
- package/dist/{storage → storage-browser}/LocalStorageManager.js +2 -1
- package/dist/swapper/Swapper.d.ts +533 -0
- package/dist/swapper/Swapper.js +1566 -0
- package/dist/swapper/SwapperFactory.d.ts +87 -0
- package/dist/{SwapperFactory.js → swapper/SwapperFactory.js} +37 -19
- package/dist/swapper/SwapperUtils.d.ts +153 -0
- package/dist/swapper/SwapperUtils.js +420 -0
- package/dist/swapper/SwapperWithChain.d.ts +214 -0
- package/dist/swapper/SwapperWithChain.js +315 -0
- package/dist/swapper/SwapperWithSigner.d.ts +178 -0
- package/dist/swapper/SwapperWithSigner.js +172 -0
- package/dist/swaps/IAddressSwap.d.ts +13 -0
- package/dist/swaps/IAddressSwap.js +13 -0
- package/dist/swaps/IBTCWalletSwap.d.ts +55 -0
- package/dist/swaps/IBTCWalletSwap.js +17 -0
- package/dist/swaps/IClaimableSwap.d.ts +17 -0
- package/dist/swaps/IClaimableSwap.js +14 -0
- package/dist/swaps/IClaimableSwapWrapper.d.ts +5 -0
- package/dist/swaps/IClaimableSwapWrapper.js +2 -0
- package/dist/swaps/IRefundableSwap.d.ts +17 -0
- package/dist/swaps/IRefundableSwap.js +13 -0
- package/dist/swaps/ISwap.d.ts +207 -0
- package/dist/swaps/ISwap.js +264 -0
- package/dist/swaps/ISwapWithGasDrop.d.ts +15 -0
- package/dist/swaps/ISwapWithGasDrop.js +11 -0
- package/dist/swaps/ISwapWrapper.d.ts +153 -0
- package/dist/swaps/ISwapWrapper.js +227 -0
- package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.d.ts +53 -0
- package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.js +116 -0
- package/dist/swaps/escrow_swaps/IEscrowSwap.d.ts +70 -0
- package/dist/swaps/escrow_swaps/IEscrowSwap.js +132 -0
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.d.ts +85 -0
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.js +122 -0
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.d.ts +86 -0
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.js +115 -0
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.d.ts +93 -0
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.js +121 -0
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.d.ts +45 -0
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.js +65 -0
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.d.ts +263 -0
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +933 -0
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +110 -0
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +307 -0
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.d.ts +236 -0
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +898 -0
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +125 -0
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +393 -0
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.d.ts +245 -0
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +841 -0
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.d.ts +120 -0
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.js +294 -0
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.d.ts +228 -0
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +721 -0
- package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.d.ts +37 -0
- package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.js +93 -0
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.d.ts +86 -0
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.js +213 -0
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.d.ts +170 -0
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.js +520 -0
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.d.ts +50 -0
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.js +109 -0
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.d.ts +93 -0
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.js +217 -0
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +315 -0
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +1098 -0
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.d.ts +125 -0
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.js +631 -0
- package/dist/swaps/trusted/ln/LnForGasSwap.d.ts +107 -0
- package/dist/swaps/trusted/ln/LnForGasSwap.js +343 -0
- package/dist/swaps/trusted/ln/LnForGasWrapper.d.ts +21 -0
- package/dist/swaps/trusted/ln/LnForGasWrapper.js +62 -0
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.d.ts +164 -0
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.js +520 -0
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.d.ts +48 -0
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.js +74 -0
- package/dist/types/AmountData.d.ts +9 -0
- package/dist/types/AmountData.js +2 -0
- package/dist/types/CustomPriceFunction.d.ts +5 -0
- package/dist/types/CustomPriceFunction.js +2 -0
- package/dist/types/PriceInfoType.d.ts +28 -0
- package/dist/types/PriceInfoType.js +57 -0
- package/dist/types/SwapExecutionAction.d.ts +7 -0
- package/dist/types/SwapExecutionAction.js +2 -0
- package/dist/types/SwapWithSigner.d.ts +14 -0
- package/dist/types/SwapWithSigner.js +40 -0
- package/dist/types/Token.d.ts +53 -0
- package/dist/types/Token.js +58 -0
- package/dist/types/TokenAmount.d.ts +57 -0
- package/dist/types/TokenAmount.js +47 -0
- package/dist/types/fees/Fee.d.ts +49 -0
- package/dist/types/fees/Fee.js +2 -0
- package/dist/types/fees/FeeBreakdown.d.ts +10 -0
- package/dist/types/fees/FeeBreakdown.js +2 -0
- package/dist/types/fees/PercentagePPM.d.ts +15 -0
- package/dist/types/fees/PercentagePPM.js +17 -0
- package/dist/types/lnurl/LNURLPay.d.ts +54 -0
- package/dist/types/lnurl/LNURLPay.js +28 -0
- package/dist/types/lnurl/LNURLWithdraw.d.ts +42 -0
- package/dist/types/lnurl/LNURLWithdraw.js +24 -0
- package/dist/utils/AutomaticClockDriftCorrection.d.ts +1 -0
- package/dist/utils/AutomaticClockDriftCorrection.js +70 -0
- package/dist/utils/BitcoinUtils.d.ts +13 -0
- package/dist/utils/BitcoinUtils.js +98 -0
- package/dist/utils/BitcoinWalletUtils.d.ts +7 -0
- package/dist/utils/BitcoinWalletUtils.js +14 -0
- package/dist/utils/Logger.d.ts +7 -0
- package/dist/utils/Logger.js +12 -0
- package/dist/utils/RetryUtils.d.ts +21 -0
- package/dist/utils/RetryUtils.js +66 -0
- package/dist/utils/SwapUtils.d.ts +31 -0
- package/dist/utils/SwapUtils.js +18 -0
- package/dist/{Utils.d.ts → utils/TimeoutUtils.d.ts} +9 -3
- package/dist/utils/TimeoutUtils.js +55 -0
- package/dist/utils/TokenUtils.d.ts +11 -0
- package/dist/utils/TokenUtils.js +29 -0
- package/dist/utils/TypeUtils.d.ts +7 -0
- package/dist/utils/TypeUtils.js +2 -0
- package/dist/utils/Utils.d.ts +57 -0
- package/dist/utils/Utils.js +178 -0
- package/package.json +14 -6
- package/src/SmartChainAssets.ts +11 -3
- package/src/bitcoin/BitcoinRpcWithAddressIndex.ts +87 -0
- package/src/bitcoin/LightningNetworkApi.ts +16 -0
- package/src/bitcoin/coinselect2/accumulative.ts +68 -0
- package/src/bitcoin/coinselect2/blackjack.ts +49 -0
- package/src/bitcoin/coinselect2/index.ts +92 -0
- package/src/bitcoin/coinselect2/utils.ts +189 -0
- package/src/bitcoin/mempool/MempoolApi.ts +554 -0
- package/src/bitcoin/mempool/MempoolBitcoinBlock.ts +88 -0
- package/src/bitcoin/mempool/MempoolBitcoinRpc.ts +437 -0
- package/src/bitcoin/mempool/synchronizer/MempoolBtcRelaySynchronizer.ts +134 -0
- package/src/bitcoin/wallet/BitcoinWallet.ts +375 -0
- package/src/bitcoin/wallet/IBitcoinWallet.ts +44 -0
- package/src/bitcoin/wallet/MinimalBitcoinWalletInterface.ts +19 -0
- package/src/bitcoin/wallet/MinimalLightningNetworkWalletInterface.ts +7 -0
- package/src/bitcoin/wallet/SingleAddressBitcoinWallet.ts +108 -0
- package/src/enums/FeeType.ts +9 -0
- package/src/enums/SwapAmountType.ts +9 -0
- package/src/enums/SwapDirection.ts +9 -0
- package/src/enums/SwapType.ts +15 -0
- package/src/errors/IntermediaryError.ts +24 -0
- package/src/errors/PaymentAuthError.ts +26 -0
- package/src/errors/RequestError.ts +51 -0
- package/src/errors/UserError.ts +14 -0
- package/src/events/UnifiedSwapEventListener.ts +171 -0
- package/src/http/HttpUtils.ts +92 -0
- package/src/http/paramcoders/IParamReader.ts +10 -0
- package/src/http/paramcoders/ParamDecoder.ts +142 -0
- package/src/http/paramcoders/ParamEncoder.ts +37 -0
- package/src/http/paramcoders/SchemaVerifier.ts +153 -0
- package/src/http/paramcoders/client/ResponseParamDecoder.ts +58 -0
- package/src/http/paramcoders/client/StreamParamEncoder.ts +29 -0
- package/src/http/paramcoders/client/StreamingFetchPromise.ts +193 -0
- package/src/index.ts +102 -4
- package/src/intermediaries/Intermediary.ts +204 -0
- package/src/intermediaries/IntermediaryDiscovery.ts +485 -0
- package/src/intermediaries/apis/IntermediaryAPI.ts +940 -0
- package/src/intermediaries/apis/TrustedIntermediaryAPI.ts +257 -0
- package/src/lnurl/LNURL.ts +403 -0
- package/src/prices/RedundantSwapPrice.ts +245 -0
- package/src/prices/SingleSwapPrice.ts +47 -0
- package/src/prices/SwapPriceWithChain.ts +157 -0
- package/src/prices/abstract/ICachedSwapPrice.ts +86 -0
- package/src/prices/abstract/IPriceProvider.ts +128 -0
- package/src/prices/abstract/ISwapPrice.ts +328 -0
- package/src/prices/providers/BinancePriceProvider.ts +41 -0
- package/src/prices/providers/CoinGeckoPriceProvider.ts +40 -0
- package/src/prices/providers/CoinPaprikaPriceProvider.ts +44 -0
- package/src/prices/providers/CustomPriceProvider.ts +29 -0
- package/src/prices/providers/KrakenPriceProvider.ts +74 -0
- package/src/prices/providers/OKXPriceProvider.ts +53 -0
- package/src/prices/providers/abstract/ExchangePriceProvider.ts +29 -0
- package/src/prices/providers/abstract/HttpPriceProvider.ts +15 -0
- package/src/storage/IUnifiedStorage.ts +83 -0
- package/src/storage/UnifiedSwapStorage.ts +104 -0
- package/src/storage-browser/IndexedDBUnifiedStorage.ts +328 -0
- package/src/{storage → storage-browser}/LocalStorageManager.ts +2 -1
- package/src/swapper/Swapper.ts +2107 -0
- package/src/{SwapperFactory.ts → swapper/SwapperFactory.ts} +113 -72
- package/src/swapper/SwapperUtils.ts +510 -0
- package/src/swapper/SwapperWithChain.ts +464 -0
- package/src/swapper/SwapperWithSigner.ts +300 -0
- package/src/swaps/IAddressSwap.ts +20 -0
- package/src/swaps/IBTCWalletSwap.ts +77 -0
- package/src/swaps/IClaimableSwap.ts +30 -0
- package/src/swaps/IClaimableSwapWrapper.ts +9 -0
- package/src/swaps/IRefundableSwap.ts +29 -0
- package/src/swaps/ISwap.ts +490 -0
- package/src/swaps/ISwapWithGasDrop.ts +19 -0
- package/src/swaps/ISwapWrapper.ts +344 -0
- package/src/swaps/escrow_swaps/IEscrowSelfInitSwap.ts +168 -0
- package/src/swaps/escrow_swaps/IEscrowSwap.ts +197 -0
- package/src/swaps/escrow_swaps/IEscrowSwapWrapper.ts +210 -0
- package/src/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.ts +150 -0
- package/src/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.ts +219 -0
- package/src/swaps/escrow_swaps/frombtc/IFromBTCWrapper.ts +84 -0
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.ts +1082 -0
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +429 -0
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +1078 -0
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +549 -0
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.ts +974 -0
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.ts +443 -0
- package/src/swaps/escrow_swaps/tobtc/IToBTCSwap.ts +860 -0
- package/src/swaps/escrow_swaps/tobtc/IToBTCWrapper.ts +104 -0
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.ts +256 -0
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.ts +716 -0
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.ts +151 -0
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.ts +299 -0
- package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +1394 -0
- package/src/swaps/spv_swaps/SpvFromBTCWrapper.ts +796 -0
- package/src/swaps/trusted/ln/LnForGasSwap.ts +402 -0
- package/src/swaps/trusted/ln/LnForGasWrapper.ts +70 -0
- package/src/swaps/trusted/onchain/OnchainForGasSwap.ts +633 -0
- package/src/swaps/trusted/onchain/OnchainForGasWrapper.ts +110 -0
- package/src/types/AmountData.ts +9 -0
- package/src/types/CustomPriceFunction.ts +5 -0
- package/src/types/PriceInfoType.ts +67 -0
- package/src/types/SwapExecutionAction.ts +8 -0
- package/src/types/SwapWithSigner.ts +57 -0
- package/src/types/Token.ts +90 -0
- package/src/types/TokenAmount.ts +110 -0
- package/src/types/fees/Fee.ts +55 -0
- package/src/types/fees/FeeBreakdown.ts +11 -0
- package/src/types/fees/PercentagePPM.ts +24 -0
- package/src/types/lnurl/LNURLPay.ts +72 -0
- package/src/types/lnurl/LNURLWithdraw.ts +55 -0
- package/src/utils/AutomaticClockDriftCorrection.ts +71 -0
- package/src/utils/BitcoinUtils.ts +86 -0
- package/src/utils/BitcoinWalletUtils.ts +16 -0
- package/src/utils/Logger.ts +15 -0
- package/src/utils/RetryUtils.ts +71 -0
- package/src/utils/SwapUtils.ts +38 -0
- package/src/utils/TimeoutUtils.ts +50 -0
- package/src/utils/TokenUtils.ts +25 -0
- package/src/utils/TypeUtils.ts +9 -0
- package/src/utils/Utils.ts +182 -0
- package/dist/SwapperFactory.d.ts +0 -52
- package/dist/Utils.js +0 -37
- package/dist/fs-storage/FileSystemStorageManager.d.ts +0 -15
- package/dist/fs-storage/FileSystemStorageManager.js +0 -60
- package/dist/fs-storage/index.d.ts +0 -1
- package/dist/fs-storage/index.js +0 -17
- package/src/SmartChainAssets.js +0 -75
- package/src/SwapperFactory.js +0 -120
- package/src/Utils.js +0 -37
- package/src/Utils.ts +0 -31
- package/src/fs-storage/FileSystemStorageManager.ts +0 -71
- package/src/fs-storage/index.ts +0 -1
- package/src/index.js +0 -21
- package/src/storage/LocalStorageManager.js +0 -72
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { ChainType } from "@atomiqlabs/base";
|
|
2
|
+
import { IUnifiedStorage, QueryParams } from "./IUnifiedStorage";
|
|
3
|
+
import { ISwap } from "../swaps/ISwap";
|
|
4
|
+
declare const indexes: readonly [{
|
|
5
|
+
readonly key: "id";
|
|
6
|
+
readonly type: "string";
|
|
7
|
+
readonly unique: true;
|
|
8
|
+
readonly nullable: false;
|
|
9
|
+
}, {
|
|
10
|
+
readonly key: "escrowHash";
|
|
11
|
+
readonly type: "string";
|
|
12
|
+
readonly unique: true;
|
|
13
|
+
readonly nullable: true;
|
|
14
|
+
}, {
|
|
15
|
+
readonly key: "type";
|
|
16
|
+
readonly type: "number";
|
|
17
|
+
readonly unique: false;
|
|
18
|
+
readonly nullable: false;
|
|
19
|
+
}, {
|
|
20
|
+
readonly key: "initiator";
|
|
21
|
+
readonly type: "string";
|
|
22
|
+
readonly unique: false;
|
|
23
|
+
readonly nullable: false;
|
|
24
|
+
}, {
|
|
25
|
+
readonly key: "state";
|
|
26
|
+
readonly type: "number";
|
|
27
|
+
readonly unique: false;
|
|
28
|
+
readonly nullable: false;
|
|
29
|
+
}, {
|
|
30
|
+
readonly key: "paymentHash";
|
|
31
|
+
readonly type: "string";
|
|
32
|
+
readonly unique: false;
|
|
33
|
+
readonly nullable: true;
|
|
34
|
+
}];
|
|
35
|
+
/**
|
|
36
|
+
* Index types for swap storage
|
|
37
|
+
* @category Storage
|
|
38
|
+
*/
|
|
39
|
+
export type UnifiedSwapStorageIndexes = typeof indexes;
|
|
40
|
+
declare const compositeIndexes: readonly [{
|
|
41
|
+
readonly keys: readonly ["initiator", "id"];
|
|
42
|
+
readonly unique: false;
|
|
43
|
+
}, {
|
|
44
|
+
readonly keys: readonly ["type", "state"];
|
|
45
|
+
readonly unique: false;
|
|
46
|
+
}, {
|
|
47
|
+
readonly keys: readonly ["type", "paymentHash"];
|
|
48
|
+
readonly unique: false;
|
|
49
|
+
}, {
|
|
50
|
+
readonly keys: readonly ["type", "initiator", "state"];
|
|
51
|
+
readonly unique: false;
|
|
52
|
+
}];
|
|
53
|
+
/**
|
|
54
|
+
* Composite index types for swap storage
|
|
55
|
+
* @category Storage
|
|
56
|
+
*/
|
|
57
|
+
export type UnifiedSwapStorageCompositeIndexes = typeof compositeIndexes;
|
|
58
|
+
/**
|
|
59
|
+
* Unified swap persistence layer with caching
|
|
60
|
+
* @category Storage
|
|
61
|
+
*/
|
|
62
|
+
export declare class UnifiedSwapStorage<T extends ChainType> {
|
|
63
|
+
readonly storage: IUnifiedStorage<UnifiedSwapStorageIndexes, UnifiedSwapStorageCompositeIndexes>;
|
|
64
|
+
readonly weakRefCache: Map<string, WeakRef<ISwap<T>>>;
|
|
65
|
+
readonly noWeakRefMap?: boolean;
|
|
66
|
+
constructor(storage: IUnifiedStorage<UnifiedSwapStorageIndexes, UnifiedSwapStorageCompositeIndexes>, noWeakRefMap?: boolean);
|
|
67
|
+
init(): Promise<void>;
|
|
68
|
+
/**
|
|
69
|
+
* Params are specified in the following way:
|
|
70
|
+
* - [[condition1, condition2]] - returns all rows where condition1 AND condition2 is met
|
|
71
|
+
* - [[condition1], [condition2]] - returns all rows where condition1 OR condition2 is met
|
|
72
|
+
* - [[condition1, condition2], [condition3]] - returns all rows where (condition1 AND condition2) OR condition3 is met
|
|
73
|
+
* @param params
|
|
74
|
+
* @param reviver
|
|
75
|
+
*/
|
|
76
|
+
query<S extends ISwap<T>>(params: Array<Array<QueryParams>>, reviver: (obj: any) => S | null | undefined): Promise<Array<S>>;
|
|
77
|
+
save<S extends ISwap<T>>(value: S): Promise<void>;
|
|
78
|
+
saveAll<S extends ISwap<T>>(values: S[]): Promise<void>;
|
|
79
|
+
remove<S extends ISwap<T>>(value: S): Promise<void>;
|
|
80
|
+
removeAll<S extends ISwap<T>>(values: S[]): Promise<void>;
|
|
81
|
+
}
|
|
82
|
+
export {};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UnifiedSwapStorage = void 0;
|
|
4
|
+
const Logger_1 = require("../utils/Logger");
|
|
5
|
+
const logger = (0, Logger_1.getLogger)("UnifiedSwapStorage: ");
|
|
6
|
+
const indexes = [
|
|
7
|
+
{ key: "id", type: "string", unique: true, nullable: false },
|
|
8
|
+
{ key: "escrowHash", type: "string", unique: true, nullable: true },
|
|
9
|
+
{ key: "type", type: "number", unique: false, nullable: false },
|
|
10
|
+
{ key: "initiator", type: "string", unique: false, nullable: false },
|
|
11
|
+
{ key: "state", type: "number", unique: false, nullable: false },
|
|
12
|
+
{ key: "paymentHash", type: "string", unique: false, nullable: true },
|
|
13
|
+
];
|
|
14
|
+
const compositeIndexes = [
|
|
15
|
+
{ keys: ["initiator", "id"], unique: false },
|
|
16
|
+
{ keys: ["type", "state"], unique: false },
|
|
17
|
+
{ keys: ["type", "paymentHash"], unique: false },
|
|
18
|
+
{ keys: ["type", "initiator", "state"], unique: false }
|
|
19
|
+
];
|
|
20
|
+
/**
|
|
21
|
+
* Unified swap persistence layer with caching
|
|
22
|
+
* @category Storage
|
|
23
|
+
*/
|
|
24
|
+
class UnifiedSwapStorage {
|
|
25
|
+
constructor(storage, noWeakRefMap) {
|
|
26
|
+
this.weakRefCache = new Map();
|
|
27
|
+
this.storage = storage;
|
|
28
|
+
this.noWeakRefMap = noWeakRefMap;
|
|
29
|
+
}
|
|
30
|
+
init() {
|
|
31
|
+
return this.storage.init(indexes, compositeIndexes);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Params are specified in the following way:
|
|
35
|
+
* - [[condition1, condition2]] - returns all rows where condition1 AND condition2 is met
|
|
36
|
+
* - [[condition1], [condition2]] - returns all rows where condition1 OR condition2 is met
|
|
37
|
+
* - [[condition1, condition2], [condition3]] - returns all rows where (condition1 AND condition2) OR condition3 is met
|
|
38
|
+
* @param params
|
|
39
|
+
* @param reviver
|
|
40
|
+
*/
|
|
41
|
+
async query(params, reviver) {
|
|
42
|
+
const rawSwaps = await this.storage.query(params);
|
|
43
|
+
const result = [];
|
|
44
|
+
rawSwaps.forEach(rawObj => {
|
|
45
|
+
if (!this.noWeakRefMap) {
|
|
46
|
+
const savedRef = this.weakRefCache.get(rawObj.id)?.deref();
|
|
47
|
+
if (savedRef != null) {
|
|
48
|
+
result.push(savedRef);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
logger.debug("query(): Reviving new swap instance: " + rawObj.id);
|
|
52
|
+
}
|
|
53
|
+
const value = reviver(rawObj);
|
|
54
|
+
if (value == null)
|
|
55
|
+
return;
|
|
56
|
+
if (!this.noWeakRefMap)
|
|
57
|
+
this.weakRefCache.set(rawObj.id, new WeakRef(value));
|
|
58
|
+
result.push(value);
|
|
59
|
+
});
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
save(value) {
|
|
63
|
+
if (!this.noWeakRefMap)
|
|
64
|
+
this.weakRefCache.set(value.getId(), new WeakRef(value));
|
|
65
|
+
return this.storage.save(value.serialize());
|
|
66
|
+
}
|
|
67
|
+
saveAll(values) {
|
|
68
|
+
if (!this.noWeakRefMap)
|
|
69
|
+
values.forEach(value => this.weakRefCache.set(value.getId(), new WeakRef(value)));
|
|
70
|
+
return this.storage.saveAll(values.map(obj => obj.serialize()));
|
|
71
|
+
}
|
|
72
|
+
remove(value) {
|
|
73
|
+
if (!this.noWeakRefMap)
|
|
74
|
+
this.weakRefCache.delete(value.getId());
|
|
75
|
+
return this.storage.remove(value.serialize());
|
|
76
|
+
}
|
|
77
|
+
removeAll(values) {
|
|
78
|
+
if (!this.noWeakRefMap)
|
|
79
|
+
values.forEach(value => this.weakRefCache.delete(value.getId()));
|
|
80
|
+
return this.storage.removeAll(values.map(obj => obj.serialize()));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.UnifiedSwapStorage = UnifiedSwapStorage;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { IUnifiedStorage, QueryParams, UnifiedStorageCompositeIndexes, UnifiedStoredObject } from "../storage/IUnifiedStorage";
|
|
2
|
+
import { ISwap } from "../swaps/ISwap";
|
|
3
|
+
import { SwapType } from "../enums/SwapType";
|
|
4
|
+
import { UnifiedSwapStorageIndexes } from "../storage/UnifiedSwapStorage";
|
|
5
|
+
import { LoggerType } from "../utils/Logger";
|
|
6
|
+
export type QuerySetCondition = {
|
|
7
|
+
key: string;
|
|
8
|
+
values: Set<any>;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Browser IndexedDB storage implementation
|
|
12
|
+
* @category Storage
|
|
13
|
+
*/
|
|
14
|
+
export declare class IndexedDBUnifiedStorage implements IUnifiedStorage<UnifiedSwapStorageIndexes, UnifiedStorageCompositeIndexes> {
|
|
15
|
+
protected readonly logger: LoggerType;
|
|
16
|
+
storageKey: string;
|
|
17
|
+
db?: IDBDatabase;
|
|
18
|
+
constructor(storageKey: string);
|
|
19
|
+
private tryMigrateLocalStorage;
|
|
20
|
+
private tryMigrateOldIndexedDB;
|
|
21
|
+
tryMigrate(storageKeys: [string, SwapType][], reviver: (obj: any) => ISwap): Promise<boolean>;
|
|
22
|
+
private executeTransaction;
|
|
23
|
+
private executeTransactionArr;
|
|
24
|
+
private executeTransactionWithCursor;
|
|
25
|
+
init(): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Params are specified in the following way:
|
|
28
|
+
* - [[condition1, condition2]] - returns all rows where condition1 AND condition2 is met
|
|
29
|
+
* - [[condition1], [condition2]] - returns all rows where condition1 OR condition2 is met
|
|
30
|
+
* - [[condition1, condition2], [condition3]] - returns all rows where (condition1 AND condition2) OR condition3 is met
|
|
31
|
+
* @param params
|
|
32
|
+
*/
|
|
33
|
+
query(params: Array<Array<QueryParams>>): Promise<Array<UnifiedStoredObject>>;
|
|
34
|
+
querySingle(params: Array<QueryParams>): Promise<Array<UnifiedStoredObject>>;
|
|
35
|
+
remove(object: UnifiedStoredObject): Promise<void>;
|
|
36
|
+
removeAll(arr: UnifiedStoredObject[]): Promise<void>;
|
|
37
|
+
save(object: UnifiedStoredObject): Promise<void>;
|
|
38
|
+
saveAll(arr: UnifiedStoredObject[]): Promise<void>;
|
|
39
|
+
}
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IndexedDBUnifiedStorage = void 0;
|
|
4
|
+
const Logger_1 = require("../utils/Logger");
|
|
5
|
+
function toCompositeIndex(values) {
|
|
6
|
+
if (values.length === 0)
|
|
7
|
+
return [];
|
|
8
|
+
if (values.length === 1) {
|
|
9
|
+
return values[0];
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
const compositeArray = [];
|
|
13
|
+
const firstValues = values.shift();
|
|
14
|
+
const restValues = toCompositeIndex(values);
|
|
15
|
+
for (let value of firstValues) {
|
|
16
|
+
for (let restValue of restValues) {
|
|
17
|
+
compositeArray.push([value].concat(restValue));
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return compositeArray;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function matches(conditions, obj) {
|
|
24
|
+
for (let condition of conditions) {
|
|
25
|
+
let value = obj[condition.key];
|
|
26
|
+
if (!condition.values.has(value))
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
function toSetConditions(input) {
|
|
32
|
+
return input.map(val => {
|
|
33
|
+
return {
|
|
34
|
+
key: val.key,
|
|
35
|
+
values: Array.isArray(val.value) ? new Set(val.value) : new Set([val.value])
|
|
36
|
+
};
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
const indexes = {
|
|
40
|
+
"escrowHash": { key: "escrowHash", unique: true },
|
|
41
|
+
"type": { key: "type", unique: false },
|
|
42
|
+
"initiator": { key: "initiator", unique: false },
|
|
43
|
+
"initiator, id": { key: ["initiator", "id"], unique: false },
|
|
44
|
+
"type, state": { key: ["type", "state"], unique: false },
|
|
45
|
+
"type, paymentHash": { key: ["type", "paymentHash"], unique: false },
|
|
46
|
+
"type, initiator, state": { key: ["type", "initiator", "state"], unique: false }
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Browser IndexedDB storage implementation
|
|
50
|
+
* @category Storage
|
|
51
|
+
*/
|
|
52
|
+
class IndexedDBUnifiedStorage {
|
|
53
|
+
constructor(storageKey) {
|
|
54
|
+
this.storageKey = storageKey;
|
|
55
|
+
this.logger = (0, Logger_1.getLogger)("IndexedDBUnifiedStorage(" + this.storageKey + "): ");
|
|
56
|
+
}
|
|
57
|
+
//Reviver also needs to update the swap to the latest version
|
|
58
|
+
async tryMigrateLocalStorage(storageKey, swapType, reviver) {
|
|
59
|
+
const txt = window.localStorage.getItem(storageKey);
|
|
60
|
+
if (txt == null)
|
|
61
|
+
return false;
|
|
62
|
+
let data;
|
|
63
|
+
try {
|
|
64
|
+
data = JSON.parse(txt);
|
|
65
|
+
}
|
|
66
|
+
catch (e) {
|
|
67
|
+
this.logger.warn("tryMigrate(" + storageKey + "): Tried to migrate the database, but cannot parse old local storage!");
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
let swaps = Object.keys(data).map(id => {
|
|
71
|
+
let swapData = data[id];
|
|
72
|
+
swapData.type = swapType;
|
|
73
|
+
return reviver(swapData);
|
|
74
|
+
});
|
|
75
|
+
await this.saveAll(swaps.map(swap => swap.serialize()));
|
|
76
|
+
window.localStorage.removeItem(storageKey);
|
|
77
|
+
this.logger.info("tryMigrate(" + storageKey + "): Database successfully migrated from localStorage to unifiedIndexedDB!");
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
//Reviver also needs to update the swap to the latest version
|
|
81
|
+
async tryMigrateOldIndexedDB(storageKey, swapType, reviver) {
|
|
82
|
+
const databases = await window.indexedDB.databases();
|
|
83
|
+
if (databases.find(val => val.name === storageKey) == null) {
|
|
84
|
+
this.logger.info("tryMigrateOldIndexedDB(" + storageKey + "): Old database not found!");
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
this.logger.debug("tryMigrateOldIndexedDB(" + storageKey + "): Old database found!");
|
|
88
|
+
let db;
|
|
89
|
+
try {
|
|
90
|
+
db = await new Promise((resolve, reject) => {
|
|
91
|
+
const request = window.indexedDB.open(storageKey, 1);
|
|
92
|
+
request.onerror = (e) => reject(e);
|
|
93
|
+
request.onsuccess = (e) => resolve(e.target.result);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
catch (e) {
|
|
97
|
+
this.logger.warn("tryMigrateOldIndexedDB(" + storageKey + "): Error opening old IndexedDB!", e);
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
this.logger.debug("tryMigrateOldIndexedDB(" + storageKey + "): Connection opened!");
|
|
101
|
+
try {
|
|
102
|
+
const data = await new Promise((resolve, reject) => {
|
|
103
|
+
const tx = db.transaction("swaps", "readonly", { durability: "strict" });
|
|
104
|
+
const store = tx.objectStore("swaps");
|
|
105
|
+
const req = store.getAll();
|
|
106
|
+
req.onsuccess = (event) => resolve(event.target.result);
|
|
107
|
+
req.onerror = (event) => reject(event);
|
|
108
|
+
});
|
|
109
|
+
this.logger.debug("tryMigrateOldIndexedDB(" + storageKey + "): Data retrieved!");
|
|
110
|
+
let swaps = data.map(({ id, data }) => {
|
|
111
|
+
data.type = swapType;
|
|
112
|
+
return reviver(data);
|
|
113
|
+
});
|
|
114
|
+
this.logger.debug("tryMigrateOldIndexedDB(" + storageKey + "): Data revived!");
|
|
115
|
+
await this.saveAll(swaps.map(swap => swap.serialize()));
|
|
116
|
+
this.logger.debug("tryMigrateOldIndexedDB(" + storageKey + "): Data saved!");
|
|
117
|
+
//Remove the old database
|
|
118
|
+
db.close();
|
|
119
|
+
this.logger.debug("tryMigrateOldIndexedDB(" + storageKey + "): DB connection closed!");
|
|
120
|
+
await new Promise((resolve, reject) => {
|
|
121
|
+
const res = window.indexedDB.deleteDatabase(storageKey);
|
|
122
|
+
res.onsuccess = () => resolve();
|
|
123
|
+
res.onerror = (e) => reject(e);
|
|
124
|
+
});
|
|
125
|
+
this.logger.info("tryMigrateOldIndexedDB(" + storageKey + "): Database successfully migrated from oldIndexedDB to unifiedIndexedDB!");
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
catch (e) {
|
|
129
|
+
this.logger.warn("tryMigrateOldIndexedDB(" + storageKey + "): Tried to migrate the database, but cannot parse oldIndexedDB!", e);
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
//NOTE: Reviver also needs to update the swap to the latest version
|
|
134
|
+
async tryMigrate(storageKeys, reviver) {
|
|
135
|
+
let someMigrated = false;
|
|
136
|
+
for (let storageKey of storageKeys) {
|
|
137
|
+
this.logger.info("tryMigrate(): Trying to migrate...", storageKey);
|
|
138
|
+
if (await this.tryMigrateLocalStorage(storageKey[0], storageKey[1], reviver))
|
|
139
|
+
someMigrated = true;
|
|
140
|
+
if (await this.tryMigrateOldIndexedDB(storageKey[0], storageKey[1], reviver))
|
|
141
|
+
someMigrated = true;
|
|
142
|
+
}
|
|
143
|
+
return someMigrated;
|
|
144
|
+
}
|
|
145
|
+
executeTransaction(cbk, readonly) {
|
|
146
|
+
return new Promise((resolve, reject) => {
|
|
147
|
+
if (this.db == null) {
|
|
148
|
+
reject(new Error("Not initiated, call init() first!"));
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const tx = this.db.transaction("swaps", readonly ? "readonly" : "readwrite", { durability: "strict" });
|
|
152
|
+
const req = cbk(tx.objectStore("swaps"));
|
|
153
|
+
req.onsuccess = (event) => resolve(event.target.result);
|
|
154
|
+
req.onerror = (event) => reject(event);
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
executeTransactionArr(cbk, readonly) {
|
|
158
|
+
if (this.db == null)
|
|
159
|
+
throw new Error("Not initiated, call init() first!");
|
|
160
|
+
const tx = this.db.transaction("swaps", readonly ? "readonly" : "readwrite", { durability: "strict" });
|
|
161
|
+
const reqs = cbk(tx.objectStore("swaps"));
|
|
162
|
+
return Promise.all(reqs.map(req => new Promise((resolve, reject) => {
|
|
163
|
+
req.onsuccess = (event) => resolve(event.target.result);
|
|
164
|
+
req.onerror = (event) => reject(event);
|
|
165
|
+
})));
|
|
166
|
+
}
|
|
167
|
+
async executeTransactionWithCursor(cbk, valueCbk) {
|
|
168
|
+
if (this.db == null)
|
|
169
|
+
throw new Error("Not initiated, call init() first!");
|
|
170
|
+
const tx = this.db.transaction("swaps", "readonly", { durability: "strict" });
|
|
171
|
+
const cursorRequests = cbk(tx.objectStore("swaps"));
|
|
172
|
+
const promises = cursorRequests.map(cursorRequest => new Promise((resolve, reject) => {
|
|
173
|
+
const resultObjects = [];
|
|
174
|
+
cursorRequest.onsuccess = (event) => {
|
|
175
|
+
const cursor = event.target.result;
|
|
176
|
+
if (cursor != null) {
|
|
177
|
+
const value = cursor.value;
|
|
178
|
+
if (valueCbk(value))
|
|
179
|
+
resultObjects.push(value);
|
|
180
|
+
cursor.continue();
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
resolve(resultObjects);
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
cursorRequest.onerror = (event) => reject(event);
|
|
187
|
+
}));
|
|
188
|
+
const result = await Promise.all(promises);
|
|
189
|
+
return result.flat();
|
|
190
|
+
}
|
|
191
|
+
async init() {
|
|
192
|
+
if (this.db == null) {
|
|
193
|
+
this.db = await new Promise((resolve, reject) => {
|
|
194
|
+
const request = window.indexedDB.open(this.storageKey, 1);
|
|
195
|
+
request.onupgradeneeded = (event) => {
|
|
196
|
+
const db = event.target.result;
|
|
197
|
+
const objectStore = db.createObjectStore("swaps", { keyPath: "id" });
|
|
198
|
+
Object.keys(indexes).forEach(name => {
|
|
199
|
+
const index = indexes[name];
|
|
200
|
+
objectStore.createIndex(name, index.key, { unique: index.unique });
|
|
201
|
+
});
|
|
202
|
+
};
|
|
203
|
+
request.onerror = (e) => reject(e);
|
|
204
|
+
request.onsuccess = (e) => resolve(e.target.result);
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Params are specified in the following way:
|
|
210
|
+
* - [[condition1, condition2]] - returns all rows where condition1 AND condition2 is met
|
|
211
|
+
* - [[condition1], [condition2]] - returns all rows where condition1 OR condition2 is met
|
|
212
|
+
* - [[condition1, condition2], [condition3]] - returns all rows where (condition1 AND condition2) OR condition3 is met
|
|
213
|
+
* @param params
|
|
214
|
+
*/
|
|
215
|
+
async query(params) {
|
|
216
|
+
if (params.length === 0)
|
|
217
|
+
return await this.querySingle([]);
|
|
218
|
+
const results = await Promise.all(params.map(singleParam => this.querySingle(singleParam)));
|
|
219
|
+
const resultSet = new Set(results.flat()); //Deduplicate
|
|
220
|
+
return Array.from(resultSet);
|
|
221
|
+
}
|
|
222
|
+
async querySingle(params) {
|
|
223
|
+
if (params.length === 0) {
|
|
224
|
+
return await this.executeTransaction((objectStore) => objectStore.getAll(), true);
|
|
225
|
+
}
|
|
226
|
+
const queryKeys = params.map(param => param.key);
|
|
227
|
+
const requiredIndex = queryKeys.join(", ");
|
|
228
|
+
if (requiredIndex === "id") {
|
|
229
|
+
//ID is the index
|
|
230
|
+
const values = Array.isArray(params[0].value) ? params[0].value : [params[0].value];
|
|
231
|
+
const res = await this.executeTransactionArr((objectStore) => {
|
|
232
|
+
return values.map(val => objectStore.getAll(val));
|
|
233
|
+
}, true);
|
|
234
|
+
return res.flat();
|
|
235
|
+
}
|
|
236
|
+
else if (indexes[requiredIndex] != null) {
|
|
237
|
+
//Index exists
|
|
238
|
+
const values = params.map(param => Array.isArray(param.value) ? param.value : [param.value]);
|
|
239
|
+
const compositeIndexQueries = toCompositeIndex(values);
|
|
240
|
+
const resp = await this.executeTransactionArr(objectStore => {
|
|
241
|
+
const index = objectStore.index(requiredIndex);
|
|
242
|
+
return compositeIndexQueries.map(indexQuery => index.getAll(indexQuery));
|
|
243
|
+
}, true);
|
|
244
|
+
return resp.flat();
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
//Need to go over all values
|
|
248
|
+
this.logger.warn("query(): Index cannot be used for query, required index: " + requiredIndex + " query params: ", params);
|
|
249
|
+
const setConditions = toSetConditions(params);
|
|
250
|
+
return await this.executeTransactionWithCursor(objectStore => [objectStore.openCursor()], (val) => matches(setConditions, val));
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
async remove(object) {
|
|
254
|
+
await this.executeTransaction(store => store.delete(object.id), false)
|
|
255
|
+
.catch(() => null);
|
|
256
|
+
}
|
|
257
|
+
async removeAll(arr) {
|
|
258
|
+
if (arr.length === 0)
|
|
259
|
+
return;
|
|
260
|
+
await this.executeTransactionArr(store => arr.map(object => {
|
|
261
|
+
return store.delete(object.id);
|
|
262
|
+
}), false);
|
|
263
|
+
}
|
|
264
|
+
async save(object) {
|
|
265
|
+
await this.executeTransaction(store => store.put(object), false);
|
|
266
|
+
}
|
|
267
|
+
async saveAll(arr) {
|
|
268
|
+
if (arr.length === 0)
|
|
269
|
+
return;
|
|
270
|
+
await this.executeTransactionArr(store => arr.map(object => {
|
|
271
|
+
return store.put(object);
|
|
272
|
+
}), false);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
exports.IndexedDBUnifiedStorage = IndexedDBUnifiedStorage;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { IStorageManager, StorageObject } from "@atomiqlabs/base";
|
|
2
2
|
/**
|
|
3
3
|
* StorageManager using browser's local storage API
|
|
4
|
+
* @category Storage
|
|
4
5
|
*/
|
|
5
6
|
export declare class LocalStorageManager<T extends StorageObject> implements IStorageManager<T> {
|
|
6
7
|
storageKey: string;
|
|
@@ -3,10 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.LocalStorageManager = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* StorageManager using browser's local storage API
|
|
6
|
+
* @category Storage
|
|
6
7
|
*/
|
|
7
8
|
class LocalStorageManager {
|
|
8
9
|
constructor(storageKey) {
|
|
9
|
-
this.rawData =
|
|
10
|
+
this.rawData = {};
|
|
10
11
|
this.data = {};
|
|
11
12
|
this.storageKey = storageKey;
|
|
12
13
|
}
|