Skip to content

Commit 02a46b0

Browse files
committed
feat: add liq buffer in margin calc + cleaner canBeLiquidated return
1 parent d414b57 commit 02a46b0

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

sdk/src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,3 +1902,9 @@ export type CacheInfo = {
19021902
export type AmmCache = {
19031903
cache: CacheInfo[];
19041904
};
1905+
1906+
export type AccountLiquidatableStatus = {
1907+
canBeLiquidated: boolean;
1908+
marginRequirement: BN;
1909+
totalCollateral: BN;
1910+
};

sdk/src/user.ts

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
UserAccount,
1515
UserStatus,
1616
UserStatsAccount,
17+
AccountLiquidatableStatus,
1718
} from './types';
1819
import {
1920
calculateEntryPrice,
@@ -600,16 +601,17 @@ export class User {
600601
if (perpMarketIndex !== undefined) {
601602
const isolatedMarginCalculation =
602603
marginCalc.isolatedMarginCalculations.get(perpMarketIndex);
603-
const { marginRequirement, marginRequirementPlusBuffer } = isolatedMarginCalculation;
604+
const { marginRequirement, marginRequirementPlusBuffer } =
605+
isolatedMarginCalculation;
604606

605-
if(liquidationBuffer?.gt(ZERO)){
607+
if (liquidationBuffer?.gt(ZERO)) {
606608
return marginRequirementPlusBuffer;
607609
}
608610
return marginRequirement;
609611
}
610612

611613
// Default: Cross margin requirement
612-
if(liquidationBuffer?.gt(ZERO)){
614+
if (liquidationBuffer?.gt(ZERO)) {
613615
return marginCalc.marginRequirementPlusBuffer;
614616
}
615617
return marginCalc.marginRequirement;
@@ -1241,14 +1243,15 @@ export class User {
12411243
});
12421244

12431245
if (perpMarketIndex !== undefined) {
1244-
const { totalCollateral, totalCollateralBuffer } = marginCalc.isolatedMarginCalculations.get(perpMarketIndex)
1245-
if(liquidationBuffer?.gt(ZERO)){
1246+
const { totalCollateral, totalCollateralBuffer } =
1247+
marginCalc.isolatedMarginCalculations.get(perpMarketIndex);
1248+
if (liquidationBuffer?.gt(ZERO)) {
12461249
return totalCollateralBuffer;
12471250
}
12481251
return totalCollateral;
12491252
}
12501253

1251-
if(liquidationBuffer?.gt(ZERO)){
1254+
if (liquidationBuffer?.gt(ZERO)) {
12521255
return marginCalc.totalCollateralBuffer;
12531256
}
12541257
return marginCalc.totalCollateral;
@@ -1963,26 +1966,27 @@ export class User {
19631966
return netAssetValue.mul(TEN_THOUSAND).div(totalLiabilityValue);
19641967
}
19651968

1966-
public canBeLiquidated(): {
1967-
canBeLiquidated: boolean;
1968-
marginRequirement: BN;
1969-
totalCollateral: BN;
1970-
liquidationStatuses: Map<
1971-
'cross' | number,
1972-
{ canBeLiquidated: boolean; marginRequirement: BN; totalCollateral: BN }
1973-
>;
1969+
public canBeLiquidated(): AccountLiquidatableStatus & {
1970+
isolatedPositions: Map<number, AccountLiquidatableStatus>;
19741971
} {
19751972
// Deprecated signature retained for backward compatibility in type only
19761973
// but implementation now delegates to the new Map-based API and returns cross margin status.
19771974
const map = this.getLiquidationStatuses();
19781975
const cross = map.get('cross');
1976+
const isolatedPositions: Map<number, AccountLiquidatableStatus> = new Map(
1977+
Array.from(map.entries())
1978+
.filter(
1979+
(e): e is [number, AccountLiquidatableStatus] => e[0] !== 'cross'
1980+
)
1981+
.map(([key, value]) => [key, value])
1982+
);
19791983
return cross
1980-
? { ...cross, liquidationStatuses: map }
1984+
? { ...cross, isolatedPositions }
19811985
: {
19821986
canBeLiquidated: false,
19831987
marginRequirement: ZERO,
19841988
totalCollateral: ZERO,
1985-
liquidationStatuses: map,
1989+
isolatedPositions,
19861990
};
19871991
}
19881992

@@ -1994,10 +1998,7 @@ export class User {
19941998
*/
19951999
public getLiquidationStatuses(
19962000
marginCalc?: MarginCalculation
1997-
): Map<
1998-
'cross' | number,
1999-
{ canBeLiquidated: boolean; marginRequirement: BN; totalCollateral: BN }
2000-
> {
2001+
): Map<'cross' | number, AccountLiquidatableStatus> {
20012002
// If not provided, use buffer-aware calc for canBeLiquidated checks
20022003
if (!marginCalc) {
20032004
const liquidationBuffer = this.getLiquidationBuffer();
@@ -2006,14 +2007,7 @@ export class User {
20062007
});
20072008
}
20082009

2009-
const result = new Map<
2010-
'cross' | number,
2011-
{
2012-
canBeLiquidated: boolean;
2013-
marginRequirement: BN;
2014-
totalCollateral: BN;
2015-
}
2016-
>();
2010+
const result = new Map<'cross' | number, AccountLiquidatableStatus>();
20172011

20182012
// Cross margin status
20192013
const crossTotalCollateral = marginCalc.totalCollateral;

0 commit comments

Comments
 (0)