import mangoStore from '@store/mangoStore' import { useTranslation } from 'next-i18next' import { useCallback, useState } from 'react' import { PublicKey } from '@solana/web3.js' import { IconButton } from '@components/shared/Button' import { notify } from 'utils/notifications' import { CheckIcon, LinkIcon } from '@heroicons/react/20/solid' import Tooltip from '@components/shared/Tooltip' import Loading from '@components/shared/Loading' import { useViewport } from 'hooks/useViewport' import { breakpoints } from 'utils/theme' import MarketLogos from './MarketLogos' import useMangoAccount from 'hooks/useMangoAccount' import { Table, Td, Th, TrBody, TrHead } from '@components/shared/TableElements' import useMangoGroup from 'hooks/useMangoGroup' const UnsettledTrades = ({ unsettledSpotBalances, }: { unsettledSpotBalances: any }) => { const { t } = useTranslation(['common', 'trade']) const { mangoAccount } = useMangoAccount() const { group } = useMangoGroup() const [settleMktAddress, setSettleMktAddress] = useState('') const { width } = useViewport() const showTableView = width ? width > breakpoints.md : false const handleSettleFunds = useCallback(async (mktAddress: string) => { const client = mangoStore.getState().client const group = mangoStore.getState().group const mangoAccount = mangoStore.getState().mangoAccount.current const actions = mangoStore.getState().actions if (!group || !mangoAccount) return setSettleMktAddress(mktAddress) try { const txid = await client.serum3SettleFunds( group, mangoAccount, new PublicKey(mktAddress) ) actions.fetchOpenOrders() actions.reloadMangoAccount() notify({ type: 'success', title: 'Successfully settled funds', txid, }) } catch (e: any) { notify({ type: 'error', title: t('trade:settle-funds-error'), description: e?.message, txid: e?.txid, }) console.error('Settle funds error:', e) } finally { setSettleMktAddress('') } }, []) if (!group) return null return mangoAccount ? ( Object.values(unsettledSpotBalances).flat().length ? ( showTableView ? ( {Object.entries(unsettledSpotBalances).map(([mktAddress]) => { const market = group.getSerum3MarketByExternalMarket( new PublicKey(mktAddress) ) const base = market?.name.split('/')[0] const quote = market?.name.split('/')[1] return ( ) })}
{t('market')} {t('trade:base')} {t('trade:quote')}
{market ? market.name : ''}
{unsettledSpotBalances[mktAddress].base || 0.0}{' '} {base} {unsettledSpotBalances[mktAddress].quote || 0.0}{' '} {quote}
handleSettleFunds(mktAddress)} size="small" > {settleMktAddress === mktAddress ? ( ) : ( )}
) : (
{Object.entries(unsettledSpotBalances).map(([mktAddress]) => { const market = group.getSerum3MarketByExternalMarket( new PublicKey(mktAddress) ) const base = market?.name.split('/')[0] const quote = market?.name.split('/')[1] return (
{market ? market.name : ''}
{unsettledSpotBalances[mktAddress].base ? ( {unsettledSpotBalances[mktAddress].base}{' '} {base} ) : null} {unsettledSpotBalances[mktAddress].quote ? ( {unsettledSpotBalances[mktAddress].quote}{' '} {quote} ) : null} handleSettleFunds(mktAddress)}> {settleMktAddress === mktAddress ? ( ) : ( )}
) })}
) ) : (

{t('trade:no-unsettled')}

) ) : (

{t('trade:connect-unsettled')}

) } export default UnsettledTrades