import { HealthType, I80F48, toUiDecimalsForQuote, ZERO_I80F48, } from '@blockworks-foundation/mango-v4' import { useTranslation } from 'next-i18next' import { serverSideTranslations } from 'next-i18next/serverSideTranslations' import { useEffect, useMemo, useState } from 'react' import AccountActions from './AccountActions' import DepositModal from '../modals/DepositModal' import WithdrawModal from '../modals/WithdrawModal' import mangoStore, { PerformanceDataItem } from '@store/mangoStore' import { formatDecimal, formatFixedDecimals } from '../../utils/numbers' import FlipNumbers from 'react-flip-numbers' import { DownTriangle, UpTriangle } from '../shared/DirectionTriangles' import SimpleAreaChart from '../shared/SimpleAreaChart' import { COLORS } from '../../styles/colors' import { useTheme } from 'next-themes' import { IconButton } from '../shared/Button' import { ArrowsPointingOutIcon, ChevronRightIcon, } from '@heroicons/react/20/solid' import { Transition } from '@headlessui/react' import AccountTabs from './AccountTabs' import SheenLoader from '../shared/SheenLoader' import AccountChart from './AccountChart' import { useViewport } from '../../hooks/useViewport' import { breakpoints } from '../../utils/theme' import useMangoAccount from '../shared/useMangoAccount' import PercentageChange from '../shared/PercentageChange' import Tooltip from '@components/shared/Tooltip' export async function getStaticProps({ locale }: { locale: string }) { return { props: { ...(await serverSideTranslations(locale, [ 'common', 'close-account', 'trade', ])), }, } } const AccountPage = () => { const { t } = useTranslation('common') const { mangoAccount, lastUpdatedAt } = useMangoAccount() const actions = mangoStore((s) => s.actions) const loadPerformanceData = mangoStore( (s) => s.mangoAccount.stats.performance.loading ) const performanceData = mangoStore( (s) => s.mangoAccount.stats.performance.data ) const totalInterestData = mangoStore( (s) => s.mangoAccount.stats.interestTotals.data ) const [showDepositModal, setShowDepositModal] = useState(false) const [showWithdrawModal, setShowWithdrawModal] = useState(false) const [chartToShow, setChartToShow] = useState< 'account-value' | 'cumulative-interest-value' | 'pnl' | '' >('') const [oneDayPerformanceData, setOneDayPerformanceData] = useState< PerformanceDataItem[] >([]) const [showExpandChart, setShowExpandChart] = useState(false) const { theme } = useTheme() const { width } = useViewport() const isMobile = width ? width < breakpoints.md : false useEffect(() => { if (mangoAccount) { const pubKey = mangoAccount.publicKey.toString() actions.fetchAccountPerformance(pubKey, 1) actions.fetchAccountInterestTotals(pubKey) } }, [actions, mangoAccount]) useEffect(() => { if (!oneDayPerformanceData.length && performanceData.length) { setOneDayPerformanceData(performanceData) } }, [oneDayPerformanceData, performanceData]) const onHoverMenu = (open: boolean, action: string) => { if ( (!open && action === 'onMouseEnter') || (open && action === 'onMouseLeave') ) { setShowExpandChart(!open) } } const handleShowAccountValueChart = () => { setChartToShow('account-value') setShowExpandChart(false) } const handleHideChart = () => { const set = mangoStore.getState().set set((s) => { s.mangoAccount.stats.performance.data = oneDayPerformanceData }) setChartToShow('') } const { accountPnl, accountValueChange } = useMemo(() => { if (performanceData.length && mangoAccount) { return { accountPnl: performanceData[performanceData.length - 1].pnl, accountValueChange: ((toUiDecimalsForQuote(mangoAccount.getEquity()!.toNumber()) - performanceData[0].account_equity) / performanceData[0].account_equity) * 100, } } return { accountPnl: 0, accountValueChange: 0 } }, [performanceData, mangoAccount]) const interestTotalValue = useMemo(() => { if (totalInterestData.length) { return totalInterestData.reduce( (a, c) => a + c.borrow_interest_usd + c.deposit_interest_usd, 0 ) } return 0 }, [totalInterestData]) const maintHealth = useMemo(() => { return mangoAccount ? mangoAccount.getHealthRatioUi(HealthType.maint) : 0 }, [mangoAccount]) return !chartToShow ? ( <>

{t('account-value')}

$ {mangoAccount ? ( ) : ( )}
{!loadPerformanceData ? ( mangoAccount && performanceData.length ? (
onHoverMenu(showExpandChart, 'onMouseEnter') } onMouseLeave={() => onHoverMenu(showExpandChart, 'onMouseLeave') } > = 0 ? COLORS.GREEN[theme] : COLORS.RED[theme] } data={performanceData} height={88} name="accountValue" width={180} xKey="time" yKey="account_equity" /> handleShowAccountValueChart()} >
) : null ) : (
)}

Health describes how close your account is to liquidation. The lower your account health is the more likely you are to get liquidated when prices fluctuate.

Your account health is {maintHealth}%

Scenario:{' '} If the prices of all your liabilities increase by{' '} {maintHealth}%, even for just a moment, some of your liabilities will be liquidated.

Scenario:{' '} If the value of your total collateral decreases by{' '} {((1 - 1 / ((maintHealth || 0) / 100 + 1)) * 100).toFixed( 2 )} % , some of your liabilities will be liquidated.

These are examples. A combination of events can also lead to liquidation.

} >

{t('health')}

{maintHealth}%

{t('free-collateral')}

{mangoAccount ? formatFixedDecimals( toUiDecimalsForQuote( mangoAccount.getCollateralValue()!.toNumber() ), true ) : (0).toFixed(2)}

{t('pnl')}

{formatFixedDecimals(accountPnl, true)}

{performanceData.length > 4 ? ( setChartToShow('pnl')} size={!isMobile ? 'small' : 'medium'} > ) : null}

{t('total-interest-value')}

{formatFixedDecimals(interestTotalValue, true)}

{interestTotalValue > 1 || interestTotalValue < -1 ? ( setChartToShow('cumulative-interest-value')} size={!isMobile ? 'small' : 'medium'} > ) : null}
{showDepositModal ? ( setShowDepositModal(false)} /> ) : null} {showWithdrawModal ? ( setShowWithdrawModal(false)} /> ) : null} ) : (
{chartToShow === 'account-value' ? ( ) : chartToShow === 'pnl' ? ( ) : ( ({ interest_value: d.borrow_interest_cumulative_usd + d.deposit_interest_cumulative_usd, time: d.time, }))} hideChart={handleHideChart} mangoAccount={mangoAccount!} yKey="interest_value" /> )}
) } export default AccountPage