import { Bank, MangoAccount } from '@blockworks-foundation/mango-v4' import { Transition } from '@headlessui/react' import { ChevronDownIcon, EllipsisHorizontalIcon, QuestionMarkCircleIcon, } from '@heroicons/react/20/solid' import { useWallet } from '@solana/wallet-adapter-react' import { useTranslation } from 'next-i18next' import { useTheme } from 'next-themes' import Image from 'next/image' import { useRouter } from 'next/router' import { Fragment, useCallback, useEffect, useMemo, useState } from 'react' // import useLocalStorageState from '../hooks/useLocalStorageState' import { useViewport } from '../hooks/useViewport' import mangoStore from '@store/mangoStore' import { COLORS } from '../styles/colors' // import { SHOW_ZERO_BALANCES_KEY } from '../utils/constants' import { formatDecimal, formatFixedDecimals } from '../utils/numbers' import { breakpoints } from '../utils/theme' import Switch from './forms/Switch' import BorrowModal from './modals/BorrowModal' import DepositModal from './modals/DepositModal' import WithdrawModal from './modals/WithdrawModal' import { IconButton, LinkButton } from './shared/Button' import ContentBox from './shared/ContentBox' import IconDropMenu from './shared/IconDropMenu' import PercentageChange from './shared/PercentageChange' import SimpleAreaChart from './shared/SimpleAreaChart' import Tooltip from './shared/Tooltip' const TokenList = () => { const { t } = useTranslation('common') const { connected } = useWallet() const [showZeroBalances, setShowZeroBalances] = useState(true) const mangoAccount = mangoStore((s) => s.mangoAccount.current) const coingeckoPrices = mangoStore((s) => s.coingeckoPrices.data) const loadingCoingeckoPrices = mangoStore((s) => s.coingeckoPrices.loading) const group = mangoStore((s) => s.group) const jupiterTokens = mangoStore((s) => s.jupiterTokens) const totalInterestData = mangoStore( (s) => s.mangoAccount.stats.interestTotals.data ) const { theme } = useTheme() const { width } = useViewport() const showTableView = width ? width > breakpoints.md : false const banks = useMemo(() => { if (group) { const rawBanks = Array.from(group?.banksMapByName, ([key, value]) => ({ key, value, })) const sortedBanks = mangoAccount ? rawBanks.sort( (a, b) => Math.abs( mangoAccount?.getTokenBalanceUi(b.value[0]) * b.value[0].uiPrice! ) - Math.abs( mangoAccount?.getTokenBalanceUi(a.value[0]) * a.value[0].uiPrice! ) ) : rawBanks return mangoAccount && !showZeroBalances ? sortedBanks.filter( (b) => mangoAccount?.getTokenBalanceUi(b.value[0]) !== 0 ) : sortedBanks } return [] }, [showZeroBalances, group, mangoAccount]) useEffect(() => { if (!connected) { setShowZeroBalances(true) } }, [connected]) return (
setShowZeroBalances(!showZeroBalances)} > {t('show-zero-balances')}
{showTableView ? ( {banks.map(({ key, value }) => { const bank = value[0] const oraclePrice = bank.uiPrice const coingeckoData = coingeckoPrices.find((asset) => key === 'soETH' ? asset.symbol === 'ETH' : asset.symbol === key ) const change = coingeckoData ? ((coingeckoData.prices[coingeckoData.prices.length - 1][1] - coingeckoData.prices[0][1]) / coingeckoData.prices[0][1]) * 100 : 0 const chartData = coingeckoData ? coingeckoData.prices : undefined let logoURI if (jupiterTokens.length) { logoURI = jupiterTokens.find( (t) => t.address === bank.mint.toString() )!.logoURI } const hasInterestEarned = totalInterestData.find( (d) => d.symbol === bank.name ) const interestAmount = hasInterestEarned ? hasInterestEarned.borrow_interest + hasInterestEarned.deposit_interest : 0 const interestValue = hasInterestEarned ? hasInterestEarned.borrow_interest_usd + hasInterestEarned.deposit_interest_usd : 0.0 return ( ) })}
{t('token')}
{t('balance')}
{t('interest-earned-paid')}
{t('rates')}
{t('price')} {t('rolling-change')}
{logoURI ? ( ) : ( )}

{bank.name}

{mangoAccount ? formatDecimal( mangoAccount.getTokenBalanceUi(bank), bank.mintDecimals ) : 0}

{mangoAccount ? `${formatFixedDecimals( mangoAccount.getTokenBalanceUi(bank) * oraclePrice!, true )}` : '$0.00'}

{formatDecimal(interestAmount)}

{formatFixedDecimals(interestValue, true)}

{formatDecimal(bank.getDepositRateUi(), 2, { fixed: true, })} %

|

{formatDecimal(bank.getBorrowRateUi(), 2, { fixed: true, })} %

{formatFixedDecimals(oraclePrice!, true)}

{!loadingCoingeckoPrices ? ( chartData !== undefined ? ( = 0 ? COLORS.GREEN[theme] : COLORS.RED[theme] } data={chartData} height={40} name={key} width={104} xKey="0" yKey="1" /> ) : key === 'USDC' || key === 'USDT' ? null : (

{t('unavailable')}

) ) : (
)}
) : (
{banks.map(({ key, value }) => { return })}
)}
) } export default TokenList const MobileTokenListItem = ({ bank }: { bank: Bank }) => { const { t } = useTranslation('common') const [showTokenDetails, setShowTokenDetails] = useState(false) const jupiterTokens = mangoStore((s) => s.jupiterTokens) const mangoAccount = mangoStore((s) => s.mangoAccount.current) const coingeckoPrices = mangoStore((s) => s.coingeckoPrices.data) const totalInterestData = mangoStore( (s) => s.mangoAccount.stats.interestTotals.data ) const symbol = bank.name const oraclePrice = bank.uiPrice const coingeckoData = coingeckoPrices.find((asset) => symbol === 'soETH' ? asset.symbol === 'ETH' : asset.symbol === symbol ) const change = coingeckoData ? ((coingeckoData.prices[coingeckoData.prices.length - 1][1] - coingeckoData.prices[0][1]) / coingeckoData.prices[0][1]) * 100 : 0 let logoURI if (jupiterTokens.length) { logoURI = jupiterTokens.find( (t) => t.address === bank.mint.toString() )!.logoURI } const hasInterestEarned = totalInterestData.find( (d) => d.symbol === bank.name ) const interestAmount = hasInterestEarned ? hasInterestEarned.borrow_interest + hasInterestEarned.deposit_interest : 0 const interestValue = hasInterestEarned ? hasInterestEarned.borrow_interest_usd + hasInterestEarned.deposit_interest_usd : 0.0 return (
{logoURI ? ( ) : ( )}

{bank.name}

{t('balance')}: {mangoAccount ? formatDecimal(mangoAccount.getTokenBalanceUi(bank)) : 0}

setShowTokenDetails((prev) => !prev)}>

{t('interest-earned-paid')}

{formatDecimal(interestAmount)}

({formatFixedDecimals(interestValue, true)})

{t('rates')}

{formatDecimal(bank.getDepositRate().toNumber(), 2)}% | {formatDecimal(bank.getBorrowRate().toNumber(), 2)}%

{t('price')}

{formatFixedDecimals(oraclePrice!, true)}

{t('rolling-change')}

) } const ActionsMenu = ({ bank, mangoAccount, }: { bank: Bank mangoAccount: MangoAccount | undefined }) => { const { t } = useTranslation('common') const [showDepositModal, setShowDepositModal] = useState(false) const [showWithdrawModal, setShowWithdrawModal] = useState(false) const [showBorrowModal, setShowBorrowModal] = useState(false) const [selectedToken, setSelectedToken] = useState('') const set = mangoStore.getState().set const router = useRouter() const { asPath } = router const jupiterTokens = mangoStore((s) => s.jupiterTokens) const handleShowActionModals = useCallback( (token: string, action: 'borrow' | 'deposit' | 'withdraw') => { setSelectedToken(token) action === 'borrow' ? setShowBorrowModal(true) : action === 'deposit' ? setShowDepositModal(true) : setShowWithdrawModal(true) }, [] ) const handleBuy = useCallback(() => { const outputTokenInfo = jupiterTokens.find( (t: any) => t.address === bank.mint.toString() ) set((s) => { s.swap.outputBank = bank s.swap.outputTokenInfo = outputTokenInfo }) if (asPath === '/') { router.push('/swap', undefined, { shallow: true }) } }, [bank, router, asPath, set, jupiterTokens]) const handleSell = useCallback(() => { const inputTokenInfo = jupiterTokens.find( (t: any) => t.address === bank.mint.toString() ) set((s) => { s.swap.inputBank = bank s.swap.inputTokenInfo = inputTokenInfo }) if (asPath === '/') { router.push('/swap', undefined, { shallow: true }) } }, [router, asPath, set, bank, jupiterTokens]) return ( <> } postion="leftBottom" >

{bank.name}

handleShowActionModals(bank.name, 'deposit')} > {t('deposit')} handleShowActionModals(bank.name, 'withdraw')} > {t('withdraw')} handleShowActionModals(bank.name, 'borrow')} > {t('borrow')} {t('buy')} {t('sell')}
{showDepositModal ? ( setShowDepositModal(false)} token={selectedToken} /> ) : null} {showWithdrawModal ? ( setShowWithdrawModal(false)} token={selectedToken} /> ) : null} {showBorrowModal ? ( setShowBorrowModal(false)} token={selectedToken} /> ) : null} ) }