From 11434762e080cfa1a0696fc84b0cd09be3b7c3d7 Mon Sep 17 00:00:00 2001 From: Chris Heaney Date: Thu, 21 Nov 2024 07:52:57 -0500 Subject: [PATCH] sdk: fixes for getMaxTradeSizeUSDCForPerp --- sdk/src/user.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/sdk/src/user.ts b/sdk/src/user.ts index 4c60b0530..356732de8 100644 --- a/sdk/src/user.ts +++ b/sdk/src/user.ts @@ -2728,7 +2728,7 @@ export class User { lpBuffer ); - if (maxPositionSize.gte(ZERO)) { + if (maxPositionSize.gt(ZERO)) { if (oppositeSizeLiabilityValue.eq(ZERO)) { // case 1 : Regular trade where current total position less than max, and no opposite position to account for // do nothing @@ -2750,8 +2750,18 @@ export class User { ); const totalCollateral = this.getTotalCollateral(); const marginRequirement = this.getInitialMarginRequirement(); + + let marginRatio = new BN( + calculateMarketMarginRatio( + market, + currentPosition.baseAssetAmount.abs(), + 'Initial', + this.getUserAccount().maxMarginRatio, + this.isHighLeverageMode() + ) + ); const marginFreedByClosing = perpLiabilityValue - .mul(new BN(market.marginRatioInitial)) + .mul(marginRatio) .div(MARGIN_PRECISION); const marginRequirementAfterClosing = marginRequirement.sub(marginFreedByClosing);