2022-08-11 12:29:07 -07:00
|
|
|
import {
|
|
|
|
HealthType,
|
|
|
|
toUiDecimalsForQuote,
|
|
|
|
} from '@blockworks-foundation/mango-v4'
|
2022-07-15 04:09:23 -07:00
|
|
|
import { useTranslation } from 'next-i18next'
|
2022-11-18 09:09:39 -08:00
|
|
|
import useMangoAccount from 'hooks/useMangoAccount'
|
2023-01-22 15:54:47 -08:00
|
|
|
import useMangoGroup from 'hooks/useMangoGroup'
|
|
|
|
import { useMemo } from 'react'
|
2023-01-24 16:54:24 -08:00
|
|
|
import FormatNumericValue from '@components/shared/FormatNumericValue'
|
2023-01-22 15:54:47 -08:00
|
|
|
|
2023-01-24 16:54:24 -08:00
|
|
|
const SummaryItem = ({
|
|
|
|
label,
|
|
|
|
value,
|
|
|
|
isUsd,
|
|
|
|
suffix,
|
|
|
|
}: {
|
|
|
|
label: string
|
|
|
|
value: number
|
|
|
|
isUsd?: boolean
|
|
|
|
suffix?: string
|
|
|
|
}) => {
|
2023-01-22 15:54:47 -08:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<p className="text-sm text-th-fgd-3">{label}</p>
|
2023-01-24 16:54:24 -08:00
|
|
|
<p className="font-mono text-sm text-th-fgd-1">
|
|
|
|
<FormatNumericValue value={value} decimals={2} isUsd={isUsd} />
|
|
|
|
{suffix}
|
|
|
|
</p>
|
2023-01-22 15:54:47 -08:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2022-07-14 20:38:02 -07:00
|
|
|
|
2022-10-05 17:04:16 -07:00
|
|
|
const MangoAccountSummary = () => {
|
2022-07-15 04:09:23 -07:00
|
|
|
const { t } = useTranslation('common')
|
2023-01-22 15:54:47 -08:00
|
|
|
const { group } = useMangoGroup()
|
2022-11-18 09:09:39 -08:00
|
|
|
const { mangoAccount } = useMangoAccount()
|
2023-01-22 15:54:47 -08:00
|
|
|
|
2023-06-05 22:29:04 -07:00
|
|
|
const [accountPnl, accountValue, freeCollateral, health] = useMemo(() => {
|
|
|
|
if (!group || !mangoAccount) return [0, 0, 0, 0]
|
|
|
|
const accountPnl = toUiDecimalsForQuote(
|
2023-07-21 11:47:53 -07:00
|
|
|
mangoAccount.getPnl(group).toNumber(),
|
2023-06-05 22:29:04 -07:00
|
|
|
)
|
2023-01-22 15:54:47 -08:00
|
|
|
const accountValue = toUiDecimalsForQuote(
|
2023-07-21 11:47:53 -07:00
|
|
|
mangoAccount.getEquity(group).toNumber(),
|
2023-01-22 15:54:47 -08:00
|
|
|
)
|
|
|
|
const freeCollateral = toUiDecimalsForQuote(
|
2023-07-21 11:47:53 -07:00
|
|
|
mangoAccount.getCollateralValue(group).toNumber(),
|
2023-01-22 15:54:47 -08:00
|
|
|
)
|
|
|
|
const health = mangoAccount.getHealthRatioUi(group, HealthType.maint)
|
2023-06-05 22:29:04 -07:00
|
|
|
return [accountPnl, accountValue, freeCollateral, health]
|
2023-01-22 15:54:47 -08:00
|
|
|
}, [group, mangoAccount])
|
|
|
|
|
|
|
|
const leverage = useMemo(() => {
|
|
|
|
if (!group || !mangoAccount) return 0
|
|
|
|
const assetsValue = toUiDecimalsForQuote(
|
2023-07-21 11:47:53 -07:00
|
|
|
mangoAccount.getAssetsValue(group).toNumber(),
|
2023-01-22 15:54:47 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
if (isNaN(assetsValue / accountValue)) {
|
|
|
|
return 0
|
|
|
|
} else {
|
|
|
|
return Math.abs(1 - assetsValue / accountValue)
|
|
|
|
}
|
|
|
|
}, [mangoAccount, group, accountValue])
|
2022-07-14 20:38:02 -07:00
|
|
|
|
|
|
|
return (
|
2023-01-22 15:54:47 -08:00
|
|
|
<div className="space-y-2">
|
2023-01-24 16:54:24 -08:00
|
|
|
<SummaryItem label={t('account-value')} value={accountValue} isUsd />
|
|
|
|
<SummaryItem label={t('health')} value={health} suffix="%" />
|
|
|
|
<SummaryItem label={t('free-collateral')} value={freeCollateral} isUsd />
|
|
|
|
<SummaryItem label={t('leverage')} value={leverage} suffix="x" />
|
2023-06-05 22:29:04 -07:00
|
|
|
<SummaryItem label={t('pnl')} value={accountPnl} isUsd />
|
2023-01-22 15:54:47 -08:00
|
|
|
</div>
|
2022-07-14 20:38:02 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default MangoAccountSummary
|