import { PerpMarket } from '@blockworks-foundation/mango-v4' import { useTranslation } from 'next-i18next' import { useTheme } from 'next-themes' 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' import dynamic from 'next/dynamic' import { Table, Td, Th, TrBody, TrHead } from '@components/shared/TableElements' import { usePerpFundingRate } from '@components/trade/PerpFundingRate' import { IconButton } from '@components/shared/Button' import { ChevronRightIcon } from '@heroicons/react/20/solid' import FormatNumericValue from '@components/shared/FormatNumericValue' import { getDecimalCount } from 'utils/numbers' import Tooltip from '@components/shared/Tooltip' import { PerpStatsItem } from 'types' const SimpleAreaChart = dynamic( () => import('@components/shared/SimpleAreaChart'), { ssr: false } ) 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() : [] } const PerpMarketsTable = ({ setShowPerpDetails, }: { setShowPerpDetails: (x: string) => void }) => { const { t } = useTranslation(['common', 'trade']) const perpMarkets = mangoStore((s) => s.perpMarkets) const loadingPerpStats = mangoStore((s) => s.perpStats.loading) const perpStats = mangoStore((s) => s.perpStats.data) const { theme } = useTheme() const { width } = useViewport() const showTableView = width ? width > breakpoints.md : false const rate = usePerpFundingRate() return ( {showTableView ? ( {perpMarkets.map((market) => { const symbol = market.name.split('-')[0] const marketStats = getOneDayPerpStats(perpStats, market.name) const change = marketStats.length ? ((market.uiPrice - marketStats[0].price) / marketStats[0].price) * 100 : 0 let fundingRate if (rate.isSuccess) { const marketRate = rate?.data?.find( (r) => r.market_index === market.perpMarketIndex ) fundingRate = marketRate ? `${marketRate.funding_rate_hourly.toFixed(4)}%` : '–' } else { fundingRate = '–' } const openInterest = market.baseLotsToUi(market.openInterest) return ( ) })}
{t('market')} {t('price')} {t('trade:stable-price')} {t('trade:funding-rate')} {t('trade:open-interest')} {t('rolling-change')}

{market.name}

{!loadingPerpStats ? ( marketStats.length ? (
= 0 ? COLORS.UP[theme] : COLORS.DOWN[theme] } data={marketStats} name={symbol} xKey="date_hour" yKey="price" />
) : symbol === 'USDC' || symbol === 'USDT' ? null : (

{t('unavailable')}

) ) : (
)}

{fundingRate}

setShowPerpDetails(market.name)} size="small" >
) : (
{perpMarkets.map((market) => { return ( ) })}
)}
) } export default PerpMarketsTable const MobilePerpMarketItem = ({ market, setShowPerpDetails, }: { market: PerpMarket setShowPerpDetails: (x: string) => void }) => { const { t } = useTranslation('common') const loadingPerpStats = mangoStore((s) => s.perpStats.loading) const perpStats = mangoStore((s) => s.perpStats.data) const { theme } = useTheme() // const rate = usePerpFundingRate() const symbol = market.name.split('-')[0] const marketStats = getOneDayPerpStats(perpStats, market.name) const change = marketStats.length ? ((market.uiPrice - marketStats[0].price) / marketStats[0].price) * 100 : 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 (

{market.name}

{!loadingPerpStats ? ( marketStats.length ? (
= 0 ? COLORS.UP[theme] : COLORS.DOWN[theme]} data={marketStats} name={market.name} xKey="date_hour" yKey="price" />
) : symbol === 'USDC' || symbol === 'USDT' ? null : (

{t('unavailable')}

) ) : (
)}
setShowPerpDetails(market.name)} size="medium" >
) }