import { useState } from 'react' import Link from 'next/link' import { useRouter } from 'next/router' import { useOpenOrders } from '../hooks/useOpenOrders' import Button from './Button' import Loading from './Loading' import useMangoStore from '../stores/useMangoStore' import { notify } from '../utils/notifications' import SideBadge from './SideBadge' import { Order, Market } from '@project-serum/serum/lib/market' import { PerpOrder, PerpMarket } from '@blockworks-foundation/mango-client' import { formatUsdValue } from '../utils' import { Table, Td, Th, TrBody, TrHead } from './TableElements' import { useViewport } from '../hooks/useViewport' import { breakpoints } from './TradePageGrid' import { Row } from './TableElements' import { PerpTriggerOrder } from '../@types/types' const DesktopTable = ({ openOrders, cancelledOrderId, handleCancelOrder }) => { return ( {openOrders.map(({ order, market }, index) => { return ( ) })}
Market Side Size Price Value Condition Edit
{market.config.name}
{order.size} {formatUsdValue(order.price)} {formatUsdValue(order.price * order.size)} {order.perpTrigger && `${order.orderType} ${ order.triggerCondition } ${order.triggerPrice.toString()}`}
) } const MobileTable = ({ openOrders, cancelledOrderId, handleCancelOrder }) => { return ( <> {openOrders.map(({ market, order }, index) => (
{market.config.name}
{order.side.toUpperCase()} {order.perpTrigger ? `${order.size} ${order.triggerCondition} ${order.triggerPrice}` : `${order.size} at ${formatUsdValue(order.price)}`}
))} ) } const OpenOrdersTable = () => { const { asPath } = useRouter() const openOrders = useOpenOrders() const [cancelId, setCancelId] = useState(null) const actions = useMangoStore((s) => s.actions) const { width } = useViewport() const isMobile = width ? width < breakpoints.md : false const handleCancelOrder = async ( order: Order | PerpOrder | PerpTriggerOrder, market: Market | PerpMarket ) => { const wallet = useMangoStore.getState().wallet.current const selectedMangoGroup = useMangoStore.getState().selectedMangoGroup.current const selectedMangoAccount = useMangoStore.getState().selectedMangoAccount.current const mangoClient = useMangoStore.getState().connection.client setCancelId(order.orderId) let txid try { if (!selectedMangoGroup || !selectedMangoAccount) return if (market instanceof Market) { txid = await mangoClient.cancelSpotOrder( selectedMangoGroup, selectedMangoAccount, wallet, market, order as Order ) } else if (market instanceof PerpMarket) { // TODO: this is not ideal if (order['triggerCondition']) { txid = await mangoClient.removeAdvancedOrder( selectedMangoGroup, selectedMangoAccount, wallet, (order as PerpTriggerOrder).orderId ) } else { txid = await mangoClient.cancelPerpOrder( selectedMangoGroup, selectedMangoAccount, wallet, market, order as PerpOrder, false ) } } notify({ title: 'Successfully cancelled order', txid }) } catch (e) { notify({ title: 'Error cancelling order', description: e.message, txid: e.txid, type: 'error', }) console.log('error', `${e}`) } finally { // await sleep(600) actions.reloadMangoAccount() setCancelId(null) } } const tableProps = { openOrders, cancelledOrderId: cancelId, handleCancelOrder, } return (
{openOrders && openOrders.length > 0 ? ( !isMobile ? ( ) : ( ) ) : (
No open orders {asPath === '/account' ? ( Make a trade ) : null}
)}
) } export default OpenOrdersTable