@@ -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