Skip to content

Commit 94d763d

Browse files
lowkeyniccharsh4786
authored andcommitted
add includeOpenOrders option for perp position health (drift-labs#1966)
1 parent 6e9df3b commit 94d763d

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

sdk/src/user.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3590,11 +3590,13 @@ export class User {
35903590
perpPosition,
35913591
oraclePriceData,
35923592
quoteOraclePriceData,
3593+
includeOpenOrders = true,
35933594
}: {
35943595
marginCategory: MarginCategory;
35953596
perpPosition: PerpPosition;
35963597
oraclePriceData?: OraclePriceData;
35973598
quoteOraclePriceData?: OraclePriceData;
3599+
includeOpenOrders?: boolean;
35983600
}): HealthComponent {
35993601
const perpMarket = this.driftClient.getPerpMarketAccount(
36003602
perpPosition.marketIndex
@@ -3603,14 +3605,25 @@ export class User {
36033605
oraclePriceData ||
36043606
this.driftClient.getOracleDataForPerpMarket(perpMarket.marketIndex);
36053607
const oraclePrice = _oraclePriceData.price;
3606-
const {
3607-
worstCaseBaseAssetAmount: worstCaseBaseAmount,
3608-
worstCaseLiabilityValue,
3609-
} = calculateWorstCasePerpLiabilityValue(
3610-
perpPosition,
3611-
perpMarket,
3612-
oraclePrice
3613-
);
3608+
3609+
let worstCaseBaseAmount;
3610+
let worstCaseLiabilityValue;
3611+
if (includeOpenOrders) {
3612+
const worstCaseIncludeOrders = calculateWorstCasePerpLiabilityValue(
3613+
perpPosition,
3614+
perpMarket,
3615+
oraclePrice
3616+
);
3617+
worstCaseBaseAmount = worstCaseIncludeOrders.worstCaseBaseAssetAmount;
3618+
worstCaseLiabilityValue = worstCaseIncludeOrders.worstCaseLiabilityValue;
3619+
} else {
3620+
worstCaseBaseAmount = perpPosition.baseAssetAmount;
3621+
worstCaseLiabilityValue = calculatePerpLiabilityValue(
3622+
perpPosition.baseAssetAmount,
3623+
oraclePrice,
3624+
isVariant(perpMarket.contractType, 'prediction')
3625+
);
3626+
}
36143627

36153628
const userCustomMargin = Math.max(
36163629
perpPosition.maxMarginRatio,

0 commit comments

Comments
 (0)