Skip to content

Commit

Permalink
refactor: network alert config
Browse files Browse the repository at this point in the history
  • Loading branch information
moritzkirstein committed Mar 15, 2024
1 parent 1625516 commit 5ebc573
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
12 changes: 9 additions & 3 deletions app.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,14 @@ module.exports = {
// Display alert banner for the developer preview deployment
showPreviewAlert: process.env.NEXT_PUBLIC_SHOW_PREVIEW_ALERT || 'false',

// Show network alerts based on chainId
networkAlertApi: {
100: 'https://status.genx.delta-dao.com/api/check-blocks'
networkAlertConfig: {
// Refresh interval for network status - 30 sec
refreshInterval: 30000,
// Margin of error for block count (how much difference between min / max block numbers before showing an alert)
errorMargin: 10,
// Map chainIds to their respective status endpoints
statusEndpoints: {
100: 'https://status.genx.delta-dao.com/api/check-blocks'
}
}
}
11 changes: 9 additions & 2 deletions src/@context/MarketMetadata/_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,15 @@ export interface AppConfig {
roughTxGasEstimate: number
}
showPreviewAlert: string
networkAlertApi: {
[chainId: number]: string
networkAlertConfig: {
// Refresh interval for network status - 30 sec
refreshInterval: number
// Margin of error for block count (how much difference between min / max block numbers before showing an alert)
errorMargin: number
// Map chainIds to their respective status endpoints
statusEndpoints: {
[chainId: number]: string
}
}
}
export interface SiteContent {
Expand Down
15 changes: 6 additions & 9 deletions src/components/@shared/NetworkStatus/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,18 @@ export default function NetworkStatus({
}: {
className?: string
}): ReactElement {
const [showNetworkAlert, setShowNetworkAlert] = useState(false)
const [showNetworkAlert, setShowNetworkAlert] = useState(true)
const [network, setNetwork] = useState<string>()
const { appConfig } = useMarketMetadata()
const { chain } = useNetwork()

// Refresh interval for network status - 30 sec
const refreshInterval = 30000
// Margin of error for block count (how much difference between min / max block numbers before showing an alert)
const errorMargin = 5
const { networkAlertConfig } = appConfig

const fetchNetworkStatus = useCallback(
async (chainId: number) => {
if (!chainId) return
setNetwork(chain?.name)
const apiEndpoint = appConfig.networkAlertApi[chainId]
const apiEndpoint = networkAlertConfig.statusEndpoints[chainId]
if (!apiEndpoint) return
LoggerInstance.log(`[NetworkStatus] retrieving network status`, {
apiEndpoint
Expand All @@ -39,7 +36,7 @@ export default function NetworkStatus({
if (!minBlock || block < minBlock) minBlock = block
if (!maxBlock || block > maxBlock) maxBlock = block
})
const hasError = maxBlock - minBlock > errorMargin
const hasError = maxBlock - minBlock > networkAlertConfig.errorMargin
setShowNetworkAlert(hasError)
LoggerInstance.log(`[NetworkStatus] network status updated:`, {
minBlock,
Expand All @@ -53,7 +50,7 @@ export default function NetworkStatus({
)
}
},
[appConfig.networkAlertApi, chain]
[networkAlertConfig, chain]
)

useEffect(() => {
Expand All @@ -64,7 +61,7 @@ export default function NetworkStatus({
// init periodic refresh for network status
const networkStatusInterval = setInterval(
() => fetchNetworkStatus(chain?.id),
refreshInterval
networkAlertConfig.refreshInterval
)

return () => {
Expand Down

0 comments on commit 5ebc573

Please sign in to comment.