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

357 lines
13 KiB
TypeScript
Raw Normal View History

2022-10-08 04:37:08 -07:00
import { Serum3Market } from '@blockworks-foundation/mango-v4'
import { useTranslation } from 'next-i18next'
import { useTheme } from 'next-themes'
import { useMemo } from 'react'
2022-10-08 04:37:08 -07:00
import { useViewport } from '../../hooks/useViewport'
import mangoStore from '@store/mangoStore'
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'
2022-11-18 11:11:06 -08:00
import useMangoGroup from 'hooks/useMangoGroup'
2022-11-20 02:44:14 -08:00
import { Table, Td, Th, TrBody, TrHead } from '@components/shared/TableElements'
2023-01-24 16:54:24 -08:00
import FormatNumericValue from '@components/shared/FormatNumericValue'
2023-04-03 04:01:17 -07:00
import { useBirdeyeMarketPrices } from 'hooks/useBirdeyeMarketPrices'
2023-05-31 05:15:50 -07:00
import { floorToDecimal, getDecimalCount, numberCompacter } from 'utils/numbers'
2023-05-01 19:01:14 -07:00
import SimpleAreaChart from '@components/shared/SimpleAreaChart'
2023-05-31 05:15:50 -07:00
import { useQuery } from '@tanstack/react-query'
import { fetchSpotVolume } from '@components/trade/AdvancedMarketHeader'
import { TickerData } from 'types'
import { Disclosure, Transition } from '@headlessui/react'
import { ChevronDownIcon } from '@heroicons/react/20/solid'
2022-10-08 04:37:08 -07:00
const SpotMarketsTable = () => {
const { t } = useTranslation('common')
2022-11-20 12:32:38 -08:00
const { group } = useMangoGroup()
2022-10-08 04:37:08 -07:00
const serumMarkets = mangoStore((s) => s.serumMarkets)
const { theme } = useTheme()
const { width } = useViewport()
const showTableView = width ? width > breakpoints.md : false
2023-04-03 04:01:17 -07:00
const { data: birdeyePrices, isLoading: loadingPrices } =
useBirdeyeMarketPrices()
2022-10-08 04:37:08 -07:00
2023-05-31 05:15:50 -07:00
const { data: spotVolumeData } = useQuery(
['spot-market-volume'],
() => fetchSpotVolume(),
{
cacheTime: 1000 * 60 * 10,
staleTime: 1000 * 60,
retry: 3,
refetchOnWindowFocus: false,
}
)
2022-10-08 04:37:08 -07:00
return (
<ContentBox hideBorder hidePadding>
{showTableView ? (
2022-11-20 02:44:14 -08:00
<Table>
2022-10-08 04:37:08 -07:00
<thead>
2022-11-20 02:44:14 -08:00
<TrHead>
<Th className="text-left">{t('market')}</Th>
<Th className="text-right">{t('price')}</Th>
2023-05-31 05:15:50 -07:00
<Th className="hidden text-right md:block"></Th>
2022-11-20 02:44:14 -08:00
<Th className="text-right">{t('rolling-change')}</Th>
2023-05-31 05:15:50 -07:00
<Th className="text-right">{t('trade:24h-volume')}</Th>
2022-11-20 02:44:14 -08:00
</TrHead>
2022-10-08 04:37:08 -07:00
</thead>
<tbody>
2023-03-11 01:01:47 -08:00
{serumMarkets
.slice()
.sort((a, b) => a.name.localeCompare(b.name))
2023-04-25 05:41:23 -07:00
.map((mkt) => {
const baseBank = group?.getFirstBankByTokenIndex(
mkt.baseTokenIndex
2023-03-11 01:01:47 -08:00
)
2023-04-25 05:41:23 -07:00
const quoteBank = group?.getFirstBankByTokenIndex(
mkt.quoteTokenIndex
)
const market = group?.getSerum3ExternalMarket(
mkt.serumMarketExternal
)
let price
if (baseBank && market && quoteBank) {
price = floorToDecimal(
baseBank.uiPrice / quoteBank.uiPrice,
getDecimalCount(market.tickSize)
).toNumber()
}
2023-05-31 05:15:50 -07:00
let tickerData: TickerData | undefined
if (spotVolumeData && spotVolumeData.length) {
tickerData = spotVolumeData.find(
(m: TickerData) => m.ticker_id === mkt.name
)
}
2022-10-08 04:37:08 -07:00
2023-04-03 04:01:17 -07:00
const birdeyeData = birdeyePrices.find(
2023-04-25 05:41:23 -07:00
(m) => m.mint === mkt.serumMarketExternal.toString()
2023-03-11 01:01:47 -08:00
)
2022-10-08 04:37:08 -07:00
2023-03-11 01:01:47 -08:00
const change =
2023-04-25 05:41:23 -07:00
birdeyeData && price
? ((price - birdeyeData.data[0].value) /
2023-04-03 04:01:17 -07:00
birdeyeData.data[0].value) *
2023-03-11 01:01:47 -08:00
100
: 0
2023-04-03 04:01:17 -07:00
const chartData = birdeyeData ? birdeyeData.data : undefined
2022-10-08 04:37:08 -07:00
2023-03-11 01:01:47 -08:00
return (
2023-04-25 05:41:23 -07:00
<TrBody key={mkt.publicKey.toString()}>
2023-03-11 01:01:47 -08:00
<Td>
<div className="flex items-center">
2023-04-25 05:41:23 -07:00
<MarketLogos market={mkt} />
<p className="font-body">{mkt.name}</p>
2023-03-11 01:01:47 -08:00
</div>
</Td>
<Td>
<div className="flex flex-col text-right">
<p>
2023-04-25 05:41:23 -07:00
{price ? (
2023-06-15 04:24:54 -07:00
<>
<FormatNumericValue
value={price}
isUsd={quoteBank?.name === 'USDC'}
/>{' '}
{quoteBank?.name !== 'USDC' ? (
<span className="font-body text-th-fgd-4">
{quoteBank?.name}
</span>
) : null}
</>
2023-03-11 01:01:47 -08:00
) : (
''
)}
</p>
</div>
</Td>
<Td>
{!loadingPrices ? (
chartData !== undefined ? (
<div className="h-10 w-24">
<SimpleAreaChart
color={
change >= 0
? COLORS.UP[theme]
: COLORS.DOWN[theme]
}
data={chartData}
2023-06-05 21:00:22 -07:00
name={baseBank!.name + quoteBank!.name}
2023-04-03 04:01:17 -07:00
xKey="unixTime"
yKey="value"
2023-03-11 01:01:47 -08:00
/>
</div>
2023-04-25 05:41:23 -07:00
) : baseBank?.name === 'USDC' ||
baseBank?.name === 'USDT' ? null : (
2023-03-11 01:01:47 -08:00
<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>
<Td>
<div className="flex flex-col items-end">
<Change change={change} suffix="%" />
</div>
</Td>
2023-05-31 05:15:50 -07:00
<Td>
<div className="flex flex-col text-right">
<p>
{tickerData ? (
<span>
{numberCompacter.format(
parseFloat(tickerData.target_volume)
)}{' '}
<span className="font-body text-th-fgd-4">
{quoteBank?.name}
</span>
</span>
) : (
''
)}
</p>
</div>
</Td>
2023-03-11 01:01:47 -08:00
</TrBody>
)
})}
2022-10-08 04:37:08 -07:00
</tbody>
2022-11-20 02:44:14 -08:00
</Table>
2022-10-08 04:37:08 -07:00
) : (
2023-05-31 05:15:50 -07:00
<div className="border-b border-th-bkg-3">
2023-04-03 04:01:17 -07:00
{serumMarkets
.slice()
.sort((a, b) => a.name.localeCompare(b.name))
.map((market) => {
return (
<MobileSpotMarketItem
key={market.publicKey.toString()}
market={market}
2023-05-31 05:15:50 -07:00
spotVolumeData={spotVolumeData}
2023-04-03 04:01:17 -07:00
/>
)
})}
2022-10-08 04:37:08 -07:00
</div>
)}
</ContentBox>
)
}
export default SpotMarketsTable
2023-05-31 05:15:50 -07:00
const MobileSpotMarketItem = ({
market,
spotVolumeData,
}: {
market: Serum3Market
spotVolumeData: TickerData[] | undefined
}) => {
2022-10-08 04:37:08 -07:00
const { t } = useTranslation('common')
2023-04-03 04:01:17 -07:00
const { data: birdeyePrices, isLoading: loadingPrices } =
useBirdeyeMarketPrices()
2022-11-18 11:11:06 -08:00
const { group } = useMangoGroup()
2022-10-08 04:37:08 -07:00
const { theme } = useTheme()
2023-04-26 21:20:42 -07:00
const baseBank = group?.getFirstBankByTokenIndex(market.baseTokenIndex)
const quoteBank = group?.getFirstBankByTokenIndex(market.quoteTokenIndex)
const serumMarket = group?.getSerum3ExternalMarket(market.serumMarketExternal)
const price = useMemo(() => {
if (!baseBank || !quoteBank || !serumMarket) return 0
return floorToDecimal(
baseBank.uiPrice / quoteBank.uiPrice,
getDecimalCount(serumMarket.tickSize)
).toNumber()
}, [baseBank, quoteBank, serumMarket])
2022-10-08 04:37:08 -07:00
2023-04-03 04:01:17 -07:00
const birdeyeData = useMemo(() => {
2023-04-26 21:20:42 -07:00
if (!loadingPrices) {
2023-04-03 04:01:17 -07:00
return birdeyePrices.find(
(m) => m.mint === market.serumMarketExternal.toString()
2023-01-19 01:54:50 -08:00
)
2022-10-08 04:37:08 -07:00
}
return null
2023-04-26 21:20:42 -07:00
}, [loadingPrices])
2022-10-08 04:37:08 -07:00
const change = useMemo(() => {
2023-04-26 21:20:42 -07:00
if (birdeyeData && price) {
2022-10-08 04:37:08 -07:00
return (
2023-04-26 21:20:42 -07:00
((price - birdeyeData.data[0].value) / birdeyeData.data[0].value) * 100
2022-10-08 04:37:08 -07:00
)
}
return 0
2023-04-26 21:20:42 -07:00
}, [birdeyeData, price])
const chartData = useMemo(() => {
2023-04-03 04:01:17 -07:00
if (birdeyeData) {
return birdeyeData.data
}
return undefined
2023-04-03 04:01:17 -07:00
}, [birdeyeData])
2022-10-08 04:37:08 -07:00
2023-05-31 05:15:50 -07:00
let tickerData: TickerData | undefined
if (spotVolumeData && spotVolumeData.length) {
tickerData = spotVolumeData.find(
(m: TickerData) => m.ticker_id === market.name
)
}
2022-10-08 04:37:08 -07:00
return (
2023-05-31 05:15:50 -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>
<p className="leading-none text-th-fgd-1">{market.name}</p>
</div>
<div className="flex items-center space-x-3">
{!loadingPrices ? (
chartData !== undefined ? (
<div className="h-10 w-20">
<SimpleAreaChart
color={
change >= 0 ? COLORS.UP[theme] : COLORS.DOWN[theme]
}
data={chartData}
2023-06-05 21:00:22 -07:00
name={baseBank!.name + quoteBank!.name}
2023-05-31 05:15:50 -07:00
xKey="unixTime"
yKey="value"
/>
</div>
) : (
<p className="mb-0 text-th-fgd-4">{t('unavailable')}</p>
)
) : (
<div className="h-10 w-[104px] animate-pulse rounded bg-th-bkg-3" />
)}
<Change change={change} suffix="%" />
<ChevronDownIcon
className={`${
open ? 'rotate-180' : 'rotate-360'
} h-6 w-6 flex-shrink-0 text-th-fgd-3`}
/>
</div>
2022-12-21 03:07:17 -08:00
</div>
2023-05-31 05:15:50 -07:00
</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 pt-4 pb-4">
<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-15 04:24:54 -07:00
{price ? (
<>
<FormatNumericValue
value={price}
isUsd={quoteBank?.name === 'USDC'}
/>{' '}
{quoteBank?.name !== 'USDC' ? (
<span className="font-body text-th-fgd-4">
{quoteBank?.name}
</span>
) : null}
</>
) : (
'-'
)}
2023-05-31 05:15:50 -07:00
</p>
</div>
<div className="col-span-1">
<p className="text-xs text-th-fgd-3">
{t('trade:24h-volume')}
</p>
<p className="font-mono text-th-fgd-2">
{tickerData ? (
<span>
{numberCompacter.format(
parseFloat(tickerData.target_volume)
)}{' '}
<span className="font-body text-th-fgd-4">
{quoteBank?.name}
</span>
</span>
) : (
''
)}
</p>
</div>
</div>
</Disclosure.Panel>
</Transition>
</>
)}
</Disclosure>
2022-10-08 04:37:08 -07:00
)
}