From 8a05c004cafd51c11cced3d2d9caee30fcf08ea8 Mon Sep 17 00:00:00 2001 From: eenvin Date: Mon, 2 Oct 2023 08:36:48 +0100 Subject: [PATCH] fix(bpm): handle getEta in case of error --- src/hooks/use-swap-info.js | 62 +++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/src/hooks/use-swap-info.js b/src/hooks/use-swap-info.js index e1052eec..32f2b9d7 100644 --- a/src/hooks/use-swap-info.js +++ b/src/hooks/use-swap-info.js @@ -1,11 +1,42 @@ +import { chainIdToTypeMap, BlockchainType } from 'ptokens-constants' import { useMemo } from 'react' + +import { getAssetById } from '../store/swap/swap.selectors' import { getPeginOrPegoutMinutesEstimationByBlockchainAndEta } from '../utils/estimations' import { getFormattedFees } from '../utils/fee' -import { getAssetById } from '../store/swap/swap.selectors' -import { chainIdToTypeMap, BlockchainType } from 'ptokens-constants' const useSwapInfo = ({ from, to, amount, bpm, swappersBalances, fees }) => { return useMemo(() => { + function getBpmInfo(chainId, symbol, isNative, eosLikeChainIds) { + try { + if (!isNative) { + const selectedBpm = Object.values(bpm).find( + (_el) => _el.bridgeName.includes(`${symbol.toLowerCase()}-on-`) && _el.hostChainId === chainId + ) + return selectedBpm + ? eosLikeChainIds.includes(chainId) + ? selectedBpm.bpmMedianHost > 0 + ? 0 + : -1 + : selectedBpm.estimatedHostSyncTime + : -2 + } else { + const selectedBpm = Object.values(bpm).find( + (_el) => _el.bridgeName.includes(`${symbol.toLowerCase()}-on-`) && _el.nativeChainId === chainId + ) + return selectedBpm + ? eosLikeChainIds.includes(chainId) + ? selectedBpm.bpmMedianNative > 0 + ? 0 + : -1 + : selectedBpm.estimatedNativeSyncTime + : -2 + } + } catch (_err) { + console.error(_err) + } + } + function getEta() { let fromAsset = from if (from.requiresCurve) { @@ -17,31 +48,8 @@ const useSwapInfo = ({ from, to, amount, bpm, swappersBalances, fees }) => { const eosLikeChainIds = [...chainIdToTypeMap] .filter(([_k, _v]) => _v === BlockchainType.EOSIO) .map(([_id]) => _id) - if (!fromAsset.isNative) { - const selectedBpm = Object.values(bpm).find( - (_el) => - _el.bridgeName.includes(`${fromAsset.symbol.toLowerCase()}-on-`) && _el.hostChainId === fromAsset.chainId - ) - return selectedBpm - ? eosLikeChainIds.includes(fromAsset.chainId) - ? selectedBpm.bpmMedianHost > 0 - ? 0 - : -1 - : selectedBpm.estimatedHostSyncTime - : -2 - } else { - const selectedBpm = Object.values(bpm).find( - (_el) => - _el.bridgeName.includes(`${fromAsset.symbol.toLowerCase()}-on-`) && _el.nativeChainId === fromAsset.chainId - ) - return selectedBpm - ? eosLikeChainIds.includes(fromAsset.chainId) - ? selectedBpm.bpmMedianNative > 0 - ? 0 - : -1 - : selectedBpm.estimatedNativeSyncTime - : -2 - } + + return getBpmInfo(fromAsset.chainId, fromAsset.symbol, fromAsset.isNative, eosLikeChainIds) } if (!from || !to) {