From 605f712d448d446311aaf7fbafb603b0a8cb20a2 Mon Sep 17 00:00:00 2001 From: saml33 Date: Thu, 17 Nov 2022 16:17:18 +1100 Subject: [PATCH] populate trade form from positions table --- components/trade/PerpPositions.tsx | 43 ++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/components/trade/PerpPositions.tsx b/components/trade/PerpPositions.tsx index f4f6e026..4d05435f 100644 --- a/components/trade/PerpPositions.tsx +++ b/components/trade/PerpPositions.tsx @@ -1,6 +1,10 @@ +import { PerpMarket } from '@blockworks-foundation/mango-v4' +import { LinkButton } from '@components/shared/Button' import SideBadge from '@components/shared/SideBadge' import mangoStore from '@store/mangoStore' +import Decimal from 'decimal.js' import { useTranslation } from 'next-i18next' +import { calculateMarketPrice } from 'utils/tradeForm' import MarketLogos from './MarketLogos' import PerpSideBadge from './PerpSideBadge' @@ -8,6 +12,27 @@ const PerpPositions = () => { const { t } = useTranslation(['common', 'trade']) const group = mangoStore((s) => s.group) const perpPositions = mangoStore((s) => s.mangoAccount.perpPositions) + const selectedMarket = mangoStore((s) => s.selectedMarket.current) + + const handlePositionClick = (positionSize: number) => { + const tradeForm = mangoStore.getState().tradeForm + const set = mangoStore.getState().set + + let price = new Decimal(tradeForm.price).toNumber() + if (tradeForm.tradeType === 'Market') { + const orderbook = mangoStore.getState().selectedMarket.orderbook + const side = tradeForm.side === 'buy' ? 'sell' : 'buy' + price = calculateMarketPrice(orderbook, positionSize, side) + } + + if (tradeForm.side === 'buy') { + set((s) => { + s.tradeForm.side = 'sell' + s.tradeForm.baseSize = positionSize.toFixed() + s.tradeForm.quoteSize = (positionSize / price).toFixed() + }) + } + } if (!group) return null @@ -28,6 +53,10 @@ const PerpPositions = () => { position.marketIndex ) const basePosition = position.getBasePositionUi(market) + const isSelectedMarket = + selectedMarket instanceof PerpMarket && + selectedMarket.perpMarketIndex === position.marketIndex + return ( @@ -40,10 +69,20 @@ const PerpPositions = () => { -
{basePosition}
+

+ {isSelectedMarket ? ( + handlePositionClick(basePosition)} + > + {basePosition} + + ) : ( + basePosition + )} +

-
+
${Math.abs(basePosition * market._uiPrice).toFixed(2)}