Skip to content

Commit

Permalink
fix(bpm): handle getEta to break the dApp 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 989fa06
Showing 1 changed file with 42 additions and 36 deletions.
78 changes: 42 additions & 36 deletions src/hooks/use-swap-info.js
Original file line number Diff line number Diff line change
@@ -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)
}
}

Expand Down

0 comments on commit 989fa06

Please sign in to comment.