mango-v4-ui/components/swap/SwapOrders.tsx

346 lines
10 KiB
TypeScript
Raw Normal View History

2023-08-08 20:40:26 -07:00
import { IconButton, LinkButton } from '@components/shared/Button'
import ConnectEmptyState from '@components/shared/ConnectEmptyState'
import {
SortableColumnHeader,
Table,
Td,
Th,
TrBody,
TrHead,
} from '@components/shared/TableElements'
import { NoSymbolIcon, TrashIcon } from '@heroicons/react/20/solid'
import { BN } from '@project-serum/anchor'
import { useWallet } from '@solana/wallet-adapter-react'
import mangoStore from '@store/mangoStore'
import useMangoAccount from 'hooks/useMangoAccount'
import useMangoGroup from 'hooks/useMangoGroup'
import { useSortableData } from 'hooks/useSortableData'
import { useCallback, useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { notify } from 'utils/notifications'
import { floorToDecimal } from 'utils/numbers'
import * as sentry from '@sentry/nextjs'
import { isMangoError } from 'types'
import Loading from '@components/shared/Loading'
2023-08-08 19:53:59 -07:00
import SideBadge from '@components/shared/SideBadge'
// todo: add mobile table
const SwapOrders = () => {
const { t } = useTranslation(['common', 'swap', 'trade'])
const { mangoAccount, mangoAccountAddress } = useMangoAccount()
const { group } = useMangoGroup()
const { connected } = useWallet()
const [cancelId, setCancelId] = useState('')
const orders = useMemo(() => {
if (!mangoAccount) return []
return mangoAccount.tokenConditionalSwaps.filter((tcs) => tcs.hasData)
}, [mangoAccount])
const formattedTableData = useCallback(() => {
if (!group) return []
const formatted = []
for (const order of orders) {
const buyBank = group.getFirstBankByTokenIndex(order.buyTokenIndex)
const sellBank = group.getFirstBankByTokenIndex(order.sellTokenIndex)
2023-07-31 05:25:46 -07:00
const pair = `${sellBank.name}/${buyBank.name}`
2023-08-08 19:53:59 -07:00
const maxBuy = floorToDecimal(
order.getMaxBuyUi(group),
buyBank.mintDecimals,
).toNumber()
const maxSell = floorToDecimal(
order.getMaxSellUi(group),
sellBank.mintDecimals,
).toNumber()
2023-08-08 19:53:59 -07:00
let size
let side
if (maxBuy === 0 || maxBuy > maxSell) {
size = maxSell
side = 'sell'
} else {
size = maxBuy
side = 'buy'
}
2023-08-08 18:03:54 -07:00
const triggerPrice = order.getThresholdPriceUi(group)
const pricePremium = order.getPricePremium()
2023-07-31 05:25:46 -07:00
const filled = order.getSoldUi(group)
2023-08-13 05:47:25 -07:00
const currentPrice = order.getCurrentPairPriceUi(group)
const data = {
...order,
buyBank,
2023-08-01 22:32:20 -07:00
currentPrice,
sellBank,
2023-07-31 05:25:46 -07:00
pair,
2023-08-08 19:53:59 -07:00
side,
size,
2023-07-31 05:25:46 -07:00
filled,
triggerPrice,
fee: pricePremium,
}
formatted.push(data)
}
return formatted
}, [group, orders])
const {
items: tableData,
requestSort,
sortConfig,
} = useSortableData(formattedTableData())
const handleCancel = async (id: BN) => {
try {
const client = mangoStore.getState().client
const group = mangoStore.getState().group
const actions = mangoStore.getState().actions
const mangoAccount = mangoStore.getState().mangoAccount.current
if (!mangoAccount || !group) return
setCancelId(id.toString())
try {
2023-08-13 04:21:08 -07:00
const { signature: tx, slot } = await client.tokenConditionalSwapCancel(
group,
mangoAccount,
id,
)
notify({
title: 'Transaction confirmed',
type: 'success',
txid: tx,
noSound: true,
})
actions.fetchGroup()
2023-08-13 04:21:08 -07:00
await actions.reloadMangoAccount(slot)
} catch (e) {
console.error('failed to cancel swap order', e)
sentry.captureException(e)
if (isMangoError(e)) {
notify({
title: 'Transaction failed',
description: e.message,
txid: e?.txid,
type: 'error',
})
}
}
2023-08-08 20:40:26 -07:00
} catch (e) {
console.error('failed to cancel trigger order', e)
} finally {
setCancelId('')
}
}
const handleCancelAll = async () => {
try {
const client = mangoStore.getState().client
const group = mangoStore.getState().group
const actions = mangoStore.getState().actions
const mangoAccount = mangoStore.getState().mangoAccount.current
if (!mangoAccount || !group) return
setCancelId('all')
try {
2023-08-13 04:21:08 -07:00
const { signature: tx, slot } =
await client.tokenConditionalSwapCancelAll(group, mangoAccount)
2023-08-08 20:40:26 -07:00
notify({
title: 'Transaction confirmed',
type: 'success',
txid: tx,
noSound: true,
})
actions.fetchGroup()
2023-08-13 04:21:08 -07:00
await actions.reloadMangoAccount(slot)
2023-08-08 20:40:26 -07:00
} catch (e) {
console.error('failed to cancel trigger orders', e)
sentry.captureException(e)
if (isMangoError(e)) {
notify({
title: 'Transaction failed',
description: e.message,
txid: e?.txid,
type: 'error',
})
}
}
} catch (e) {
console.error('failed to cancel swap order', e)
} finally {
setCancelId('')
}
}
return orders.length ? (
<Table>
<thead>
<TrHead>
<Th className="text-left">
<SortableColumnHeader
2023-07-31 05:25:46 -07:00
sortKey="pair"
sort={() => requestSort('pair')}
sortConfig={sortConfig}
2023-07-31 05:25:46 -07:00
title={t('swap:pair')}
/>
</Th>
2023-08-08 19:53:59 -07:00
<Th>
<div className="flex justify-end">
<SortableColumnHeader
sortKey="side"
sort={() => requestSort('side')}
sortConfig={sortConfig}
title={t('trade:side')}
/>
</div>
</Th>
<Th>
<div className="flex justify-end">
<SortableColumnHeader
sortKey="size"
sort={() => requestSort('size')}
sortConfig={sortConfig}
2023-07-31 05:25:46 -07:00
title={t('trade:size')}
/>
</div>
</Th>
<Th>
<div className="flex justify-end">
<SortableColumnHeader
sortKey="filled"
sort={() => requestSort('filled')}
sortConfig={sortConfig}
title={t('trade:filled')}
/>
</div>
</Th>
<Th>
<div className="flex justify-end">
<SortableColumnHeader
2023-08-01 22:32:20 -07:00
sortKey="currentPrice"
sort={() => requestSort('currentPrice')}
sortConfig={sortConfig}
2023-08-01 22:32:20 -07:00
title={t('trade:current-price')}
/>
</div>
</Th>
<Th>
<div className="flex justify-end">
<SortableColumnHeader
2023-08-01 22:32:20 -07:00
sortKey="triggerPrice"
sort={() => requestSort('triggerPrice')}
sortConfig={sortConfig}
2023-08-01 22:32:20 -07:00
title={t('trade:trigger-price')}
/>
</div>
</Th>
<Th>
<div className="flex justify-end">
<SortableColumnHeader
sortKey="fee"
sort={() => requestSort('fee')}
sortConfig={sortConfig}
2023-08-02 17:45:40 -07:00
title={t('trade:est-slippage')}
/>
</div>
</Th>
2023-08-08 20:40:26 -07:00
<Th>
<div className="flex justify-end">
<LinkButton onClick={handleCancelAll}>
{t('trade:cancel-all')}
</LinkButton>
</div>
</Th>
</TrHead>
</thead>
<tbody>
{tableData.map((data, i) => {
const {
buyBank,
2023-08-01 22:32:20 -07:00
currentPrice,
fee,
2023-07-31 05:25:46 -07:00
pair,
sellBank,
2023-08-08 19:53:59 -07:00
side,
size,
2023-07-31 05:25:46 -07:00
filled,
triggerPrice,
} = data
2023-08-08 19:53:59 -07:00
const bank = side === 'buy' ? buyBank : sellBank
return (
<TrBody key={i} className="text-sm">
2023-07-31 05:25:46 -07:00
<Td>{pair}</Td>
2023-08-08 19:53:59 -07:00
<Td>
<div className="flex justify-end">
<SideBadge side={side} />
</div>
</Td>
<Td>
<p className="text-right">
{size}
2023-08-08 19:53:59 -07:00
<span className="text-th-fgd-3 font-body"> {bank.name}</span>
</p>
</Td>
<Td>
2023-07-31 05:25:46 -07:00
<p className="text-right">
{filled}/{size}
2023-08-08 19:53:59 -07:00
<span className="text-th-fgd-3 font-body"> {bank.name}</span>
2023-07-31 05:25:46 -07:00
</p>
</Td>
<Td>
2023-08-01 22:32:20 -07:00
<p className="text-right">
{currentPrice}
<span className="text-th-fgd-3 font-body">
{' '}
{buyBank.name}
</span>
</p>
</Td>
<Td>
2023-08-01 22:32:20 -07:00
<p className="text-right">
{triggerPrice}
<span className="text-th-fgd-3 font-body">
{' '}
2023-08-08 19:53:59 -07:00
{side === 'buy' ? sellBank.name : buyBank.name}
2023-08-01 22:32:20 -07:00
</span>
</p>
</Td>
<Td>
<p className="text-right">{fee.toFixed(2)}%</p>
</Td>
<Td className="flex justify-end">
2023-08-01 22:32:20 -07:00
<IconButton
2023-08-08 20:40:26 -07:00
disabled={
cancelId === data.id.toString() || cancelId === 'all'
}
2023-08-01 22:32:20 -07:00
onClick={() => handleCancel(data.id)}
size="small"
>
2023-08-08 20:40:26 -07:00
{cancelId === data.id.toString() || cancelId === 'all' ? (
<Loading />
) : (
<TrashIcon className="h-4 w-4" />
)}
</IconButton>
</Td>
</TrBody>
)
})}
</tbody>
</Table>
) : mangoAccountAddress || connected ? (
<div className="flex flex-col items-center p-8">
<NoSymbolIcon className="mb-2 h-6 w-6 text-th-fgd-4" />
<p>{t('trade:no-orders')}</p>
</div>
) : (
<div className="p-8">
<ConnectEmptyState text={t('connect-orders')} />
</div>
)
}
export default SwapOrders