mango-ui-v3/components/MarginInfo.tsx

208 lines
7.3 KiB
TypeScript
Raw Normal View History

2021-08-04 11:31:32 -07:00
import {
nativeToUi,
ZERO_BN,
ZERO_I80F48,
} from '@blockworks-foundation/mango-client'
2021-07-05 08:03:57 -07:00
import { HeartIcon } from '@heroicons/react/outline'
2021-08-04 11:31:32 -07:00
import { useMemo } from 'react'
import useMangoStore, { mangoClient, MNGO_INDEX } from '../stores/useMangoStore'
import { notify } from '../utils/notifications'
2021-08-05 07:07:29 -07:00
import Button, { LinkButton } from './Button'
import FloatingElement from './FloatingElement'
import { ElementTitle } from './styles'
2021-04-16 11:16:31 -07:00
import Tooltip from './Tooltip'
2021-07-05 08:03:57 -07:00
2021-04-14 23:16:36 -07:00
export default function MarginInfo() {
2021-07-22 04:34:03 -07:00
const connected = useMangoStore((s) => s.wallet.connected)
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache)
2021-07-06 17:13:17 -07:00
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
2021-08-04 11:31:32 -07:00
const actions = useMangoStore((s) => s.actions)
2021-07-07 13:38:04 -07:00
const equity = mangoAccount
? mangoAccount.computeValue(mangoGroup, mangoCache)
: ZERO_I80F48
2021-08-04 11:31:32 -07:00
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.fetchMangoAccounts()
notify({
title: 'Successfully redeemed MNGO',
description: '',
txid,
})
} catch (e) {
notify({
title: 'Error redeeming MNGO',
description: e.message,
txid: e.txid,
type: 'error',
})
}
}
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])
2021-07-05 08:03:57 -07:00
return (
2021-07-22 04:34:03 -07:00
<FloatingElement showConnect>
2021-07-24 11:12:52 -07:00
<div className={!connected ? 'filter blur-sm' : undefined}>
<ElementTitle>Account</ElementTitle>
2021-07-22 04:34:03 -07:00
<div>
2021-07-05 08:03:57 -07:00
<div>
2021-07-24 11:12:52 -07:00
<div className="flex justify-between pt-2 pb-2">
2021-07-06 21:34:21 -07:00
<Tooltip content="Account value">
2021-08-04 12:25:56 -07:00
<div className="cursor-help font-normal text-th-fgd-3 leading-4 border-b border-th-fgd-3 border-dashed border-opacity-20 default-transition hover:border-th-bkg-2">
2021-07-06 21:34:21 -07:00
Equity
</div>
</Tooltip>
2021-07-24 11:12:52 -07:00
<div className="text-th-fgd-1">${equity.toFixed(2)}</div>
2021-07-06 21:34:21 -07:00
</div>
<div className="flex justify-between pt-2 pb-2">
<div className="font-normal text-th-fgd-3 leading-4">
Leverage
</div>
<div className="text-th-fgd-1">0.00x</div>
</div>
2021-07-06 21:34:21 -07:00
<div className={`flex justify-between pt-2 pb-2`}>
2021-07-23 07:07:05 -07:00
<div className="font-normal text-th-fgd-3 leading-4">
Total Assets Value
</div>
2021-07-06 21:34:21 -07:00
<div className={`text-th-fgd-1`}>
2021-07-22 04:34:03 -07:00
$
{mangoAccount
? mangoAccount.getAssetsVal(mangoGroup, mangoCache).toFixed(2)
: 0}
2021-07-06 21:34:21 -07:00
</div>
</div>
<div className={`flex justify-between pt-2 pb-2`}>
2021-07-23 07:07:05 -07:00
<div className="font-normal text-th-fgd-3 leading-4">
Total Liabilities Value
</div>
2021-07-06 21:34:21 -07:00
<div className={`text-th-fgd-1`}>
2021-07-22 04:34:03 -07:00
$
{mangoAccount
? mangoAccount.getLiabsVal(mangoGroup, mangoCache).toFixed(2)
: 0}
2021-07-06 21:34:21 -07:00
</div>
</div>
2021-08-04 11:31:32 -07:00
<div className={`flex justify-between pt-2 pb-2`}>
2021-08-04 12:25:56 -07:00
<Tooltip
content={
<div>
2021-08-05 07:07:29 -07:00
Earn MNGO by market making on Perp markets.{' '}
2021-08-04 12:25:56 -07:00
<a
href="https://docs.mango.markets/mango-v3/liquidity-incentives"
target="_blank"
rel="noopener noreferrer"
>
Learn more
</a>
</div>
}
>
<div className="cursor-help font-normal text-th-fgd-3 leading-4 border-b border-th-fgd-3 border-dashed border-opacity-20 default-transition hover:border-th-bkg-2">
2021-08-05 07:07:29 -07:00
MNGO Rewards
2021-08-04 12:25:56 -07:00
</div>
</Tooltip>
2021-08-05 07:07:29 -07:00
<div className={`flex items-center text-th-fgd-1`}>
{mangoGroup
? nativeToUi(
mngoAccrued.toNumber(),
mangoGroup.tokens[MNGO_INDEX].decimals
)
: 0}
<LinkButton
onClick={handleRedeemMngo}
className="ml-2 text-th-primary text-xs"
disabled={mngoAccrued.eq(ZERO_BN)}
>
Claim
</LinkButton>
2021-08-04 11:31:32 -07:00
</div>
</div>
2021-07-05 08:03:57 -07:00
</div>
2021-07-29 06:19:32 -07:00
<div className="border border-th-bkg-4 mt-4 p-4 rounded">
2021-07-22 04:34:03 -07:00
<div className="flex flex-col">
<div className="flex justify-between">
<div className="flex items-center">
2021-07-29 06:19:32 -07:00
<HeartIcon
className="h-5 w-5 text-th-primary"
aria-hidden="true"
/>
<span className="ml-2">
<Tooltip
content={
<div>
Account will be liquidated if Health Ratio reaches 0%.{' '}
<a
href="https://docs.mango.markets/mango-v3/overview#health"
target="_blank"
rel="noopener noreferrer"
>
Learn more
</a>
</div>
}
>
<div className="cursor-help font-normal text-th-fgd-3 leading-4 border-b border-th-fgd-3 border-dashed border-opacity-20 default-transition hover:border-th-bkg-2">
Health Ratio
</div>
</Tooltip>
</span>
2021-07-22 04:34:03 -07:00
</div>
<div className="text-right">{maintHealthRatio.toFixed(2)}%</div>
2021-07-01 07:12:42 -07:00
</div>
<div className="mt-1">
<div className="h-1.5 flex rounded bg-th-bkg-3 mt-4">
2021-07-22 04:34:03 -07:00
<div
style={{
width: `${maintHealthRatio}%`,
2021-07-22 04:34:03 -07:00
}}
2021-07-29 06:19:32 -07:00
className={`flex rounded ${
maintHealthRatio > 30
2021-07-29 06:19:32 -07:00
? 'bg-th-green'
: initHealthRatio > 0
2021-07-29 06:19:32 -07:00
? 'bg-th-orange'
: 'bg-th-red'
}`}
2021-07-22 04:34:03 -07:00
></div>
</div>
2021-07-01 07:12:42 -07:00
</div>
</div>
2021-07-05 08:03:57 -07:00
</div>
</div>
</div>
</FloatingElement>
)
}