import { ElementTitle } from './styles' import useMangoStore from '../stores/useMangoStore' import { i80f48ToPercent, floorToDecimal } from '../utils/index' import Tooltip from './Tooltip' import { getMarketIndexBySymbol, nativeI80F48ToUi, } from '@blockworks-foundation/mango-client' import { useViewport } from '../hooks/useViewport' import { breakpoints } from './TradePageGrid' import { useTranslation } from 'next-i18next' export default function MarketBalances() { const { t } = useTranslation('common') const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current) const mangoGroupConfig = useMangoStore((s) => s.selectedMangoGroup.config) const mangoGroupCache = useMangoStore((s) => s.selectedMangoGroup.cache) const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current) const selectedMarket = useMangoStore((s) => s.selectedMarket.current) const marketConfig = useMangoStore((s) => s.selectedMarket.config) const setMangoStore = useMangoStore((s) => s.set) const price = useMangoStore((s) => s.tradeForm.price) const connected = useMangoStore((s) => s.wallet.connected) const isLoading = useMangoStore((s) => s.selectedMangoAccount.initialLoad) const baseSymbol = marketConfig.baseSymbol const { width } = useViewport() const isMobile = width ? width < breakpoints.sm : false const handleSizeClick = (size, symbol) => { const step = selectedMarket.minOrderSize const marketIndex = getMarketIndexBySymbol( mangoGroupConfig, marketConfig.baseSymbol ) const priceOrDefault = price ? price : mangoGroup.getPrice(marketIndex, mangoGroupCache).toNumber() if (symbol === 'USDC') { const baseSize = Math.floor(size / priceOrDefault / step) * step setMangoStore((state) => { state.tradeForm.baseSize = baseSize state.tradeForm.quoteSize = (baseSize * priceOrDefault).toFixed(2) state.tradeForm.side = 'buy' }) } else { const roundedSize = Math.round(size / step) * step const quoteSize = roundedSize * priceOrDefault setMangoStore((state) => { state.tradeForm.baseSize = roundedSize state.tradeForm.quoteSize = quoteSize.toFixed(2) state.tradeForm.side = 'sell' }) } } if (!mangoGroup || !selectedMarket) return null return (
{!isMobile ? Balances : null} {mangoGroup ? (
{mangoGroupConfig.tokens .filter((t) => t.symbol === baseSymbol || t.symbol === 'USDC') .reverse() .map(({ decimals, symbol, mintKey }) => { const tokenIndex = mangoGroup.getTokenIndex(mintKey) const deposit = mangoAccount ? mangoAccount.getUiDeposit( mangoGroupCache.rootBankCache[tokenIndex], mangoGroup, tokenIndex ) : null const borrow = mangoAccount ? mangoAccount.getUiBorrow( mangoGroupCache.rootBankCache[tokenIndex], mangoGroup, tokenIndex ) : null const availableBalance = mangoAccount ? floorToDecimal( nativeI80F48ToUi( mangoAccount.getAvailableBalance( mangoGroup, mangoGroupCache, tokenIndex ), decimals ).toNumber(), decimals ) : 0 return (
{symbol}
{t('balance')}
{isLoading ? ( ) : mangoAccount ? ( deposit.gt(borrow) ? ( deposit.toFixed() ) : borrow.toNumber() > 0 ? ( `-${borrow.toFixed()}` ) : ( 0 ) ) : ( 0 )}
{t('available-balance')}
selectedMarket.minOrderSize ? 'cursor-pointer underline hover:no-underline' : '' }`} onClick={() => handleSizeClick(availableBalance, symbol)} > {isLoading ? ( ) : mangoAccount ? ( availableBalance ) : ( 0 )}
{t('rates')}
{i80f48ToPercent( mangoGroup.getDepositRate(tokenIndex) ).toFixed(2)} % {' / '} {i80f48ToPercent( mangoGroup.getBorrowRate(tokenIndex) ).toFixed(2)} %
) })}
) : null}
) } export const DataLoader = () => (
)