Skip to content

Commit c6b850e

Browse files
committed
fix: lint and formatting
1 parent 0a373ea commit c6b850e

File tree

4 files changed

+52
-34
lines changed

4 files changed

+52
-34
lines changed

sdk/src/marginCalculation.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,6 @@ export class MarginContext {
8080
this.marginRatioOverride = ratio;
8181
return this;
8282
}
83-
84-
trackMarketMarginRequirement(marketIdentifier: MarketIdentifier): this {
85-
if (this.mode.type !== 'Liquidation') {
86-
throw new Error(
87-
'InvalidMarginCalculation: Cant track market outside of liquidation mode'
88-
);
89-
}
90-
return this;
91-
}
9283
}
9384

9485
export class IsolatedMarginCalculation {
@@ -312,4 +303,4 @@ export class MarginCalculation {
312303
hasIsolatedMarginCalculation(marketIndex: number): boolean {
313304
return this.isolatedMarginCalculations.has(marketIndex);
314305
}
315-
}
306+
}

sdk/src/math/spotPosition.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function getWorstCaseTokenAmounts(
3333
spotMarketAccount: SpotMarketAccount,
3434
strictOraclePrice: StrictOraclePrice,
3535
marginCategory: MarginCategory,
36-
customMarginRatio?: number,
36+
customMarginRatio?: number,
3737
includeOpenOrders?: boolean
3838
): OrderFillSimulation {
3939
const tokenAmount = getSignedTokenAmount(
@@ -51,7 +51,10 @@ export function getWorstCaseTokenAmounts(
5151
strictOraclePrice
5252
);
5353

54-
if (spotPosition.openBids.eq(ZERO) && spotPosition.openAsks.eq(ZERO) || !includeOpenOrders) {
54+
if (
55+
(spotPosition.openBids.eq(ZERO) && spotPosition.openAsks.eq(ZERO)) ||
56+
!includeOpenOrders
57+
) {
5558
const { weight, weightedTokenValue } = calculateWeightedTokenValue(
5659
tokenAmount,
5760
tokenValue,

sdk/src/user.ts

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,7 @@ import { StrictOraclePrice } from './oracles/strictOraclePrice';
107107

108108
import { calculateSpotFuelBonus, calculatePerpFuelBonus } from './math/fuel';
109109
import { grpcUserAccountSubscriber } from './accounts/grpcUserAccountSubscriber';
110-
import {
111-
MarginCalculation,
112-
MarginContext,
113-
} from './marginCalculation';
110+
import { MarginCalculation, MarginContext } from './marginCalculation';
114111

115112
export 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,

sdk/tests/user/getMarginCalculation.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
QUOTE_PRECISION,
1313
SPOT_MARKET_BALANCE_PRECISION,
1414
SpotBalanceType,
15-
MARGIN_PRECISION,
1615
OPEN_ORDER_MARGIN_REQUIREMENT,
1716
SPOT_MARKET_WEIGHT_PRECISION,
1817
PositionFlag,

0 commit comments

Comments
 (0)