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

286 lines
10 KiB
TypeScript
Raw Normal View History

2022-10-31 11:26:17 -07:00
import {
PerpMarket,
PerpOrder,
Serum3Market,
Serum3Side,
} from '@blockworks-foundation/mango-v4'
2022-09-26 04:53:49 -07:00
import { IconButton } from '@components/shared/Button'
2022-10-03 17:45:43 -07:00
import Loading from '@components/shared/Loading'
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'
import { PublicKey } from '@solana/web3.js'
2022-09-13 23:24:26 -07:00
import mangoStore from '@store/mangoStore'
2022-11-18 09:09:39 -08:00
import useMangoAccount from 'hooks/useMangoAccount'
2022-10-03 20:19:27 -07:00
import { useViewport } from 'hooks/useViewport'
2022-09-13 23:24:26 -07:00
import { useTranslation } from 'next-i18next'
2022-10-03 17:45:43 -07:00
import { useCallback, useState } from 'react'
2022-09-13 23:24:26 -07:00
import { notify } from 'utils/notifications'
import { formatFixedDecimals, getDecimalCount } from 'utils/numbers'
2022-10-03 20:19:27 -07:00
import { breakpoints } from 'utils/theme'
2022-09-26 04:53:49 -07:00
import MarketLogos from './MarketLogos'
2022-09-13 23:24:26 -07:00
const OpenOrders = () => {
2022-10-03 03:38:05 -07:00
const { t } = useTranslation(['common', 'trade'])
2022-11-18 09:09:39 -08:00
const { mangoAccount } = useMangoAccount()
2022-09-13 23:24:26 -07:00
const openOrders = mangoStore((s) => s.mangoAccount.openOrders)
2022-10-03 17:45:43 -07:00
const [cancelId, setCancelId] = useState<string>('')
2022-10-03 20:19:27 -07:00
const { width } = useViewport()
const showTableView = width ? width > breakpoints.md : false
2022-09-13 23:24:26 -07:00
2022-10-31 11:26:17 -07:00
const handleCancelSerumOrder = useCallback(
2022-09-13 23:24:26 -07:00
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
2022-10-03 17:45:43 -07:00
setCancelId(o.orderId.toString())
2022-09-13 23:24:26 -07:00
try {
2022-10-10 19:16:13 -07:00
if (selectedMarket instanceof Serum3Market) {
const tx = await client.serum3CancelOrder(
group,
mangoAccount,
selectedMarket!.serumMarketExternal,
o.side === 'buy' ? Serum3Side.bid : Serum3Side.ask,
o.orderId
)
2022-10-31 11:26:17 -07:00
actions.fetchOpenOrders()
2022-10-10 19:16:13 -07:00
notify({
type: 'success',
title: 'Transaction successful',
txid: tx,
})
}
2022-09-13 23:24:26 -07:00
} catch (e: any) {
console.error('Error canceling', e)
notify({
2022-10-03 17:45:43 -07:00
title: t('trade:cancel-order-error'),
2022-09-13 23:24:26 -07:00
description: e.message,
txid: e.txid,
type: 'error',
})
2022-10-03 17:45:43 -07:00
} finally {
setCancelId('')
2022-09-13 23:24:26 -07:00
}
},
[t]
)
2022-10-31 11:26:17 -07:00
const handleCancelPerpOrder = useCallback(
async (o: PerpOrder) => {
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
if (!group || !mangoAccount) return
setCancelId(o.orderId.toString())
try {
2022-11-02 09:31:24 -07:00
if (selectedMarket instanceof PerpMarket) {
2022-11-01 10:46:16 -07:00
const tx = await client.perpCancelOrder(
2022-10-31 11:26:17 -07:00
group,
mangoAccount,
o.perpMarketIndex,
2022-11-01 10:46:16 -07:00
o.orderId
2022-10-31 11:26:17 -07:00
)
actions.fetchOpenOrders()
notify({
type: 'success',
title: 'Transaction successful',
txid: tx,
})
}
} catch (e: any) {
console.error('Error canceling', e)
notify({
title: t('trade:cancel-order-error'),
description: e.message,
txid: e.txid,
type: 'error',
})
} finally {
setCancelId('')
}
},
[t]
)
return mangoAccount ? (
Object.values(openOrders).flat().length ? (
2022-10-04 18:59:04 -07:00
showTableView ? (
<table>
<thead>
<tr>
<th className="text-left">{t('market')}</th>
<th className="text-right">{t('trade:side')}</th>
<th className="text-right">{t('trade:size')}</th>
<th className="text-right">{t('price')}</th>
<th className="text-right">{t('value')}</th>
<th className="text-right"></th>
</tr>
</thead>
<tbody>
{Object.entries(openOrders)
.map(([marketPk, orders]) => {
return orders.map((o) => {
2022-10-31 11:26:17 -07:00
const group = mangoStore.getState().group!
let market: PerpMarket | Serum3Market
let quoteSymbol
if (o instanceof PerpOrder) {
market = group.getPerpMarketByMarketIndex(o.perpMarketIndex)
quoteSymbol = group.getFirstBankByTokenIndex(
market.settleTokenIndex
).name
} else {
market = group.getSerum3MarketByExternalMarket(
new PublicKey(marketPk)
)
quoteSymbol = group.getFirstBankByTokenIndex(
market!.quoteTokenIndex
).name
}
2022-10-04 18:59:04 -07:00
return (
<tr
key={`${o.side}${o.size}${o.price}`}
className="my-1 p-2"
>
<td>
<div className="flex items-center">
2022-10-31 11:26:17 -07:00
<MarketLogos market={market!} />
{market?.name}
2022-10-04 18:59:04 -07:00
</div>
</td>
<td className="text-right">
<SideBadge side={o.side} />
</td>
<td className="text-right font-mono">
2022-10-03 20:19:27 -07:00
{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-10-02 20:14:26 -07:00
</span>
2022-10-03 20:19:27 -07:00
</td>
2022-10-04 18:59:04 -07:00
<td className="text-right">
{formatFixedDecimals(o.size * o.price, true)}
</td>
<td>
<div className="flex justify-end">
<Tooltip content={t('cancel')}>
<IconButton
disabled={cancelId === o.orderId.toString()}
2022-10-31 11:26:17 -07:00
onClick={() =>
o instanceof PerpOrder
? handleCancelPerpOrder(o)
: handleCancelSerumOrder(o)
}
2022-10-04 18:59:04 -07:00
size="small"
>
{cancelId === o.orderId.toString() ? (
<Loading className="h-4 w-4" />
) : (
<TrashIcon className="h-4 w-4" />
)}
</IconButton>
</Tooltip>
</div>
</td>
</tr>
)
})
})
2022-10-03 20:19:27 -07:00
.flat()}
</tbody>
</table>
) : (
<div className="pb-20">
{Object.entries(openOrders).map(([marketPk, orders]) => {
return orders.map((o) => {
const group = mangoStore.getState().group
const serumMarket = group?.getSerum3MarketByExternalMarket(
2022-10-04 18:59:04 -07:00
new PublicKey(marketPk)
)
2022-10-03 20:19:27 -07:00
const quoteSymbol = group?.getFirstBankByTokenIndex(
2022-10-04 18:59:04 -07:00
serumMarket!.quoteTokenIndex
2022-10-03 20:19:27 -07:00
).name
return (
<div
className="flex items-center justify-between border-b border-th-bkg-3 p-4"
key={`${o.side}${o.size}${o.price}`}
>
<div className="flex items-center">
2022-10-10 19:16:13 -07:00
<MarketLogos market={serumMarket!} />
2022-10-03 20:19:27 -07:00
<div>
2022-10-04 18:59:04 -07:00
<p className="text-sm text-th-fgd-1">
{serumMarket?.name}
</p>
2022-10-03 20:19:27 -07:00
<span
className={`capitalize ${
o.side === 'buy' ? 'text-th-green' : 'text-th-red'
}`}
>
2022-10-31 11:26:17 -07:00
<SideBadge side={o.side} />
2022-10-03 20:19:27 -07:00
</span>{' '}
<span className="font-mono">
{o.size.toLocaleString(undefined, {
maximumFractionDigits: getDecimalCount(o.size),
})}
</span>{' '}
<span className="text-th-fgd-4">at</span>{' '}
<span className="font-mono">
{o.price.toLocaleString(undefined, {
maximumFractionDigits: getDecimalCount(o.price),
2022-10-03 20:19:27 -07:00
})}
</span>{' '}
<span className="text-th-fgd-4">{quoteSymbol}</span>
</div>
</div>
<div className="flex items-center space-x-3 pl-4">
<span>{formatFixedDecimals(o.size * o.price, true)}</span>
<IconButton
disabled={cancelId === o.orderId.toString()}
2022-10-31 11:26:17 -07:00
onClick={() =>
o instanceof PerpOrder
? handleCancelPerpOrder(o)
: handleCancelSerumOrder(o)
}
2022-10-03 20:19:27 -07:00
>
{cancelId === o.orderId.toString() ? (
<Loading className="h-4 w-4" />
) : (
<TrashIcon className="h-4 w-4" />
)}
</IconButton>
</div>
</div>
)
})
2022-10-03 20:19:27 -07:00
})}
</div>
)
2022-09-19 04:32:59 -07:00
) : (
<div className="flex flex-col items-center p-8">
2022-10-03 03:38:05 -07:00
<p>{t('trade:no-orders')}</p>
2022-09-19 04:32:59 -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-orders')}</p>
2022-09-19 04:32:59 -07:00
</div>
2022-09-13 23:24:26 -07:00
)
}
export default OpenOrders