mango-v4-ui/components/account/MangoAccountSummary.tsx

126 lines
4.0 KiB
TypeScript
Raw Normal View History

2022-09-12 08:53:57 -07:00
import mangoStore from '@store/mangoStore'
2022-08-11 12:29:07 -07:00
import {
HealthType,
toUiDecimalsForQuote,
} from '@blockworks-foundation/mango-v4'
import { formatDecimal, formatFixedDecimals } from '../../utils/numbers'
2022-07-15 04:09:23 -07:00
import Button from '../shared/Button'
2022-10-04 20:30:19 -07:00
import { useMemo, useState } from 'react'
2022-07-15 04:09:23 -07:00
import DepositModal from '../modals/DepositModal'
import WithdrawModal from '../modals/WithdrawModal'
import { useTranslation } from 'next-i18next'
2022-09-07 17:47:59 -07:00
import { ArrowDownTrayIcon } from '@heroicons/react/20/solid'
2022-09-11 17:22:37 -07:00
import { useWallet } from '@solana/wallet-adapter-react'
2022-11-18 09:09:39 -08:00
import useMangoAccount from 'hooks/useMangoAccount'
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')
2022-09-11 17:22:37 -07:00
const { connected } = useWallet()
const group = mangoStore.getState().group
2022-11-18 09:09:39 -08:00
const { mangoAccount } = useMangoAccount()
2022-07-15 04:09:23 -07:00
const [showDepositModal, setShowDepositModal] = useState(false)
const [showWithdrawModal, setShowWithdrawModal] = useState(false)
2022-07-14 20:38:02 -07:00
2022-10-04 20:30:19 -07:00
const leverage = useMemo(() => {
if (!group || !mangoAccount) return 0
const liabsValue = mangoAccount
.getLiabsValue(group, HealthType.init)!
.toNumber()
2022-10-06 19:10:12 -07:00
const totalCollateral = mangoAccount
.getAssetsValue(group, HealthType.init)!
2022-10-06 19:10:12 -07:00
.toNumber()
if (isNaN(liabsValue / totalCollateral)) {
return 0
} else return liabsValue / totalCollateral
2022-10-04 20:30:19 -07:00
}, [mangoAccount])
2022-07-14 20:38:02 -07:00
return (
<>
2022-09-07 05:00:21 -07:00
<div className="mb-4 space-y-2">
<div>
<p className="text-sm text-th-fgd-3">{t('health')}</p>
2022-10-05 16:03:10 -07:00
<p className="font-mono text-sm text-th-fgd-1">
{group && mangoAccount
? mangoAccount.getHealthRatioUi(group, HealthType.maint)
: 0}
2022-09-07 05:00:21 -07:00
%
</p>
</div>
<div>
<p className="text-sm text-th-fgd-3">{t('account-value')}</p>
2022-10-05 16:03:10 -07:00
<p className="font-mono text-sm text-th-fgd-1">
2022-07-14 20:38:02 -07:00
$
{group && mangoAccount
2022-07-14 20:38:02 -07:00
? formatDecimal(
toUiDecimalsForQuote(
mangoAccount.getEquity(group)!.toNumber()
),
2022-07-14 20:38:02 -07:00
2
)
: (0).toFixed(2)}
</p>
</div>
2022-10-04 19:45:06 -07:00
<div>
<p className="text-sm text-th-fgd-3">{t('free-collateral')}</p>
2022-10-05 16:03:10 -07:00
<p className="font-mono text-sm text-th-fgd-1">
{group && mangoAccount
? formatFixedDecimals(
2022-10-04 19:45:06 -07:00
toUiDecimalsForQuote(
mangoAccount.getCollateralValue(group)!.toNumber()
2022-10-04 19:45:06 -07:00
),
true
2022-10-04 19:45:06 -07:00
)
: `$${(0).toFixed(2)}`}
2022-10-04 19:45:06 -07:00
</p>
</div>
2022-09-07 05:00:21 -07:00
<div>
<p className="text-sm text-th-fgd-3">{t('total-collateral')}</p>
2022-10-05 16:03:10 -07:00
<p className="font-mono text-sm text-th-fgd-1">
{group && mangoAccount
? formatFixedDecimals(
2022-08-11 12:29:07 -07:00
toUiDecimalsForQuote(
mangoAccount
.getAssetsValue(group, HealthType.init)!
.toNumber()
2022-08-11 12:29:07 -07:00
),
true
2022-07-14 20:38:02 -07:00
)
: `$${(0).toFixed(2)}`}
2022-07-14 20:38:02 -07:00
</p>
</div>
2022-10-04 20:30:19 -07:00
<div>
<p className="text-sm text-th-fgd-3">{t('leverage')}</p>
2022-10-05 16:03:10 -07:00
<p className="font-mono text-sm text-th-fgd-1">
2022-10-04 20:30:19 -07:00
{leverage.toFixed(2)}x
</p>
</div>
2022-07-14 20:38:02 -07:00
</div>
2022-09-07 05:00:21 -07:00
<div className="space-y-2">
2022-09-07 17:47:59 -07:00
<Button
className="flex w-full items-center justify-center"
2022-09-11 17:22:37 -07:00
disabled={!mangoAccount || !connected}
2022-09-07 17:47:59 -07:00
onClick={() => setShowDepositModal(true)}
>
<ArrowDownTrayIcon className="mr-2 h-5 w-5" />
2022-07-15 04:09:23 -07:00
{t('deposit')}
</Button>
2022-09-14 07:52:18 -07:00
</div>
2022-07-15 04:09:23 -07:00
{showDepositModal ? (
<DepositModal
isOpen={showDepositModal}
onClose={() => setShowDepositModal(false)}
/>
) : null}
{showWithdrawModal ? (
<WithdrawModal
isOpen={showWithdrawModal}
onClose={() => setShowWithdrawModal(false)}
/>
) : null}
2022-07-14 20:38:02 -07:00
</>
)
}
export default MangoAccountSummary