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

161 lines
5.7 KiB
TypeScript
Raw Normal View History

2022-09-13 23:24:26 -07:00
import { Serum3Side } from '@blockworks-foundation/mango-v4'
2022-09-26 04:53:49 -07:00
import { IconButton } from '@components/shared/Button'
2022-09-13 23:24:26 -07:00
import SideBadge from '@components/shared/SideBadge'
2022-09-26 04:33:07 -07:00
import Tooltip from '@components/shared/Tooltip'
import { LinkIcon, TrashIcon } from '@heroicons/react/20/solid'
2022-09-13 23:24:26 -07:00
import { Order } from '@project-serum/serum/lib/market'
2022-09-19 04:32:59 -07:00
import { useWallet } from '@solana/wallet-adapter-react'
import { PublicKey } from '@solana/web3.js'
2022-09-13 23:24:26 -07:00
import mangoStore from '@store/mangoStore'
import { useTranslation } from 'next-i18next'
import { useCallback } from 'react'
2022-09-13 23:24:26 -07:00
import { notify } from 'utils/notifications'
import { formatFixedDecimals, getDecimalCount } from 'utils/numbers'
2022-09-26 04:53:49 -07:00
import MarketLogos from './MarketLogos'
2022-09-13 23:24:26 -07:00
const OpenOrders = () => {
const { t } = useTranslation('common')
2022-09-19 04:32:59 -07:00
const { connected } = useWallet()
2022-09-13 23:24:26 -07:00
const openOrders = mangoStore((s) => s.mangoAccount.openOrders)
2022-09-26 04:53:49 -07:00
const jupiterTokens = mangoStore((s) => s.jupiterTokens)
2022-09-13 23:24:26 -07:00
const handleCancelOrder = useCallback(
async (o: Order) => {
const client = mangoStore.getState().client
const group = mangoStore.getState().group
const mangoAccount = mangoStore.getState().mangoAccount.current
const selectedMarket = mangoStore.getState().selectedMarket.current
const actions = mangoStore.getState().actions
2022-09-13 23:24:26 -07:00
if (!group || !mangoAccount) return
try {
const tx = await client.serum3CancelOrder(
group,
mangoAccount,
selectedMarket!.serumMarketExternal,
o.side === 'buy' ? Serum3Side.bid : Serum3Side.ask,
o.orderId
)
actions.fetchSerumOpenOrders()
2022-09-13 23:24:26 -07:00
notify({
type: 'success',
title: 'Transaction successful',
txid: tx,
})
} catch (e: any) {
console.error('Error canceling', e)
notify({
title: t('order-error'),
description: e.message,
txid: e.txid,
type: 'error',
})
}
},
[t]
)
2022-09-19 04:32:59 -07:00
return connected ? (
Object.values(openOrders).flat().length ? (
2022-09-19 04:32:59 -07:00
<table>
<thead>
2022-09-26 04:33:07 -07:00
<tr>
2022-09-26 18:31:16 -07:00
<th className="text-left">{t('market')}</th>
2022-09-26 04:33:07 -07:00
<th className="text-right">{t('side')}</th>
<th className="text-right">{t('size')}</th>
<th className="text-right">{t('price')}</th>
<th className="text-right">{t('value')}</th>
2022-09-19 04:32:59 -07:00
<th className="text-right"></th>
</tr>
</thead>
<tbody>
{Object.entries(openOrders)
.map(([marketPk, orders]) => {
return orders.map((o) => {
const group = mangoStore.getState().group
2022-09-26 04:33:07 -07:00
const market = group?.getSerum3MarketByPk(
new PublicKey(marketPk)
)
2022-09-26 04:53:49 -07:00
let baseLogoURI = ''
let quoteLogoURI = ''
const baseSymbol = group?.getFirstBankByTokenIndex(
market!.baseTokenIndex
).name
const quoteSymbol = group?.getFirstBankByTokenIndex(
market!.quoteTokenIndex
).name
if (jupiterTokens.length) {
baseLogoURI = jupiterTokens.find(
(t) => t.symbol === baseSymbol
)!.logoURI
quoteLogoURI = jupiterTokens.find(
(t) => t.symbol === quoteSymbol
)!.logoURI
}
return (
<tr key={`${o.side}${o.size}${o.price}`} className="my-1 p-2">
2022-09-26 18:31:16 -07:00
<td>
<div className="flex items-center">
<MarketLogos
baseURI={baseLogoURI}
quoteURI={quoteLogoURI}
/>
{market?.name}
</div>
2022-09-26 04:53:49 -07:00
</td>
<td className="text-right">
<SideBadge side={o.side} />
</td>
<td className="text-right font-mono">
{o.size.toLocaleString(undefined, {
maximumFractionDigits: getDecimalCount(o.size),
})}
</td>
<td className="text-right">
<span className="font-mono">
{o.price.toLocaleString(undefined, {
maximumFractionDigits: getDecimalCount(o.price),
})}{' '}
<span className="font-body tracking-wide text-th-fgd-4">
{quoteSymbol}
</span>
2022-09-26 04:33:07 -07:00
</span>
</td>
<td className="text-right font-mono">
2022-09-26 04:33:07 -07:00
{formatFixedDecimals(o.size * o.price, true)}
</td>
<td>
<div className="flex justify-end">
<Tooltip content={t('cancel')}>
<IconButton
onClick={() => handleCancelOrder(o)}
size="small"
>
<TrashIcon className="h-4 w-4" />
</IconButton>
</Tooltip>
</div>
</td>
</tr>
)
})
})
.flat()}
2022-09-19 04:32:59 -07:00
</tbody>
</table>
) : (
<div className="flex flex-col items-center p-8">
<p>No open orders...</p>
</div>
)
) : (
<div className="flex flex-col items-center p-8">
<LinkIcon className="mb-2 h-6 w-6 text-th-fgd-4" />
<p>Connect to view your open orders</p>
</div>
2022-09-13 23:24:26 -07:00
)
}
export default OpenOrders