Skip to content

Commit ff2f8dc

Browse files
authored
fix: bug with perp buying power isolated pos (#2094)
1 parent c399145 commit ff2f8dc

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

sdk/src/user.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,8 @@ export class User {
504504
)
505505
: ZERO;
506506

507-
let freeCollateral: BN = ZERO;
508507
// if position is isolated, we always add on available quote from the cross account
508+
let freeCollateral: BN = ZERO;
509509
if (positionType === 'isolated') {
510510
const {
511511
totalAssetValue: quoteSpotMarketAssetValue,
@@ -518,19 +518,26 @@ export class User {
518518
true
519519
);
520520

521-
freeCollateral = quoteSpotMarketAssetValue.sub(
521+
const usdcAvailableForIsolatedMargin = quoteSpotMarketAssetValue.sub(
522522
quoteSpotMarketLiabilityValue
523523
);
524-
}
525-
526-
// adding free collateral from the cross account or from within isolated margin calc for this marketIndex
527-
freeCollateral = freeCollateral.add(
528-
this.getFreeCollateral(
524+
const generalFreeCollateral = this.getFreeCollateral(
529525
'Initial',
530526
enterHighLeverageMode,
531-
positionType === 'isolated' ? marketIndex : undefined
532-
).sub(collateralBuffer)
533-
);
527+
undefined
528+
);
529+
freeCollateral = BN.min(
530+
usdcAvailableForIsolatedMargin,
531+
generalFreeCollateral
532+
).sub(collateralBuffer);
533+
} else {
534+
// free collateral from the cross account only
535+
freeCollateral = this.getFreeCollateral(
536+
'Initial',
537+
enterHighLeverageMode,
538+
undefined
539+
).sub(collateralBuffer);
540+
}
534541

535542
return this.getPerpBuyingPowerFromFreeCollateralAndBaseAssetAmount(
536543
marketIndex,

0 commit comments

Comments
 (0)