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 { 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' import { useTranslation } from 'next-i18next' export default function AccountBorrows() { const { t } = useTranslation('common') 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, amount) => { setDepositToSettle({ symbol: symbol, amount: amount }) setShowDepositModal(true) } return ( <>
{t('your-borrows')}
{/* TODO: calculate LiabsVal without perp markets
{t('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 ( ) })}
{t('asset')} {t('balance')} {t('value')} {t('borrow-rate')} (APR)
{asset.symbol}
{asset.borrows.toFixed()} {formatUsdValue( asset.borrows .mul( mangoGroup.getPrice( tokenIndex, mangoCache ) ) .toNumber() )} {( mangoGroup .getBorrowRate(tokenIndex) .toNumber() * 100 ).toFixed(2)} %
) : ( <> {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={ <>
{t('value')}
{formatUsdValue( asset.borrows .mul( mangoGroup.getPrice( tokenIndex, mangoCache ) ) .toNumber() )}
{t('borrow-rate')} (APR)
{( mangoGroup .getBorrowRate(tokenIndex) .toNumber() * 100 ).toFixed(2)} %
} /> ) })} ) ) : (
{t('no-borrows')}
) ) : null}
{t('all-assets')}
{!isMobile ? ( {mangoConfig.tokens.map((token, i) => { const tokenIndex = mangoGroup.getTokenIndex(token.mintKey) return ( ) })}
{t('asset')} {t('price')} {t('borrow-rate')} (APR) {t('max-borrow')} {t('liquidity')}
{token.symbol}
{formatUsdValue( mangoGroup .getPrice(tokenIndex, mangoCache) .toNumber() )} {i80f48ToPercent( mangoGroup.getBorrowRate(tokenIndex) ).toFixed(2)} % {mangoAccount .getMaxWithBorrowForToken( mangoGroup, mangoCache, tokenIndex ) .mul(I80F48.fromString('0.995')) .toNumber() > 0 ? mangoAccount .getMaxWithBorrowForToken( mangoGroup, mangoCache, tokenIndex ) .mul(I80F48.fromString('0.995')) .toNumber() .toLocaleString(undefined, { minimumFractionDigits: tokenPrecision[token.symbol], maximumFractionDigits: tokenPrecision[token.symbol], }) : 0} {mangoGroup .getUiTotalDeposit(tokenIndex) .sub(mangoGroup.getUiTotalBorrow(tokenIndex)) .toNumber() .toLocaleString(undefined, { minimumFractionDigits: tokenPrecision[token.symbol], maximumFractionDigits: tokenPrecision[token.symbol], })}
) : ( <> {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={
{t('price')}
{formatUsdValue( mangoGroup .getPrice(tokenIndex, mangoCache) .toNumber() )}
{t('max-borrow')}
{mangoAccount .getMaxWithBorrowForToken( mangoGroup, mangoCache, tokenIndex ) .mul(I80F48.fromString('0.995')) .toNumber() .toLocaleString(undefined, { minimumFractionDigits: tokenPrecision[token.symbol], maximumFractionDigits: tokenPrecision[token.symbol], })}
{t('liquidity')}
{mangoGroup .getUiTotalDeposit(tokenIndex) .sub(mangoGroup.getUiTotalBorrow(tokenIndex)) .toNumber() .toLocaleString(undefined, { minimumFractionDigits: tokenPrecision[token.symbol], maximumFractionDigits: tokenPrecision[token.symbol], })}
} /> ) })} )}
{showBorrowModal && ( )} {showDepositModal && ( )} ) }