Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 39 additions & 15 deletions sdk/src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,10 @@ export class User {
};
}

public isPositionEmpty(position: PerpPosition): boolean {
return position.baseAssetAmount.eq(ZERO) && position.openOrders === 0;
}

public getIsolatePerpPositionTokenAmount(perpMarketIndex: number): BN {
const perpPosition = this.getPerpPosition(perpMarketIndex);
if (!perpPosition) return ZERO;
Expand Down Expand Up @@ -485,7 +489,8 @@ export class User {
marketIndex: number,
collateralBuffer = ZERO,
enterHighLeverageMode = undefined,
maxMarginRatio = undefined
maxMarginRatio = undefined,
positionType: 'isolated' | 'cross' = 'cross'
): BN {
const perpPosition = this.getPerpPositionOrEmpty(marketIndex);

Expand All @@ -499,10 +504,29 @@ export class User {
)
: ZERO;

const freeCollateral = this.getFreeCollateral(
'Initial',
enterHighLeverageMode
).sub(collateralBuffer);
let freeCollateral: BN;
if (positionType === 'isolated' && this.isPositionEmpty(perpPosition)) {
const {
totalAssetValue: quoteSpotMarketAssetValue,
totalLiabilityValue: quoteSpotMarketLiabilityValue,
} = this.getSpotMarketAssetAndLiabilityValue(
perpMarket.quoteSpotMarketIndex,
'Initial',
undefined,
undefined,
true
);

freeCollateral = quoteSpotMarketAssetValue.sub(
quoteSpotMarketLiabilityValue
);
} else {
freeCollateral = this.getFreeCollateral(
'Initial',
enterHighLeverageMode,
positionType === 'isolated' ? marketIndex : undefined
).sub(collateralBuffer);
}

return this.getPerpBuyingPowerFromFreeCollateralAndBaseAssetAmount(
marketIndex,
Expand Down Expand Up @@ -544,17 +568,15 @@ export class User {
enterHighLeverageMode = false,
perpMarketIndex?: number
): BN {
const { totalCollateral, marginRequirement, getIsolatedFreeCollateral } =
this.getMarginCalculation(marginCategory, {
enteringHighLeverage: enterHighLeverageMode,
strict: marginCategory === 'Initial',
});
const calc = this.getMarginCalculation(marginCategory, {
enteringHighLeverage: enterHighLeverageMode,
strict: marginCategory === 'Initial',
});

if (perpMarketIndex !== undefined) {
return getIsolatedFreeCollateral(perpMarketIndex);
return calc.getIsolatedFreeCollateral(perpMarketIndex);
} else {
const freeCollateral = totalCollateral.sub(marginRequirement);
return freeCollateral.gte(ZERO) ? freeCollateral : ZERO;
return calc.getCrossFreeCollateral();
}
}

Expand Down Expand Up @@ -2752,7 +2774,8 @@ export class User {
tradeSide: PositionDirection,
isLp = false,
enterHighLeverageMode = undefined,
maxMarginRatio = undefined
maxMarginRatio = undefined,
positionType: 'isolated' | 'cross' = 'cross'
): { tradeSize: BN; oppositeSideTradeSize: BN } {
let tradeSize = ZERO;
let oppositeSideTradeSize = ZERO;
Expand Down Expand Up @@ -2792,7 +2815,8 @@ export class User {
targetMarketIndex,
lpBuffer,
enterHighLeverageMode,
maxMarginRatio
maxMarginRatio,
positionType
);

if (maxPositionSize.gte(ZERO)) {
Expand Down
Loading