import { I80F48 } from '@blockworks-foundation/mango-client' import Chart from '../Chart' import { tokenPrecision } from '../../utils' import { useViewport } from '../../hooks/useViewport' import { breakpoints } from '../TradePageGrid' import { Table, Td, Th, TrBody, TrHead } from '../TableElements' import { ExpandableRow, Row } from '../TableElements' import { useTranslation } from 'next-i18next' function formatNumberString(x: number, decimals): string { return new Intl.NumberFormat('en-US', { minimumFractionDigits: decimals, maximumFractionDigits: decimals, }).format(x) } const getAverageStats = ( stats, daysAgo: number, symbol: string, type: string ) => { const priorDate = new Date(Date.now() - daysAgo * 24 * 60 * 60 * 1000) const selectedStatsData = stats.filter((s) => s.name === symbol) const timeFilteredStats = selectedStatsData.filter( (d) => new Date(d.time).getTime() >= priorDate.getTime() ) const oldestStat = timeFilteredStats[0] const latestStat = timeFilteredStats[timeFilteredStats.length - 1] const avg = Math.pow(latestStat[type] / oldestStat[type], 365 / daysAgo) * 100 - 100 priorDate.setHours(priorDate.getHours() + 1) if (new Date(oldestStat.hourly).getDate() > priorDate.getDate()) { return '-' } else { return `${avg.toFixed(4)}%` } } export default function StatsTotals({ latestStats, stats }) { const { t } = useTranslation('common') const { width } = useViewport() const isMobile = width ? width < breakpoints.sm : false // get deposit and borrow values from stats const depositValues = [] const borrowValues = [] for (let i = 0; i < stats.length; i++) { const depositValue = stats[i].name === 'USDC' ? stats[i].totalDeposits : stats[i].totalDeposits * stats[i].baseOraclePrice const borrowValue = stats[i].name === 'USDC' ? stats[i].totalBorrows : stats[i].totalBorrows * stats[i].baseOraclePrice if (depositValue) { depositValues.push({ name: stats[i].name, value: depositValue, time: stats[i].hourly, }) } if (borrowValue) { borrowValues.push({ name: stats[i].name, value: borrowValue, time: stats[i].hourly, }) } } const formatValues = (values) => { // get value for each symbol every hour const hours = values.reduce((acc, d) => { const found = acc.find((a) => a.time === d.time && a.name === d.name) const value = { value: d.value, name: d.name, time: d.time, } if (!found) { acc.push(value) } else { found.value = d.value } return acc }, []) // sum the values for each hour const holder = {} hours.forEach(function (d) { if (d.time in holder) { holder[d.time] = holder[d.time] + d.value } else { holder[d.time] = d.value } }) const points = [] for (const prop in holder) { points.push({ time: prop, value: holder[prop] }) } return points } return ( <>
x && '$' + x.toLocaleString(undefined, { maximumFractionDigits: 0 }) } type="area" />
x && '$' + x.toLocaleString(undefined, { maximumFractionDigits: 0 }) } type="area" />
{!isMobile ? ( <>
{t('current-stats')}
{latestStats.length > 0 ? ( {latestStats.map((stat, index) => ( ))}
{t('asset')} {t('total-deposits')} {t('total-borrows')} {t('deposit-rate')} {t('borrow-rate')} {t('utilization')}
{stat.name}
{formatNumberString( stat.totalDeposits, tokenPrecision[stat.name] )} {formatNumberString( stat.totalBorrows, tokenPrecision[stat.name] )} {formatNumberString( stat.depositInterest.toNumber(), 2 )} % {formatNumberString( stat.borrowInterest.toNumber(), 2 )} % {formatNumberString( stat.utilization .mul(I80F48.fromNumber(100)) .toNumber(), 2 )} %
) : ( <>
)}
{t('average-deposit')}
{stats.length > 1 ? ( {latestStats.map((stat, index) => ( ))}
{t('asset')} 24h 7d 30d
{stat.name}
{getAverageStats(stats, 1, stat.name, 'depositIndex')} {getAverageStats(stats, 7, stat.name, 'depositIndex')} {getAverageStats(stats, 30, stat.name, 'depositIndex')}
) : ( <>
)}
{t('average-borrow')}
{stats.length > 1 ? ( {latestStats.map((stat, index) => ( ))}
{t('asset')} 24h 7d 30d
{stat.name}
{getAverageStats(stats, 1, stat.name, 'borrowIndex')} {getAverageStats(stats, 7, stat.name, 'borrowIndex')} {getAverageStats(stats, 30, stat.name, 'borrowIndex')}
) : ( <>
)} ) : ( <>
{t('current-stats')}
{latestStats.map((stat, index) => ( // latestStats.length > 0 ? (
{stat.name}
{t('total-deposits')}
{formatNumberString(stat.totalDeposits, 0)}
{t('total-borrows')}
{formatNumberString(stat.totalBorrows, 0)}
} key={stat.name} index={index} panelTemplate={
{t('deposit-rate')}
{formatNumberString(stat.depositInterest.toNumber(), 2)} %
{t('borrow-rate')}
{formatNumberString(stat.borrowInterest.toNumber(), 2)}%
{t('utilization')}
{formatNumberString( stat.utilization.mul(I80F48.fromNumber(100)).toNumber(), 2 )} %
} /> ))}
{t('average-deposit')}
{stats.length > 1 ? latestStats.map((stat, index) => (
{stat.name}
24h
{getAverageStats(stats, 1, stat.name, 'depositIndex')}
7d
{getAverageStats(stats, 7, stat.name, 'depositIndex')}
30d
{getAverageStats(stats, 30, stat.name, 'depositIndex')}
)) : null}
{t('average-borrow')}
{stats.length > 1 ? latestStats.map((stat, index) => (
{stat.name}
24h
{getAverageStats(stats, 1, stat.name, 'borrowIndex')}
7d
{getAverageStats(stats, 7, stat.name, 'borrowIndex')}
30d
{getAverageStats(stats, 30, stat.name, 'borrowIndex')}
)) : null} )} ) }