import { ElementTitle } from './styles' import useMangoStore from '../stores/useMangoStore' import { getPrecisionDigits, i80f48ToPercent } from '../utils' import Tooltip from './Tooltip' import { nativeI80F48ToUi } from '@blockworks-foundation/mango-client' import { useViewport } from '../hooks/useViewport' import { breakpoints } from './TradePageGrid' import { useTranslation } from 'next-i18next' import { useWallet } from '@solana/wallet-adapter-react' export default function MarketBalances() { const { t } = useTranslation('common') const { connected } = useWallet() 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 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 minOrderSize = selectedMarket.minOrderSize const sizePrecisionDigits = getPrecisionDigits(minOrderSize) const marketIndex = marketConfig.marketIndex const priceOrDefault = price ? price : mangoGroup.getPriceUi(marketIndex, mangoGroupCache) let roundedSize, side if (symbol === 'USDC') { roundedSize = parseFloat( ( Math.abs(size) / priceOrDefault + (size < 0 ? minOrderSize / 2 : -minOrderSize / 2) ) // round up so neg USDC gets cleared .toFixed(sizePrecisionDigits) ) side = size > 0 ? 'buy' : 'sell' } else { roundedSize = parseFloat( ( Math.abs(size) + (size < 0 ? minOrderSize / 2 : -minOrderSize / 2) ).toFixed(sizePrecisionDigits) ) side = size > 0 ? 'sell' : 'buy' } const quoteSize = parseFloat((roundedSize * priceOrDefault).toFixed(2)) setMangoStore((state) => { state.tradeForm.baseSize = roundedSize state.tradeForm.quoteSize = quoteSize state.tradeForm.side = side }) } if (!mangoGroup || !selectedMarket) return null return (
{!isMobile ? ( {t('balances')} ) : null} {mangoGroup ? (
{mangoGroupConfig.tokens .filter((t) => t.symbol === baseSymbol || t.symbol === 'USDC') .reverse() .map(({ decimals, symbol, mintKey }) => { const tokenIndex = mangoGroup.getTokenIndex(mintKey) const balance = mangoAccount ? nativeI80F48ToUi( mangoAccount.getNet( mangoGroupCache.rootBankCache[tokenIndex], tokenIndex ), decimals ) : 0 const availableBalance = mangoAccount ? nativeI80F48ToUi( mangoAccount.getAvailableBalance( mangoGroup, mangoGroupCache, tokenIndex ), decimals ) : 0 return (
{symbol}
{t('balance')}
handleSizeClick(balance, symbol)} > {isLoading ? ( ) : ( balance.toLocaleString(undefined, { maximumFractionDigits: decimals, }) )}
{t('available-balance')}
selectedMarket.minOrderSize ? 'cursor-pointer underline hover:no-underline' : '' }`} onClick={() => handleSizeClick(availableBalance, symbol)} > {isLoading ? ( ) : ( availableBalance.toLocaleString(undefined, { maximumFractionDigits: decimals, }) )}
{t('rates')}
{i80f48ToPercent( mangoGroup.getDepositRate(tokenIndex) ).toFixed(2)} % {' / '} {i80f48ToPercent( mangoGroup.getBorrowRate(tokenIndex) ).toFixed(2)} %
) })}
) : null}
) } export const DataLoader = () => (
)