import { useEffect, useState } from 'react' import sumBy from 'lodash/sumBy' import useInterval from '../hooks/useInterval' import { SECONDS } from '../stores/useMangoStore' import { useTranslation } from 'next-i18next' import { ExclamationIcon } from '@heroicons/react/outline' import { Connection } from '@solana/web3.js' const tpsAlertThreshold = 1000 const tpsWarningThreshold = 1300 const connection = new Connection('https://mango.genesysgo.net') const getRecentPerformance = async (setShow, setTps) => { try { const samples = 2 const response = await connection.getRecentPerformanceSamples(samples) const totalSecs = sumBy(response, 'samplePeriodSecs') const totalTransactions = sumBy(response, 'numTransactions') const tps = totalTransactions / totalSecs if (tps < tpsWarningThreshold) { setShow(true) setTps(tps) } else { setShow(false) } } catch { console.log('Unable to fetch TPS') } } const GlobalNotification = () => { const [show, setShow] = useState(false) const [tps, setTps] = useState(0) const { t } = useTranslation('common') useEffect(() => { getRecentPerformance(setShow, setTps) }, []) useInterval(() => { getRecentPerformance(setShow, setTps) }, 45 * SECONDS) if (show) { return (
{tps < 50 ? ( {t('solana-down')} ) : ( {t('degraded-performance')} )}
TPS:{' '} {tps?.toLocaleString(undefined, { maximumFractionDigits: 0, })}
) } else { return null } } export default GlobalNotification