import { useEffect, useState } from 'react' import styled from '@emotion/styled' import { LineChart, Line, ReferenceLine, XAxis, YAxis, Tooltip } from 'recharts' import useDimensions from 'react-cool-dimensions' import { IDS, MangoClient } from '@blockworks-foundation/mango-client' import { PublicKey, Connection } from '@solana/web3.js' import { DEFAULT_MANGO_GROUP } from '../utils/mango' import FloatingElement from '../components/FloatingElement' import useConnection from '../hooks/useConnection' import TopBar from '../components/TopBar' import Select from '../components/Select' const DECIMALS = { BTC: 4, ETH: 3, USDT: 2, USDC: 2, WUSDT: 2, } const icons = { BTC: '/assets/icons/btc.svg', ETH: '/assets/icons/eth.svg', USDT: '/assets/icons/usdt.svg', USDC: '/assets/icons/usdc.svg', WUSDT: '/assets/icons/usdt.svg', } const ChartLayover = styled.div` position: absolute; top: 0; left: 0; height: 100%; width: 100%; color: #525a6a; ` const ChartWrapper = styled.div` height: 100%; width: 100%; ` const useMangoStats = () => { const [stats, setStats] = useState([ { symbol: '', depositInterest: 0, borrowInterest: 0, totalDeposits: 0, totalBorrows: 0, utilization: '0', }, ]) const [latestStats, setLatestStats] = useState([]) const { cluster } = useConnection() useEffect(() => { const fetchStats = async () => { const response = await fetch( `https://mango-stats.herokuapp.com?mangoGroup=BTC_ETH_USDT` ) const stats = await response.json() setStats(stats) } fetchStats() }, []) useEffect(() => { const getLatestStats = async () => { const client = new MangoClient() const connection = new Connection( IDS.cluster_urls[cluster], 'singleGossip' ) const assets = IDS[cluster].mango_groups?.[DEFAULT_MANGO_GROUP]?.symbols const mangoGroupId = IDS[cluster].mango_groups?.[DEFAULT_MANGO_GROUP]?.mango_group_pk if (!mangoGroupId) return const mangoGroupPk = new PublicKey(mangoGroupId) const mangoGroup = await client.getMangoGroup(connection, mangoGroupPk) const latestStats = Object.keys(assets).map((symbol, index) => { const totalDeposits = mangoGroup.getUiTotalDeposit(index) const totalBorrows = mangoGroup.getUiTotalBorrow(index) return { time: new Date(), symbol, totalDeposits, totalBorrows, depositInterest: mangoGroup.getDepositRate(index) * 100, borrowInterest: mangoGroup.getBorrowRate(index) * 100, utilization: totalDeposits > 0.0 ? totalBorrows / totalDeposits : 0.0, } }) setLatestStats(latestStats) } getLatestStats() }, [cluster]) return { latestStats, stats } } const StatsChart = ({ title, xAxis, yAxis, data, labelFormat }) => { const [mouseData, setMouseData] = useState(null) // @ts-ignore const { observe, width, height } = useDimensions() const handleMouseMove = (coords) => { if (coords.activePayload) { setMouseData(coords.activePayload[0].payload) } } const handleMouseLeave = () => { setMouseData(null) } return (
{title} {mouseData ? `: ${labelFormat(mouseData[yAxis])}` : null}
{mouseData ? (
Date {mouseData ? `: ${new Date(mouseData[xAxis]).toDateString()}` : null}
) : null}
{width > 0 ? ( } /> {mouseData ? ( ) : null} ) : null}
) } export default function StatsPage() { const [selectedAsset, setSelectedAsset] = useState('BTC') const { latestStats, stats } = useMangoStats() const selectedStatsData = stats.filter( (stat) => stat.symbol === selectedAsset ) return (

Mango Stats

{latestStats.map((stat) => ( ))}
Asset Total Deposits Total Borrows Deposit Interest Borrow Interest Utilization
{icons[stat.symbol]} {stat.totalDeposits.toFixed(DECIMALS[stat.symbol])} {stat.totalBorrows.toFixed(DECIMALS[stat.symbol])} {stat.depositInterest.toFixed(2)}% {stat.borrowInterest.toFixed(2)}% {(parseFloat(stat.utilization) * 100).toFixed(2)}%
{selectedAsset ? (
Historical Stats
x.toFixed(DECIMALS[selectedAsset])} />
x.toFixed(DECIMALS[selectedAsset])} />
`${(x * 100).toFixed(5)}%`} />
`${(x * 100).toFixed(5)}%`} />
) : null}
) }