From 989fa068a837b2d2dd25392f77808175f379eeb7 Mon Sep 17 00:00:00 2001 From: eenvin Date: Mon, 2 Oct 2023 08:36:48 +0100 Subject: [PATCH] fix(bpm): handle getEta to break the dApp in case of error --- src/hooks/use-swap-info.js | 78 ++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/src/hooks/use-swap-info.js b/src/hooks/use-swap-info.js index e1052eec..976c236f 100644 --- a/src/hooks/use-swap-info.js +++ b/src/hooks/use-swap-info.js @@ -1,46 +1,52 @@ +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 getEta() { - let fromAsset = from - if (from.requiresCurve) { - fromAsset = getAssetById(from.pTokenId) - } - // ATM, the API returns untrustworthy estimates for EOS-like chains. - // For those chains, assume the sync ETA is 0 if the BPM is > 0 - // as EOS-like chains are usually very fast. - 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 + try { + let fromAsset = from + if (from.requiresCurve) { + fromAsset = getAssetById(from.pTokenId) + } + // ATM, the API returns untrustworthy estimates for EOS-like chains. + // For those chains, assume the sync ETA is 0 if the BPM is > 0 + // as EOS-like chains are usually very fast. + 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 + } + } catch (_err) { + console.error(_err) } }