import { I80F48, nativeToUi, QUOTE_INDEX, ZERO_BN, ZERO_I80F48, } from '@blockworks-foundation/mango-client' import { useCallback, useMemo, useState } from 'react' import { HeartIcon } from '@heroicons/react/solid' import useMangoStore, { mangoClient, MNGO_INDEX } from '../stores/useMangoStore' import { formatUsdValue } from '../utils' import { notify } from '../utils/notifications' import { LinkButton } from './Button' import FloatingElement from './FloatingElement' import { ElementTitle } from './styles' import Tooltip from './Tooltip' import DepositModal from './DepositModal' import WithdrawModal from './WithdrawModal' import Button from './Button' const I80F48_100 = I80F48.fromString('100') export default function MarginInfo() { const connected = useMangoStore((s) => s.wallet.connected) const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current) const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache) const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current) const isLoading = useMangoStore((s) => s.selectedMangoAccount.initialLoad) const actions = useMangoStore((s) => s.actions) const [showDepositModal, setShowDepositModal] = useState(false) const [showWithdrawModal, setShowWithdrawModal] = useState(false) const handleCloseDeposit = useCallback(() => { setShowDepositModal(false) }, []) const handleCloseWithdraw = useCallback(() => { setShowWithdrawModal(false) }, []) const equity = mangoAccount ? mangoAccount.computeValue(mangoGroup, mangoCache) : ZERO_I80F48 // TODO: correct calc const marginAvailable = mangoAccount ? mangoAccount.getMaxWithBorrowForToken(mangoGroup, mangoCache, QUOTE_INDEX) : ZERO_I80F48 const mngoAccrued = mangoAccount ? mangoAccount.perpAccounts.reduce((acc, perpAcct) => { return perpAcct.mngoAccrued.add(acc) }, ZERO_BN) : ZERO_BN 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') : I80F48_100 }, [mangoAccount, mangoGroup, mangoCache]) const initHealthRatio = useMemo(() => { return mangoAccount ? mangoAccount.getHealthRatio(mangoGroup, mangoCache, 'Init') : I80F48_100 }, [mangoAccount, mangoGroup, mangoCache]) return (
Account
Equity
{isLoading ? (
) : ( formatUsdValue(+equity) )}
Leverage
{isLoading ? (
) : mangoAccount ? ( `${mangoAccount .getLeverage(mangoGroup, mangoCache) .toFixed(2)}x` ) : ( '0.00x' )}
Total Assets Value
{isLoading ? (
) : mangoAccount ? ( formatUsdValue( +mangoAccount.getAssetsVal(mangoGroup, mangoCache) ) ) : ( '--' )}
Total Liabilities Value
{isLoading ? (
) : mangoAccount ? ( formatUsdValue( +mangoAccount.getLiabsVal(mangoGroup, mangoCache) ) ) : ( '--' )}
Margin Available
{mangoAccount ? formatUsdValue(marginAvailable) : '--'}
Earn MNGO by market making on Perp markets.{' '} Learn more
} >
MNGO Rewards
{isLoading ? (
) : mangoGroup ? ( nativeToUi( mngoAccrued.toNumber(), mangoGroup.tokens[MNGO_INDEX].decimals ) ) : ( 0 )} Claim
} >
Health
30 ? 'bg-th-green' : initHealthRatio.toNumber() > 0 ? 'bg-th-orange' : 'bg-th-red' }`} >
{maintHealthRatio.gt(I80F48_100) ? '>100' : maintHealthRatio.toFixed(2)} %
{showDepositModal && ( )} {showWithdrawModal && ( )} ) }