import { Disclosure, Transition } from '@headlessui/react' import { ChevronDownIcon, ChevronRightIcon } from '@heroicons/react/20/solid' import { useTranslation } from 'next-i18next' import { useViewport } from '../../../hooks/useViewport' import { breakpoints } from '../../../utils/theme' import { LinkButton } from '../../shared/Button' import ContentBox from '../../shared/ContentBox' import Tooltip from '@components/shared/Tooltip' import { Bank, toUiDecimals } from '@blockworks-foundation/mango-v4' import { NextRouter, useRouter } from 'next/router' import { SortableColumnHeader, Table, Td, Th, TrBody, TrHead, } from '@components/shared/TableElements' import useMangoGroup from 'hooks/useMangoGroup' import FormatNumericValue from '@components/shared/FormatNumericValue' import BankAmountWithValue from '@components/shared/BankAmountWithValue' import useBanksWithBalances from 'hooks/useBanksWithBalances' import Decimal from 'decimal.js' import TokenLogo from '@components/shared/TokenLogo' import { useCallback } from 'react' import { useSortableData } from 'hooks/useSortableData' export const goToTokenPage = (token: string, router: NextRouter) => { const query = { ...router.query, ['token']: token } router.push({ pathname: router.pathname, query }) } const TokenOverviewTable = () => { const { t } = useTranslation(['common', 'token']) const { group } = useMangoGroup() const { width } = useViewport() const showTableView = width ? width > breakpoints.md : false const router = useRouter() const banks = useBanksWithBalances() const formattedTableData = useCallback(() => { const formatted = [] for (const b of banks) { const bank: Bank = b.bank const deposits = bank.uiDeposits() const depositsValue = deposits * bank.uiPrice const borrows = bank.uiBorrows() const borrowsValue = borrows * bank.uiPrice const availableVaultBalance = group ? group.getTokenVaultBalanceByMintUi(bank.mint) - deposits * bank.minVaultToDepositsRatio : 0 const available = Decimal.max( 0, availableVaultBalance.toFixed(bank.mintDecimals), ) const availableValue = available.toNumber() * bank.uiPrice const feesEarned = toUiDecimals( bank.collectedFeesNative, bank.mintDecimals, ) const feeValue = feesEarned * bank.uiPrice const utilization = bank.uiDeposits() > 0 ? (bank.uiBorrows() / bank.uiDeposits()) * 100 : 0 const depositRate = bank.getDepositRateUi() const borrowRate = bank.getBorrowRateUi() const symbol = bank.name const data = { available, availableValue, bank, borrowRate, borrows, borrowsValue, depositRate, deposits, depositsValue, feesEarned, feeValue, symbol, utilization, } formatted.push(data) } return formatted.sort( (a, b) => b.deposits * b.bank.uiPrice - a.deposits * a.bank.uiPrice, ) }, [banks, group]) const { items: tableData, requestSort, sortConfig, } = useSortableData(formattedTableData()) return ( {showTableView ? (
{tableData.map((data) => { const { available, bank, borrows, borrowRate, deposits, depositRate, feesEarned, symbol, utilization, } = data return ( goToTokenPage(bank.name.split(' ')[0], router) } > ) })}
requestSort('symbol')} sortConfig={sortConfig} title={t('token')} />
requestSort('depositsValue')} sortConfig={sortConfig} title={t('total-deposits')} />
requestSort('borrowsValue')} sortConfig={sortConfig} title={t('total-borrows')} />
requestSort('availableValue')} sortConfig={sortConfig} title={t('available')} titleClass="tooltip-underline" />
requestSort('feeValue')} sortConfig={sortConfig} title={t('fees')} titleClass="tooltip-underline" />
requestSort('depositRate')} sortConfig={sortConfig} title={t('rates')} titleClass="tooltip-underline" />
requestSort('utilization')} sortConfig={sortConfig} title={t('utilization')} titleClass="tooltip-underline" />

{symbol}

%

|

%

{utilization.toFixed(1)}%

) : (
{tableData.map((data, i) => { const { available, bank, borrows, borrowRate, deposits, depositRate, feesEarned, symbol, utilization, } = data return ( {({ open }) => ( <>

{symbol}

{t('total-deposits')}

{t('total-borrows')}

{t('available')}

{t('fees')}

{t('rates')}

% | %

{t('utilization')}

{utilization.toFixed(1)}%

goToTokenPage(bank.name.split(' ')[0], router) } > {t('token:token-stats', { token: bank.name })}
)}
) })}
)}
) } export default TokenOverviewTable