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 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.time).getTime() > priorDate.getTime()) { return '-' } else { return `${avg.toFixed(4)}%` } } 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 } return ( <>
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 )} % |
---|
Asset | 24h | 7d | 30d |
{stat.name}
|
{getAverageStats(stats, 1, stat.name, 'depositIndex')} | {getAverageStats(stats, 7, stat.name, 'depositIndex')} | {getAverageStats(stats, 30, stat.name, 'depositIndex')} |
---|
Asset | 24h | 7d | 30d |
{stat.name}
|
{getAverageStats(stats, 1, stat.name, 'borrowIndex')} | {getAverageStats(stats, 7, stat.name, 'borrowIndex')} | {getAverageStats(stats, 30, stat.name, 'borrowIndex')} |
---|