@@ -107,10 +107,7 @@ import { StrictOraclePrice } from './oracles/strictOraclePrice';
107107
108108import { calculateSpotFuelBonus , calculatePerpFuelBonus } from './math/fuel' ;
109109import { grpcUserAccountSubscriber } from './accounts/grpcUserAccountSubscriber' ;
110- import {
111- MarginCalculation ,
112- MarginContext ,
113- } from './marginCalculation' ;
110+ import { MarginCalculation , MarginContext } from './marginCalculation' ;
114111
115112export type MarginType = 'Cross' | 'Isolated' ;
116113
@@ -716,7 +713,7 @@ export class User {
716713 const market = this . driftClient . getPerpMarketAccount (
717714 perpPosition . marketIndex
718715 ) ;
719- if ( ! market ) return unrealizedPnl ;
716+ if ( ! market ) return unrealizedPnl ;
720717 const oraclePriceData = this . getMMOracleDataForPerpMarket (
721718 market . marketIndex
722719 ) ;
@@ -1267,7 +1264,6 @@ export class User {
12671264 return 0 ;
12681265 }
12691266
1270-
12711267 let totalCollateral : BN ;
12721268 let maintenanceMarginReq : BN ;
12731269
@@ -1317,7 +1313,7 @@ export class User {
13171313 perpPosition . marketIndex
13181314 ) ;
13191315
1320- if ( ! market ) return ZERO ;
1316+ if ( ! market ) return ZERO ;
13211317
13221318 let valuationPrice = this . getOracleDataForPerpMarket (
13231319 market . marketIndex
@@ -1960,13 +1956,23 @@ export class User {
19601956 canBeLiquidated : boolean ;
19611957 marginRequirement : BN ;
19621958 totalCollateral : BN ;
1963- liquidationStatuses : Map < 'cross' | number , { canBeLiquidated : boolean ; marginRequirement : BN ; totalCollateral : BN } > ;
1959+ liquidationStatuses : Map <
1960+ 'cross' | number ,
1961+ { canBeLiquidated : boolean ; marginRequirement : BN ; totalCollateral : BN }
1962+ > ;
19641963 } {
19651964 // Deprecated signature retained for backward compatibility in type only
19661965 // but implementation now delegates to the new Map-based API and returns cross margin status.
19671966 const map = this . getLiquidationStatuses ( ) ;
19681967 const cross = map . get ( 'cross' ) ;
1969- return cross ? { ...cross , liquidationStatuses : map } : { canBeLiquidated : false , marginRequirement : ZERO , totalCollateral : ZERO , liquidationStatuses : map } ;
1968+ return cross
1969+ ? { ...cross , liquidationStatuses : map }
1970+ : {
1971+ canBeLiquidated : false ,
1972+ marginRequirement : ZERO ,
1973+ totalCollateral : ZERO ,
1974+ liquidationStatuses : map ,
1975+ } ;
19701976 }
19711977
19721978 /**
@@ -1975,18 +1981,28 @@ export class User {
19751981 * - 'cross' for cross margin
19761982 * - marketIndex (number) for each isolated perp position
19771983 */
1978- public getLiquidationStatuses ( marginCalc ?: MarginCalculation ) : Map < 'cross' | number , { canBeLiquidated : boolean ; marginRequirement : BN ; totalCollateral : BN } > {
1984+ public getLiquidationStatuses (
1985+ marginCalc ?: MarginCalculation
1986+ ) : Map <
1987+ 'cross' | number ,
1988+ { canBeLiquidated : boolean ; marginRequirement : BN ; totalCollateral : BN }
1989+ > {
19791990 // If not provided, use buffer-aware calc for canBeLiquidated checks
19801991 if ( ! marginCalc ) {
19811992 const liquidationBuffer = this . getLiquidationBuffer ( ) ;
1982- marginCalc = this . getMarginCalculation ( 'Maintenance' , { liquidationBuffer } ) ;
1993+ marginCalc = this . getMarginCalculation ( 'Maintenance' , {
1994+ liquidationBuffer,
1995+ } ) ;
19831996 }
19841997
1985- const result = new Map < 'cross' | number , {
1986- canBeLiquidated : boolean ;
1987- marginRequirement : BN ;
1988- totalCollateral : BN ;
1989- } > ( ) ;
1998+ const result = new Map <
1999+ 'cross' | number ,
2000+ {
2001+ canBeLiquidated : boolean ;
2002+ marginRequirement : BN ;
2003+ totalCollateral : BN ;
2004+ }
2005+ > ( ) ;
19902006
19912007 // Cross margin status
19922008 const crossTotalCollateral = marginCalc . totalCollateral ;
@@ -1998,7 +2014,10 @@ export class User {
19982014 } ) ;
19992015
20002016 // Isolated positions status
2001- for ( const [ marketIndex , isoCalc ] of marginCalc . isolatedMarginCalculations ) {
2017+ for ( const [
2018+ marketIndex ,
2019+ isoCalc ,
2020+ ] of marginCalc . isolatedMarginCalculations ) {
20022021 const isoTotalCollateral = isoCalc . totalCollateral ;
20032022 const isoMarginRequirement = isoCalc . marginRequirement ;
20042023 result . set ( marketIndex , {
@@ -2015,7 +2034,8 @@ export class User {
20152034 // Consider on-chain flags OR computed margin status (cross or any isolated)
20162035 const hasOnChainFlag =
20172036 ( this . getUserAccount ( ) . status &
2018- ( UserStatus . BEING_LIQUIDATED | UserStatus . BANKRUPT ) ) > 0 ;
2037+ ( UserStatus . BEING_LIQUIDATED | UserStatus . BANKRUPT ) ) >
2038+ 0 ;
20192039 const calc = marginCalc ?? this . getMarginCalculation ( 'Maintenance' ) ;
20202040 return (
20212041 hasOnChainFlag ||
@@ -2031,7 +2051,9 @@ export class User {
20312051 }
20322052
20332053 /** Returns true if any isolated perp position is currently below its maintenance requirement (no buffer). */
2034- public isIsolatedMarginBeingLiquidated ( marginCalc ?: MarginCalculation ) : boolean {
2054+ public isIsolatedMarginBeingLiquidated (
2055+ marginCalc ?: MarginCalculation
2056+ ) : boolean {
20352057 const calc = marginCalc ?? this . getMarginCalculation ( 'Maintenance' ) ;
20362058 for ( const [ , isoCalc ] of calc . isolatedMarginCalculations ) {
20372059 if ( isoCalc . totalCollateral . lt ( isoCalc . marginRequirement ) ) {
@@ -4307,11 +4329,14 @@ export class User {
43074329 calculateWorstCasePerpLiabilityValue (
43084330 marketPosition ,
43094331 market ,
4310- oraclePriceData . price ,
4332+ oraclePriceData . price
43114333 ) ;
43124334
43134335 // margin ratio for this perp
4314- const customMarginRatio = Math . max ( this . getUserAccount ( ) . maxMarginRatio , marketPosition . maxMarginRatio ) ;
4336+ const customMarginRatio = Math . max (
4337+ this . getUserAccount ( ) . maxMarginRatio ,
4338+ marketPosition . maxMarginRatio
4339+ ) ;
43154340 let marginRatio = new BN (
43164341 calculateMarketMarginRatio (
43174342 market ,
0 commit comments