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

321 lines
12 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-11-20 02:44:14 -08:00
import { Table, Td, Th, TrBody, TrHead } from '@components/shared/TableElements'
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-11-24 15:30:28 -08: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'
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-24 15:30:28 -08:00
const { connected } = useWallet()
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]
)
2022-11-24 15:30:28 -08:00
return connected ? (
Object.values(openOrders).flat().length ? (
2022-10-04 18:59:04 -07:00
showTableView ? (
2022-11-20 02:44:14 -08:00
<Table>
2022-10-04 18:59:04 -07:00
<thead>
2022-11-20 02:44:14 -08:00
<TrHead>
<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>
</TrHead>
2022-10-04 18:59:04 -07:00
</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
2022-11-21 01:42:18 -08:00
let tickSize: number
2022-11-21 02:37:31 -08:00
let minOrderSize: number
2022-10-31 11:26:17 -07:00
let quoteSymbol
if (o instanceof PerpOrder) {
market = group.getPerpMarketByMarketIndex(o.perpMarketIndex)
quoteSymbol = group.getFirstBankByTokenIndex(
market.settleTokenIndex
).name
2022-11-21 01:42:18 -08:00
tickSize = market.tickSize
2022-11-21 02:37:31 -08:00
minOrderSize = market.minOrderSize
2022-10-31 11:26:17 -07:00
} else {
market = group.getSerum3MarketByExternalMarket(
new PublicKey(marketPk)
)
quoteSymbol = group.getFirstBankByTokenIndex(
market!.quoteTokenIndex
).name
2022-11-21 02:37:31 -08:00
const serumMarket = group.getSerum3ExternalMarket(
2022-11-21 01:42:18 -08:00
market.serumMarketExternal
2022-11-21 02:37:31 -08:00
)
tickSize = serumMarket.tickSize
minOrderSize = serumMarket.minOrderSize
2022-10-31 11:26:17 -07:00
}
2022-10-04 18:59:04 -07:00
return (
2022-11-20 02:44:14 -08:00
<TrBody
2022-10-04 18:59:04 -07:00
key={`${o.side}${o.size}${o.price}`}
className="my-1 p-2"
>
2022-11-20 02:44:14 -08:00
<Td>
2022-10-04 18:59:04 -07:00
<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>
2022-11-20 02:44:14 -08:00
</Td>
<Td className="text-right">
2022-10-04 18:59:04 -07:00
<SideBadge side={o.side} />
2022-11-20 02:44:14 -08:00
</Td>
<Td className="text-right font-mono">
2022-10-03 20:19:27 -07:00
{o.size.toLocaleString(undefined, {
2022-11-21 02:37:31 -08:00
maximumFractionDigits: getDecimalCount(minOrderSize),
2022-10-03 20:19:27 -07:00
})}
2022-11-20 02:44:14 -08:00
</Td>
<Td className="text-right">
2022-10-03 20:19:27 -07:00
<span className="font-mono">
{o.price.toLocaleString(undefined, {
2022-11-21 01:42:18 -08:00
minimumFractionDigits: getDecimalCount(tickSize),
maximumFractionDigits: getDecimalCount(tickSize),
2022-10-03 20:19:27 -07:00
})}{' '}
<span className="font-body tracking-wide text-th-fgd-4">
{quoteSymbol}
</span>
2022-10-02 20:14:26 -07:00
</span>
2022-11-20 02:44:14 -08:00
</Td>
<Td className="text-right">
2022-10-04 18:59:04 -07:00
{formatFixedDecimals(o.size * o.price, true)}
2022-11-20 02:44:14 -08:00
</Td>
<Td>
2022-10-04 18:59:04 -07:00
<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>
2022-11-20 02:44:14 -08:00
</Td>
</TrBody>
2022-10-04 18:59:04 -07:00
)
})
})
2022-10-03 20:19:27 -07:00
.flat()}
</tbody>
2022-11-20 02:44:14 -08:00
</Table>
2022-10-03 20:19:27 -07:00
) : (
2022-11-22 15:43:25 -08:00
<div>
2022-10-03 20:19:27 -07:00
{Object.entries(openOrders).map(([marketPk, orders]) => {
return orders.map((o) => {
2022-11-21 02:37:31 -08:00
const group = mangoStore.getState().group!
let market: PerpMarket | Serum3Market
let tickSize: number
let minOrderSize: number
2022-11-22 15:43:25 -08:00
let quoteSymbol: string
let baseSymbol: string
2022-11-21 02:37:31 -08:00
if (o instanceof PerpOrder) {
market = group.getPerpMarketByMarketIndex(o.perpMarketIndex)
2022-11-22 15:43:25 -08:00
baseSymbol = market.name.split('-')[0]
2022-11-21 02:37:31 -08:00
quoteSymbol = group.getFirstBankByTokenIndex(
market.settleTokenIndex
).name
tickSize = market.tickSize
minOrderSize = market.minOrderSize
} else {
market = group.getSerum3MarketByExternalMarket(
new PublicKey(marketPk)
)
2022-11-22 15:43:25 -08:00
baseSymbol = market.name.split('/')[0]
2022-11-21 02:37:31 -08:00
quoteSymbol = group.getFirstBankByTokenIndex(
market!.quoteTokenIndex
).name
const serumMarket = group.getSerum3ExternalMarket(
market.serumMarketExternal
)
tickSize = serumMarket.tickSize
minOrderSize = serumMarket.minOrderSize
}
2022-10-03 20:19:27 -07:00
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-11-21 02:37:31 -08:00
<MarketLogos market={market} />
2022-10-03 20:19:27 -07:00
<div>
2022-11-21 02:37:31 -08:00
<div className="mb-0.5 flex items-center space-x-2">
2022-11-22 15:43:25 -08:00
<p className="whitespace-nowrap text-sm text-th-fgd-1">
{market.name}
</p>
2022-10-31 11:26:17 -07:00
<SideBadge side={o.side} />
2022-11-21 02:37:31 -08:00
</div>
2022-10-03 20:19:27 -07:00
</div>
</div>
2022-11-22 15:43:25 -08:00
<div className="flex items-center space-x-3 pl-8">
<div className="text-right">
<p className="mb-0.5 text-th-fgd-4">
<span className="font-mono text-th-fgd-3">
{o.size.toLocaleString(undefined, {
maximumFractionDigits:
getDecimalCount(minOrderSize),
})}
</span>{' '}
{baseSymbol}
</p>
<p className="text-xs text-th-fgd-4">
<span className="font-mono text-th-fgd-3">
{o.price.toLocaleString(undefined, {
minimumFractionDigits: getDecimalCount(tickSize),
maximumFractionDigits: getDecimalCount(tickSize),
})}
</span>{' '}
{quoteSymbol}
</p>
</div>
2022-10-03 20:19:27 -07:00
<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