mango-v4-ui/components/trade/UnsettledTrades.tsx

134 lines
4.5 KiB
TypeScript
Raw Normal View History

import mangoStore from '@store/mangoStore'
import { useTranslation } from 'next-i18next'
2022-10-03 19:26:50 -07:00
import { useCallback, useState } from 'react'
2022-09-28 18:50:02 -07:00
import { PublicKey } from '@solana/web3.js'
2022-10-02 22:42:28 -07:00
import { IconButton } from '@components/shared/Button'
2022-09-28 18:50:02 -07:00
import { notify } from 'utils/notifications'
2022-10-02 22:42:28 -07:00
import { useWallet } from '@solana/wallet-adapter-react'
import { CheckIcon, LinkIcon } from '@heroicons/react/20/solid'
import Tooltip from '@components/shared/Tooltip'
2022-10-03 17:59:25 -07:00
import Loading from '@components/shared/Loading'
2022-10-02 22:42:28 -07:00
const UnsettledTrades = ({
unsettledSpotBalances,
}: {
unsettledSpotBalances: any
}) => {
2022-10-03 03:38:05 -07:00
const { t } = useTranslation(['common', 'trade'])
2022-10-02 22:42:28 -07:00
const { connected } = useWallet()
const group = mangoStore((s) => s.group)
2022-09-28 18:50:02 -07:00
// const jupiterTokens = mangoStore((s) => s.jupiterTokens)
2022-10-03 17:59:25 -07:00
const [settleMktAddress, setSettleMktAddress] = useState<string>('')
2022-09-28 18:50:02 -07:00
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
2022-10-03 17:59:25 -07:00
setSettleMktAddress(mktAddress)
2022-09-28 18:50:02 -07:00
try {
const txid = await client.serum3SettleFunds(
group,
mangoAccount,
new PublicKey(mktAddress)
)
actions.fetchSerumOpenOrders()
actions.reloadMangoAccount()
notify({
type: 'success',
title: 'Successfully settled funds',
txid,
})
} catch (e: any) {
notify({
type: 'error',
2022-10-03 17:59:25 -07:00
title: t('trade:settle-funds-error'),
2022-09-28 18:50:02 -07:00
description: e?.message,
txid: e?.txid,
})
console.error('Settle funds error:', e)
2022-10-03 17:59:25 -07:00
} finally {
setSettleMktAddress('')
}
2022-09-28 18:50:02 -07:00
}, [])
if (!group) return null
2022-10-02 22:42:28 -07:00
return connected ? (
Object.values(unsettledSpotBalances).flat().length ? (
<table className="min-w-full">
<thead>
<tr>
<th className="bg-th-bkg-1 text-left">{t('market')}</th>
2022-10-03 03:38:05 -07:00
<th className="bg-th-bkg-1 text-right">{t('trade:base')}</th>
<th className="bg-th-bkg-1 text-right">{t('trade:quote')}</th>
2022-10-02 22:42:28 -07:00
<th className="bg-th-bkg-1 text-right" />
</tr>
</thead>
<tbody>
{Object.entries(unsettledSpotBalances).map(
([mktAddress, balance]) => {
const market = group.getSerum3MarketByPk(
new PublicKey(mktAddress)
)
const base = market?.name.split('/')[0]
const quote = market?.name.split('/')[1]
2022-10-02 22:42:28 -07:00
return (
<tr key={mktAddress} className="text-sm">
<td>
<div className="flex items-center">
<span>{market ? market.name : ''}</span>
</div>
</td>
<td className="text-right font-mono">
{unsettledSpotBalances[mktAddress].base || 0.0}{' '}
<span className="font-body tracking-wide text-th-fgd-4">
{base}
</span>
</td>
<td className="text-right font-mono">
{unsettledSpotBalances[mktAddress].quote || 0.0}{' '}
<span className="font-body tracking-wide text-th-fgd-4">
{quote}
</span>
</td>
<td>
<div className="flex justify-end">
2022-10-03 17:59:25 -07:00
<Tooltip content={t('trade:settle-funds')}>
2022-10-02 22:42:28 -07:00
<IconButton
onClick={() => handleSettleFunds(mktAddress)}
size="small"
>
2022-10-03 17:59:25 -07:00
{settleMktAddress === mktAddress ? (
<Loading className="h-4 w-4" />
) : (
<CheckIcon className="h-4 w-4" />
)}
2022-10-02 22:42:28 -07:00
</IconButton>
</Tooltip>
</div>
</td>
</tr>
)
}
)}
</tbody>
</table>
) : (
<div className="flex flex-col items-center p-8">
2022-10-03 03:38:05 -07:00
<p>{t('trade:no-unsettled')}</p>
2022-10-02 22:42:28 -07:00
</div>
)
) : (
<div className="flex flex-col items-center p-8">
<LinkIcon className="mb-2 h-6 w-6 text-th-fgd-4" />
2022-10-03 03:38:05 -07:00
<p>{t('trade:connect-unsettled')}</p>
2022-10-02 22:42:28 -07:00
</div>
)
}
export default UnsettledTrades