import { useMemo } from 'react' import { // ChartBarIcon, ScaleIcon, CurrencyDollarIcon, GiftIcon, HeartIcon, } from '@heroicons/react/outline' import { nativeToUi, ZERO_BN } from '@blockworks-foundation/mango-client' import useMangoStore, { MNGO_INDEX } from '../../stores/useMangoStore' import { formatUsdValue } from '../../utils' import { notify } from '../../utils/notifications' 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' const SHOW_ZERO_BALANCE_KEY = 'showZeroAccountBalances-0.2' export default function AccountOverview() { const { t } = useTranslation('common') const actions = useMangoStore((s) => s.actions) const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current) const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current) const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache) const mangoClient = useMangoStore((s) => s.connection.client) const [showZeroBalances, setShowZeroBalances] = useLocalStorageState( SHOW_ZERO_BALANCE_KEY, true ) 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() notify({ title: t('redeem-success'), description: '', txid, }) } catch (e) { notify({ title: t('redeem-failure'), description: e.message, txid: e.txid, type: 'error', }) } } return mangoAccount ? ( <>
{t('account-value')}
{formatUsdValue( +mangoAccount.computeValue(mangoGroup, mangoCache) )}
{/*
PNL
{formatUsdValue( +mangoAccount.computeValue(mangoGroup, mangoCache) )}
*/}
{t('leverage')}
{mangoAccount.getLeverage(mangoGroup, mangoCache).toFixed(2)}x
{t('health-ratio')}
{maintHealthRatio < 1000 ? maintHealthRatio.toFixed(2) : '>100'}%
30 ? 'bg-th-green' : initHealthRatio > 0 ? 'bg-th-orange' : 'bg-th-red' }`} >
{mangoAccount.beingLiquidated ? (
{t('being-liquidated')}
) : null}
{t('mngo-rewards')}
{mangoGroup ? nativeToUi( mngoAccrued.toNumber(), mangoGroup.tokens[MNGO_INDEX].decimals ) : 0}
{t('claim-reward')}
{t('perp-positions')}
{t('assets-liabilities')}
{t('total-assets')}
{formatUsdValue( +mangoAccount.getAssetsVal(mangoGroup, mangoCache) )}
{t('total-liabilities')}
{formatUsdValue( +mangoAccount.getLiabsVal(mangoGroup, mangoCache) )}
Balances
setShowZeroBalances(!showZeroBalances)} > {t('show-zero')}
) : null }