import { useEffect, useState } from 'react' import sumBy from 'lodash/sumBy' import { Connection } from '@solana/web3.js' import mangoStore, { CLUSTER } from '@store/mangoStore' import useInterval from './shared/useInterval' import { formatNumericValue } from 'utils/numbers' import Tooltip from './shared/Tooltip' import { useTranslation } from 'react-i18next' const tpsAlertThreshold = 1000 const tpsWarningThreshold = 1300 const getRecentPerformance = async ( connection: Connection, setTps: (x: number) => void, ) => { try { const samples = 2 const response = await connection.getRecentPerformanceSamples(samples) const totalSecs = sumBy(response, 'samplePeriodSecs') const totalTransactions = sumBy(response, 'numTransactions') const tps = totalTransactions / totalSecs setTps(tps) } catch { console.warn('Unable to fetch TPS') } } const Tps = () => { const { t } = useTranslation('common') const connection = mangoStore((s) => s.connection) const [tps, setTps] = useState(0) useEffect(() => { getRecentPerformance(connection, setTps) }, []) useInterval(() => { getRecentPerformance(connection, setTps) }, 60 * 1000) if (CLUSTER == 'mainnet-beta') { return (
{formatNumericValue(tps, 0)} TPS
) } else { return null } } export default Tps export const StatusDot = ({ status, alert, warning, isLessThan, }: { status: number alert: number warning: number isLessThan?: boolean }) => { const greaterOrLessThan = (status: number, threshold: number) => { if (isLessThan) { return status < threshold } else return status > threshold } const dotColor = isLessThan ? greaterOrLessThan(status, alert) ? 'bg-th-warning' : greaterOrLessThan(status, warning) ? 'bg-th-error' : 'bg-th-success' : greaterOrLessThan(status, warning) ? 'bg-th-error' : greaterOrLessThan(status, alert) ? 'bg-th-warning' : 'bg-th-success' return (
) }