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 selectedMangoAccount = useMangoStore( (s) => s.selectedMangoAccount.current ) const loadingMangoAccount = useMangoStore( (s) => s.selectedMangoAccount.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 mangoAccount = useMangoStore.getState().selectedMangoAccount.current const mangoGroup = useMangoStore.getState().selectedMangoGroup.current const wallet = useMangoStore.getState().wallet.current if (borrowQuantity > depositBalance) { const deficit = borrowQuantity - depositBalance handleShowDeposit(token, deficit) return } try { await settleBorrow( connection, new PublicKey(programId), mangoGroup, mangoAccount, wallet, new PublicKey(symbols[token]), Number(borrowQuantity) ) await sleep(250) actions.fetchMangoAccounts() } 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 ( <>
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 ) ) .toNumber() * 100 ).toFixed(2)} % |
|
Asset | Price | Interest APR | Available Liquidity | |
---|---|---|---|---|
{asset}
|
${prices[i]} | {(selectedMangoGroup.getBorrowRate(i) * 100).toFixed(2)}% | {( selectedMangoGroup.getUiTotalDeposit(i) - selectedMangoGroup.getUiTotalBorrow(i) ).toLocaleString()} |
|