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' 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 selectedStatsData = stats.filter((s) => s.name === symbol) const timeFilteredStats = selectedStatsData.filter( (d) => new Date(d.time).getTime() > new Date(Date.now() - daysAgo * 24 * 60 * 60 * 1000).getTime() ) const oldestStat = timeFilteredStats[0] const latestStat = timeFilteredStats[timeFilteredStats.length - 1] const avg = Math.pow(latestStat[type] / oldestStat[type], 365 / daysAgo) * 100 - 100 return avg.toFixed(4) } export default function StatsTotals({ latestStats, stats }) { console.log('latest stats', latestStats) const startTimestamp = 1622905200000 const { width } = useViewport() const isMobile = width ? width < breakpoints.sm : false const trimmedStats = stats.filter( (stat) => new Date(stat.hourly).getTime() >= startTimestamp ) // get deposit and borrow values from stats const depositValues = [] const borrowValues = [] for (let i = 0; i < trimmedStats.length; i++) { const depositValue = trimmedStats[i].name === 'USDC' ? trimmedStats[i].totalDeposits : trimmedStats[i].totalDeposits * trimmedStats[i].baseOraclePrice const borrowValue = trimmedStats[i].name === 'USDC' ? trimmedStats[i].totalBorrows : trimmedStats[i].totalBorrows * trimmedStats[i].baseOraclePrice depositValues.push({ name: trimmedStats[i].name, value: depositValue, time: trimmedStats[i].hourly, }) if (borrowValue) { borrowValues.push({ name: trimmedStats[i].name, value: borrowValue, time: trimmedStats[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 ? ( <>
Current Stats
{latestStats.length > 0 ? ( {latestStats.map((stat, index) => ( ))}
Asset Total Deposits Total Borrows Deposit Rate Borrow Rate 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 )} %
) : ( <>
)}
Average Deposit Rates
{stats.length > 1 ? ( {latestStats.map((stat, index) => ( ))}
Asset 24h 7d 30d
{stat.name}
{getAverageStats(stats, 1, stat.name, 'depositIndex')}% {getAverageStats(stats, 7, stat.name, 'depositIndex')}% {getAverageStats(stats, 30, stat.name, 'depositIndex')}%
) : ( <>
)}
Average Borrow Rates
{stats.length > 1 ? ( {latestStats.map((stat, index) => ( ))}
Asset 24h 7d 30d
{stat.name}
{getAverageStats(stats, 1, stat.name, 'borrowIndex')}% {getAverageStats(stats, 7, stat.name, 'borrowIndex')}% {getAverageStats(stats, 30, stat.name, 'borrowIndex')}%
) : ( <>
)} ) : ( <>
Current Stats
{latestStats.map((stat, index) => ( // latestStats.length > 0 ? (
{stat.name}
Total Deposits
{formatNumberString(stat.totalDeposits, 0)}
Total Borrows
{formatNumberString(stat.totalBorrows, 0)}
} key={stat.name} index={index} panelTemplate={ <>
Deposit Rate
{formatNumberString(stat.depositInterest.toNumber(), 2)} %
Borrow Rate
{formatNumberString(stat.borrowInterest.toNumber(), 2)}%
Utilization
{formatNumberString( stat.utilization.mul(I80F48.fromNumber(100)).toNumber(), 2 )} %
} /> ))}
Average Deposit Rates
{latestStats.map((stat, index) => ( // stats.length > 1 ? (
{stat.name}
24h
{getAverageStats( stats, dailyStartTime, stat.name, 'depositRate' )} %
7d
{getAverageStats( stats, weeklyStartTime, stat.name, 'depositRate' )} %
30d
{getAverageStats( stats, monthlyStartTime, stat.name, 'depositRate' )} %
))}
Average Borrow Rates
{latestStats.map((stat, index) => ( // stats.length > 1 ? (
{stat.name}
24h
{getAverageStats( stats, dailyStartTime, stat.name, 'borrowRate' )} %
7d
{getAverageStats( stats, weeklyStartTime, stat.name, 'borrowRate' )} %
30d
{getAverageStats( stats, monthlyStartTime, stat.name, 'borrowRate' )} %
))} )} ) }