mango-ui-v3/components/account_page/AccountOverview.tsx

228 lines
8.4 KiB
TypeScript
Raw Normal View History

2021-09-03 05:11:21 -07:00
import { useMemo } from 'react'
2021-07-29 06:19:32 -07:00
import {
2021-08-16 10:00:43 -07:00
// ChartBarIcon,
ScaleIcon,
2021-07-29 06:19:32 -07:00
CurrencyDollarIcon,
2021-08-05 07:07:29 -07:00
GiftIcon,
2021-07-29 06:19:32 -07:00
HeartIcon,
} from '@heroicons/react/outline'
2021-09-03 05:11:21 -07:00
import { nativeToUi, ZERO_BN } from '@blockworks-foundation/mango-client'
2021-09-13 14:21:58 -07:00
import useMangoStore, { MNGO_INDEX } from '../../stores/useMangoStore'
2021-09-03 05:11:21 -07:00
import { formatUsdValue } from '../../utils'
2021-08-01 05:48:15 -07:00
import { notify } from '../../utils/notifications'
2021-09-03 05:11:21 -07:00
import { LinkButton } from '../Button'
import BalancesTable from '../BalancesTable'
import PositionsTable from '../PerpPositionsTable'
import Switch from '../Switch'
import useLocalStorageState from '../../hooks/useLocalStorageState'
import { ExclamationIcon } from '@heroicons/react/solid'
import { useTranslation } from 'next-i18next'
2021-07-25 06:54:25 -07:00
const SHOW_ZERO_BALANCE_KEY = 'showZeroAccountBalances-0.2'
2021-07-25 06:54:25 -07:00
export default function AccountOverview() {
const { t } = useTranslation('common')
2021-08-01 05:48:15 -07:00
const actions = useMangoStore((s) => s.actions)
2021-07-25 06:54:25 -07:00
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache)
2021-09-13 14:21:58 -07:00
const mangoClient = useMangoStore((s) => s.connection.client)
const [showZeroBalances, setShowZeroBalances] = useLocalStorageState(
SHOW_ZERO_BALANCE_KEY,
true
)
2021-07-29 06:19:32 -07:00
2021-08-05 07:07:29 -07:00
const maintHealthRatio = useMemo(() => {
return mangoAccount
? mangoAccount.getHealthRatio(mangoGroup, mangoCache, 'Maint')
: 100
}, [mangoAccount, mangoGroup, mangoCache])
const initHealthRatio = useMemo(() => {
return mangoAccount
? mangoAccount.getHealthRatio(mangoGroup, mangoCache, 'Init')
: 100
}, [mangoAccount, mangoGroup, mangoCache])
const mngoAccrued = useMemo(() => {
return mangoAccount
? mangoAccount.perpAccounts.reduce((acc, perpAcct) => {
return perpAcct.mngoAccrued.add(acc)
}, ZERO_BN)
: ZERO_BN
}, [mangoAccount])
const handleRedeemMngo = async () => {
const wallet = useMangoStore.getState().wallet.current
const mngoNodeBank =
mangoGroup.rootBankAccounts[MNGO_INDEX].nodeBankAccounts[0]
try {
const txid = await mangoClient.redeemAllMngo(
mangoGroup,
mangoAccount,
wallet,
mangoGroup.tokens[MNGO_INDEX].rootBank,
mngoNodeBank.publicKey,
mngoNodeBank.vault
)
actions.reloadMangoAccount()
2021-08-05 07:07:29 -07:00
notify({
title: t('redeem-success'),
2021-08-05 07:07:29 -07:00
description: '',
txid,
})
} catch (e) {
notify({
title: t('redeem-failure'),
2021-08-05 07:07:29 -07:00
description: e.message,
txid: e.txid,
type: 'error',
})
}
}
2021-07-25 06:54:25 -07:00
return mangoAccount ? (
<>
<div className="grid grid-flow-col grid-cols-2 grid-rows-2 lg:grid-cols-4 lg:grid-rows-1 gap-2 sm:gap-4 pb-8">
<div className="border border-th-bkg-4 p-3 sm:p-4 rounded-md sm:rounded-lg">
<div className="pb-0.5 sm:pb-2 text-th-fgd-3 text-xs sm:text-sm">
{t('account-value')}
</div>
<div className="flex items-center pb-1 sm:pb-3">
<CurrencyDollarIcon className="flex-shrink-0 h-5 w-5 sm:h-7 sm:w-7 mr-1.5 text-th-primary" />
<div className="font-bold text-th-fgd-1 text-xl sm:text-2xl">
2021-08-15 06:31:59 -07:00
{formatUsdValue(
+mangoAccount.computeValue(mangoGroup, mangoCache)
2021-08-01 05:48:15 -07:00
)}
</div>
2021-08-01 05:48:15 -07:00
</div>
</div>
{/* <div className="border border-th-bkg-4 p-3 sm:p-4 rounded-md sm:rounded-lg">
<div className="pb-0.5 sm:pb-2 text-th-fgd-3 text-xs sm:text-sm">PNL</div>
<div className="flex items-center pb-1 sm:pb-3">
<ChartBarIcon className="flex-shrink-0 h-5 w-5 sm:h-7 sm:w-7 mr-1.5 text-th-primary" />
<div className="font-bold text-th-fgd-1 text-xl sm:text-2xl">
2021-08-15 06:31:59 -07:00
{formatUsdValue(
+mangoAccount.computeValue(mangoGroup, mangoCache)
2021-08-01 05:48:15 -07:00
)}
</div>
2021-08-01 05:48:15 -07:00
</div>
2021-08-16 10:00:43 -07:00
</div> */}
<div className="border border-th-bkg-4 p-3 sm:p-4 rounded-md sm:rounded-lg">
<div className="pb-0.5 sm:pb-2 text-th-fgd-3 text-xs sm:text-sm">
{t('leverage')}
</div>
<div className="flex items-center pb-1 sm:pb-3">
<ScaleIcon className="flex-shrink-0 h-5 w-5 sm:h-7 sm:w-7 mr-1.5 text-th-primary" />
<div className="font-bold text-th-fgd-1 text-xl sm:text-2xl">
2021-08-16 10:00:43 -07:00
{mangoAccount.getLeverage(mangoGroup, mangoCache).toFixed(2)}x
</div>
2021-08-16 10:00:43 -07:00
</div>
2021-07-25 06:54:25 -07:00
</div>
<div className="border border-th-bkg-4 p-3 sm:p-4 rounded-md sm:rounded-lg">
<div className="pb-0.5 sm:pb-2 text-th-fgd-3 text-xs sm:text-sm">
{t('health-ratio')}
</div>
<div className="flex items-center pb-3 sm:pb-4">
<HeartIcon className="flex-shrink-0 h-5 w-5 sm:h-7 sm:w-7 mr-1.5 text-th-primary" />
<div className="font-bold text-th-fgd-1 text-xl sm:text-2xl">
{maintHealthRatio < 1000 ? maintHealthRatio.toFixed(2) : '>100'}%
</div>
2021-07-25 06:54:25 -07:00
</div>
2021-08-05 07:07:29 -07:00
<div className="h-1.5 flex rounded bg-th-bkg-3">
<div
style={{
width: `${maintHealthRatio}%`,
}}
className={`flex rounded ${
maintHealthRatio > 30
? 'bg-th-green'
: initHealthRatio > 0
? 'bg-th-orange'
: 'bg-th-red'
}`}
></div>
2021-07-25 06:54:25 -07:00
</div>
{mangoAccount.beingLiquidated ? (
<div className="pt-0.5 sm:pt-2 text-xs sm:text-sm flex items-center">
<ExclamationIcon className="flex-shrink-0 h-5 w-5 sm:h-7 sm:w-7 mr-1.5 text-th-red" />
<span className="text-th-red">{t('being-liquidated')}</span>
</div>
) : null}
2021-07-25 06:54:25 -07:00
</div>
<div className="border border-th-bkg-4 p-3 sm:p-4 rounded-md sm:rounded-lg">
<div className="pb-0.5 sm:pb-2 text-th-fgd-3 text-xs sm:text-sm">
{t('mngo-rewards')}
</div>
<div className="flex items-center pb-1 sm:pb-2">
<GiftIcon className="flex-shrink-0 h-5 w-5 sm:h-7 sm:w-7 mr-1.5 text-th-primary" />
<div className="font-bold text-th-fgd-1 text-xl sm:text-2xl">
2021-08-05 07:07:29 -07:00
{mangoGroup
? nativeToUi(
mngoAccrued.toNumber(),
mangoGroup.tokens[MNGO_INDEX].decimals
)
: 0}
</div>
2021-07-25 06:54:25 -07:00
</div>
2021-08-05 07:07:29 -07:00
<LinkButton
onClick={handleRedeemMngo}
disabled={mngoAccrued.eq(ZERO_BN)}
className="text-th-primary text-xs"
>
{t('claim-reward')}
2021-08-05 07:07:29 -07:00
</LinkButton>
2021-07-25 06:54:25 -07:00
</div>
</div>
2021-07-29 06:19:32 -07:00
<div className="pb-8">
2021-11-09 01:24:43 -08:00
<div className="text-th-fgd-1 text-lg md:pb-2">
{t('perp-positions')}
</div>
2021-07-29 06:19:32 -07:00
<PositionsTable />
</div>
<div className="pb-4 text-th-fgd-1 text-lg">
{t('assets-liabilities')}
</div>
2021-08-05 07:07:29 -07:00
<div className="grid grid-flow-col grid-cols-1 grid-rows-2 md:grid-cols-2 md:grid-rows-1 gap-2 sm:gap-4 pb-8">
<div className="border border-th-bkg-4 p-3 sm:p-4 rounded-md sm:rounded-lg">
<div className="pb-0.5 text-xs text-th-fgd-3">
{t('total-assets')}
</div>
2021-08-05 07:07:29 -07:00
<div className="flex items-center">
<div className="text-lg text-th-fgd-1">
2021-08-15 06:31:59 -07:00
{formatUsdValue(
2021-08-05 07:07:29 -07:00
+mangoAccount.getAssetsVal(mangoGroup, mangoCache)
)}
</div>
</div>
</div>
<div className="border border-th-bkg-4 p-3 sm:p-4 rounded-md sm:rounded-lg">
2021-08-05 07:07:29 -07:00
<div className="pb-0.5 text-xs text-th-fgd-3">
{t('total-liabilities')}
2021-08-05 07:07:29 -07:00
</div>
<div className="flex items-center">
<div className="text-lg text-th-fgd-1">
2021-08-15 06:31:59 -07:00
{formatUsdValue(
2021-08-05 07:07:29 -07:00
+mangoAccount.getLiabsVal(mangoGroup, mangoCache)
)}
</div>
</div>
</div>
</div>
2021-11-09 01:24:43 -08:00
<div className="flex justify-between pb-4 sm:pb-0 md:pb-2">
<div className="text-th-fgd-1 text-lg">Balances</div>
<Switch
checked={showZeroBalances}
className="text-xs"
onChange={() => setShowZeroBalances(!showZeroBalances)}
>
{t('show-zero')}
</Switch>
</div>
<BalancesTable showZeroBalances={showZeroBalances} />
2021-07-25 06:54:25 -07:00
</>
) : null
}