mango-ui-v3/components/PositionsTable.tsx

165 lines
5.8 KiB
TypeScript
Raw Normal View History

2021-06-27 21:44:48 -07:00
import useMangoStore from '../stores/useMangoStore'
import { Table, Thead, Tbody, Tr, Th, Td } from 'react-super-responsive-table'
import {
getMarketByPublicKey,
nativeI80F48ToUi,
nativeToUi,
PerpMarket,
} from '@blockworks-foundation/mango-client'
2021-06-27 21:44:48 -07:00
import { useMemo } from 'react'
const PositionsTable = () => {
2021-07-06 21:34:21 -07:00
const groupConfig = useMangoStore((s) => s.selectedMangoGroup.config)
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache)
const allMarkets = useMangoStore((s) => s.selectedMangoGroup.markets)
2021-07-06 21:34:21 -07:00
const perpMarkets = useMemo(
() =>
mangoGroup
? groupConfig.perpMarkets.map(
(m) => mangoGroup.perpMarkets[m.marketIndex]
)
: [],
[mangoGroup]
)
const perpAccounts = useMemo(
() =>
mangoAccount
? groupConfig.perpMarkets.map(
(m) => mangoAccount.perpAccounts[m.marketIndex]
)
: [],
[mangoAccount]
)
2021-06-27 21:44:48 -07:00
2021-07-06 21:34:21 -07:00
console.log({ perpMarkets, perpAccounts })
2021-06-27 21:44:48 -07:00
return (
<div className={`flex flex-col py-4`}>
<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`}>
{perpAccounts.length ? (
<div
className={`overflow-hidden border-b border-th-bkg-2 sm:rounded-md`}
>
<Table className={`min-w-full divide-y divide-th-bkg-2`}>
<Thead>
<Tr className="text-th-fgd-3">
<Th
scope="col"
className={`px-6 py-2 text-left font-normal`}
>
Market
</Th>
<Th
scope="col"
className={`px-6 py-2 text-left font-normal`}
>
Base Position
</Th>
<Th
scope="col"
className={`px-6 py-2 text-left font-normal`}
>
Quote Position
</Th>
<Th
scope="col"
className={`px-6 py-2 text-left font-normal`}
>
Unrealized PnL
</Th>
<Th
scope="col"
className={`px-6 py-2 text-left font-normal`}
>
Health
</Th>
</Tr>
</Thead>
<Tbody>
{perpAccounts.map((acc, index) => {
2021-07-06 21:34:21 -07:00
const market = perpMarkets[index]
const marketConfig = getMarketByPublicKey(
groupConfig,
market.perpMarket
)
2021-07-06 21:34:21 -07:00
const marketCache =
mangoCache.perpMarketCache[marketConfig.marketIndex]
const price =
mangoCache.priceCache[marketConfig.marketIndex].price
const perpMarket = allMarkets[
marketConfig.publicKey.toString()
] as PerpMarket
2021-07-06 21:34:21 -07:00
2021-06-27 21:44:48 -07:00
return (
2021-07-06 21:34:21 -07:00
<Tr
key={`${index}`}
className={`border-b border-th-bkg-3
2021-06-27 21:44:48 -07:00
${index % 2 === 0 ? `bg-th-bkg-3` : `bg-th-bkg-2`}
`}
>
2021-07-06 21:34:21 -07:00
<Td
2021-07-19 13:24:49 -07:00
className={`px-6 py-2.5 whitespace-nowrap text-sm text-th-fgd-1`}
2021-07-06 21:34:21 -07:00
>
{marketConfig.name}
</Td>
<Td
2021-07-19 13:24:49 -07:00
className={`px-6 py-2.5 whitespace-nowrap text-sm text-th-fgd-1`}
2021-07-06 21:34:21 -07:00
>
{perpMarket.baseLotsToNumber(acc.basePosition)}
2021-07-06 21:34:21 -07:00
</Td>
<Td
2021-07-19 13:24:49 -07:00
className={`px-6 py-2.5 whitespace-nowrap text-sm text-th-fgd-1`}
2021-07-06 21:34:21 -07:00
>
{nativeI80F48ToUi(
acc.quotePosition,
marketConfig.quoteDecimals
).toFixed()}
2021-07-06 21:34:21 -07:00
</Td>
<Td
2021-07-19 13:24:49 -07:00
className={`px-6 py-2.5 whitespace-nowrap text-sm text-th-fgd-1`}
2021-07-06 21:34:21 -07:00
>
$
{nativeI80F48ToUi(
acc.getPnl(market, price),
marketConfig.quoteDecimals
).toFixed()}
2021-07-06 21:34:21 -07:00
</Td>
<Td
2021-07-19 13:24:49 -07:00
className={`px-6 py-2.5 whitespace-nowrap text-sm text-th-fgd-1`}
2021-07-06 21:34:21 -07:00
>
{acc
.getHealth(
market,
price,
market.maintAssetWeight,
market.maintLiabWeight,
marketCache.longFunding,
marketCache.shortFunding
)
.toFixed(3)}
</Td>
</Tr>
2021-06-27 21:44:48 -07:00
)
})}
</Tbody>
</Table>
</div>
) : (
<div
className={`w-full text-center py-6 bg-th-bkg-1 text-th-fgd-3 rounded-md`}
>
No open positions
</div>
)}
</div>
</div>
</div>
)
}
export default PositionsTable