2022-11-20 18:22:45 -08:00
|
|
|
import { Bank, MangoAccount } from '@blockworks-foundation/mango-v4'
|
2023-03-18 05:15:01 -07:00
|
|
|
import { Disclosure, Transition } from '@headlessui/react'
|
2022-08-15 18:59:15 -07:00
|
|
|
import {
|
|
|
|
ChevronDownIcon,
|
2022-09-06 21:36:35 -07:00
|
|
|
EllipsisHorizontalIcon,
|
2022-08-15 18:59:15 -07:00
|
|
|
QuestionMarkCircleIcon,
|
2022-09-06 21:36:35 -07:00
|
|
|
} from '@heroicons/react/20/solid'
|
2022-07-19 22:49:09 -07:00
|
|
|
import { useTranslation } from 'next-i18next'
|
2022-10-28 14:46:38 -07:00
|
|
|
import Image from 'next/legacy/image'
|
2022-07-25 22:27:53 -07:00
|
|
|
import { useRouter } from 'next/router'
|
2023-04-13 05:35:23 -07:00
|
|
|
import { useCallback, useMemo, useState } from 'react'
|
2022-07-19 22:49:09 -07:00
|
|
|
import { useViewport } from '../hooks/useViewport'
|
2022-09-12 08:53:57 -07:00
|
|
|
import mangoStore from '@store/mangoStore'
|
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-06-21 03:58:57 -07:00
|
|
|
import ContentBox from './shared/ContentBox'
|
2022-07-24 04:48:55 -07:00
|
|
|
import IconDropMenu from './shared/IconDropMenu'
|
2022-09-19 23:16:03 -07:00
|
|
|
import Tooltip from './shared/Tooltip'
|
2022-10-05 19:12:41 -07:00
|
|
|
import { formatTokenSymbol } from 'utils/tokens'
|
2022-11-18 09:09:39 -08:00
|
|
|
import useMangoAccount from 'hooks/useMangoAccount'
|
2022-11-18 11:11:06 -08:00
|
|
|
import useJupiterMints from '../hooks/useJupiterMints'
|
2022-11-20 02:44:14 -08:00
|
|
|
import { Table, Td, Th, TrBody, TrHead } from './shared/TableElements'
|
2022-12-17 04:37:00 -08:00
|
|
|
import DepositWithdrawModal from './modals/DepositWithdrawModal'
|
2022-12-18 04:30:49 -08:00
|
|
|
import BorrowRepayModal from './modals/BorrowRepayModal'
|
2023-01-04 18:50:36 -08:00
|
|
|
import { WRAPPED_SOL_MINT } from '@project-serum/serum/lib/token-instructions'
|
2023-04-13 05:35:23 -07:00
|
|
|
import { SHOW_ZERO_BALANCES_KEY, USDC_MINT } from 'utils/constants'
|
2023-01-04 18:50:36 -08:00
|
|
|
import { PublicKey } from '@solana/web3.js'
|
2023-01-05 01:04:09 -08:00
|
|
|
import ActionsLinkButton from './account/ActionsLinkButton'
|
2023-01-23 17:26:14 -08:00
|
|
|
import FormatNumericValue from './shared/FormatNumericValue'
|
2023-01-28 17:13:36 -08:00
|
|
|
import BankAmountWithValue from './shared/BankAmountWithValue'
|
2023-01-29 20:13:38 -08:00
|
|
|
import useBanksWithBalances, {
|
|
|
|
BankWithBalance,
|
|
|
|
} from 'hooks/useBanksWithBalances'
|
2023-02-12 18:46:41 -08:00
|
|
|
import useUnownedAccount from 'hooks/useUnownedAccount'
|
2023-04-13 05:35:23 -07:00
|
|
|
import useLocalStorageState from 'hooks/useLocalStorageState'
|
2022-05-03 21:20:14 -07:00
|
|
|
|
2022-07-10 19:01:16 -07:00
|
|
|
const TokenList = () => {
|
2022-10-29 04:38:40 -07:00
|
|
|
const { t } = useTranslation(['common', 'token', 'trade'])
|
2023-04-13 05:35:23 -07:00
|
|
|
const [showZeroBalances, setShowZeroBalances] = useLocalStorageState(
|
|
|
|
SHOW_ZERO_BALANCES_KEY,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
const { mangoAccount, mangoAccountAddress } = useMangoAccount()
|
2022-10-06 18:57:47 -07:00
|
|
|
const spotBalances = mangoStore((s) => s.mangoAccount.spotBalances)
|
2022-11-18 11:11:06 -08:00
|
|
|
const { mangoTokens } = useJupiterMints()
|
2022-08-13 04:29:48 -07:00
|
|
|
const totalInterestData = mangoStore(
|
2023-01-15 21:13:34 -08:00
|
|
|
(s) => s.mangoAccount.interestTotals.data
|
2022-08-13 04:29:48 -07:00
|
|
|
)
|
2022-07-19 22:49:09 -07:00
|
|
|
const { width } = useViewport()
|
|
|
|
const showTableView = width ? width > breakpoints.md : false
|
2023-01-29 20:13:38 -08:00
|
|
|
const banks = useBanksWithBalances('balance')
|
2022-05-03 21:20:14 -07:00
|
|
|
|
2023-01-29 20:13:38 -08:00
|
|
|
const filteredBanks = useMemo(() => {
|
|
|
|
if (banks.length) {
|
2023-04-13 05:35:23 -07:00
|
|
|
return showZeroBalances || !mangoAccountAddress
|
2023-01-29 20:13:38 -08:00
|
|
|
? banks
|
|
|
|
: banks.filter((b) => Math.abs(b.balance) > 0)
|
2022-07-25 20:59:46 -07:00
|
|
|
}
|
|
|
|
return []
|
2023-04-13 05:35:23 -07:00
|
|
|
}, [banks, mangoAccountAddress, showZeroBalances])
|
2022-05-03 21:20:14 -07:00
|
|
|
|
|
|
|
return (
|
2023-02-03 02:36:44 -08:00
|
|
|
<ContentBox hideBorder hidePadding>
|
2023-04-13 05:35:23 -07:00
|
|
|
{mangoAccountAddress ? (
|
2023-06-15 16:27:49 -07:00
|
|
|
<div className="flex w-full items-center justify-end border-b border-th-bkg-3 py-3 px-6 lg:-mt-[36px] lg:mr-12 lg:mb-4 lg:w-auto lg:border-0 lg:py-0">
|
2023-04-13 05:35:23 -07:00
|
|
|
<Switch
|
|
|
|
checked={showZeroBalances}
|
|
|
|
disabled={!mangoAccount}
|
|
|
|
onChange={() => setShowZeroBalances(!showZeroBalances)}
|
|
|
|
>
|
|
|
|
{t('show-zero-balances')}
|
|
|
|
</Switch>
|
|
|
|
</div>
|
|
|
|
) : null}
|
2022-07-19 22:49:09 -07:00
|
|
|
{showTableView ? (
|
2022-11-20 02:44:14 -08:00
|
|
|
<Table>
|
2022-07-19 22:49:09 -07:00
|
|
|
<thead>
|
2022-11-20 02:44:14 -08:00
|
|
|
<TrHead>
|
|
|
|
<Th className="text-left">{t('token')}</Th>
|
|
|
|
<Th>
|
2022-09-25 17:37:04 -07:00
|
|
|
<div className="flex justify-end">
|
2023-01-22 20:25:43 -08:00
|
|
|
<Tooltip content="A negative balance represents a borrow">
|
2022-09-25 17:37:04 -07:00
|
|
|
<span className="tooltip-underline">{t('balance')}</span>
|
|
|
|
</Tooltip>
|
|
|
|
</div>
|
2022-11-20 02:44:14 -08:00
|
|
|
</Th>
|
2023-03-13 02:03:23 -07:00
|
|
|
<Th className="text-right">{t('trade:in-orders')}</Th>
|
|
|
|
<Th className="text-right">{t('trade:unsettled')}</Th>
|
2022-11-20 02:44:14 -08:00
|
|
|
<Th className="flex justify-end" id="account-step-nine">
|
2023-01-22 20:25:43 -08:00
|
|
|
<Tooltip content="The sum of interest earned and interest paid for each token">
|
2022-09-19 23:16:03 -07:00
|
|
|
<span className="tooltip-underline">
|
|
|
|
{t('interest-earned-paid')}
|
|
|
|
</span>
|
|
|
|
</Tooltip>
|
2022-11-20 02:44:14 -08:00
|
|
|
</Th>
|
|
|
|
<Th id="account-step-ten">
|
2022-10-06 05:08:29 -07:00
|
|
|
<div className="flex justify-end">
|
2023-01-22 20:25:43 -08:00
|
|
|
<Tooltip content="The interest rates for depositing (green/left) and borrowing (red/right)">
|
2022-10-06 05:08:29 -07:00
|
|
|
<span className="tooltip-underline">{t('rates')}</span>
|
|
|
|
</Tooltip>
|
|
|
|
</div>
|
2022-11-20 02:44:14 -08:00
|
|
|
</Th>
|
2023-03-13 02:03:23 -07:00
|
|
|
<Th className="text-right">
|
2023-03-16 03:48:15 -07:00
|
|
|
<span>{t('actions')}</span>
|
2023-03-13 02:03:23 -07:00
|
|
|
</Th>
|
2022-11-20 02:44:14 -08:00
|
|
|
</TrHead>
|
2022-07-19 22:49:09 -07:00
|
|
|
</thead>
|
|
|
|
<tbody>
|
2023-01-29 20:13:38 -08:00
|
|
|
{filteredBanks.map((b) => {
|
|
|
|
const bank = b.bank
|
2022-08-02 17:17:42 -07:00
|
|
|
|
2022-08-11 19:27:21 -07:00
|
|
|
let logoURI
|
2022-11-18 11:11:06 -08:00
|
|
|
if (mangoTokens?.length) {
|
|
|
|
logoURI = mangoTokens.find(
|
2022-08-18 13:50:34 -07:00
|
|
|
(t) => t.address === bank.mint.toString()
|
2022-11-19 11:20:36 -08:00
|
|
|
)?.logoURI
|
2022-08-11 19:27:21 -07:00
|
|
|
}
|
|
|
|
|
2023-01-29 20:13:38 -08:00
|
|
|
const tokenBalance = b.balance
|
2022-11-20 18:22:45 -08:00
|
|
|
|
2022-08-13 04:29:48 -07:00
|
|
|
const hasInterestEarned = totalInterestData.find(
|
2022-08-18 13:50:34 -07:00
|
|
|
(d) => d.symbol === bank.name
|
2022-08-13 04:29:48 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const interestAmount = hasInterestEarned
|
2023-01-05 20:19:36 -08:00
|
|
|
? hasInterestEarned.borrow_interest * -1 +
|
2022-08-13 04:29:48 -07:00
|
|
|
hasInterestEarned.deposit_interest
|
2022-08-13 22:07:53 -07:00
|
|
|
: 0
|
|
|
|
|
|
|
|
const interestValue = hasInterestEarned
|
2023-01-05 20:19:36 -08:00
|
|
|
? hasInterestEarned.borrow_interest_usd * -1 +
|
2022-08-13 22:07:53 -07:00
|
|
|
hasInterestEarned.deposit_interest_usd
|
2022-08-13 04:29:48 -07:00
|
|
|
: 0.0
|
|
|
|
|
2022-11-21 21:14:35 -08:00
|
|
|
const inOrders = spotBalances[bank.mint.toString()]?.inOrders || 0
|
2022-10-06 18:57:47 -07:00
|
|
|
|
|
|
|
const unsettled =
|
2022-11-21 21:14:35 -08:00
|
|
|
spotBalances[bank.mint.toString()]?.unsettled || 0
|
2022-10-06 18:57:47 -07:00
|
|
|
|
2022-07-19 22:49:09 -07:00
|
|
|
return (
|
2023-03-16 03:48:15 -07:00
|
|
|
<TrBody key={bank.name}>
|
2022-11-20 02:44:14 -08:00
|
|
|
<Td>
|
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} />
|
2022-08-15 18:59:15 -07:00
|
|
|
) : (
|
2022-09-18 23:26:42 -07:00
|
|
|
<QuestionMarkCircleIcon className="h-6 w-6 text-th-fgd-3" />
|
2022-08-15 18:59:15 -07:00
|
|
|
)}
|
2022-07-19 22:49:09 -07:00
|
|
|
</div>
|
2023-01-14 04:41:30 -08:00
|
|
|
<p className="font-body">{bank.name}</p>
|
2022-07-19 22:49:09 -07:00
|
|
|
</div>
|
2022-11-20 02:44:14 -08:00
|
|
|
</Td>
|
|
|
|
<Td className="text-right">
|
2023-01-28 17:13:36 -08:00
|
|
|
<BankAmountWithValue
|
2023-01-24 02:56:04 -08:00
|
|
|
amount={tokenBalance}
|
2023-01-28 17:13:36 -08:00
|
|
|
bank={bank}
|
2023-01-24 02:56:04 -08:00
|
|
|
stacked
|
|
|
|
/>
|
2022-11-20 02:44:14 -08:00
|
|
|
</Td>
|
|
|
|
<Td className="text-right">
|
2023-01-28 17:13:36 -08:00
|
|
|
<BankAmountWithValue
|
2023-01-24 02:56:04 -08:00
|
|
|
amount={inOrders}
|
2023-01-28 17:13:36 -08:00
|
|
|
bank={bank}
|
2023-01-24 02:56:04 -08:00
|
|
|
stacked
|
|
|
|
/>
|
2022-11-20 02:44:14 -08:00
|
|
|
</Td>
|
|
|
|
<Td className="text-right">
|
2023-01-28 17:13:36 -08:00
|
|
|
<BankAmountWithValue
|
2023-01-24 02:56:04 -08:00
|
|
|
amount={unsettled}
|
2023-01-28 17:13:36 -08:00
|
|
|
bank={bank}
|
2023-01-24 02:56:04 -08:00
|
|
|
stacked
|
|
|
|
/>
|
2022-11-20 02:44:14 -08:00
|
|
|
</Td>
|
|
|
|
<Td>
|
2022-09-04 23:32:10 -07:00
|
|
|
<div className="flex flex-col text-right">
|
2023-01-28 17:13:36 -08:00
|
|
|
<BankAmountWithValue
|
2023-01-23 17:26:14 -08:00
|
|
|
amount={interestAmount}
|
2023-01-28 17:13:36 -08:00
|
|
|
bank={bank}
|
2023-01-23 17:26:14 -08:00
|
|
|
value={interestValue}
|
2023-01-08 19:38:09 -08:00
|
|
|
stacked
|
|
|
|
/>
|
2022-09-04 23:32:10 -07:00
|
|
|
</div>
|
2022-11-20 02:44:14 -08:00
|
|
|
</Td>
|
|
|
|
<Td>
|
2023-01-08 19:38:09 -08:00
|
|
|
<div className="flex justify-end space-x-1.5">
|
2022-11-30 19:32:32 -08:00
|
|
|
<p className="text-th-up">
|
2023-01-23 17:26:14 -08:00
|
|
|
<FormatNumericValue
|
|
|
|
value={bank.getDepositRateUi()}
|
|
|
|
decimals={2}
|
|
|
|
/>
|
2022-09-07 05:00:21 -07:00
|
|
|
%
|
|
|
|
</p>
|
|
|
|
<span className="text-th-fgd-4">|</span>
|
2022-11-30 19:32:32 -08:00
|
|
|
<p className="text-th-down">
|
2023-01-23 17:26:14 -08:00
|
|
|
<FormatNumericValue
|
|
|
|
value={bank.getBorrowRateUi()}
|
|
|
|
decimals={2}
|
|
|
|
/>
|
2022-09-07 05:00:21 -07:00
|
|
|
%
|
|
|
|
</p>
|
|
|
|
</div>
|
2022-11-20 02:44:14 -08:00
|
|
|
</Td>
|
|
|
|
<Td>
|
2023-03-13 02:03:23 -07:00
|
|
|
<div className="flex items-center justify-end">
|
2022-08-18 13:50:34 -07:00
|
|
|
<ActionsMenu bank={bank} mangoAccount={mangoAccount} />
|
2022-07-19 22:49:09 -07:00
|
|
|
</div>
|
2022-11-20 02:44:14 -08:00
|
|
|
</Td>
|
|
|
|
</TrBody>
|
2022-07-19 22:49:09 -07:00
|
|
|
)
|
|
|
|
})}
|
|
|
|
</tbody>
|
2022-11-20 02:44:14 -08:00
|
|
|
</Table>
|
2022-07-19 22:49:09 -07:00
|
|
|
) : (
|
2023-03-18 05:15:01 -07:00
|
|
|
<div className="border-b border-th-bkg-3">
|
2023-01-29 20:13:38 -08:00
|
|
|
{filteredBanks.map((b) => {
|
|
|
|
return <MobileTokenListItem key={b.bank.name} bank={b} />
|
2022-07-14 18:46:34 -07:00
|
|
|
})}
|
2022-07-19 22:49:09 -07:00
|
|
|
</div>
|
|
|
|
)}
|
2022-07-24 20:28:10 -07:00
|
|
|
</ContentBox>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default TokenList
|
|
|
|
|
2023-01-29 20:13:38 -08:00
|
|
|
const MobileTokenListItem = ({ bank }: { bank: BankWithBalance }) => {
|
2022-10-11 04:59:01 -07:00
|
|
|
const { t } = useTranslation(['common', 'token'])
|
2022-11-18 11:11:06 -08:00
|
|
|
const { mangoTokens } = useJupiterMints()
|
2022-10-06 18:57:47 -07:00
|
|
|
const spotBalances = mangoStore((s) => s.mangoAccount.spotBalances)
|
2022-11-18 09:09:39 -08:00
|
|
|
const { mangoAccount } = useMangoAccount()
|
2022-09-07 19:36:39 -07:00
|
|
|
const totalInterestData = mangoStore(
|
2023-01-15 21:13:34 -08:00
|
|
|
(s) => s.mangoAccount.interestTotals.data
|
2022-09-07 19:36:39 -07:00
|
|
|
)
|
2023-01-29 20:13:38 -08:00
|
|
|
const tokenBank = bank.bank
|
|
|
|
const mint = tokenBank.mint
|
|
|
|
const symbol = tokenBank.name
|
2022-09-07 19:36:39 -07:00
|
|
|
|
2023-03-18 05:15:01 -07:00
|
|
|
let logoURI: string | undefined
|
2022-11-18 11:11:06 -08:00
|
|
|
if (mangoTokens?.length) {
|
|
|
|
logoURI = mangoTokens.find(
|
2023-01-29 20:13:38 -08:00
|
|
|
(t) => t.address === tokenBank.mint.toString()
|
2022-09-02 11:36:57 -07:00
|
|
|
)!.logoURI
|
|
|
|
}
|
2022-09-07 19:36:39 -07:00
|
|
|
|
2023-01-29 20:13:38 -08:00
|
|
|
const hasInterestEarned = totalInterestData.find((d) => d.symbol === symbol)
|
2022-09-07 19:36:39 -07:00
|
|
|
|
|
|
|
const interestAmount = hasInterestEarned
|
2023-01-05 20:19:36 -08:00
|
|
|
? hasInterestEarned.borrow_interest * -1 +
|
|
|
|
hasInterestEarned.deposit_interest
|
2022-09-07 19:36:39 -07:00
|
|
|
: 0
|
|
|
|
|
|
|
|
const interestValue = hasInterestEarned
|
2023-01-05 20:19:36 -08:00
|
|
|
? hasInterestEarned.borrow_interest_usd * -1 +
|
2022-09-07 19:36:39 -07:00
|
|
|
hasInterestEarned.deposit_interest_usd
|
2022-11-21 21:14:35 -08:00
|
|
|
: 0
|
2022-11-20 21:01:44 -08:00
|
|
|
|
2023-01-29 20:13:38 -08:00
|
|
|
const tokenBalance = bank.balance
|
2023-01-24 02:56:04 -08:00
|
|
|
|
2023-01-29 20:13:38 -08:00
|
|
|
const inOrders = spotBalances[mint.toString()]?.inOrders || 0
|
2022-10-06 18:57:47 -07:00
|
|
|
|
2023-01-29 20:13:38 -08:00
|
|
|
const unsettled = spotBalances[mint.toString()]?.unsettled || 0
|
2022-10-06 18:57:47 -07:00
|
|
|
|
2022-09-02 11:36:57 -07:00
|
|
|
return (
|
2023-03-18 05:15:01 -07:00
|
|
|
<Disclosure>
|
|
|
|
{({ open }) => (
|
|
|
|
<>
|
|
|
|
<Disclosure.Button
|
2023-03-19 03:28:13 -07:00
|
|
|
className={`w-full border-t border-th-bkg-3 p-4 text-left first:border-t-0 focus:outline-none`}
|
2023-03-18 05:15:01 -07:00
|
|
|
>
|
|
|
|
<div className="flex items-center justify-between">
|
|
|
|
<div className="flex items-start">
|
|
|
|
<div className="mr-2.5 mt-0.5 flex flex-shrink-0 items-center">
|
|
|
|
{logoURI ? (
|
|
|
|
<Image alt="" width="24" height="24" src={logoURI} />
|
|
|
|
) : (
|
|
|
|
<QuestionMarkCircleIcon className="h-7 w-7 text-th-fgd-3" />
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<p className="mb-0.5 leading-none text-th-fgd-1">{symbol}</p>
|
|
|
|
<p className="font-mono text-sm text-th-fgd-2">
|
|
|
|
<FormatNumericValue
|
|
|
|
value={tokenBalance}
|
|
|
|
decimals={tokenBank.mintDecimals}
|
|
|
|
/>
|
|
|
|
<span className="mt-0.5 block text-sm leading-none text-th-fgd-4">
|
|
|
|
<FormatNumericValue
|
|
|
|
value={
|
|
|
|
mangoAccount ? tokenBalance * tokenBank.uiPrice : 0
|
|
|
|
}
|
|
|
|
decimals={2}
|
|
|
|
isUsd
|
|
|
|
/>
|
|
|
|
</span>
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="flex items-center space-x-3">
|
|
|
|
<ActionsMenu bank={tokenBank} mangoAccount={mangoAccount} />
|
|
|
|
<ChevronDownIcon
|
|
|
|
className={`${
|
|
|
|
open ? 'rotate-180' : 'rotate-360'
|
2023-03-19 03:28:13 -07:00
|
|
|
} h-6 w-6 flex-shrink-0 text-th-fgd-3`}
|
2023-01-24 02:56:04 -08:00
|
|
|
/>
|
2023-03-18 05:15:01 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Disclosure.Button>
|
|
|
|
<Transition
|
|
|
|
enter="transition ease-in duration-200"
|
|
|
|
enterFrom="opacity-0"
|
|
|
|
enterTo="opacity-100"
|
|
|
|
>
|
|
|
|
<Disclosure.Panel>
|
2023-03-19 03:28:13 -07:00
|
|
|
<div className="mx-4 grid grid-cols-2 gap-4 border-t border-th-bkg-3 pt-4 pb-4">
|
2023-03-18 05:15:01 -07:00
|
|
|
<div className="col-span-1">
|
|
|
|
<p className="text-xs text-th-fgd-3">
|
|
|
|
{t('trade:in-orders')}
|
|
|
|
</p>
|
|
|
|
<BankAmountWithValue amount={inOrders} bank={tokenBank} />
|
|
|
|
</div>
|
|
|
|
<div className="col-span-1">
|
|
|
|
<p className="text-xs text-th-fgd-3">
|
|
|
|
{t('trade:unsettled')}
|
|
|
|
</p>
|
|
|
|
<BankAmountWithValue amount={unsettled} bank={tokenBank} />
|
|
|
|
</div>
|
|
|
|
<div className="col-span-1">
|
|
|
|
<p className="text-xs text-th-fgd-3">
|
|
|
|
{t('interest-earned-paid')}
|
|
|
|
</p>
|
|
|
|
<BankAmountWithValue
|
|
|
|
amount={interestAmount}
|
|
|
|
bank={tokenBank}
|
|
|
|
value={interestValue}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="col-span-1">
|
|
|
|
<p className="text-xs text-th-fgd-3">{t('rates')}</p>
|
|
|
|
<p className="space-x-2 font-mono">
|
|
|
|
<span className="text-th-up">
|
|
|
|
<FormatNumericValue
|
|
|
|
value={tokenBank.getDepositRateUi()}
|
|
|
|
decimals={2}
|
|
|
|
/>
|
|
|
|
%
|
|
|
|
</span>
|
|
|
|
<span className="font-normal text-th-fgd-4">|</span>
|
|
|
|
<span className="text-th-down">
|
|
|
|
<FormatNumericValue
|
|
|
|
value={tokenBank.getBorrowRateUi()}
|
|
|
|
decimals={2}
|
|
|
|
/>
|
|
|
|
%
|
|
|
|
</span>
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Disclosure.Panel>
|
|
|
|
</Transition>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Disclosure>
|
2022-09-02 11:36:57 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-07-24 20:28:10 -07:00
|
|
|
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)
|
2022-11-16 04:14:53 -08:00
|
|
|
const [showRepayModal, setShowRepayModal] = useState(false)
|
2022-07-24 20:28:10 -07:00
|
|
|
const [selectedToken, setSelectedToken] = useState('')
|
2023-01-04 18:50:36 -08:00
|
|
|
const set = mangoStore.getState().set
|
|
|
|
const router = useRouter()
|
2022-11-18 11:11:06 -08:00
|
|
|
const { mangoTokens } = useJupiterMints()
|
2023-01-04 20:06:19 -08:00
|
|
|
const spotMarkets = mangoStore((s) => s.serumMarkets)
|
2023-03-04 11:17:34 -08:00
|
|
|
const { isUnownedAccount } = useUnownedAccount()
|
2023-01-04 20:06:19 -08:00
|
|
|
|
|
|
|
const spotMarket = useMemo(() => {
|
|
|
|
return spotMarkets.find((m) => {
|
|
|
|
const base = m.name.split('/')[0]
|
|
|
|
return base.toUpperCase() === bank.name.toUpperCase()
|
|
|
|
})
|
|
|
|
}, [spotMarkets])
|
2022-07-24 20:28:10 -07:00
|
|
|
|
2022-08-18 13:50:34 -07:00
|
|
|
const handleShowActionModals = useCallback(
|
2022-11-16 04:14:53 -08:00
|
|
|
(token: string, action: 'borrow' | 'deposit' | 'withdraw' | 'repay') => {
|
2022-08-18 13:50:34 -07:00
|
|
|
setSelectedToken(token)
|
|
|
|
action === 'borrow'
|
|
|
|
? setShowBorrowModal(true)
|
|
|
|
: action === 'deposit'
|
|
|
|
? setShowDepositModal(true)
|
2022-11-16 04:14:53 -08:00
|
|
|
: action === 'withdraw'
|
|
|
|
? setShowWithdrawModal(true)
|
|
|
|
: setShowRepayModal(true)
|
2022-08-18 13:50:34 -07:00
|
|
|
},
|
|
|
|
[]
|
|
|
|
)
|
2022-07-24 20:28:10 -07:00
|
|
|
|
2023-01-04 18:50:36 -08:00
|
|
|
const balance = useMemo(() => {
|
|
|
|
if (!mangoAccount || !bank) return 0
|
|
|
|
return mangoAccount.getTokenBalanceUi(bank)
|
|
|
|
}, [bank, mangoAccount])
|
|
|
|
|
|
|
|
const handleSwap = useCallback(() => {
|
|
|
|
const tokenInfo = mangoTokens.find(
|
2023-02-25 16:22:39 -08:00
|
|
|
(t) => t.address === bank.mint.toString()
|
2023-01-04 18:50:36 -08:00
|
|
|
)
|
|
|
|
const group = mangoStore.getState().group
|
|
|
|
if (balance && balance > 0) {
|
|
|
|
if (tokenInfo?.symbol === 'SOL') {
|
2023-02-25 16:22:39 -08:00
|
|
|
const usdcTokenInfo = mangoTokens.find((t) => t.address === USDC_MINT)
|
2023-01-04 18:50:36 -08:00
|
|
|
const usdcBank = group?.getFirstBankByMint(new PublicKey(USDC_MINT))
|
|
|
|
set((s) => {
|
|
|
|
s.swap.inputBank = usdcBank
|
|
|
|
s.swap.inputTokenInfo = usdcTokenInfo
|
|
|
|
})
|
|
|
|
}
|
|
|
|
set((s) => {
|
|
|
|
s.swap.inputBank = bank
|
|
|
|
s.swap.inputTokenInfo = tokenInfo
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
if (tokenInfo?.symbol === 'USDC') {
|
|
|
|
const solTokenInfo = mangoTokens.find(
|
2023-02-25 16:22:39 -08:00
|
|
|
(t) => t.address === WRAPPED_SOL_MINT.toString()
|
2023-01-04 18:50:36 -08:00
|
|
|
)
|
|
|
|
const solBank = group?.getFirstBankByMint(WRAPPED_SOL_MINT)
|
|
|
|
set((s) => {
|
|
|
|
s.swap.inputBank = solBank
|
|
|
|
s.swap.inputTokenInfo = solTokenInfo
|
|
|
|
})
|
|
|
|
}
|
|
|
|
set((s) => {
|
|
|
|
s.swap.outputBank = bank
|
|
|
|
s.swap.outputTokenInfo = tokenInfo
|
|
|
|
})
|
|
|
|
}
|
|
|
|
router.push('/swap', undefined, { shallow: true })
|
|
|
|
}, [bank, router, set, mangoTokens, mangoAccount])
|
2022-07-25 22:27:53 -07:00
|
|
|
|
2023-01-04 20:06:19 -08:00
|
|
|
const handleTrade = useCallback(() => {
|
|
|
|
router.push(`/trade?name=${spotMarket?.name}`, undefined, { shallow: true })
|
|
|
|
}, [spotMarket, router])
|
|
|
|
|
2022-10-05 19:12:41 -07:00
|
|
|
const logoURI = useMemo(() => {
|
2022-11-18 11:11:06 -08:00
|
|
|
if (!bank || !mangoTokens?.length) return ''
|
|
|
|
return mangoTokens.find((t) => t.address === bank.mint.toString())?.logoURI
|
|
|
|
}, [bank, mangoTokens])
|
2022-10-05 19:12:41 -07:00
|
|
|
|
2022-07-24 20:28:10 -07:00
|
|
|
return (
|
|
|
|
<>
|
2023-02-12 18:46:41 -08:00
|
|
|
{isUnownedAccount ? null : (
|
2023-01-05 00:52:16 -08:00
|
|
|
<IconDropMenu
|
|
|
|
icon={<EllipsisHorizontalIcon className="h-5 w-5" />}
|
2023-03-13 02:03:23 -07:00
|
|
|
panelClassName="w-40 shadow-md"
|
2023-01-05 00:52:16 -08:00
|
|
|
postion="leftBottom"
|
2022-11-20 18:22:45 -08:00
|
|
|
>
|
2023-01-05 00:52:16 -08:00
|
|
|
<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={logoURI || ''} />
|
|
|
|
</div>
|
2023-01-14 04:41:30 -08:00
|
|
|
<p className="font-body">{formatTokenSymbol(bank.name)}</p>
|
2023-01-05 00:52:16 -08:00
|
|
|
</div>
|
2023-01-05 01:04:09 -08:00
|
|
|
<ActionsLinkButton
|
|
|
|
mangoAccount={mangoAccount!}
|
2023-01-05 00:52:16 -08:00
|
|
|
onClick={() => handleShowActionModals(bank.name, 'deposit')}
|
2022-11-16 04:14:53 -08:00
|
|
|
>
|
2023-01-05 00:52:16 -08:00
|
|
|
{t('deposit')}
|
2023-01-05 01:04:09 -08:00
|
|
|
</ActionsLinkButton>
|
2023-01-20 15:21:57 -08:00
|
|
|
{balance < 0 ? (
|
2023-01-05 01:04:09 -08:00
|
|
|
<ActionsLinkButton
|
|
|
|
mangoAccount={mangoAccount!}
|
2023-01-05 00:52:16 -08:00
|
|
|
onClick={() => handleShowActionModals(bank.name, 'repay')}
|
|
|
|
>
|
|
|
|
{t('repay')}
|
2023-01-05 01:04:09 -08:00
|
|
|
</ActionsLinkButton>
|
2023-01-05 00:52:16 -08:00
|
|
|
) : null}
|
|
|
|
{balance && balance > 0 ? (
|
2023-01-05 01:04:09 -08:00
|
|
|
<ActionsLinkButton
|
|
|
|
mangoAccount={mangoAccount!}
|
2023-01-05 00:52:16 -08:00
|
|
|
onClick={() => handleShowActionModals(bank.name, 'withdraw')}
|
|
|
|
>
|
|
|
|
{t('withdraw')}
|
2023-01-05 01:04:09 -08:00
|
|
|
</ActionsLinkButton>
|
2023-01-05 00:52:16 -08:00
|
|
|
) : null}
|
2023-01-05 01:04:09 -08:00
|
|
|
<ActionsLinkButton
|
|
|
|
mangoAccount={mangoAccount!}
|
2023-01-05 00:52:16 -08:00
|
|
|
onClick={() => handleShowActionModals(bank.name, 'borrow')}
|
2023-01-04 18:50:36 -08:00
|
|
|
>
|
2023-01-05 00:52:16 -08:00
|
|
|
{t('borrow')}
|
2023-01-05 01:04:09 -08:00
|
|
|
</ActionsLinkButton>
|
|
|
|
<ActionsLinkButton mangoAccount={mangoAccount!} onClick={handleSwap}>
|
2023-01-05 00:52:16 -08:00
|
|
|
{t('swap')}
|
2023-01-05 01:04:09 -08:00
|
|
|
</ActionsLinkButton>
|
2023-01-05 00:52:16 -08:00
|
|
|
{spotMarket ? (
|
2023-01-05 01:04:09 -08:00
|
|
|
<ActionsLinkButton
|
|
|
|
mangoAccount={mangoAccount!}
|
2023-01-05 00:52:16 -08:00
|
|
|
onClick={handleTrade}
|
|
|
|
>
|
|
|
|
{t('trade')}
|
2023-01-05 01:04:09 -08:00
|
|
|
</ActionsLinkButton>
|
2023-01-05 00:52:16 -08:00
|
|
|
) : null}
|
|
|
|
</IconDropMenu>
|
|
|
|
)}
|
2022-07-24 19:55:10 -07:00
|
|
|
{showDepositModal ? (
|
2022-12-17 04:37:00 -08:00
|
|
|
<DepositWithdrawModal
|
|
|
|
action="deposit"
|
2022-07-24 19:55:10 -07:00
|
|
|
isOpen={showDepositModal}
|
|
|
|
onClose={() => setShowDepositModal(false)}
|
|
|
|
token={selectedToken}
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
{showWithdrawModal ? (
|
2022-12-17 04:37:00 -08:00
|
|
|
<DepositWithdrawModal
|
|
|
|
action="withdraw"
|
2022-07-24 19:55:10 -07:00
|
|
|
isOpen={showWithdrawModal}
|
|
|
|
onClose={() => setShowWithdrawModal(false)}
|
|
|
|
token={selectedToken}
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
{showBorrowModal ? (
|
2022-12-18 04:30:49 -08:00
|
|
|
<BorrowRepayModal
|
|
|
|
action="borrow"
|
2022-07-24 19:55:10 -07:00
|
|
|
isOpen={showBorrowModal}
|
|
|
|
onClose={() => setShowBorrowModal(false)}
|
|
|
|
token={selectedToken}
|
|
|
|
/>
|
|
|
|
) : null}
|
2022-11-16 04:14:53 -08:00
|
|
|
{showRepayModal ? (
|
2022-12-18 04:30:49 -08:00
|
|
|
<BorrowRepayModal
|
|
|
|
action="repay"
|
2022-11-16 04:14:53 -08:00
|
|
|
isOpen={showRepayModal}
|
|
|
|
onClose={() => setShowRepayModal(false)}
|
|
|
|
token={selectedToken}
|
|
|
|
/>
|
|
|
|
) : null}
|
2022-07-24 20:28:10 -07:00
|
|
|
</>
|
2022-05-03 21:20:14 -07:00
|
|
|
)
|
|
|
|
}
|