2023-02-09 16:38:20 -08:00
|
|
|
import { useTranslation } from 'next-i18next'
|
|
|
|
import { useMemo, useState } from 'react'
|
|
|
|
import dynamic from 'next/dynamic'
|
2023-02-23 16:28:49 -08:00
|
|
|
import mangoStore from '@store/mangoStore'
|
|
|
|
import { PerpStatsItem } from 'types'
|
2023-02-09 16:38:20 -08:00
|
|
|
const DetailedAreaChart = dynamic(
|
|
|
|
() => import('@components/shared/DetailedAreaChart'),
|
|
|
|
{ ssr: false }
|
|
|
|
)
|
|
|
|
|
|
|
|
interface OiValueItem {
|
|
|
|
date: string
|
|
|
|
openInterest: number
|
|
|
|
}
|
|
|
|
|
|
|
|
interface FeeValueItem {
|
|
|
|
date: string
|
|
|
|
feeValue: number
|
|
|
|
}
|
|
|
|
|
|
|
|
const MangoPerpStatsCharts = () => {
|
|
|
|
const { t } = useTranslation(['common', 'token', 'trade'])
|
|
|
|
const loadingPerpStats = mangoStore((s) => s.perpStats.loading)
|
|
|
|
const perpStats = mangoStore((s) => s.perpStats.data)
|
|
|
|
const [oiDaysToShow, setOiDaysToShow] = useState('30')
|
|
|
|
const [feesDaysToShow, setFeesDaysToShow] = useState('30')
|
|
|
|
// const perpMarkets = mangoStore((s) => s.perpMarkets)
|
|
|
|
|
|
|
|
// const currentTotalOpenInterestValue = useMemo(() => {
|
|
|
|
// if (!perpMarkets.length) return 0
|
|
|
|
// return perpMarkets.reduce((a: number, c: PerpMarket) => {
|
|
|
|
// const value = a + c.openInterest.toNumber() * c.uiPrice
|
|
|
|
// return value
|
|
|
|
// }, 0)
|
|
|
|
// }, [perpMarkets])
|
|
|
|
|
|
|
|
const totalFeeValues = useMemo(() => {
|
2023-02-09 19:20:46 -08:00
|
|
|
if (!perpStats || !perpStats.length) return []
|
|
|
|
const values = perpStats.reduce((a: FeeValueItem[], c: PerpStatsItem) => {
|
|
|
|
const hasDate = a.find((d: FeeValueItem) => d.date === c.date_hour)
|
2023-02-09 16:38:20 -08:00
|
|
|
if (!hasDate) {
|
|
|
|
a.push({
|
|
|
|
date: c.date_hour,
|
|
|
|
feeValue: c.fees_accrued,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
hasDate.feeValue = hasDate.feeValue + c.fees_accrued
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}, [])
|
|
|
|
return values.reverse()
|
|
|
|
}, [perpStats])
|
|
|
|
|
|
|
|
const totalOpenInterestValues = useMemo(() => {
|
2023-02-09 19:20:46 -08:00
|
|
|
if (!perpStats || !perpStats.length) return []
|
|
|
|
const values = perpStats.reduce((a: OiValueItem[], c: PerpStatsItem) => {
|
|
|
|
const hasDate = a.find((d: OiValueItem) => d.date === c.date_hour)
|
2023-02-09 16:38:20 -08:00
|
|
|
if (!hasDate) {
|
|
|
|
a.push({
|
|
|
|
date: c.date_hour,
|
|
|
|
openInterest: Math.floor(c.open_interest * c.price),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
hasDate.openInterest =
|
|
|
|
hasDate.openInterest + Math.floor(c.open_interest * c.price)
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}, [])
|
|
|
|
return values.reverse()
|
|
|
|
}, [perpStats])
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-02-10 04:55:06 -08:00
|
|
|
{totalOpenInterestValues.length ? (
|
2023-02-09 16:38:20 -08:00
|
|
|
<div className="col-span-2 border-b border-th-bkg-3 py-4 px-6 md:col-span-1">
|
|
|
|
<DetailedAreaChart
|
2023-02-10 04:55:06 -08:00
|
|
|
data={totalOpenInterestValues}
|
2023-02-09 16:38:20 -08:00
|
|
|
daysToShow={oiDaysToShow}
|
|
|
|
setDaysToShow={setOiDaysToShow}
|
|
|
|
heightClass="h-64"
|
|
|
|
loading={loadingPerpStats}
|
|
|
|
loaderHeightClass="h-[350px]"
|
|
|
|
prefix="$"
|
|
|
|
tickFormat={(x) => `$${Math.floor(x)}`}
|
|
|
|
title={t('trade:open-interest')}
|
|
|
|
xKey="date"
|
|
|
|
yKey={'openInterest'}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : null}
|
2023-02-10 04:55:06 -08:00
|
|
|
{totalFeeValues.length ? (
|
2023-02-09 16:38:20 -08:00
|
|
|
<div className="col-span-2 border-b border-th-bkg-3 py-4 px-6 md:col-span-1 md:border-l md:pl-6">
|
|
|
|
<DetailedAreaChart
|
2023-02-10 04:55:06 -08:00
|
|
|
data={totalFeeValues}
|
2023-02-09 16:38:20 -08:00
|
|
|
daysToShow={feesDaysToShow}
|
|
|
|
setDaysToShow={setFeesDaysToShow}
|
|
|
|
heightClass="h-64"
|
|
|
|
loading={loadingPerpStats}
|
|
|
|
loaderHeightClass="h-[350px]"
|
|
|
|
prefix="$"
|
|
|
|
tickFormat={(x) => `$${x.toFixed(2)}`}
|
|
|
|
title="Perp Fees"
|
|
|
|
xKey="date"
|
|
|
|
yKey={'feeValue'}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default MangoPerpStatsCharts
|