import { useCallback, useState } from 'react' import { Table, Thead, Tbody, Tr, Th, Td } from 'react-super-responsive-table' import useConnection from '../../hooks/useConnection' import useMangoStore from '../../stores/useMangoStore' import useMarketList from '../../hooks/useMarketList' import { useBalances } from '../../hooks/useBalances' import { notify } from '../../utils/notifications' import { sleep } from '../../utils' import { PublicKey } from '@solana/web3.js' import { tokenPrecision } from '../../utils/index' import { settleBorrow } from '../../utils/mango' import BorrowModal from '../BorrowModal' import Button from '../Button' import DepositModal from '../DepositModal' export default function AccountBorrows() { 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 { symbols } = useMarketList() const prices = useMangoStore((s) => s.selectedMangoGroup.prices) const [borrowSymbol, setBorrowSymbol] = useState('') const [depositToSettle, setDepositToSettle] = useState(null) const [showBorrowModal, setShowBorrowModal] = useState(false) const [showDepositModal, setShowDepositModal] = useState(false) async function handleSettleBorrow(token, borrowQuantity, depositBalance) { const { selectedMarginAccount, selectedMangoGroup, wallet } = useMangoStore.getState() as any if (borrowQuantity > depositBalance) { const deficit = borrowQuantity - depositBalance handleShowDeposit(token, deficit) return } try { await settleBorrow( connection, new PublicKey(programId), selectedMangoGroup.current, selectedMarginAccount.current, wallet.current, new PublicKey(symbols[token]), Number(borrowQuantity) ) await sleep(250) actions.fetchMarginAccounts() } catch (e) { console.warn('Error settling all:', e) if (e.message === 'No unsettled borrows') { notify({ message: 'There are no unsettled borrows', type: 'error', }) } else { notify({ message: 'Error settling borrows', description: e.message, txid: e.txid, type: 'error', }) } } } const handleCloseWithdraw = useCallback(() => { setShowBorrowModal(false) }, []) const handleCloseDeposit = useCallback(() => { setShowDepositModal(false) setDepositToSettle(null) }, []) const handleShowBorrow = (symbol) => { setBorrowSymbol(symbol) setShowBorrowModal(true) } const handleShowDeposit = (symbol, deficit) => { setDepositToSettle({ symbol, deficit }) setShowDepositModal(true) } console.log(depositToSettle) return ( <>
Your Borrows
Total Borrow Value:
$ {balances .reduce((acc, d, i) => acc + d.borrows * prices[i], 0) .toFixed(2)}
{selectedMangoGroup ? ( balances.find((b) => b.borrows > 0) ? ( {balances .filter((assets) => assets.borrows > 0) .map((asset, i) => ( ))}
Asset Balance Value Interest APR
{asset.coin}
{asset.borrows.toFixed(tokenPrecision[asset.coin])} $ {( asset.borrows * prices[ Object.keys(symbols).findIndex( (key) => key === asset.coin ) ] ).toFixed(tokenPrecision[asset.coin])} {( selectedMangoGroup.getBorrowRate( Object.keys(symbols).findIndex( (key) => key === asset.coin ) ) * 100 ).toFixed(2)} %
) : (
No borrows
) ) : null}
Available Assets
{Object.entries(symbols).map(([asset], i) => ( ))}
Asset Price Interest APR Available Liquidity
{asset}
${prices[i]} {(selectedMangoGroup.getBorrowRate(i) * 100).toFixed(2)}% {( selectedMangoGroup.getUiTotalDeposit(i) - selectedMangoGroup.getUiTotalBorrow(i) ).toLocaleString()}
{showBorrowModal && ( )} {showDepositModal && ( )} ) }