Skip to content

Commit

Permalink
Merge pull request #2673 from nervosnetwork/fix-fee-rate-api-error
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith-CY authored May 19, 2023
2 parents 91ed812 + e29efd9 commit 7a640f2
Showing 1 changed file with 22 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useState, useEffect, useCallback } from 'react'
import { getFeeRateStats } from 'services/chain'
import { AppActions, StateDispatch, useDispatch } from 'states'
import { MEDIUM_FEE_RATE } from 'utils/const'
import { MEDIUM_FEE_RATE, METHOD_NOT_FOUND } from 'utils/const'

type CountdownOptions = {
seconds?: number
Expand All @@ -15,34 +14,29 @@ const useGetCountDownAndFeeRateStats = ({ seconds = 30, interval = 1000 }: Count
median?: string
suggestFeeRate: number
}>({ suggestFeeRate: MEDIUM_FEE_RATE })
const dispatch = useDispatch()

const handleGetFeeRateStatis = useCallback(
(stateDispatch: StateDispatch) => {
getFeeRateStats()
.then(res => {
const { mean, median } = res ?? {}
const suggested = mean && median ? Math.max(1000, Number(mean), Number(median)) : MEDIUM_FEE_RATE
const handleGetFeeRateStatis = useCallback(() => {
getFeeRateStats()
.then(res => {
const { mean, median } = res ?? {}
const suggested = mean && median ? Math.max(1000, Number(mean), Number(median)) : MEDIUM_FEE_RATE

setFeeFatestatsData(states => ({ ...states, ...res, suggestFeeRate: suggested }))
})
.catch((err: Error & { response?: { status: number } }) => {
setFeeFatestatsData(states => ({ ...states, ...res, suggestFeeRate: suggested }))
})
.catch((err: Error & { response?: { status: number } }) => {
try {
if (err?.response?.status === 404) {
setFeeFatestatsData(states => ({ ...states, suggestFeeRate: MEDIUM_FEE_RATE }))
} else {
stateDispatch({
type: AppActions.AddNotification,
payload: {
type: 'alert',
timestamp: +new Date(),
content: err.message,
},
})
throw new Error('method not found')
}
})
},
[getFeeRateStats, setFeeFatestatsData]
)
const errMsg = JSON.parse(err.message)
if (errMsg?.code === METHOD_NOT_FOUND) {
throw new Error('method not found')
}
} catch (error) {
setFeeFatestatsData(states => ({ ...states, suggestFeeRate: MEDIUM_FEE_RATE }))
}
})
}, [])

useEffect(() => {
const countInterval = setInterval(() => {
Expand All @@ -56,9 +50,9 @@ const useGetCountDownAndFeeRateStats = ({ seconds = 30, interval = 1000 }: Count

useEffect(() => {
if (countDown === seconds) {
handleGetFeeRateStatis(dispatch)
handleGetFeeRateStatis()
}
}, [countDown, seconds, dispatch])
}, [countDown, seconds])

return { countDown, ...feeFatestatsData }
}
Expand Down

1 comment on commit 7a640f2

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packaging for test is done in 5022179565

Please sign in to comment.