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

255 lines
8.1 KiB
TypeScript
Raw Normal View History

2022-09-13 23:24:26 -07:00
import { Serum3Side } from '@blockworks-foundation/mango-v4'
2022-09-26 04:33:07 -07:00
import Button, { IconButton } from '@components/shared/Button'
2022-09-13 23:24:26 -07:00
import SideBadge from '@components/shared/SideBadge'
2022-09-14 23:06:23 -07:00
import TabButtons from '@components/shared/TabButtons'
2022-09-26 04:33:07 -07:00
import Tooltip from '@components/shared/Tooltip'
import {
LinkIcon,
QuestionMarkCircleIcon,
TrashIcon,
} from '@heroicons/react/20/solid'
2022-09-13 23:24:26 -07:00
import { Order } from '@project-serum/serum/lib/market'
2022-09-19 04:32:59 -07: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'
import { useTranslation } from 'next-i18next'
import Image from 'next/image'
import { useCallback, useMemo, useState } from 'react'
2022-09-13 23:24:26 -07:00
import { notify } from 'utils/notifications'
2022-09-26 04:33:07 -07:00
import { formatDecimal, formatFixedDecimals } from 'utils/numbers'
2022-09-13 23:24:26 -07:00
2022-09-19 21:39:17 -07:00
const TABS = ['Balances', 'Orders']
2022-09-13 23:24:26 -07:00
const BalanceAndOpenOrders = () => {
2022-09-14 07:52:18 -07:00
const [selectedTab, setSelectedTab] = useState('Balances')
2022-09-13 23:24:26 -07:00
return (
2022-09-17 06:03:46 -07:00
<div className="hide-scroll h-full overflow-y-scroll">
<div className="sticky top-0 z-10">
<TabButtons
activeValue={selectedTab}
onChange={(tab: string) => setSelectedTab(tab)}
values={TABS}
showBorders
/>
</div>
2022-09-13 23:24:26 -07:00
{selectedTab === 'Balances' ? <Balances /> : null}
2022-09-19 21:39:17 -07:00
{selectedTab === 'Orders' ? <OpenOrders /> : null}
2022-09-13 23:24:26 -07:00
</div>
)
}
const Balances = () => {
const { t } = useTranslation('common')
const mangoAccount = mangoStore((s) => s.mangoAccount.current)
const spotBalances = mangoStore((s) => s.mangoAccount.spotBalances)
2022-09-13 23:24:26 -07:00
const group = mangoStore((s) => s.group)
const jupiterTokens = mangoStore((s) => s.jupiterTokens)
const banks = useMemo(() => {
if (group) {
const rawBanks = Array.from(group?.banksMapByName, ([key, value]) => ({
key,
value,
}))
const sortedBanks = mangoAccount
? rawBanks.sort(
(a, b) =>
Math.abs(
mangoAccount?.getTokenBalanceUi(b.value[0]) *
b.value[0].uiPrice!
) -
Math.abs(
mangoAccount?.getTokenBalanceUi(a.value[0]) *
a.value[0].uiPrice!
)
)
: rawBanks
return mangoAccount
? sortedBanks.filter(
(b) => mangoAccount?.getTokenBalanceUi(b.value[0]) !== 0
)
: sortedBanks
}
return []
}, [group, mangoAccount])
return (
<table className="min-w-full">
<thead>
<tr>
<th className="bg-th-bkg-1 text-left">{t('token')}</th>
<th className="bg-th-bkg-1 text-right">{t('balance')}</th>
2022-09-26 04:33:07 -07:00
<th className="bg-th-bkg-1 text-right">{t('in-orders')}</th>
<th className="bg-th-bkg-1 text-right">{t('unsettled')}</th>
2022-09-13 23:24:26 -07:00
</tr>
</thead>
<tbody>
{banks.map(({ key, value }) => {
const bank = value[0]
let logoURI
if (jupiterTokens.length) {
logoURI = jupiterTokens.find(
(t) => t.address === bank.mint.toString()
)!.logoURI
}
return (
<tr key={key} className="text-sm">
<td>
<div className="flex items-center">
<div className="mr-2.5 flex flex-shrink-0 items-center">
{logoURI ? (
<Image alt="" width="20" height="20" src={logoURI} />
) : (
<QuestionMarkCircleIcon className="h-7 w-7 text-th-fgd-3" />
)}
</div>
<span>{bank.name}</span>
</div>
</td>
2022-09-14 07:52:18 -07:00
<td className="pt-4 text-right font-mono">
<div>
2022-09-13 23:24:26 -07:00
{mangoAccount
? formatDecimal(
mangoAccount.getTokenBalanceUi(bank),
bank.mintDecimals
)
: 0}
2022-09-14 07:52:18 -07:00
</div>
2022-09-13 23:24:26 -07:00
</td>
<td className="text-right font-mono">
{spotBalances[bank.mint.toString()]?.inOrders || 0.0}
</td>
<td className="text-right font-mono">
{spotBalances[bank.mint.toString()]?.unsettled || 0.0}
</td>
2022-09-13 23:24:26 -07:00
</tr>
)
})}
</tbody>
</table>
)
}
const OpenOrders = () => {
const { t } = useTranslation('common')
2022-09-19 04:32:59 -07:00
const { connected } = useWallet()
2022-09-13 23:24:26 -07:00
const openOrders = mangoStore((s) => s.mangoAccount.openOrders)
const handleCancelOrder = useCallback(
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
try {
const tx = await client.serum3CancelOrder(
group,
mangoAccount,
selectedMarket!.serumMarketExternal,
o.side === 'buy' ? Serum3Side.bid : Serum3Side.ask,
o.orderId
)
actions.fetchSerumOpenOrders()
2022-09-13 23:24:26 -07:00
notify({
type: 'success',
title: 'Transaction successful',
txid: tx,
})
} catch (e: any) {
console.error('Error canceling', e)
notify({
title: t('order-error'),
description: e.message,
txid: e.txid,
type: 'error',
})
}
},
[t]
)
2022-09-19 04:32:59 -07:00
return connected ? (
Object.values(openOrders).flat().length ? (
2022-09-19 04:32:59 -07:00
<table>
<thead>
2022-09-26 04:33:07 -07:00
<tr>
<th className="text-left">{t('token')}</th>
<th className="text-right">{t('side')}</th>
<th className="text-right">{t('size')}</th>
<th className="text-right">{t('price')}</th>
<th className="text-right">{t('value')}</th>
2022-09-19 04:32:59 -07:00
<th className="text-right"></th>
</tr>
</thead>
<tbody>
{Object.entries(openOrders)
.map(([marketPk, orders]) => {
return orders.map((o) => {
const group = mangoStore.getState().group
2022-09-26 04:33:07 -07:00
const market = group?.getSerum3MarketByPk(
new PublicKey(marketPk)
)
return (
<tr key={`${o.side}${o.size}${o.price}`} className="my-1 p-2">
2022-09-26 04:33:07 -07:00
<td>{market?.name}</td>
<td className="text-right">
<SideBadge side={o.side} />
</td>
<td className="text-right">{o.size}</td>
<td className="text-right">
2022-09-26 04:33:07 -07:00
<span>
{o.price}{' '}
<span className="text-th-fgd-4">
{market
? group?.getFirstBankByTokenIndex(
market.quoteTokenIndex
).name
: ''}
</span>
</span>
</td>
<td className="text-right">
{formatFixedDecimals(o.size * o.price, true)}
</td>
<td>
<div className="flex justify-end">
<Tooltip content={t('cancel')}>
<IconButton
onClick={() => handleCancelOrder(o)}
size="small"
>
<TrashIcon className="h-4 w-4" />
</IconButton>
</Tooltip>
</div>
</td>
</tr>
)
})
})
.flat()}
2022-09-19 04:32:59 -07:00
</tbody>
</table>
) : (
<div className="flex flex-col items-center p-8">
<p>No open orders...</p>
</div>
)
) : (
<div className="flex flex-col items-center p-8">
<LinkIcon className="mb-2 h-6 w-6 text-th-fgd-4" />
<p>Connect to view your open orders</p>
</div>
2022-09-13 23:24:26 -07:00
)
}
export default BalanceAndOpenOrders