mango-v4-ui/pages/dashboard/risks.tsx

165 lines
5.4 KiB
TypeScript
Raw Normal View History

2023-05-11 14:33:07 -07:00
import useMangoGroup from 'hooks/useMangoGroup'
import type { NextPage } from 'next'
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
import { DashboardNavbar } from '.'
import { Table, Td, Th, TrBody, TrHead } from '@components/shared/TableElements'
2023-05-14 10:15:22 -07:00
import { useQuery } from '@tanstack/react-query'
2023-05-22 07:50:44 -07:00
import { emptyWallet } from '@store/mangoStore'
import {
MANGO_V4_ID,
MangoClient,
getRiskStats,
} from '@blockworks-foundation/mango-v4'
2023-05-14 10:15:22 -07:00
import { PublicKey } from '@solana/web3.js'
import { formatNumericValue } from 'utils/numbers'
2023-05-22 07:50:44 -07:00
import { AnchorProvider, web3 } from '@project-serum/anchor'
2023-05-11 14:33:07 -07:00
export async function getStaticProps({ locale }: { locale: string }) {
return {
props: {
...(await serverSideTranslations(locale, ['common'])),
},
}
}
type TableRow = {
val: string | number | PublicKey
highlight: boolean
}
2023-05-14 10:15:22 -07:00
type TableData = {
title: string
data: Array<Record<string, TableRow>>
2023-05-14 10:15:22 -07:00
}
const formatValue = (val: string | number | PublicKey) => {
if (val instanceof PublicKey || typeof val === 'object') {
2023-05-14 10:15:22 -07:00
return val.toString()
}
if (typeof val === 'string') {
return val
} else {
return formatNumericValue(val)
}
}
2023-05-11 14:33:07 -07:00
const RiskDashboard: NextPage = () => {
const { group } = useMangoGroup()
2023-05-14 10:15:22 -07:00
const { data } = useQuery(
2023-05-14 10:15:22 -07:00
['risks'],
2023-05-22 07:50:44 -07:00
() => {
const provider = new AnchorProvider(
new web3.Connection(
'https://mango.rpcpool.com/0f9acc0d45173b51bf7d7e09c1e5',
'processed'
),
emptyWallet,
AnchorProvider.defaultOptions()
)
const client = MangoClient.connect(
provider,
'mainnet-beta',
MANGO_V4_ID['mainnet-beta']
)
if (group) {
return getRiskStats(client, group)
}
},
2023-05-14 10:15:22 -07:00
{
2023-05-22 07:29:20 -07:00
cacheTime: 1000 * 60 * 5,
staleTime: 1000 * 60 * 5,
retry: 0,
2023-05-22 07:29:20 -07:00
refetchOnWindowFocus: false,
2023-05-14 10:15:22 -07:00
enabled: !!group,
}
)
console.log('resp', data)
2023-05-11 14:33:07 -07:00
return (
<div className="grid grid-cols-12">
<div className="col-span-12 xl:col-span-8 xl:col-start-3">
<div className="p-8 pb-20 text-th-fgd-1 md:pb-16 xl:p-10">
2023-05-11 14:33:07 -07:00
<h1>Dashboard</h1>
<DashboardNavbar />
2023-05-14 10:15:22 -07:00
{group && data ? (
2023-05-11 14:33:07 -07:00
<div className="mt-4">
2023-05-14 10:15:22 -07:00
{Object.entries(data).map(
([tableType, table]: [string, TableData]) => {
console.log('table', table)
if (!table?.data?.length) return null
2023-05-14 10:15:22 -07:00
return (
<div className="mt-12" key={tableType}>
<div className="mb-4">
<p className="text-th-fgd-4">{table.title}</p>
</div>
<Table>
<thead>
<TrHead className="border">
{Object.keys(table.data[0]).map(
(colName: string) => {
return (
<Th
xBorder
className="text-left"
key={colName}
>
{colName}{' '}
{colName.toLowerCase().includes('fee') ||
colName.toLowerCase().includes('slippage')
? '(bps)'
: ''}
{colName.toLowerCase().includes('assets') ||
colName.toLowerCase().includes('liabs') ||
colName.toLowerCase().includes('equity') ||
colName.toLowerCase().includes('price') ||
colName.toLowerCase().includes('position')
? '($)'
: ''}
2023-05-14 10:15:22 -07:00
</Th>
)
}
)}
</TrHead>
</thead>
<tbody>
{table.data.map((rowData, index: number) => {
return (
<TrBody key={index}>
{Object.values(rowData).map(
(val, idx: number) => {
2023-05-14 10:15:22 -07:00
return (
<Td
xBorder
className={`${
val?.highlight ? 'bg-th-bkg-2' : ''
}`}
key={idx}
>
{formatValue(val?.val)}
2023-05-14 10:15:22 -07:00
</Td>
)
}
)}
</TrBody>
)
})}
</tbody>
</Table>
</div>
)
}
)}
2023-05-11 14:33:07 -07:00
</div>
) : (
'Loading'
)}
</div>
</div>
</div>
)
}
export default RiskDashboard