import { useCallback, useState } from 'react' import { Table, Thead, Tbody, Tr, Th, Td } from 'react-super-responsive-table' import { InformationCircleIcon } from '@heroicons/react/outline' import useMangoStore, { mangoClient } from '../../stores/useMangoStore' // import { settleAllTrades } from '../../utils/mango' import { useBalances } from '../../hooks/useBalances' import { tokenPrecision } from '../../utils/index' import DepositModal from '../DepositModal' import WithdrawModal from '../WithdrawModal' import Button from '../Button' import Tooltip from '../Tooltip' import { Market } from '@project-serum/serum' import { getTokenBySymbol, I80F48, PerpMarket, } from '@blockworks-foundation/mango-client' import { notify } from '../../utils/notifications' export default function AccountAssets() { const balances = useBalances() const actions = useMangoStore((s) => s.actions) const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current) const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache) const groupConfig = useMangoStore((s) => s.selectedMangoGroup.config) const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current) const loadingMangoAccount = useMangoStore( (s) => s.selectedMangoAccount.initialLoad ) const connected = useMangoStore((s) => s.wallet.connected) const [showDepositModal, setShowDepositModal] = useState(false) const [showWithdrawModal, setShowWithdrawModal] = useState(false) const [withdrawSymbol, setWithdrawSymbol] = useState('') const [depositSymbol, setDepositSymbol] = useState('') const handleCloseDeposit = useCallback(() => { setShowDepositModal(false) }, []) const handleCloseWithdraw = useCallback(() => { setShowWithdrawModal(false) }, []) const handleShowWithdraw = (symbol) => { setWithdrawSymbol(symbol) setShowWithdrawModal(true) } const handleShowDeposit = (symbol) => { setDepositSymbol(symbol) setShowDepositModal(true) } async function handleSettleAllTrades() { const markets: Array = Object.values( useMangoStore.getState().selectedMangoGroup.markets ) const mangoAccount = useMangoStore.getState().selectedMangoAccount.current const mangoGroup = useMangoStore.getState().selectedMangoGroup.current const wallet = useMangoStore.getState().wallet.current const spotMarkets = markets.filter( (mkt) => mkt instanceof Market ) as Market[] try { await mangoClient.settleAll(mangoGroup, mangoAccount, spotMarkets, wallet) actions.fetchMangoAccounts() } catch (e) { if (e.message === 'No unsettled funds') { notify({ title: 'There are no unsettled funds', type: 'error', }) } else { notify({ title: 'Error settling funds', description: e.message, txid: e.txid, type: 'error', }) } } } return mangoAccount ? ( <>
Your Assets
{balances.length > 0 ? (
Total Asset Value:
$ {mangoAccount.getAssetsVal(mangoGroup, mangoCache).toFixed(2)}
) : null}
{balances.length > 0 && balances.find(({ unsettled }) => unsettled > 0) ? (
You have unsettled funds
) : null} {mangoGroup && balances.length > 0 ? (
{balances.map((bal, i) => { const token = getTokenBySymbol(groupConfig, bal.symbol) const tokenIndex = mangoGroup.getTokenIndex(token.mintKey) // console.log( // 'price cache', // mangoCache.priceCache[tokenIndex] // ) return ( ) })}
Asset Available In Orders Unsettled Value Interest APY
{bal.symbol}
{bal.marginDeposits.toFixed( tokenPrecision[bal.symbol] )} {bal.orders.toFixed(tokenPrecision[bal.symbol])} {bal.unsettled.toFixed(tokenPrecision[bal.symbol])} $ {( (bal.marginDeposits.toNumber() + bal.orders + bal.unsettled) * mangoGroup .getPrice(tokenIndex, mangoCache) .toNumber() ).toFixed(2)} {mangoGroup .getDepositRate(tokenIndex) .mul(I80F48.fromNumber(100)) .toFixed(2)} %
) : (
No assets found.
)} {showDepositModal && ( )} {showWithdrawModal && ( )} ) : null }