Skip to content

Commit

Permalink
Nick/fix resolved pnl (#133)
Browse files Browse the repository at this point in the history
* try to fix resolved market pnl

* fix

* fix
  • Loading branch information
lowkeynicc authored Nov 6, 2024
1 parent a064652 commit e375150
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions common-ts/src/common-ui-utils/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import {
BN,
BigNum,
DriftClient,
MarketStatus,
ONE,
PRICE_PRECISION,
PRICE_PRECISION_EXP,
PerpMarketConfig,
PerpPosition,
Expand All @@ -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,
Expand Down Expand Up @@ -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;

Expand All @@ -69,7 +75,7 @@ const getOpenPositionData = (
true
)[0];

const estExitPrice = user.getPositionEstimatedExitPriceAndPnl(
let estExitPrice = user.getPositionEstimatedExitPriceAndPnl(
perpPositionWithRemainderBaseAdded,
perpPositionWithRemainderBaseAdded.baseAssetAmount
)[0];
Expand All @@ -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(),
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e375150

Please sign in to comment.