diff --git a/common-ts/src/common-ui-utils/user.ts b/common-ts/src/common-ui-utils/user.ts index fc57f1cf..85edbc8e 100644 --- a/common-ts/src/common-ui-utils/user.ts +++ b/common-ts/src/common-ui-utils/user.ts @@ -3,6 +3,9 @@ import { BN, BigNum, DriftClient, + MarketStatus, + ONE, + PRICE_PRECISION, PRICE_PRECISION_EXP, PerpMarketConfig, PerpPosition, @@ -19,8 +22,9 @@ import { calculatePositionPNL, isOracleValid, } from '@drift-labs/sdk'; -import { OpenPosition } from 'src/types'; +import { OpenPosition, UIMarket } from '../types'; import { TRADING_COMMON_UTILS } from './trading'; +import { ENUM_UTILS } from '@drift/common'; const getOpenPositionData = ( driftClient: DriftClient, @@ -50,8 +54,10 @@ const getOpenPositionData = ( position.marketIndex ); + let oraclePrice = oraclePriceData.price; + // mark price fetched with a callback so we don't need extra dlob server calls. fallback to oracle - const markPrice = markPriceCallback + let markPrice = markPriceCallback ? markPriceCallback(position.marketIndex) ?? oraclePriceData.price : oraclePriceData.price; @@ -69,7 +75,7 @@ const getOpenPositionData = ( true )[0]; - const estExitPrice = user.getPositionEstimatedExitPriceAndPnl( + let estExitPrice = user.getPositionEstimatedExitPriceAndPnl( perpPositionWithRemainderBaseAdded, perpPositionWithRemainderBaseAdded.baseAssetAmount )[0]; @@ -81,6 +87,26 @@ const getOpenPositionData = ( const isShort = perpPositionWithRemainderBaseAdded.baseAssetAmount.isNeg(); + if (UIMarket.checkIsPredictionMarket(perpMarketConfig)) { + const isResolved = + ENUM_UTILS.match(perpMarket?.status, MarketStatus.SETTLEMENT) || + ENUM_UTILS.match(perpMarket?.status, MarketStatus.DELISTED); + + if (isResolved) { + const resolvedToNo = perpMarket.expiryPrice.lte( + ZERO.add(perpMarket.amm.orderTickSize) + ); + + const price = resolvedToNo + ? ZERO.mul(PRICE_PRECISION) + : ONE.mul(PRICE_PRECISION); + + estExitPrice = price; + markPrice = price; + oraclePrice = price; + } + } + const pnlVsMark = TRADING_COMMON_UTILS.calculatePotentialProfit({ currentPositionSize: BigNum.from( perpPositionWithRemainderBaseAdded.baseAssetAmount.abs(), @@ -118,7 +144,7 @@ const getOpenPositionData = ( perpPositionWithRemainderBaseAdded.baseAssetAmount.abs(), BASE_PRECISION_EXP ), - exitPrice: BigNum.from(oraclePriceData.price, PRICE_PRECISION_EXP), + exitPrice: BigNum.from(oraclePrice, PRICE_PRECISION_EXP), slippageTolerance: 0, takerFeeBps: 0, }).estimatedProfit.shiftTo(QUOTE_PRECISION_EXP).val;