import { useCallback, useState } from 'react' import { getTokenBySymbol, ZERO_I80F48, I80F48, } from '@blockworks-foundation/mango-client' import useMangoStore from '../../stores/useMangoStore' import { useBalances } from '../../hooks/useBalances' import { floorToDecimal, formatUsdValue, i80f48ToPercent, tokenPrecision, } from '../../utils/index' import WithdrawModal from '../WithdrawModal' import Button from '../Button' import DepositModal from '../DepositModal' import { useViewport } from '../../hooks/useViewport' import { breakpoints } from '../TradePageGrid' import { Table, Td, Th, TrBody, TrHead } from '../TableElements' import { ExpandableRow } from '../TableElements' import MobileTableHeader from '../mobile/MobileTableHeader' export default function AccountBorrows() { const balances = useBalances() const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current) const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache) const mangoConfig = 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 [borrowSymbol, setBorrowSymbol] = useState('') const [depositToSettle, setDepositToSettle] = useState(null) const [showBorrowModal, setShowBorrowModal] = useState(false) const [showDepositModal, setShowDepositModal] = useState(false) const { width } = useViewport() const isMobile = width ? width < breakpoints.sm : false const handleCloseWithdraw = useCallback(() => { setShowBorrowModal(false) }, []) const handleCloseDeposit = useCallback(() => { setShowDepositModal(false) setDepositToSettle(null) }, []) const handleShowBorrow = (symbol) => { setBorrowSymbol(symbol) setShowBorrowModal(true) } const handleShowDeposit = (symbol) => { setDepositToSettle({ symbol: symbol }) setShowDepositModal(true) } return ( <>
Your Borrows
{/* TODO: calculate LiabsVal without perp markets
Total Borrow Value:
{formatUsdValue(+mangoAccount.getLiabsVal(mangoGroup, mangoCache))}
*/}
{mangoGroup ? ( balances.find((b) => b.borrows.gt(ZERO_I80F48)) ? ( !isMobile ? ( {balances .filter((assets) => assets.borrows.gt(ZERO_I80F48)) .map((asset, i) => { const token = getTokenBySymbol( mangoConfig, asset.symbol ) const tokenIndex = mangoGroup.getTokenIndex( token.mintKey ) return ( ) })}
Asset Balance Value Borrow Rate (APR)
{asset.symbol}
{asset.borrows.toFixed( tokenPrecision[asset.symbol] )} {formatUsdValue( asset.borrows.mul( mangoGroup.getPrice(tokenIndex, mangoCache) ) )} {( mangoGroup .getBorrowRate(tokenIndex) .toNumber() * 100 ).toFixed(2)} %
) : ( <>
Asset
Balance
} /> {balances .filter((assets) => assets.borrows.gt(ZERO_I80F48)) .map((asset, i) => { const token = getTokenBySymbol( mangoConfig, asset.symbol ) const tokenIndex = mangoGroup.getTokenIndex( token.mintKey ) return (
{asset.symbol}
{asset.borrows.toFixed( tokenPrecision[asset.symbol] )}
} key={`${asset.symbol}${i}`} index={i} panelTemplate={ <>
Value
{formatUsdValue( asset.borrows.mul( mangoGroup.getPrice( tokenIndex, mangoCache ) ) )}
Borrow Rate (APR)
{( mangoGroup .getBorrowRate(tokenIndex) .toNumber() * 100 ).toFixed(2)} %
} /> ) })} ) ) : (
No borrows found.
) ) : null}
All Assets
{!isMobile ? ( {mangoConfig.tokens.map((token, i) => { const tokenIndex = mangoGroup.getTokenIndex(token.mintKey) return ( ) })}
Asset Price Borrow Rate (APR) Max Borrow Amount Liquidity
{token.symbol}
{formatUsdValue( mangoGroup.getPrice(tokenIndex, mangoCache) )} {i80f48ToPercent( mangoGroup.getBorrowRate(tokenIndex) ).toFixed(2)} % {mangoAccount .getMaxWithBorrowForToken( mangoGroup, mangoCache, tokenIndex ) .mul(I80F48.fromString('0.995')) .toNumber() > 0 ? floorToDecimal( mangoAccount .getMaxWithBorrowForToken( mangoGroup, mangoCache, tokenIndex ) .mul(I80F48.fromString('0.995')) .toNumber(), mangoGroup.tokens[tokenIndex].decimals ) : 0} {/* floorToDecimal(parseFloat(maxWithdraw.toFixed()), token.decimals) */} {mangoGroup .getUiTotalDeposit(tokenIndex) .sub(mangoGroup.getUiTotalBorrow(tokenIndex)) .toNumber() .toLocaleString(undefined, { minimumFractionDigits: tokenPrecision[token.symbol], maximumFractionDigits: tokenPrecision[token.symbol], })}
) : ( <>
Asset
Borrow Rate (APR)
} /> {mangoConfig.tokens.map((token, i) => { const tokenIndex = mangoGroup.getTokenIndex(token.mintKey) return (
{token.symbol}
{i80f48ToPercent( mangoGroup.getBorrowRate(tokenIndex) ).toFixed(2)} %
} key={`${token.symbol}${i}`} index={i} panelTemplate={ <>
Price
{formatUsdValue( mangoGroup.getPrice(tokenIndex, mangoCache) )}
Max Borrow Amount
{mangoAccount .getMaxWithBorrowForToken( mangoGroup, mangoCache, tokenIndex ) .mul(I80F48.fromString('0.995')) .toNumber() .toLocaleString(undefined, { minimumFractionDigits: tokenPrecision[token.symbol], maximumFractionDigits: tokenPrecision[token.symbol], })}
Liquidity
{mangoGroup .getUiTotalDeposit(tokenIndex) .sub(mangoGroup.getUiTotalBorrow(tokenIndex)) .toNumber() .toLocaleString(undefined, { minimumFractionDigits: tokenPrecision[token.symbol], maximumFractionDigits: tokenPrecision[token.symbol], })}
} /> ) })} )}
{showBorrowModal && ( )} {showDepositModal && ( )} ) }