2021-04-12 20:39:08 -07:00
|
|
|
import { useState } from 'react'
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
import Link from 'next/link'
|
|
|
|
import { useRouter } from 'next/router'
|
2021-04-07 14:49:37 -07:00
|
|
|
import { useOpenOrders } from '../hooks/useOpenOrders'
|
2021-09-06 10:35:40 -07:00
|
|
|
import Button from './Button'
|
2021-04-12 20:39:08 -07:00
|
|
|
import Loading from './Loading'
|
2021-09-13 14:14:59 -07:00
|
|
|
import useMangoStore from '../stores/useMangoStore'
|
2021-04-12 20:39:08 -07:00
|
|
|
import { notify } from '../utils/notifications'
|
2021-04-20 07:19:08 -07:00
|
|
|
import SideBadge from './SideBadge'
|
2021-06-18 20:07:57 -07:00
|
|
|
import { Order, Market } from '@project-serum/serum/lib/market'
|
2021-06-18 21:26:47 -07:00
|
|
|
import { PerpOrder, PerpMarket } from '@blockworks-foundation/mango-client'
|
2021-09-23 04:50:34 -07:00
|
|
|
import { formatUsdValue } from '../utils'
|
2021-09-06 10:35:40 -07:00
|
|
|
import { Table, Td, Th, TrBody, TrHead } from './TableElements'
|
|
|
|
import { useViewport } from '../hooks/useViewport'
|
|
|
|
import { breakpoints } from './TradePageGrid'
|
|
|
|
import { Row } from './TableElements'
|
2021-09-23 04:50:34 -07:00
|
|
|
import { PerpTriggerOrder } from '../@types/types'
|
2021-04-05 14:38:59 -07:00
|
|
|
|
2021-09-27 08:45:39 -07:00
|
|
|
const DesktopTable = ({ openOrders, cancelledOrderId, handleCancelOrder }) => {
|
|
|
|
return (
|
|
|
|
<Table>
|
|
|
|
<thead>
|
|
|
|
<TrHead>
|
|
|
|
<Th>Market</Th>
|
|
|
|
<Th>Side</Th>
|
|
|
|
<Th>Size</Th>
|
|
|
|
<Th>Price</Th>
|
|
|
|
<Th>Value</Th>
|
|
|
|
<Th>Condition</Th>
|
|
|
|
<Th>
|
|
|
|
<span className={`sr-only`}>Edit</span>
|
|
|
|
</Th>
|
|
|
|
</TrHead>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{openOrders.map(({ order, market }, index) => {
|
|
|
|
return (
|
|
|
|
<TrBody index={index} key={`${order.orderId}${order.side}`}>
|
|
|
|
<Td>
|
|
|
|
<div className="flex items-center">
|
|
|
|
<img
|
|
|
|
alt=""
|
|
|
|
width="20"
|
|
|
|
height="20"
|
|
|
|
src={`/assets/icons/${market.config.baseSymbol.toLowerCase()}.svg`}
|
|
|
|
className={`mr-2.5`}
|
|
|
|
/>
|
|
|
|
<div>{market.config.name}</div>
|
|
|
|
</div>
|
|
|
|
</Td>
|
|
|
|
<Td>
|
|
|
|
<SideBadge side={order.side} />
|
|
|
|
</Td>
|
|
|
|
<Td>{order.size}</Td>
|
|
|
|
<Td>{formatUsdValue(order.price)}</Td>
|
|
|
|
<Td>{formatUsdValue(order.price * order.size)}</Td>
|
|
|
|
<Td>
|
|
|
|
{order.perpTrigger &&
|
|
|
|
`${order.orderType} ${
|
|
|
|
order.triggerCondition
|
2021-10-12 13:59:21 -07:00
|
|
|
} ${order.triggerPrice.toFixed(2)}`}
|
2021-09-27 08:45:39 -07:00
|
|
|
</Td>
|
|
|
|
<Td>
|
|
|
|
<div className={`flex justify-end`}>
|
|
|
|
<Button
|
|
|
|
onClick={() => handleCancelOrder(order, market.account)}
|
|
|
|
className="ml-3 text-xs pt-0 pb-0 h-8 pl-3 pr-3"
|
|
|
|
>
|
|
|
|
{cancelledOrderId + '' === order.orderId + '' ? (
|
|
|
|
<Loading />
|
|
|
|
) : (
|
|
|
|
<span>Cancel</span>
|
|
|
|
)}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</Td>
|
|
|
|
</TrBody>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
</tbody>
|
|
|
|
</Table>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const MobileTable = ({ openOrders, cancelledOrderId, handleCancelOrder }) => {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{openOrders.map(({ market, order }, index) => (
|
|
|
|
<Row key={`${order.orderId}${order.side}`} index={index}>
|
|
|
|
<div className="col-span-12 flex items-center justify-between text-fgd-1 text-left">
|
|
|
|
<div className="flex items-center">
|
|
|
|
<img
|
|
|
|
alt=""
|
|
|
|
width="20"
|
|
|
|
height="20"
|
|
|
|
src={`/assets/icons/${market.config.baseSymbol.toLowerCase()}.svg`}
|
|
|
|
className={`mr-2.5`}
|
|
|
|
/>
|
|
|
|
<div>
|
|
|
|
<div className="mb-0.5">{market.config.name}</div>
|
|
|
|
<div className="text-th-fgd-3 text-xs">
|
|
|
|
<span
|
|
|
|
className={`mr-1
|
|
|
|
${
|
|
|
|
order.side === 'buy'
|
|
|
|
? 'text-th-green'
|
|
|
|
: 'text-th-red'
|
|
|
|
}
|
|
|
|
`}
|
|
|
|
>
|
|
|
|
{order.side.toUpperCase()}
|
|
|
|
</span>
|
2021-09-29 12:09:51 -07:00
|
|
|
{order.perpTrigger
|
|
|
|
? `${order.size} ${order.triggerCondition} ${order.triggerPrice}`
|
|
|
|
: `${order.size} at ${formatUsdValue(order.price)}`}
|
2021-09-27 08:45:39 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<Button
|
|
|
|
onClick={() => handleCancelOrder(order, market.account)}
|
|
|
|
className="ml-3 text-xs pt-0 pb-0 h-8 pl-3 pr-3"
|
|
|
|
>
|
|
|
|
{cancelledOrderId + '' === order.orderId + '' ? (
|
|
|
|
<Loading />
|
|
|
|
) : (
|
|
|
|
<span>Cancel</span>
|
|
|
|
)}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</Row>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-05 14:38:59 -07:00
|
|
|
const OpenOrdersTable = () => {
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
const { asPath } = useRouter()
|
2021-04-07 14:49:37 -07:00
|
|
|
const openOrders = useOpenOrders()
|
2021-04-12 20:39:08 -07:00
|
|
|
const [cancelId, setCancelId] = useState(null)
|
2021-04-15 15:45:26 -07:00
|
|
|
const actions = useMangoStore((s) => s.actions)
|
2021-09-06 10:35:40 -07:00
|
|
|
const { width } = useViewport()
|
|
|
|
const isMobile = width ? width < breakpoints.md : false
|
2021-04-12 20:39:08 -07:00
|
|
|
|
2021-06-23 08:32:33 -07:00
|
|
|
const handleCancelOrder = async (
|
2021-09-23 04:50:34 -07:00
|
|
|
order: Order | PerpOrder | PerpTriggerOrder,
|
2021-06-23 08:32:33 -07:00
|
|
|
market: Market | PerpMarket
|
|
|
|
) => {
|
2021-06-18 13:46:20 -07:00
|
|
|
const wallet = useMangoStore.getState().wallet.current
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
const selectedMangoGroup =
|
|
|
|
useMangoStore.getState().selectedMangoGroup.current
|
2021-06-23 08:32:33 -07:00
|
|
|
const selectedMangoAccount =
|
|
|
|
useMangoStore.getState().selectedMangoAccount.current
|
2021-09-13 14:14:59 -07:00
|
|
|
const mangoClient = useMangoStore.getState().connection.client
|
2021-06-18 20:07:57 -07:00
|
|
|
setCancelId(order.orderId)
|
2021-08-23 10:21:20 -07:00
|
|
|
let txid
|
2021-04-12 20:39:08 -07:00
|
|
|
try {
|
2021-06-23 08:32:33 -07:00
|
|
|
if (!selectedMangoGroup || !selectedMangoAccount) return
|
2021-06-18 20:07:57 -07:00
|
|
|
if (market instanceof Market) {
|
2021-08-23 10:21:20 -07:00
|
|
|
txid = await mangoClient.cancelSpotOrder(
|
2021-06-18 21:26:47 -07:00
|
|
|
selectedMangoGroup,
|
2021-06-23 08:32:33 -07:00
|
|
|
selectedMangoAccount,
|
2021-06-18 21:26:47 -07:00
|
|
|
wallet,
|
|
|
|
market,
|
2021-09-07 12:27:54 -07:00
|
|
|
order as Order
|
2021-06-18 21:26:47 -07:00
|
|
|
)
|
2021-06-18 20:07:57 -07:00
|
|
|
} else if (market instanceof PerpMarket) {
|
2021-09-23 04:50:34 -07:00
|
|
|
// 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
|
|
|
|
)
|
|
|
|
}
|
2021-06-18 20:07:57 -07:00
|
|
|
}
|
2021-08-23 10:21:20 -07:00
|
|
|
notify({ title: 'Successfully cancelled order', txid })
|
2021-04-12 20:39:08 -07:00
|
|
|
} catch (e) {
|
|
|
|
notify({
|
2021-07-06 15:04:20 -07:00
|
|
|
title: 'Error cancelling order',
|
2021-04-12 20:39:08 -07:00
|
|
|
description: e.message,
|
2021-05-21 15:00:39 -07:00
|
|
|
txid: e.txid,
|
2021-04-12 20:39:08 -07:00
|
|
|
type: 'error',
|
|
|
|
})
|
2021-06-18 13:46:20 -07:00
|
|
|
console.log('error', `${e}`)
|
2021-04-12 20:39:08 -07:00
|
|
|
} finally {
|
2021-09-23 04:50:34 -07:00
|
|
|
// await sleep(600)
|
2021-09-07 16:27:14 -07:00
|
|
|
actions.reloadMangoAccount()
|
2021-04-12 20:39:08 -07:00
|
|
|
setCancelId(null)
|
|
|
|
}
|
|
|
|
}
|
2021-04-05 14:38:59 -07:00
|
|
|
|
2021-09-27 08:45:39 -07:00
|
|
|
const tableProps = {
|
|
|
|
openOrders,
|
|
|
|
cancelledOrderId: cancelId,
|
|
|
|
handleCancelOrder,
|
|
|
|
}
|
|
|
|
|
2021-04-05 14:38:59 -07:00
|
|
|
return (
|
2021-09-19 17:36:02 -07:00
|
|
|
<div className={`flex flex-col py-2 sm:pb-4 sm:pt-4`}>
|
2021-04-12 09:49:02 -07:00
|
|
|
<div className={`-my-2 overflow-x-auto sm:-mx-6 lg:-mx-8`}>
|
|
|
|
<div className={`align-middle inline-block min-w-full sm:px-6 lg:px-8`}>
|
2021-04-11 21:17:23 -07:00
|
|
|
{openOrders && openOrders.length > 0 ? (
|
2021-09-06 10:35:40 -07:00
|
|
|
!isMobile ? (
|
2021-09-27 08:45:39 -07:00
|
|
|
<DesktopTable {...tableProps} />
|
2021-09-06 10:35:40 -07:00
|
|
|
) : (
|
2021-09-27 08:45:39 -07:00
|
|
|
<MobileTable {...tableProps} />
|
2021-09-06 10:35:40 -07:00
|
|
|
)
|
2021-04-07 14:49:37 -07:00
|
|
|
) : (
|
|
|
|
<div
|
2021-04-19 06:45:59 -07:00
|
|
|
className={`w-full text-center py-6 bg-th-bkg-1 text-th-fgd-3 rounded-md`}
|
2021-04-07 14:49:37 -07:00
|
|
|
>
|
2021-08-15 06:31:59 -07:00
|
|
|
No open orders
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
{asPath === '/account' ? (
|
|
|
|
<Link href={'/'}>
|
|
|
|
<a
|
|
|
|
className={`inline-flex ml-2 py-0
|
|
|
|
`}
|
|
|
|
>
|
|
|
|
Make a trade
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
) : null}
|
2021-04-07 14:49:37 -07:00
|
|
|
</div>
|
|
|
|
)}
|
2021-04-05 14:38:59 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default OpenOrdersTable
|