add leverage and pnl to account summary
This commit is contained in:
parent
e08cea4a35
commit
c499a0b514
|
@ -6,89 +6,71 @@ import {
|
||||||
import { formatFixedDecimals } from '../../utils/numbers'
|
import { formatFixedDecimals } from '../../utils/numbers'
|
||||||
import { useTranslation } from 'next-i18next'
|
import { useTranslation } from 'next-i18next'
|
||||||
import useMangoAccount from 'hooks/useMangoAccount'
|
import useMangoAccount from 'hooks/useMangoAccount'
|
||||||
|
import useMangoGroup from 'hooks/useMangoGroup'
|
||||||
|
import { useMemo } from 'react'
|
||||||
|
|
||||||
|
const SummaryItem = ({ label, value }: { label: string; value: string }) => {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<p className="text-sm text-th-fgd-3">{label}</p>
|
||||||
|
<p className="font-mono text-sm text-th-fgd-1">{value}</p>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
const MangoAccountSummary = () => {
|
const MangoAccountSummary = () => {
|
||||||
const { t } = useTranslation('common')
|
const { t } = useTranslation('common')
|
||||||
const group = mangoStore.getState().group
|
const { group } = useMangoGroup()
|
||||||
const { mangoAccount } = useMangoAccount()
|
const { mangoAccount } = useMangoAccount()
|
||||||
|
const performanceData = mangoStore((s) => s.mangoAccount.performance.data)
|
||||||
|
|
||||||
// const leverage = useMemo(() => {
|
const [accountValue, freeCollateral, health] = useMemo(() => {
|
||||||
// if (!group || !mangoAccount) return 0
|
if (!group || !mangoAccount) return [0, 0, 0]
|
||||||
// const liabsValue = mangoAccount
|
const accountValue = toUiDecimalsForQuote(
|
||||||
// .getLiabsValue(group, HealthType.init)
|
mangoAccount.getEquity(group).toNumber()
|
||||||
// .toNumber()
|
)
|
||||||
// const totalCollateral = mangoAccount
|
const freeCollateral = toUiDecimalsForQuote(
|
||||||
// .getAssetsValue(group, HealthType.init)
|
mangoAccount.getCollateralValue(group).toNumber()
|
||||||
// .toNumber()
|
)
|
||||||
// if (isNaN(liabsValue / totalCollateral)) {
|
const health = mangoAccount.getHealthRatioUi(group, HealthType.maint)
|
||||||
// return 0
|
return [accountValue, freeCollateral, health]
|
||||||
// } else return liabsValue / totalCollateral
|
}, [group, mangoAccount])
|
||||||
// }, [mangoAccount])
|
|
||||||
|
const leverage = useMemo(() => {
|
||||||
|
if (!group || !mangoAccount) return 0
|
||||||
|
const assetsValue = toUiDecimalsForQuote(
|
||||||
|
mangoAccount.getAssetsValue(group).toNumber()
|
||||||
|
)
|
||||||
|
|
||||||
|
if (isNaN(assetsValue / accountValue)) {
|
||||||
|
return 0
|
||||||
|
} else {
|
||||||
|
return Math.abs(1 - assetsValue / accountValue)
|
||||||
|
}
|
||||||
|
}, [mangoAccount, group, accountValue])
|
||||||
|
|
||||||
|
const pnl = useMemo(() => {
|
||||||
|
if (!performanceData.length) return 0
|
||||||
|
return performanceData[performanceData.length - 1].pnl
|
||||||
|
}, [performanceData])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<div>
|
<SummaryItem
|
||||||
<p className="text-sm text-th-fgd-3">{t('health')}</p>
|
label={t('account-value')}
|
||||||
<p className="font-mono text-sm text-th-fgd-1">
|
value={formatFixedDecimals(accountValue, true, true)}
|
||||||
{group && mangoAccount
|
/>
|
||||||
? mangoAccount.getHealthRatioUi(group, HealthType.maint)
|
<SummaryItem label={t('health')} value={`${health}%`} />
|
||||||
: 0}
|
<SummaryItem
|
||||||
%
|
label={t('free-collateral')}
|
||||||
</p>
|
value={formatFixedDecimals(freeCollateral, true, true)}
|
||||||
|
/>
|
||||||
|
<SummaryItem label={t('leverage')} value={`${leverage.toFixed(2)}x`} />
|
||||||
|
<SummaryItem
|
||||||
|
label={t('pnl')}
|
||||||
|
value={formatFixedDecimals(pnl, true, true)}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
|
||||||
<p className="text-sm text-th-fgd-3">{t('account-value')}</p>
|
|
||||||
<p className="font-mono text-sm text-th-fgd-1">
|
|
||||||
{group && mangoAccount
|
|
||||||
? formatFixedDecimals(
|
|
||||||
toUiDecimalsForQuote(
|
|
||||||
mangoAccount.getEquity(group).toNumber()
|
|
||||||
),
|
|
||||||
false,
|
|
||||||
true
|
|
||||||
)
|
|
||||||
: '$0.00'}
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<p className="text-sm text-th-fgd-3">{t('free-collateral')}</p>
|
|
||||||
<p className="font-mono text-sm text-th-fgd-1">
|
|
||||||
{group && mangoAccount
|
|
||||||
? formatFixedDecimals(
|
|
||||||
toUiDecimalsForQuote(
|
|
||||||
mangoAccount.getCollateralValue(group).toNumber()
|
|
||||||
),
|
|
||||||
false,
|
|
||||||
true
|
|
||||||
)
|
|
||||||
: '$0.00'}
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<p className="text-sm text-th-fgd-3">{t('total-collateral')}</p>
|
|
||||||
<p className="font-mono text-sm text-th-fgd-1">
|
|
||||||
{group && mangoAccount
|
|
||||||
? formatFixedDecimals(
|
|
||||||
toUiDecimalsForQuote(
|
|
||||||
mangoAccount
|
|
||||||
.getAssetsValue(group, HealthType.init)
|
|
||||||
.toNumber()
|
|
||||||
),
|
|
||||||
false,
|
|
||||||
true
|
|
||||||
)
|
|
||||||
: '$0.00'}
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
{/* <div>
|
|
||||||
<p className="text-sm text-th-fgd-3">{t('leverage')}</p>
|
|
||||||
<p className="font-mono text-sm text-th-fgd-1">
|
|
||||||
{leverage.toFixed(2)}x
|
|
||||||
</p>
|
|
||||||
</div> */}
|
|
||||||
</div>
|
|
||||||
</>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue