mango-v4-ui/components/TokenList.tsx

493 lines
18 KiB
TypeScript
Raw Normal View History

import { Bank, MangoAccount } from '@blockworks-foundation/mango-v4'
2022-07-19 22:49:09 -07:00
import { Transition } from '@headlessui/react'
2022-07-24 04:48:55 -07:00
import { ChevronDownIcon, DotsHorizontalIcon } from '@heroicons/react/solid'
2022-07-25 20:59:46 -07:00
import { useWallet } from '@solana/wallet-adapter-react'
2022-07-19 22:49:09 -07:00
import { useTranslation } from 'next-i18next'
2022-08-02 17:17:42 -07:00
import { useTheme } from 'next-themes'
2022-06-21 03:58:57 -07:00
import Image from 'next/image'
2022-07-25 22:27:53 -07:00
import { useRouter } from 'next/router'
2022-07-25 20:59:46 -07:00
import { Fragment, useEffect, useMemo, useState } from 'react'
2022-07-19 22:49:09 -07:00
import { useViewport } from '../hooks/useViewport'
2022-05-03 21:20:14 -07:00
import mangoStore from '../store/state'
2022-08-02 17:17:42 -07:00
import { COLORS } from '../styles/colors'
2022-08-11 19:27:21 -07:00
import { formatDecimal, formatFixedDecimals } from '../utils/numbers'
2022-07-19 22:49:09 -07:00
import { breakpoints } from '../utils/theme'
2022-07-25 20:59:46 -07:00
import Switch from './forms/Switch'
2022-07-24 19:55:10 -07:00
import BorrowModal from './modals/BorrowModal'
import DepositModal from './modals/DepositModal'
import WithdrawModal from './modals/WithdrawModal'
2022-07-25 20:59:46 -07:00
import { IconButton, LinkButton } from './shared/Button'
2022-06-21 03:58:57 -07:00
import ContentBox from './shared/ContentBox'
2022-07-24 04:48:55 -07:00
import { UpTriangle } from './shared/DirectionTriangles'
import IconDropMenu from './shared/IconDropMenu'
2022-08-02 17:17:42 -07:00
import SimpleAreaChart from './shared/SimpleAreaChart'
2022-07-28 16:32:15 -07:00
import { FadeInList } from './shared/Transitions'
2022-05-03 21:20:14 -07:00
2022-07-10 19:01:16 -07:00
const TokenList = () => {
2022-07-19 22:49:09 -07:00
const { t } = useTranslation('common')
2022-07-25 20:59:46 -07:00
const { connected } = useWallet()
2022-07-19 22:49:09 -07:00
const [showTokenDetails, setShowTokenDetails] = useState('')
2022-07-25 20:59:46 -07:00
const [showZeroBalances, setShowZeroBalances] = useState(true)
2022-07-27 23:35:18 -07:00
const mangoAccount = mangoStore((s) => s.mangoAccount.current)
2022-08-02 17:17:42 -07:00
const coingeckoPrices = mangoStore((s) => s.coingeckoPrices.data)
const loadingCoingeckoPrices = mangoStore((s) => s.coingeckoPrices.loading)
const actions = mangoStore((s) => s.actions)
2022-05-03 21:20:14 -07:00
const group = mangoStore((s) => s.group)
2022-08-11 19:27:21 -07:00
const jupiterTokens = mangoStore((s) => s.jupiterTokens)
2022-08-13 04:29:48 -07:00
const totalInterestData = mangoStore(
(s) => s.mangoAccount.stats.interestTotals.data
)
2022-08-02 17:17:42 -07:00
const { theme } = useTheme()
2022-07-19 22:49:09 -07:00
const { width } = useViewport()
const showTableView = width ? width > breakpoints.md : false
2022-05-03 21:20:14 -07:00
2022-08-02 17:17:42 -07:00
useEffect(() => {
if (coingeckoPrices.length === 0) {
actions.fetchCoingeckoPrices()
}
2022-08-02 17:37:05 -07:00
}, [coingeckoPrices, actions])
2022-08-02 17:17:42 -07:00
2022-07-25 20:59:46 -07:00
const banks = useMemo(() => {
if (group?.banksMap) {
const rawBanks = Array.from(group?.banksMap, ([key, value]) => ({
key,
value,
}))
return mangoAccount
? showZeroBalances
? rawBanks.sort(
(a, b) =>
Math.abs(mangoAccount?.getUi(b.value) * Number(b.value.price)) -
Math.abs(mangoAccount?.getUi(a.value) * Number(a.value.price))
)
: rawBanks
.filter((b) => mangoAccount?.getUi(b.value) !== 0)
.sort(
(a, b) =>
Math.abs(
mangoAccount?.getUi(b.value) * Number(b.value.price)
) -
Math.abs(mangoAccount?.getUi(a.value) * Number(a.value.price))
)
: rawBanks
}
return []
}, [showZeroBalances, group, mangoAccount])
useEffect(() => {
if (!connected) {
setShowZeroBalances(true)
}
}, [connected])
2022-05-03 21:20:14 -07:00
2022-07-19 22:49:09 -07:00
const handleShowTokenDetails = (name: string) => {
showTokenDetails ? setShowTokenDetails('') : setShowTokenDetails(name)
}
2022-05-03 21:20:14 -07:00
return (
2022-08-10 04:17:10 -07:00
<ContentBox hideBorder hidePadding className="-mt-10">
<div className="mb-6 flex items-center justify-end">
2022-07-25 20:59:46 -07:00
<Switch
className="text-th-fgd-3"
checked={showZeroBalances}
disabled={!mangoAccount}
onChange={() => setShowZeroBalances(!showZeroBalances)}
>
{t('show-zero-balances')}
</Switch>
</div>
2022-07-19 22:49:09 -07:00
{showTableView ? (
<table className="min-w-full">
<thead>
<tr>
2022-08-02 17:17:42 -07:00
<th className="w-[16.67%] text-left">{t('token')}</th>
<th className="w-[16.67%] text-right">{t('price')}</th>
<th className="className='hidden lg:block' w-[16.67%] text-right"></th>
2022-08-15 16:09:49 -07:00
<th className="w-[16.67%] text-center">{t('rates')}</th>
2022-08-13 04:29:48 -07:00
<th className="w-[16.67%] text-right">{t('interest-earned')}</th>
2022-08-02 17:17:42 -07:00
<th className="w-[16.67%] text-right">
{t('available-balance')}
</th>
2022-07-19 22:49:09 -07:00
</tr>
</thead>
<tbody>
2022-07-28 16:32:15 -07:00
{banks.map((bank, index) => {
2022-07-19 22:49:09 -07:00
const oraclePrice = bank.value.price
2022-08-02 17:17:42 -07:00
const coingeckoData = coingeckoPrices.find(
(asset) => asset.symbol === bank.key
)
2022-08-10 04:17:10 -07:00
const change = coingeckoData
? ((coingeckoData.prices[coingeckoData.prices.length - 1][1] -
coingeckoData.prices[0][1]) /
coingeckoData.prices[0][1]) *
100
: 0
2022-08-02 17:17:42 -07:00
const chartData = coingeckoData ? coingeckoData.prices : undefined
2022-08-13 04:29:48 -07:00
2022-08-11 19:27:21 -07:00
let logoURI
if (jupiterTokens.length) {
logoURI = jupiterTokens.find(
(t) => t.address === bank.value.mint.toString()
)!.logoURI
}
2022-08-13 04:29:48 -07:00
const hasInterestEarned = totalInterestData.find(
(d) => d.symbol === bank.value.name
)
const interestAmount = hasInterestEarned
? hasInterestEarned.borrow_interest +
hasInterestEarned.deposit_interest
2022-08-13 22:07:53 -07:00
: 0
const interestValue = hasInterestEarned
? hasInterestEarned.borrow_interest_usd +
hasInterestEarned.deposit_interest_usd
2022-08-13 04:29:48 -07:00
: 0.0
2022-07-19 22:49:09 -07:00
return (
2022-07-28 16:32:15 -07:00
<FadeInList as="tr" index={index} key={bank.key}>
2022-08-02 17:17:42 -07:00
<td className="w-[16.67%]">
2022-07-19 22:49:09 -07:00
<div className="flex items-center">
<div className="mr-2.5 flex flex-shrink-0 items-center">
2022-08-11 19:27:21 -07:00
{logoURI ? (
<Image alt="" width="24" height="24" src={logoURI} />
) : null}
2022-07-19 22:49:09 -07:00
</div>
2022-07-24 04:48:55 -07:00
<p>{bank.value.name}</p>
2022-07-19 22:49:09 -07:00
</div>
</td>
2022-08-02 17:17:42 -07:00
<td className="w-[16.67%]">
2022-07-19 22:49:09 -07:00
<div className="flex flex-col text-right">
2022-08-13 22:07:53 -07:00
<p>{formatFixedDecimals(oraclePrice.toNumber(), true)}</p>
2022-07-19 22:49:09 -07:00
</div>
</td>
2022-08-10 04:17:10 -07:00
<td className="hidden lg:table-cell">
{!loadingCoingeckoPrices ? (
chartData !== undefined ? (
<SimpleAreaChart
color={
change >= 0
? COLORS.GREEN[theme]
: COLORS.RED[theme]
}
data={chartData}
height={40}
name={bank.key}
width={104}
2022-08-10 21:09:58 -07:00
xKey="0"
yKey="1"
2022-08-10 04:17:10 -07:00
/>
) : bank.key === 'USDC' || bank.key === '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" />
)}
2022-08-02 17:17:42 -07:00
</td>
<td className="w-[16.67%]">
2022-08-15 16:09:49 -07:00
<div className="flex justify-center space-x-2">
2022-07-19 22:49:09 -07:00
<p className="text-th-green">
{formatDecimal(
bank.value.getDepositRate().toNumber(),
2022-08-15 16:09:49 -07:00
2,
{ fixed: true }
2022-07-19 22:49:09 -07:00
)}
%
</p>
<span className="text-th-fgd-4">|</span>
<p className="text-th-red">
{formatDecimal(
bank.value.getBorrowRate().toNumber(),
2022-08-15 16:09:49 -07:00
2,
{ fixed: true }
2022-07-19 22:49:09 -07:00
)}
%
</p>
</div>
</td>
2022-08-02 17:17:42 -07:00
<td className="w-[16.67%]">
2022-07-19 22:49:09 -07:00
<div className="flex flex-col text-right">
2022-08-13 04:29:48 -07:00
<p>{formatDecimal(interestAmount)}</p>
2022-08-13 22:07:53 -07:00
<p className="text-sm text-th-fgd-4">
{formatFixedDecimals(interestValue, true)}
</p>
2022-07-19 22:49:09 -07:00
</div>
</td>
2022-08-02 17:17:42 -07:00
<td className="w-[16.67%] pt-4 text-right">
2022-07-19 22:49:09 -07:00
<p className="px-2">
{mangoAccount
? formatDecimal(mangoAccount.getUi(bank.value))
: 0}
</p>
2022-07-24 04:48:55 -07:00
<p className="px-2 text-sm text-th-fgd-4">
2022-07-19 22:49:09 -07:00
{mangoAccount
2022-08-13 22:07:53 -07:00
? `${formatFixedDecimals(
2022-07-19 22:49:09 -07:00
mangoAccount.getUi(bank.value) *
oraclePrice.toNumber(),
2022-08-13 22:07:53 -07:00
true
2022-07-19 22:49:09 -07:00
)}`
2022-08-13 22:07:53 -07:00
: '$0.00'}
2022-07-19 22:49:09 -07:00
</p>
</td>
2022-08-02 17:17:42 -07:00
<td className="w-[16.67%]">
2022-07-19 22:49:09 -07:00
<div className="flex justify-end space-x-2">
<ActionsMenu
bank={bank.value}
mangoAccount={mangoAccount}
/>
2022-07-19 22:49:09 -07:00
</div>
</td>
2022-07-28 16:32:15 -07:00
</FadeInList>
2022-07-19 22:49:09 -07:00
)
})}
</tbody>
</table>
) : (
<div className="mt-4 space-y-2">
2022-07-14 18:46:34 -07:00
{banks.map((bank) => {
const oraclePrice = bank.value.price
return (
2022-07-22 10:07:55 -07:00
<div
key={bank.key}
className="rounded-md border border-th-bkg-4 p-4"
>
2022-07-19 22:49:09 -07:00
<div className="flex items-center justify-between">
2022-07-14 18:46:34 -07:00
<div className="flex items-center">
2022-07-15 04:09:23 -07:00
<div className="mr-2.5 flex flex-shrink-0 items-center">
2022-07-14 18:46:34 -07:00
<Image
alt=""
2022-07-19 22:49:09 -07:00
width="32"
height="32"
2022-07-14 18:46:34 -07:00
src={`/icons/${bank.value.name.toLowerCase()}.svg`}
/>
2022-06-21 03:58:57 -07:00
</div>
2022-07-19 22:49:09 -07:00
<div>
2022-07-24 04:48:55 -07:00
<p>{bank.value.name}</p>
<p className="text-sm">
<span className="mr-1 text-th-fgd-4">
{t('available')}:
</span>
2022-07-19 22:49:09 -07:00
{mangoAccount
? formatDecimal(mangoAccount.getUi(bank.value))
: 0}
</p>
</div>
2022-07-14 18:46:34 -07:00
</div>
2022-08-10 04:17:10 -07:00
<div className="flex items-center space-x-3">
<ActionsMenu
bank={bank.value}
mangoAccount={mangoAccount}
/>
2022-07-19 22:49:09 -07:00
<IconButton
onClick={() => handleShowTokenDetails(bank.value.name)}
>
<ChevronDownIcon
className={`${
showTokenDetails === bank.value.name
? 'rotate-180 transform'
: 'rotate-360 transform'
} default-transition h-6 w-6 flex-shrink-0 text-th-fgd-1`}
2022-07-19 22:49:09 -07:00
/>
</IconButton>
2022-07-14 18:46:34 -07:00
</div>
2022-07-19 22:49:09 -07:00
</div>
<Transition
appear={true}
show={showTokenDetails === bank.value.name}
as={Fragment}
enter="transition-all ease-in duration-200"
enterFrom="opacity-0"
enterTo="opacity-100"
leave="transition ease-out"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<div className="mt-4 grid grid-cols-2 gap-4 border-t border-th-bkg-3 pt-4">
<div className="col-span-1">
<p className="text-xs text-th-fgd-3">{t('price')}</p>
<p className="font-bold">
${formatDecimal(oraclePrice.toNumber(), 2)}
</p>
</div>
<div className="col-span-1">
<p className="text-xs text-th-fgd-3">
{t('rolling-change')}
</p>
2022-07-24 04:48:55 -07:00
<div className="flex items-center">
<UpTriangle />
<p className="ml-1 font-bold text-th-green">0%</p>
</div>
2022-07-19 22:49:09 -07:00
</div>
<div className="col-span-1">
2022-07-24 04:48:55 -07:00
<p className="text-xs text-th-fgd-3">{t('rates')}</p>
2022-07-19 22:49:09 -07:00
<p className="space-x-2 font-bold">
<span className="text-th-green">
{formatDecimal(
bank.value.getDepositRate().toNumber(),
2
)}
%
</span>
<span className="font-normal text-th-fgd-4">|</span>
<span className="text-th-red">
{formatDecimal(
bank.value.getBorrowRate().toNumber(),
2
)}
%
</span>
</p>
</div>
<div className="col-span-1">
<p className="text-xs text-th-fgd-3">{t('liquidity')}</p>
<p className="font-bold">
{formatDecimal(
bank.value.uiDeposits() - bank.value.uiBorrows(),
bank.value.mintDecimals
)}
</p>
</div>
2022-07-18 22:27:11 -07:00
</div>
2022-07-19 22:49:09 -07:00
</Transition>
</div>
2022-07-14 18:46:34 -07:00
)
})}
2022-07-19 22:49:09 -07:00
</div>
)}
</ContentBox>
)
}
export default TokenList
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('')
2022-07-25 22:27:53 -07:00
const set = mangoStore.getState().set
const router = useRouter()
const { asPath } = router
const handleShowActionModals = (
token: string,
action: 'borrow' | 'deposit' | 'withdraw'
) => {
setSelectedToken(token)
action === 'borrow'
? setShowBorrowModal(true)
: action === 'deposit'
? setShowDepositModal(true)
: setShowWithdrawModal(true)
}
2022-07-25 22:27:53 -07:00
const handleBuy = () => {
set((s) => {
2022-08-02 11:04:00 -07:00
s.swap.inputToken = 'USDC'
2022-07-25 22:27:53 -07:00
s.swap.outputToken = bank.name
})
if (asPath === '/') {
router.push('/trade', undefined, { shallow: true })
}
}
const handleSell = () => {
set((s) => {
s.swap.inputToken = bank.name
2022-08-02 11:04:00 -07:00
s.swap.outputToken = 'USDC'
2022-07-25 22:27:53 -07:00
})
if (asPath === '/') {
router.push('/trade', undefined, { shallow: true })
}
}
return (
<>
<IconDropMenu
icon={<DotsHorizontalIcon className="h-5 w-5" />}
postion="leftBottom"
>
<div className="flex items-center justify-center border-b border-th-bkg-3 pb-2">
<div className="mr-2 flex flex-shrink-0 items-center">
<Image
alt=""
width="20"
height="20"
src={`/icons/${bank.name.toLowerCase()}.svg`}
/>
</div>
<p>{bank.name}</p>
</div>
<LinkButton
className="w-full text-left"
disabled={!mangoAccount}
onClick={() => handleShowActionModals(bank.name, 'deposit')}
>
{t('deposit')}
</LinkButton>
2022-07-25 20:59:46 -07:00
<LinkButton
className="w-full text-left"
disabled={!mangoAccount}
onClick={() => handleShowActionModals(bank.name, 'withdraw')}
>
{t('withdraw')}
</LinkButton>
<LinkButton
className="w-full text-left"
disabled={!mangoAccount}
onClick={() => handleShowActionModals(bank.name, 'borrow')}
>
{t('borrow')}
</LinkButton>
<LinkButton
className="w-full text-left"
2022-07-26 04:15:07 -07:00
disabled={!mangoAccount}
2022-07-25 22:27:53 -07:00
onClick={() => handleBuy()}
>
{t('buy')}
</LinkButton>
<LinkButton
className="w-full text-left"
2022-07-26 04:15:07 -07:00
disabled={!mangoAccount}
2022-07-25 22:27:53 -07:00
onClick={() => handleSell()}
>
{t('sell')}
</LinkButton>
</IconDropMenu>
2022-07-24 19:55:10 -07:00
{showDepositModal ? (
<DepositModal
isOpen={showDepositModal}
onClose={() => setShowDepositModal(false)}
token={selectedToken}
/>
) : null}
{showWithdrawModal ? (
<WithdrawModal
isOpen={showWithdrawModal}
onClose={() => setShowWithdrawModal(false)}
token={selectedToken}
/>
) : null}
{showBorrowModal ? (
<BorrowModal
isOpen={showBorrowModal}
onClose={() => setShowBorrowModal(false)}
token={selectedToken}
/>
) : null}
</>
2022-05-03 21:20:14 -07:00
)
}