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) } export default function StatsTotals({ latestStats, stats }) { 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 } const dailyStartTime = new Date( Date.now() - 1 * 24 * 60 * 60 * 1000 ).getTime() const weeklyStartTime = new Date( Date.now() - 7 * 24 * 60 * 60 * 1000 ).getTime() const monthlyStartTime = new Date( Date.now() - 30 * 24 * 60 * 60 * 1000 ).getTime() const getAverageStats = (stats, startFrom, symbol, type) => { const selectedStatsData = stats.filter((s) => s.name === symbol) const timeFilteredStats = selectedStatsData.filter( (d) => new Date(d.time).getTime() > startFrom ) const sum = timeFilteredStats.map((s) => s[type]).reduce((a, b) => a + b, 0) const avg = sum / timeFilteredStats.length || 0 return (avg * 100).toFixed(4) } 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, dailyStartTime, stat.name, 'depositRate' )} % {getAverageStats( stats, weeklyStartTime, stat.name, 'depositRate' )} % {getAverageStats( stats, monthlyStartTime, stat.name, 'depositRate' )} %
) : ( <>
)}
Average Borrow Rates
{stats.length > 1 ? ( {latestStats.map((stat, index) => ( ))}
Asset 24h 7d 30d
{stat.name}
{getAverageStats( stats, dailyStartTime, stat.name, 'borrowRate' )} % {getAverageStats( stats, weeklyStartTime, stat.name, 'borrowRate' )} % {getAverageStats( stats, monthlyStartTime, stat.name, 'borrowRate' )} %
) : ( <>
)} ) : ( <>
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' )} %
) : ( <>
) )} )} ) }