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 from '../../stores/useMangoStore' import { settleAllTrades } from '../../utils/mango' import { useBalances } from '../../hooks/useBalances' import useConnection from '../../hooks/useConnection' import { tokenPrecision } from '../../utils/index' import { notify } from '../../utils/notifications' import { sleep } from '../../utils' import { PublicKey } from '@solana/web3.js' import DepositModal from '../DepositModal' import WithdrawModal from '../WithdrawModal' import Button from '../Button' import Tooltip from '../Tooltip' export default function AccountAssets() { const balances = useBalances() const { programId, connection } = useConnection() const actions = useMangoStore((s) => s.actions) const selectedMangoGroup = useMangoStore((s) => s.selectedMangoGroup.current) const selectedMarginAccount = useMangoStore( (s) => s.selectedMarginAccount.current ) const loadingMarginAccount = useMangoStore( (s) => s.selectedMarginAccount.initialLoad ) const connected = useMangoStore((s) => s.wallet.connected) const prices = useMangoStore((s) => s.selectedMangoGroup.prices) 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 = Object.values( useMangoStore.getState().selectedMangoGroup.markets ) const marginAccount = useMangoStore.getState().selectedMarginAccount.current const mangoGroup = useMangoStore.getState().selectedMangoGroup.current const wallet = useMangoStore.getState().wallet.current try { await settleAllTrades( connection, new PublicKey(programId), mangoGroup, marginAccount, markets, wallet ) await sleep(250) actions.fetchMarginAccounts() } catch (e) { console.warn('Error settling all:', e) if (e.message === 'No unsettled funds') { notify({ message: 'There are no unsettled funds', type: 'error', }) } else { notify({ message: 'Error settling funds', description: e.message, txid: e.txid, type: 'error', }) } } } return selectedMarginAccount ? ( <>
Your Assets
{balances.length > 0 ? (
Total Asset Value:
$ {balances .reduce( (acc, d, i) => acc + (d.marginDeposits + d.orders + d.unsettled) * prices[i], 0 ) .toFixed(2)}
) : null}
{balances.length > 0 && balances.find(({ unsettled }) => unsettled > 0) ? (
You have unsettled funds
) : null} {selectedMangoGroup && balances.length > 0 ? (
{balances.map((bal, i) => ( ))}
Asset Deposits In Orders Unsettled Value Interest APY
{bal.coin}
{bal.marginDeposits.toFixed(tokenPrecision[bal.coin])} {bal.orders.toFixed(tokenPrecision[bal.coin])} {bal.unsettled.toFixed(tokenPrecision[bal.coin])} $ {( (bal.marginDeposits + bal.orders + bal.unsettled) * prices[i] ).toFixed(2)} {(selectedMangoGroup.getDepositRate(i) * 100).toFixed( 2 )} %
) : (
No assets found.
)} {showDepositModal && ( )} {showWithdrawModal && ( )} ) : null }