new layout and add liq price

This commit is contained in:
saml33 2023-06-27 12:43:15 +10:00
parent dff8132bb2
commit b76df3c87c
9 changed files with 370 additions and 142 deletions

View File

@ -2,18 +2,20 @@ import Decimal from 'decimal.js'
import { formatCurrencyValue, formatNumericValue } from 'utils/numbers'
const FormatNumericValue = ({
classNames,
value,
decimals,
isUsd,
roundUp,
}: {
classNames?: string
value: Decimal | number | string
decimals?: number
isUsd?: boolean
roundUp?: boolean
}) => {
return (
<span>
<span className={classNames}>
{isUsd
? formatCurrencyValue(value, decimals)
: formatNumericValue(value, decimals, roundUp)}

View File

@ -5,7 +5,12 @@ import Button, { IconButton, LinkButton } from '@components/shared/Button'
import ConnectEmptyState from '@components/shared/ConnectEmptyState'
import FormatNumericValue from '@components/shared/FormatNumericValue'
import { Table, Td, Th, TrBody, TrHead } from '@components/shared/TableElements'
import { NoSymbolIcon } from '@heroicons/react/20/solid'
import {
ArrowTrendingDownIcon,
ArrowTrendingUpIcon,
ChevronDownIcon,
NoSymbolIcon,
} from '@heroicons/react/20/solid'
import { useWallet } from '@solana/wallet-adapter-react'
import mangoStore from '@store/mangoStore'
import useMangoAccount from 'hooks/useMangoAccount'
@ -14,21 +19,20 @@ import useSelectedMarket from 'hooks/useSelectedMarket'
import useUnownedAccount from 'hooks/useUnownedAccount'
import { useViewport } from 'hooks/useViewport'
import { useTranslation } from 'next-i18next'
import Link from 'next/link'
import { useRouter } from 'next/router'
import { useCallback, useState } from 'react'
import {
floorToDecimal,
formatCurrencyValue,
formatNumericValue,
getDecimalCount,
} from 'utils/numbers'
import { breakpoints } from 'utils/theme'
import { calculateLimitPriceForMarketOrder } from 'utils/tradeForm'
import MarketCloseModal from './MarketCloseModal'
import MarketLogos from './MarketLogos'
import PerpSideBadge from './PerpSideBadge'
import TableMarketName from './TableMarketName'
import Tooltip from '@components/shared/Tooltip'
import { Disclosure, Transition } from '@headlessui/react'
const PerpPositions = () => {
const { t } = useTranslation(['common', 'trade'])
@ -44,11 +48,10 @@ const PerpPositions = () => {
const perpPositions = mangoStore((s) => s.mangoAccount.perpPositions)
const { selectedMarket } = useSelectedMarket()
const { connected } = useWallet()
const { mangoAccountAddress } = useMangoAccount()
const { mangoAccount, mangoAccountAddress } = useMangoAccount()
const { isUnownedAccount } = useUnownedAccount()
const { width } = useViewport()
const showTableView = width ? width > breakpoints.md : false
const { asPath } = useRouter()
const handlePositionClick = (positionSize: number, market: PerpMarket) => {
const tradeForm = mangoStore.getState().tradeForm
@ -92,7 +95,7 @@ const PerpPositions = () => {
setShowShareModal(true)
}
if (!group) return null
if (!group || !mangoAccount) return null
const openPerpPositions = Object.values(perpPositions)
.filter((p) => p.basePositionLots.toNumber())
@ -116,11 +119,9 @@ const PerpPositions = () => {
<thead>
<TrHead>
<Th className="text-left">{t('market')}</Th>
<Th className="text-right">{t('trade:side')}</Th>
<Th className="text-right">{t('trade:size')}</Th>
<Th className="text-right">{t('trade:notional')}</Th>
<Th className="text-right">{t('trade:entry-price')}</Th>
<Th className="text-right">{t('trade:oracle-price')}</Th>
<Th className="text-right">{t('trade:est-liq-price')}</Th>
<Th className="text-right">{`${t('trade:unsettled')} ${t(
'pnl'
)}`}</Th>
@ -144,11 +145,17 @@ const PerpPositions = () => {
if (!basePosition) return null
const isLong = basePosition > 0
const unsettledPnl = position.getUnsettledPnlUi(market)
const totalPnl =
position.cumulativePnlOverPositionLifetimeUi(market)
const unrealizedPnl = position.getUnRealizedPnlUi(market)
const realizedPnl = position.getRealizedPnlUi()
const roe = unrealizedPnl / basePosition
const estLiqPrice = position.getLiquidationPriceUi(
group,
mangoAccount
)
return (
<TrBody
@ -156,14 +163,14 @@ const PerpPositions = () => {
className="my-1 p-2"
>
<Td>
<TableMarketName market={market} />
</Td>
<Td className="text-right">
<PerpSideBadge basePosition={basePosition} />
<TableMarketName
market={market}
side={isLong ? 'buy' : 'sell'}
/>
</Td>
<Td className="text-right font-mono">
<p className="flex justify-end">
{isSelectedMarket ? (
{isSelectedMarket ? (
<div className="flex flex-col items-end space-y-0.5">
<LinkButton
className="font-normal underline underline-offset-2 md:underline-offset-4 md:hover:no-underline"
onClick={() =>
@ -177,35 +184,55 @@ const PerpPositions = () => {
)}
/>
</LinkButton>
) : (
<FormatNumericValue
classNames="text-xs text-th-fgd-3"
value={
Math.abs(floorBasePosition) * market._uiPrice
}
isUsd
/>
</div>
) : (
<div className="flex flex-col items-end space-y-0.5">
<FormatNumericValue
value={Math.abs(basePosition)}
decimals={getDecimalCount(market.minOrderSize)}
/>
)}
</p>
<FormatNumericValue
classNames="text-xs text-th-fgd-3"
value={
Math.abs(floorBasePosition) * market._uiPrice
}
isUsd
/>
</div>
)}
</Td>
<Td className="font-mono">
<div className="flex flex-col items-end space-y-0.5">
<FormatNumericValue
value={position.getAverageEntryPriceUi(market)}
decimals={getDecimalCount(market.tickSize)}
isUsd
/>
<FormatNumericValue
classNames="text-xs text-th-fgd-3"
value={market.uiPrice}
decimals={getDecimalCount(market.tickSize)}
isUsd
/>
</div>
</Td>
<Td className="text-right font-mono">
<FormatNumericValue
value={
Math.abs(floorBasePosition) * market._uiPrice
}
isUsd
/>
</Td>
<Td className="text-right font-mono">
<FormatNumericValue
value={position.getAverageEntryPriceUi(market)}
decimals={getDecimalCount(market.tickSize)}
isUsd
/>
</Td>
<Td className="text-right font-mono">
<FormatNumericValue
value={market.uiPrice}
decimals={getDecimalCount(market.tickSize)}
isUsd
/>
{estLiqPrice ? (
<FormatNumericValue
value={estLiqPrice}
decimals={getDecimalCount(market.tickSize)}
isUsd
/>
) : (
''
)}
</Td>
<Td className={`text-right font-mono`}>
<FormatNumericValue
@ -221,12 +248,13 @@ const PerpPositions = () => {
unrealizedPnl={unrealizedPnl}
realizedPnl={realizedPnl}
totalPnl={totalPnl}
roe={roe}
/>
}
delay={100}
>
<span
className={`tooltip-underline ${
className={`tooltip-underline mb-1 ${
unrealizedPnl > 0
? 'text-th-up'
: 'text-th-down'
@ -278,8 +306,8 @@ const PerpPositions = () => {
) : null}
</>
) : (
<>
{openPerpPositions.map((position) => {
<div className="border-b border-th-bkg-3">
{openPerpPositions.map((position, i) => {
const market = group.getPerpMarketByMarketIndex(
position.marketIndex
)
@ -293,109 +321,250 @@ const PerpPositions = () => {
selectedMarket.perpMarketIndex === position.marketIndex
if (!basePosition) return null
const side =
basePosition > 0 ? 'buy' : basePosition < 0 ? 'sell' : ''
const totalPnl =
position.cumulativePnlOverPositionLifetimeUi(market)
const unrealizedPnl = position.getUnRealizedPnlUi(market)
const realizedPnl = position.getRealizedPnlUi()
const roe = unrealizedPnl / basePosition
const estLiqPrice = position.getLiquidationPriceUi(
group,
mangoAccount
)
const unsettledPnl = position.getUnsettledPnlUi(market)
return (
<div
className="flex items-center justify-between border-b border-th-bkg-3 p-4"
key={`${position.marketIndex}`}
>
<div className="flex items-start">
<div className="mt-0.5">
<MarketLogos market={market} size="large" />
</div>
<div>
<div className="mb-1 flex space-x-1 leading-none text-th-fgd-2">
{selectedMarket?.name === market.name ? (
<span className="whitespace-nowrap">
{market.name}
</span>
) : (
<Link href={`/trade?name=${market.name}`}>
<div className="flex items-center underline underline-offset-2 md:hover:text-th-fgd-3 md:hover:no-underline">
<span className="whitespace-nowrap">
{market.name}
</span>
</div>
</Link>
)}
<PerpSideBadge basePosition={basePosition} />
</div>
<div className="flex items-center space-x-1 leading-none">
<p className="flex text-th-fgd-4">
<span className="font-mono text-th-fgd-3">
{isSelectedMarket && asPath === '/trade' ? (
<LinkButton
className="font-normal underline underline-offset-2 md:underline-offset-4 md:hover:no-underline"
onClick={() =>
handlePositionClick(floorBasePosition, market)
}
>
<FormatNumericValue
value={Math.abs(basePosition)}
decimals={getDecimalCount(
market.minOrderSize
)}
/>
</LinkButton>
) : (
<Disclosure key={position.marketIndex}>
{({ open }) => (
<>
<Disclosure.Button
className={`flex w-full items-center justify-between border-t border-th-bkg-3 p-4 text-left focus:outline-none ${
i === 0 ? 'border-t-0' : ''
}`}
>
<div className="flex w-full flex-col sm:flex-row sm:items-center sm:justify-between">
<div
className={`flex items-center ${
side === 'buy'
? 'text-th-up'
: side === 'sell'
? 'text-th-down'
: 'text-th-fgd-2'
}`}
>
<MarketLogos market={market} size="large" />
<span className="mr-1 whitespace-nowrap">
{market.name}
</span>
{side === 'buy' ? (
<ArrowTrendingUpIcon className="h-5 w-5" />
) : side === 'sell' ? (
<ArrowTrendingDownIcon className="h-5 w-5" />
) : null}
</div>
<div className="flex items-center space-x-2">
<span className="font-mono">
<FormatNumericValue
value={Math.abs(basePosition)}
decimals={getDecimalCount(market.minOrderSize)}
/>{' '}
<span className="font-body text-th-fgd-3">
{market.name.split('-')[0]}
</span>
</span>
<span className="text-th-fgd-4">|</span>
<span
className={`font-mono ${
unrealizedPnl > 0
? 'text-th-up'
: 'text-th-down'
}`}
>
<FormatNumericValue
value={unrealizedPnl}
isUsd
decimals={2}
/>
)}
</span>
<span className="mx-1">from</span>
<span className="font-mono text-th-fgd-3">
<FormatNumericValue
value={position.getAverageEntryPriceUi(market)}
decimals={getDecimalCount(market.tickSize)}
isUsd
/>
</span>
</p>
</div>
</div>
</div>
<div className="flex items-center space-x-4">
<div
className={`text-right font-mono leading-none ${
unrealizedPnl > 0 ? 'text-th-up' : 'text-th-down'
}`}
>
<Tooltip
content={
<PnlTooltipContent
unrealizedPnl={unrealizedPnl}
realizedPnl={realizedPnl}
totalPnl={totalPnl}
/>
}
delay={100}
</span>
</div>
</div>
<ChevronDownIcon
className={`${
open ? 'rotate-180' : 'rotate-360'
} ml-3 h-6 w-6 flex-shrink-0 text-th-fgd-3`}
/>
</Disclosure.Button>
<Transition
enter="transition ease-in duration-200"
enterFrom="opacity-0"
enterTo="opacity-100"
>
<p className="tooltip-underline mb-1 font-body text-th-fgd-4">
{t('trade:unrealized-pnl')}
</p>
</Tooltip>
<FormatNumericValue value={unrealizedPnl} isUsd />
</div>
{!isUnownedAccount ? (
<Button
className="text-xs"
secondary
size="small"
onClick={() => showClosePositionModal(position)}
>
Close
</Button>
) : null}
</div>
</div>
<Disclosure.Panel>
<div className="mx-4 grid grid-cols-2 gap-4 border-t border-th-bkg-3 pt-4 pb-4">
<div className="col-span-1">
<p className="text-xs text-th-fgd-3">
{t('trade:size')}
</p>
<p>
{isSelectedMarket ? (
<div className=" space-y-0.5">
<LinkButton
className="font-normal underline underline-offset-2 md:underline-offset-4 md:hover:no-underline"
onClick={() =>
handlePositionClick(
floorBasePosition,
market
)
}
>
<FormatNumericValue
value={Math.abs(basePosition)}
decimals={getDecimalCount(
market.minOrderSize
)}
/>
</LinkButton>
<FormatNumericValue
classNames="text-xs text-th-fgd-3"
value={
Math.abs(floorBasePosition) *
market._uiPrice
}
isUsd
/>
</div>
) : (
<div className="flex flex-col font-mono text-th-fgd-2">
<FormatNumericValue
value={Math.abs(basePosition)}
decimals={getDecimalCount(
market.minOrderSize
)}
/>
<FormatNumericValue
classNames="text-xs text-th-fgd-3"
value={
Math.abs(floorBasePosition) *
market._uiPrice
}
isUsd
/>
</div>
)}
</p>
</div>
<div className="col-span-1">
<p className="text-xs text-th-fgd-3">
{t('trade:entry-price')}
</p>
<div className="flex flex-col font-mono">
<FormatNumericValue
classNames="text-th-fgd-2"
value={position.getAverageEntryPriceUi(
market
)}
decimals={getDecimalCount(market.tickSize)}
isUsd
/>
<FormatNumericValue
classNames="text-xs text-th-fgd-3"
value={market.uiPrice}
decimals={getDecimalCount(market.tickSize)}
isUsd
/>
</div>
</div>
<div className="col-span-1">
<p className="text-xs text-th-fgd-3">
{t('trade:est-liq-price')}
</p>
<p className="font-mono text-th-fgd-2">
{estLiqPrice ? (
<FormatNumericValue
value={estLiqPrice}
decimals={getDecimalCount(market.tickSize)}
isUsd
/>
) : (
''
)}
</p>
</div>
<div className="col-span-1">
<p className="text-xs text-th-fgd-3">
{t('trade:unsettled')}
</p>
<p className="font-mono text-th-fgd-2">
<FormatNumericValue
value={unsettledPnl}
isUsd
decimals={2}
/>
</p>
</div>
<div className="col-span-1">
<p className="text-xs text-th-fgd-3">
{t('trade:unrealized-pnl')}
</p>
<p className="font-mono text-th-fgd-2">
<Tooltip
content={
<PnlTooltipContent
unrealizedPnl={unrealizedPnl}
realizedPnl={realizedPnl}
totalPnl={totalPnl}
roe={roe}
/>
}
delay={100}
>
<span
className={`tooltip-underline mb-1 ${
unrealizedPnl > 0
? 'text-th-up'
: 'text-th-down'
}`}
>
<FormatNumericValue
value={unrealizedPnl}
isUsd
decimals={2}
/>
</span>
</Tooltip>
</p>
</div>
<div className="col-span-2 mt-3 flex space-x-3">
<Button
className="w-1/2"
secondary
onClick={() => showClosePositionModal(position)}
>
{t('trade:close-position')}
</Button>
<Button
className="w-1/2"
secondary
onClick={() =>
handleShowShare(openPerpPositions[i])
}
disabled={!group || !basePosition}
>
<div className="flex items-center justify-center">
<TwitterIcon className="mr-2 h-4 w-4" />
{t('trade:tweet-position')}
</div>
</Button>
</div>
</div>
</Disclosure.Panel>
</Transition>
</>
)}
</Disclosure>
)
})}
</>
</div>
)
) : mangoAccountAddress || connected ? (
<div className="flex flex-col items-center p-8">
@ -425,10 +594,12 @@ const PnlTooltipContent = ({
unrealizedPnl,
realizedPnl,
totalPnl,
roe,
}: {
unrealizedPnl: number
realizedPnl: number
totalPnl: number
roe: number
}) => {
const { t } = useTranslation(['common', 'trade'])
return (
@ -452,6 +623,12 @@ const PnlTooltipContent = ({
{formatCurrencyValue(totalPnl, 2)}
</span>
</div>
<div className="flex justify-between">
<p className="mr-3">ROE</p>
<span className="font-mono text-th-fgd-2">
{formatNumericValue(roe, 2)}%
</span>
</div>
</div>
<a
href="https://docs.mango.markets/mango-markets/settle-pnl"

View File

@ -3,24 +3,68 @@ import useSelectedMarket from 'hooks/useSelectedMarket'
import Link from 'next/link'
import { useRouter } from 'next/router'
import MarketLogos from './MarketLogos'
import {
ArrowTrendingDownIcon,
ArrowTrendingUpIcon,
} from '@heroicons/react/20/solid'
const TableMarketName = ({ market }: { market: PerpMarket | Serum3Market }) => {
const TableMarketName = ({
market,
side,
}: {
market: PerpMarket | Serum3Market
side?: 'buy' | 'sell'
}) => {
const { selectedMarket } = useSelectedMarket()
const { asPath } = useRouter()
return selectedMarket?.name === market.name && asPath.includes('/trade') ? (
<div className="flex items-center">
<MarketLogos market={market} size="large" />
<span className="whitespace-nowrap">{market.name}</span>
<div
className={`flex items-center ${
side === 'buy'
? 'text-th-up'
: side === 'sell'
? 'text-th-down'
: 'text-th-fgd-2'
}`}
>
<NameAndSide market={market} side={side} />
</div>
) : (
<Link href={`/trade?name=${market.name}`}>
<div className="flex items-center underline underline-offset-2 md:underline-offset-4 md:hover:text-th-fgd-3 md:hover:no-underline">
<MarketLogos market={market} size="large" />
<span className="whitespace-nowrap">{market.name}</span>
<div
className={`flex items-center underline underline-offset-2 md:underline-offset-4 md:hover:text-th-fgd-3 md:hover:no-underline ${
side === 'buy'
? 'text-th-up'
: side === 'sell'
? 'text-th-down'
: 'text-th-fgd-2'
}`}
>
<NameAndSide market={market} side={side} />
</div>
</Link>
)
}
export default TableMarketName
const NameAndSide = ({
market,
side,
}: {
market: PerpMarket | Serum3Market
side?: 'buy' | 'sell'
}) => {
return (
<>
<MarketLogos market={market} size="large" />
<span className="mr-1 whitespace-nowrap">{market.name}</span>
{side === 'buy' ? (
<ArrowTrendingUpIcon className="h-5 w-5" />
) : side === 'sell' ? (
<ArrowTrendingDownIcon className="h-5 w-5" />
) : null}
</>
)
}

View File

@ -17,6 +17,7 @@
"current-price": "Current Price",
"edit-order": "Edit Order",
"entry-price": "Entry Price",
"est-liq-price": "Est. Liq. Price",
"est-slippage": "Est. Slippage",
"funding-limits": "Funding Limits",
"funding-rate": "1h Avg Funding Rate",

View File

@ -17,6 +17,7 @@
"current-price": "Current Price",
"edit-order": "Edit Order",
"entry-price": "Entry Price",
"est-liq-price": "Est. Liq. Price",
"est-slippage": "Est. Slippage",
"funding-limits": "Funding Limits",
"funding-rate": "1h Avg Funding Rate",

View File

@ -17,6 +17,7 @@
"current-price": "Current Price",
"edit-order": "Edit Order",
"entry-price": "Entry Price",
"est-liq-price": "Est. Liq. Price",
"est-slippage": "Est. Slippage",
"funding-limits": "Funding Limits",
"funding-rate": "1h Avg Funding Rate",

View File

@ -17,6 +17,7 @@
"current-price": "Current Price",
"edit-order": "Edit Order",
"entry-price": "Entry Price",
"est-liq-price": "Est. Liq. Price",
"est-slippage": "Est. Slippage",
"funding-limits": "Funding Limits",
"funding-rate": "1h Avg Funding Rate",

View File

@ -17,6 +17,7 @@
"current-price": "Current Price",
"edit-order": "Edit Order",
"entry-price": "Entry Price",
"est-liq-price": "Est. Liq. Price",
"est-slippage": "Est. Slippage",
"funding-limits": "Funding Limits",
"funding-rate": "1h Avg Funding Rate",

View File

@ -610,7 +610,7 @@ input[type='range']::-webkit-slider-runnable-track {
}
.tooltip-underline {
@apply default-transition w-max border-b border-dashed border-current leading-tight hover:cursor-help hover:border-transparent;
@apply default-transition box-border w-max border-b border-dashed border-current leading-tight hover:cursor-help hover:border-transparent;
}
.radial-gradient-bg {