mango-v4-ui/components/stats/perps/PerpMarketsOverviewTable.tsx

453 lines
17 KiB
TypeScript
Raw Normal View History

2023-07-21 05:03:44 -07:00
import { PerpMarket } from '@blockworks-foundation/mango-v4'
2022-12-06 18:47:03 -08:00
import { useTranslation } from 'next-i18next'
import { useViewport } from '../../../hooks/useViewport'
import { COLORS } from '../../../styles/colors'
import { breakpoints } from '../../../utils/theme'
import ContentBox from '../../shared/ContentBox'
2022-12-06 18:47:03 -08:00
import MarketLogos from '@components/trade/MarketLogos'
import { Table, Td, Th, TrBody, TrHead } from '@components/shared/TableElements'
2023-03-27 04:25:23 -07:00
import {
formatFunding,
usePerpFundingRate,
} from '@components/trade/PerpFundingRate'
2023-05-31 18:50:00 -07:00
import { ChevronDownIcon, ChevronRightIcon } from '@heroicons/react/20/solid'
2023-01-24 16:54:24 -08:00
import FormatNumericValue from '@components/shared/FormatNumericValue'
2023-05-31 18:50:00 -07:00
import { getDecimalCount, numberCompacter } from 'utils/numbers'
2023-02-09 21:05:07 -08:00
import Tooltip from '@components/shared/Tooltip'
2023-04-11 20:38:21 -07:00
import { NextRouter, useRouter } from 'next/router'
2023-05-01 19:01:14 -07:00
import SimpleAreaChart from '@components/shared/SimpleAreaChart'
2023-05-31 18:50:00 -07:00
import { Disclosure, Transition } from '@headlessui/react'
import { LinkButton } from '@components/shared/Button'
2023-06-14 17:43:33 -07:00
import SoonBadge from '@components/shared/SoonBadge'
2023-07-21 05:03:44 -07:00
import MarketChange from '@components/shared/MarketChange'
2023-07-21 11:50:06 -07:00
import useThemeWrapper from 'hooks/useThemeWrapper'
2023-07-24 05:14:45 -07:00
import useListedMarketsWithMarketData, {
PerpMarketWithMarketData,
} from 'hooks/useListedMarketsWithMarketData'
import { sortPerpMarkets } from 'utils/markets'
2023-02-09 19:20:46 -08:00
export const goToPerpMarketDetails = (
market: PerpMarket,
2023-07-21 11:47:53 -07:00
router: NextRouter,
) => {
2023-04-13 04:56:18 -07:00
const query = { ...router.query, ['market']: market.name }
router.push({ pathname: router.pathname, query })
2023-04-11 20:38:21 -07:00
}
2023-05-02 10:47:26 -07:00
const PerpMarketsOverviewTable = () => {
2022-12-06 18:47:03 -08:00
const { t } = useTranslation(['common', 'trade'])
2023-07-21 11:50:06 -07:00
const { theme } = useThemeWrapper()
2022-12-06 18:47:03 -08:00
const { width } = useViewport()
const showTableView = width ? width > breakpoints.md : false
2022-12-14 03:01:15 -08:00
const rate = usePerpFundingRate()
2023-04-11 20:38:21 -07:00
const router = useRouter()
2023-07-24 05:14:45 -07:00
const { perpMarketsWithData, isLoading, isFetching } =
useListedMarketsWithMarketData()
2023-07-21 05:03:44 -07:00
const loadingMarketData = isLoading || isFetching
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-05-31 18:50:00 -07:00
<Th className="text-right">{t('rolling-change')}</Th>
2023-07-21 05:03:44 -07:00
<Th className="text-right"></Th>
<Th className="text-right">{t('trade:24h-volume')}</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>
2023-02-09 19:20:46 -08:00
<Th />
2022-12-06 18:47:03 -08:00
</TrHead>
</thead>
<tbody>
2023-07-24 05:14:45 -07:00
{sortPerpMarkets(perpMarketsWithData, 'quote_volume_24h').map(
(market) => {
const symbol = market.name.split('-')[0]
2022-12-06 18:47:03 -08:00
2023-07-24 05:14:45 -07:00
const priceHistory = market?.marketData?.price_history
2022-12-06 18:47:03 -08:00
2023-07-24 05:14:45 -07:00
const volumeData = market?.marketData?.quote_volume_24h
2023-07-21 05:03:44 -07:00
2023-07-24 05:14:45 -07:00
const volume = volumeData ? volumeData : 0
2023-05-31 18:50:00 -07:00
2023-07-24 05:14:45 -07:00
let fundingRate
let fundingRateApr
if (rate.isSuccess) {
const marketRate = rate?.data?.find(
(r) => r.market_index === market.perpMarketIndex,
2023-04-11 20:38:21 -07:00
)
2023-07-24 05:14:45 -07:00
if (marketRate) {
fundingRate = formatFunding.format(
marketRate.funding_rate_hourly,
)
fundingRateApr = formatFunding.format(
marketRate.funding_rate_hourly * 8760,
)
} else {
fundingRate = ''
fundingRateApr = ''
}
2023-04-11 20:38:21 -07:00
} else {
fundingRate = ''
fundingRateApr = ''
}
2022-12-06 18:47:03 -08:00
2023-07-24 05:14:45 -07:00
const openInterest = market.baseLotsToUi(market.openInterest)
const isComingSoon = market.oracleLastUpdatedSlot == 0
const isUp =
priceHistory && priceHistory.length
? market.uiPrice >= priceHistory[0].price
: false
2023-02-01 19:41:32 -08:00
2023-07-24 05:14:45 -07:00
return (
<TrBody
className="default-transition md:hover:cursor-pointer md:hover:bg-th-bkg-2"
key={market.publicKey.toString()}
onClick={() => goToPerpMarketDetails(market, router)}
>
<Td>
<div className="flex items-center">
<MarketLogos market={market} size="large" />
<p className="mr-2 whitespace-nowrap font-body">
{market.name}
</p>
{isComingSoon ? <SoonBadge /> : null}
</div>
</Td>
<Td>
<div className="flex flex-col text-right">
<p>
{market.uiPrice ? (
<FormatNumericValue value={market.uiPrice} isUsd />
) : (
''
)}
</p>
</div>
</Td>
<Td>
<div className="flex flex-col items-end">
<MarketChange market={market} />
</div>
</Td>
<Td>
{!loadingMarketData ? (
priceHistory && priceHistory.length ? (
<div className="h-10 w-24">
<SimpleAreaChart
color={
isUp ? COLORS.UP[theme] : COLORS.DOWN[theme]
}
data={priceHistory.concat([
{
time: new Date().toString(),
price: market.uiPrice,
},
])}
name={symbol}
xKey="time"
yKey="price"
2023-06-14 17:43:33 -07:00
/>
2023-07-24 05:14:45 -07:00
</div>
) : symbol === 'USDC' || symbol === 'USDT' ? null : (
<p className="mb-0 text-th-fgd-4">
{t('unavailable')}
2023-06-14 17:43:33 -07:00
</p>
2023-07-24 05:14:45 -07:00
)
2023-06-14 17:43:33 -07:00
) : (
2023-07-24 05:14:45 -07:00
<div className="h-10 w-[104px] animate-pulse rounded bg-th-bkg-3" />
2023-06-14 17:43:33 -07:00
)}
2023-07-24 05:14:45 -07:00
</Td>
<Td>
<div className="flex flex-col text-right">
<p>
{volume ? `$${numberCompacter.format(volume)}` : '$0'}
</p>
</div>
</Td>
<Td>
<div className="flex items-center justify-end">
{fundingRate !== '' ? (
<Tooltip
content={
<>
{fundingRateApr ? (
<div className="">
The 1hr rate as an APR is{' '}
<span className="font-mono text-th-fgd-2">
{fundingRateApr}
</span>
</div>
) : null}
<div className="mt-2">
Funding is paid continuously. The 1hr rate
displayed is a rolling average of the past 60
mins.
</div>
<div className="mt-2">
When positive, longs will pay shorts and when
negative shorts pay longs.
</div>
</>
}
>
<p className="tooltip-underline">{fundingRate}</p>
</Tooltip>
) : (
<p></p>
)}
</div>
</Td>
<Td>
<div className="flex flex-col text-right">
{openInterest ? (
<>
<p>
<FormatNumericValue
value={openInterest}
decimals={getDecimalCount(market.minOrderSize)}
/>
</p>
<p className="text-th-fgd-4">
$
{numberCompacter.format(
openInterest * market.uiPrice,
)}
</p>
</>
) : (
<>
<p></p>
<p className="text-th-fgd-4"></p>
</>
)}
</div>
</Td>
<Td>
<div className="flex justify-end">
<ChevronRightIcon className="h-5 w-5 text-th-fgd-3" />
</div>
</Td>
</TrBody>
)
},
)}
2022-12-06 18:47:03 -08:00
</tbody>
</Table>
) : (
2023-05-31 18:50:00 -07:00
<div className="border-b border-th-bkg-3">
2023-07-24 05:14:45 -07:00
{sortPerpMarkets(perpMarketsWithData, 'quote_volume_24h').map(
(market) => {
return (
<MobilePerpMarketItem
key={market.publicKey.toString()}
loadingMarketData={loadingMarketData}
market={market}
/>
)
},
)}
2022-12-06 18:47:03 -08:00
</div>
)}
</ContentBox>
)
}
2023-05-02 10:47:26 -07:00
export default PerpMarketsOverviewTable
2022-12-06 18:47:03 -08:00
2023-07-24 05:14:45 -07:00
const MobilePerpMarketItem = ({
market,
loadingMarketData,
}: {
market: PerpMarketWithMarketData
loadingMarketData: boolean
}) => {
2022-12-06 18:47:03 -08:00
const { t } = useTranslation('common')
2023-07-21 11:50:06 -07:00
const { theme } = useThemeWrapper()
2023-04-11 20:38:21 -07:00
const router = useRouter()
2023-05-31 18:50:00 -07:00
const rate = usePerpFundingRate()
2022-12-06 18:47:03 -08:00
2023-07-24 05:14:45 -07:00
const priceHistory = market?.marketData?.price_history
2022-12-06 18:47:03 -08:00
2023-07-24 05:14:45 -07:00
const volumeData = market?.marketData?.quote_volume_24h
2023-07-21 05:03:44 -07:00
2023-07-24 05:14:45 -07:00
const volume = volumeData ? volumeData : 0
2023-07-21 05:03:44 -07:00
const symbol = market.name.split('-')[0]
2023-05-31 18:50:00 -07:00
const openInterest = market.baseLotsToUi(market.openInterest)
2023-06-14 17:43:33 -07:00
const isComingSoon = market.oracleLastUpdatedSlot == 0
2023-07-21 05:03:44 -07:00
const isUp =
priceHistory && priceHistory.length
? market.uiPrice >= priceHistory[0].price
: false
2023-05-31 18:50:00 -07:00
let fundingRate: string
let fundingRateApr: string
if (rate.isSuccess) {
const marketRate = rate?.data?.find(
2023-07-21 11:47:53 -07:00
(r) => r.market_index === market.perpMarketIndex,
2023-05-31 18:50:00 -07:00
)
if (marketRate) {
fundingRate = formatFunding.format(marketRate.funding_rate_hourly)
fundingRateApr = formatFunding.format(
2023-07-21 11:47:53 -07:00
marketRate.funding_rate_hourly * 8760,
2023-05-31 18:50:00 -07:00
)
} else {
fundingRate = ''
fundingRateApr = ''
}
} else {
fundingRate = ''
fundingRateApr = ''
}
2022-12-06 18:47:03 -08:00
return (
2023-05-31 18:50:00 -07:00
<Disclosure>
{({ open }) => (
<>
<Disclosure.Button
className={`w-full border-t border-th-bkg-3 p-4 text-left first:border-t-0 focus:outline-none`}
>
<div className="flex items-center justify-between">
<div className="flex items-center">
<div className="flex flex-shrink-0 items-center">
<MarketLogos market={market} />
</div>
2023-06-14 17:43:33 -07:00
<p className="mr-2 leading-none text-th-fgd-1">{market.name}</p>
{isComingSoon ? <SoonBadge /> : null}
2023-05-31 18:50:00 -07:00
</div>
<div className="flex items-center space-x-3">
2023-07-21 05:03:44 -07:00
{!loadingMarketData ? (
priceHistory && priceHistory.length ? (
2023-05-31 18:50:00 -07:00
<div className="ml-4 h-10 w-20">
<SimpleAreaChart
2023-07-21 05:03:44 -07:00
color={isUp ? COLORS.UP[theme] : COLORS.DOWN[theme]}
data={priceHistory.concat([
{
time: new Date().toString(),
price: market.uiPrice,
},
])}
name={symbol}
xKey="time"
2023-05-31 18:50:00 -07:00
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" />
)}
2023-07-21 05:03:44 -07:00
<MarketChange market={market} />
2023-05-31 18:50:00 -07:00
<ChevronDownIcon
className={`${
open ? 'rotate-180' : 'rotate-360'
} h-6 w-6 flex-shrink-0 text-th-fgd-3`}
2023-02-10 04:55:06 -08:00
/>
</div>
2023-05-31 18:50:00 -07:00
</div>
</Disclosure.Button>
<Transition
enter="transition ease-in duration-200"
enterFrom="opacity-0"
enterTo="opacity-100"
>
<Disclosure.Panel>
<div className="mx-4 grid grid-cols-2 gap-4 border-t border-th-bkg-3 pb-4 pt-4">
2023-05-31 18:50:00 -07:00
<div className="col-span-1">
<p className="text-xs text-th-fgd-3">{t('price')}</p>
<p className="font-mono text-th-fgd-2">
2023-06-14 17:43:33 -07:00
{market.uiPrice ? (
<FormatNumericValue value={market.uiPrice} isUsd />
) : (
''
)}
2023-05-31 18:50:00 -07:00
</p>
</div>
<div className="col-span-1">
2023-05-31 18:50:00 -07:00
<p className="text-xs text-th-fgd-3">
{t('trade:24h-volume')}
</p>
<p className="font-mono text-th-fgd-2">
{volume ? (
<span>{numberCompacter.format(volume)}</span>
) : (
2023-07-21 05:20:12 -07:00
'$0'
2023-05-31 18:50:00 -07:00
)}
</p>
</div>
2023-05-31 18:50:00 -07:00
<div className="col-span-1">
<p className="text-xs text-th-fgd-3">
{t('trade:funding-rate')}
</p>
2023-06-14 17:43:33 -07:00
{fundingRate !== '' ? (
2023-05-31 18:50:00 -07:00
<Tooltip
content={
<>
{fundingRateApr ? (
<div className="">
The 1hr rate as an APR is{' '}
<span className="font-mono text-th-fgd-2">
{fundingRateApr}
</span>
</div>
) : null}
<div className="mt-2">
Funding is paid continuously. The 1hr rate displayed
is a rolling average of the past 60 mins.
</div>
<div className="mt-2">
When positive, longs will pay shorts and when
negative shorts pay longs.
</div>
</>
}
>
2023-06-14 17:43:33 -07:00
<span className="tooltip-underline font-mono text-th-fgd-2">
2023-05-31 18:50:00 -07:00
{fundingRate}
</span>
</Tooltip>
2023-06-14 17:43:33 -07:00
) : (
<p className="text-th-fgd-2"></p>
)}
2023-05-31 18:50:00 -07:00
</div>
<div className="col-span-1">
<p className="text-xs text-th-fgd-3">
{t('trade:open-interest')}
</p>
2023-06-14 17:43:33 -07:00
{openInterest ? (
<p className="font-mono text-th-fgd-2">
<FormatNumericValue
value={openInterest}
decimals={getDecimalCount(market.minOrderSize)}
/>
<span className="mx-1 text-th-fgd-4">|</span>$
{numberCompacter.format(openInterest * market.uiPrice)}
</p>
) : (
<p className="text-th-fgd-2"></p>
)}
2023-05-31 18:50:00 -07:00
</div>
<div className="col-span-1">
<LinkButton
className="flex items-center"
onClick={() => goToPerpMarketDetails(market, router)}
>
{t('token:token-stats', { token: market.name })}
<ChevronRightIcon className="ml-2 h-5 w-5" />
</LinkButton>
</div>
</div>
</Disclosure.Panel>
</Transition>
</>
)}
</Disclosure>
2022-12-06 18:47:03 -08:00
)
}