mango-v4-ui/components/stats/PerpMarketsTable.tsx

291 lines
9.9 KiB
TypeScript
Raw Normal View History

2022-12-06 18:47:03 -08:00
import { PerpMarket } from '@blockworks-foundation/mango-v4'
import { useTranslation } from 'next-i18next'
import { useTheme } from 'next-themes'
import { useViewport } from '../../hooks/useViewport'
2023-02-23 16:28:49 -08:00
import mangoStore from '@store/mangoStore'
2022-12-06 18:47:03 -08:00
import { COLORS } from '../../styles/colors'
import { breakpoints } from '../../utils/theme'
import ContentBox from '../shared/ContentBox'
import Change from '../shared/Change'
import MarketLogos from '@components/trade/MarketLogos'
import dynamic from 'next/dynamic'
import { Table, Td, Th, TrBody, TrHead } from '@components/shared/TableElements'
import { usePerpFundingRate } from '@components/trade/PerpFundingRate'
2022-12-30 03:40:52 -08:00
import { IconButton } from '@components/shared/Button'
import { ChevronRightIcon } from '@heroicons/react/20/solid'
2023-01-24 16:54:24 -08:00
import FormatNumericValue from '@components/shared/FormatNumericValue'
2023-02-01 19:41:32 -08:00
import { getDecimalCount } from 'utils/numbers'
2023-02-09 21:05:07 -08:00
import Tooltip from '@components/shared/Tooltip'
2023-02-23 16:28:49 -08:00
import { PerpStatsItem } from 'types'
2022-12-06 18:47:03 -08:00
const SimpleAreaChart = dynamic(
() => import('@components/shared/SimpleAreaChart'),
{ ssr: false }
)
2023-02-09 19:20:46 -08:00
export const getOneDayPerpStats = (
stats: PerpStatsItem[] | null,
marketName: string
) => {
return stats
? stats
.filter((s) => s.perp_market === marketName)
.filter((f) => {
const seconds = 86400
const dataTime = new Date(f.date_hour).getTime() / 1000
const now = new Date().getTime() / 1000
const limit = now - seconds
return dataTime >= limit
})
.reverse()
: []
}
2022-12-30 03:40:52 -08:00
const PerpMarketsTable = ({
setShowPerpDetails,
}: {
setShowPerpDetails: (x: string) => void
}) => {
2022-12-06 18:47:03 -08:00
const { t } = useTranslation(['common', 'trade'])
const perpMarkets = mangoStore((s) => s.perpMarkets)
2023-02-09 19:20:46 -08:00
const loadingPerpStats = mangoStore((s) => s.perpStats.loading)
const perpStats = mangoStore((s) => s.perpStats.data)
2022-12-06 18:47:03 -08:00
const { theme } = useTheme()
const { width } = useViewport()
const showTableView = width ? width > breakpoints.md : false
2022-12-14 03:01:15 -08:00
const rate = usePerpFundingRate()
2022-12-06 18:47:03 -08:00
return (
<ContentBox hideBorder hidePadding>
{showTableView ? (
<Table>
<thead>
<TrHead>
<Th className="text-left">{t('market')}</Th>
<Th className="text-right">{t('price')}</Th>
2023-02-09 21:05:07 -08:00
<Th className="text-right"></Th>
<Th className="text-right">
<Tooltip content={t('trade:tooltip-stable-price')}>
<span className="tooltip-underline">
{t('trade:stable-price')}
</span>
</Tooltip>
</Th>
2022-12-06 18:47:03 -08:00
<Th className="text-right">{t('trade:funding-rate')}</Th>
<Th className="text-right">{t('trade:open-interest')}</Th>
<Th className="text-right">{t('rolling-change')}</Th>
2023-02-09 19:20:46 -08:00
<Th />
2022-12-06 18:47:03 -08:00
</TrHead>
</thead>
<tbody>
{perpMarkets.map((market) => {
const symbol = market.name.split('-')[0]
2023-02-09 19:20:46 -08:00
const marketStats = getOneDayPerpStats(perpStats, market.name)
2022-12-06 18:47:03 -08:00
2023-02-09 19:20:46 -08:00
const change = marketStats.length
? ((market.uiPrice - marketStats[0].price) /
marketStats[0].price) *
2022-12-06 18:47:03 -08:00
100
: 0
let fundingRate
2023-02-10 04:55:06 -08:00
if (rate.isSuccess) {
2022-12-14 03:01:15 -08:00
const marketRate = rate?.data?.find(
2022-12-06 18:47:03 -08:00
(r) => r.market_index === market.perpMarketIndex
)
2022-12-14 03:01:15 -08:00
fundingRate = marketRate
? `${marketRate.funding_rate_hourly.toFixed(4)}%`
: ''
2022-12-06 18:47:03 -08:00
} else {
fundingRate = ''
}
2023-02-01 19:41:32 -08:00
const openInterest = market.baseLotsToUi(market.openInterest)
2022-12-06 18:47:03 -08:00
return (
<TrBody key={market.publicKey.toString()}>
<Td>
<div className="flex items-center">
<MarketLogos market={market} />
2023-02-09 21:05:07 -08:00
<p className="whitespace-nowrap font-body">
{market.name}
</p>
2022-12-06 18:47:03 -08:00
</div>
</Td>
<Td>
<div className="flex flex-col text-right">
2023-01-24 16:54:24 -08:00
<p>
<FormatNumericValue value={market.uiPrice} isUsd />
</p>
2022-12-06 18:47:03 -08:00
</div>
</Td>
<Td>
2023-02-09 19:20:46 -08:00
{!loadingPerpStats ? (
marketStats.length ? (
2022-12-21 03:07:17 -08:00
<div className="h-10 w-24">
<SimpleAreaChart
color={
change >= 0
? COLORS.UP[theme]
: COLORS.DOWN[theme]
}
2023-02-09 19:20:46 -08:00
data={marketStats}
2022-12-21 03:07:17 -08:00
name={symbol}
2023-02-09 19:20:46 -08:00
xKey="date_hour"
yKey="price"
2022-12-21 03:07:17 -08:00
/>
</div>
2022-12-06 18:47:03 -08:00
) : symbol === 'USDC' || symbol === 'USDT' ? null : (
<p className="mb-0 text-th-fgd-4">{t('unavailable')}</p>
)
) : (
<div className="h-10 w-[104px] animate-pulse rounded bg-th-bkg-3" />
)}
</Td>
2023-02-09 21:05:07 -08:00
<Td>
<div className="flex flex-col text-right">
<p>
<FormatNumericValue
value={market.stablePriceModel.stablePrice}
isUsd
/>
</p>
</div>
</Td>
2022-12-06 18:47:03 -08:00
<Td>
<div className="flex flex-col text-right">
<p>{fundingRate}</p>
</div>
</Td>
<Td>
<div className="flex flex-col text-right">
2022-12-20 00:16:05 -08:00
<p>
2023-02-01 19:41:32 -08:00
<FormatNumericValue
value={openInterest}
decimals={getDecimalCount(market.minOrderSize)}
/>
2022-12-20 00:16:05 -08:00
</p>
<p className="text-xs text-th-fgd-4">
2023-01-24 16:54:24 -08:00
<FormatNumericValue
2023-02-01 19:41:32 -08:00
value={openInterest * market.uiPrice}
2023-01-24 16:54:24 -08:00
isUsd
/>
2022-12-20 00:16:05 -08:00
</p>
2022-12-06 18:47:03 -08:00
</div>
</Td>
<Td>
<div className="flex flex-col items-end">
<Change change={change} suffix="%" />
</div>
</Td>
2022-12-30 03:40:52 -08:00
<Td>
<div className="flex justify-end">
<IconButton
onClick={() => setShowPerpDetails(market.name)}
size="small"
>
<ChevronRightIcon className="h-5 w-5" />
</IconButton>
</div>
</Td>
2022-12-06 18:47:03 -08:00
</TrBody>
)
})}
</tbody>
</Table>
) : (
<div>
{perpMarkets.map((market) => {
return (
<MobilePerpMarketItem
key={market.publicKey.toString()}
market={market}
2023-02-10 04:55:06 -08:00
setShowPerpDetails={setShowPerpDetails}
2022-12-06 18:47:03 -08:00
/>
)
})}
</div>
)}
</ContentBox>
)
}
export default PerpMarketsTable
2023-02-10 04:55:06 -08:00
const MobilePerpMarketItem = ({
market,
setShowPerpDetails,
}: {
market: PerpMarket
setShowPerpDetails: (x: string) => void
}) => {
2022-12-06 18:47:03 -08:00
const { t } = useTranslation('common')
2023-02-09 19:20:46 -08:00
const loadingPerpStats = mangoStore((s) => s.perpStats.loading)
const perpStats = mangoStore((s) => s.perpStats.data)
2022-12-06 18:47:03 -08:00
const { theme } = useTheme()
// const rate = usePerpFundingRate()
const symbol = market.name.split('-')[0]
2023-02-09 19:20:46 -08:00
const marketStats = getOneDayPerpStats(perpStats, market.name)
2022-12-06 18:47:03 -08:00
2023-02-09 19:20:46 -08:00
const change = marketStats.length
? ((market.uiPrice - marketStats[0].price) / marketStats[0].price) * 100
2022-12-06 18:47:03 -08:00
: 0
// let fundingRate
// if (
// rate.isSuccess
// // && bids instanceof BookSide &&
// // asks instanceof BookSide
// ) {
// const marketRate = rate.data.find(
// (r) => r.market_index === market.perpMarketIndex
// )
// fundingRate = `${marketRate?.funding_apr.toFixed(2)}%`
// } else {
// fundingRate = ''
// }
return (
<div className="border-b border-th-bkg-3 px-6 py-4">
<div className="flex items-center justify-between">
<div className="flex items-center">
<MarketLogos market={market} />
<div>
<p className="text-th-fgd-1">{market.name}</p>
<div className="flex items-center space-x-3">
<p className="font-mono">
2023-01-24 16:54:24 -08:00
<FormatNumericValue value={market.uiPrice} isUsd />
2022-12-06 18:47:03 -08:00
</p>
<Change change={change} suffix="%" />
</div>
</div>
2023-02-10 04:55:06 -08:00
{!loadingPerpStats ? (
marketStats.length ? (
<div className="ml-4 h-10 w-24">
<SimpleAreaChart
color={change >= 0 ? COLORS.UP[theme] : COLORS.DOWN[theme]}
data={marketStats}
name={market.name}
xKey="date_hour"
yKey="price"
/>
</div>
) : symbol === 'USDC' || symbol === 'USDT' ? null : (
<p className="mb-0 text-th-fgd-4">{t('unavailable')}</p>
)
) : (
<div className="h-10 w-[104px] animate-pulse rounded bg-th-bkg-3" />
)}
2022-12-06 18:47:03 -08:00
</div>
2023-02-10 04:55:06 -08:00
<IconButton
onClick={() => setShowPerpDetails(market.name)}
size="medium"
>
<ChevronRightIcon className="h-5 w-5" />
</IconButton>
2022-12-06 18:47:03 -08:00
</div>
</div>
)
}