Skip to content

Commit

Permalink
fix(bpm): handle getEta in case of error
Browse files Browse the repository at this point in the history
  • Loading branch information
envin3 committed Oct 2, 2023
1 parent e35300a commit 8a05c00
Showing 1 changed file with 35 additions and 27 deletions.
62 changes: 35 additions & 27 deletions src/hooks/use-swap-info.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 8a05c00

Please sign in to comment.