Skip to content

Commit b13eca3

Browse files
committed
sdk: enter-high-leverage-mode-false (#1791)
1 parent bec4445 commit b13eca3

File tree

2 files changed

+43
-29
lines changed

2 files changed

+43
-29
lines changed

sdk/src/driftClient.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9854,17 +9854,20 @@ export class DriftClient {
98549854
})
98559855
: undefined;
98569856

9857-
const ix = await this.program.instruction.disableUserHighLeverageMode({
9858-
accounts: {
9859-
state: await this.getStatePublicKey(),
9860-
user,
9861-
authority: this.wallet.publicKey,
9862-
highLeverageModeConfig: getHighLeverageModeConfigPublicKey(
9863-
this.program.programId
9864-
),
9865-
},
9866-
remainingAccounts,
9867-
});
9857+
const ix = await this.program.instruction.disableUserHighLeverageMode(
9858+
false,
9859+
{
9860+
accounts: {
9861+
state: await this.getStatePublicKey(),
9862+
user,
9863+
authority: this.wallet.publicKey,
9864+
highLeverageModeConfig: getHighLeverageModeConfigPublicKey(
9865+
this.program.programId
9866+
),
9867+
},
9868+
remainingAccounts,
9869+
}
9870+
);
98689871

98699872
return ix;
98709873
}

sdk/src/user.ts

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ export class User {
662662
public getPerpBuyingPower(
663663
marketIndex: number,
664664
collateralBuffer = ZERO,
665-
enterHighLeverageMode = false
665+
enterHighLeverageMode = undefined
666666
): BN {
667667
const perpPosition = this.getPerpPositionWithLPSettle(
668668
marketIndex,
@@ -697,7 +697,7 @@ export class User {
697697
marketIndex: number,
698698
freeCollateral: BN,
699699
baseAssetAmount: BN,
700-
enterHighLeverageMode = false
700+
enterHighLeverageMode = undefined
701701
): BN {
702702
const marginRatio = calculateMarketMarginRatio(
703703
this.driftClient.getPerpMarketAccount(marketIndex),
@@ -716,7 +716,7 @@ export class User {
716716
*/
717717
public getFreeCollateral(
718718
marginCategory: MarginCategory = 'Initial',
719-
enterHighLeverageMode = false
719+
enterHighLeverageMode = undefined
720720
): BN {
721721
const totalCollateral = this.getTotalCollateral(marginCategory, true);
722722
const marginRequirement =
@@ -735,7 +735,7 @@ export class User {
735735
liquidationBuffer?: BN,
736736
strict = false,
737737
includeOpenOrders = true,
738-
enteringHighLeverage = false
738+
enteringHighLeverage = undefined
739739
): BN {
740740
return this.getTotalPerpPositionLiability(
741741
marginCategory,
@@ -757,7 +757,7 @@ export class User {
757757
/**
758758
* @returns The initial margin requirement in USDC. : QUOTE_PRECISION
759759
*/
760-
public getInitialMarginRequirement(enterHighLeverageMode = false): BN {
760+
public getInitialMarginRequirement(enterHighLeverageMode = undefined): BN {
761761
return this.getMarginRequirement(
762762
'Initial',
763763
undefined,
@@ -1432,7 +1432,7 @@ export class User {
14321432
liquidationBuffer?: BN,
14331433
includeOpenOrders?: boolean,
14341434
strict = false,
1435-
enteringHighLeverage = false
1435+
enteringHighLeverage = undefined
14361436
): BN {
14371437
const market = this.driftClient.getPerpMarketAccount(
14381438
perpPosition.marketIndex
@@ -1476,13 +1476,17 @@ export class User {
14761476
}
14771477

14781478
if (marginCategory) {
1479+
const userCustomMargin = this.getUserAccount().maxMarginRatio;
14791480
let marginRatio = new BN(
14801481
calculateMarketMarginRatio(
14811482
market,
14821483
baseAssetAmount.abs(),
14831484
marginCategory,
1484-
this.getUserAccount().maxMarginRatio,
1485-
this.isHighLeverageMode(marginCategory) || enteringHighLeverage
1485+
enteringHighLeverage === false
1486+
? Math.max(market.marginRatioInitial, userCustomMargin)
1487+
: userCustomMargin,
1488+
this.isHighLeverageMode(marginCategory) ||
1489+
enteringHighLeverage === true
14861490
)
14871491
);
14881492

@@ -1570,7 +1574,7 @@ export class User {
15701574
liquidationBuffer?: BN,
15711575
includeOpenOrders?: boolean,
15721576
strict = false,
1573-
enteringHighLeverage = false
1577+
enteringHighLeverage = undefined
15741578
): BN {
15751579
return this.getActivePerpPositions().reduce(
15761580
(totalPerpValue, perpPosition) => {
@@ -1894,7 +1898,7 @@ export class User {
18941898
perpMarketIndex: number,
18951899
_marginCategory: MarginCategory = 'Initial',
18961900
isLp = false,
1897-
enterHighLeverageMode = false
1901+
enterHighLeverageMode = undefined
18981902
): BN {
18991903
const market = this.driftClient.getPerpMarketAccount(perpMarketIndex);
19001904
const marketPrice =
@@ -2245,7 +2249,7 @@ export class User {
22452249
marginCategory: MarginCategory = 'Maintenance',
22462250
includeOpenOrders = false,
22472251
offsetCollateral = ZERO,
2248-
enteringHighLeverage = false
2252+
enteringHighLeverage = undefined
22492253
): BN {
22502254
const totalCollateral = this.getTotalCollateral(
22512255
marginCategory,
@@ -2370,7 +2374,7 @@ export class User {
23702374
positionBaseSizeChange: BN,
23712375
estimatedEntryPrice: BN,
23722376
includeOpenOrders: boolean,
2373-
enteringHighLeverage = false,
2377+
enteringHighLeverage = undefined,
23742378
marginCategory: MarginCategory = 'Maintenance'
23752379
): BN {
23762380
let freeCollateralChange = ZERO;
@@ -2423,12 +2427,15 @@ export class User {
24232427
);
24242428
}
24252429

2430+
const userCustomMargin = this.getUserAccount().maxMarginRatio;
24262431
const marginRatio = calculateMarketMarginRatio(
24272432
market,
24282433
baseAssetAmount.abs(),
24292434
marginCategory,
2430-
this.getUserAccount().maxMarginRatio,
2431-
this.isHighLeverageMode(marginCategory) || enteringHighLeverage
2435+
enteringHighLeverage === false
2436+
? Math.max(market.marginRatioInitial, userCustomMargin)
2437+
: userCustomMargin,
2438+
this.isHighLeverageMode(marginCategory) || enteringHighLeverage === true
24322439
);
24332440

24342441
return liabilityValue.mul(new BN(marginRatio)).div(MARGIN_PRECISION);
@@ -2457,7 +2464,7 @@ export class User {
24572464
oraclePrice: BN,
24582465
marginCategory: MarginCategory = 'Maintenance',
24592466
includeOpenOrders = false,
2460-
enteringHighLeverage = false
2467+
enteringHighLeverage = undefined
24612468
): BN | undefined {
24622469
const baseAssetAmount = includeOpenOrders
24632470
? calculateWorstCaseBaseAssetAmount(perpPosition, market, oraclePrice)
@@ -2470,12 +2477,16 @@ export class User {
24702477

24712478
const proposedBaseAssetAmount = baseAssetAmount.add(positionBaseSizeChange);
24722479

2480+
const userCustomMargin = this.getUserAccount().maxMarginRatio;
2481+
24732482
const marginRatio = calculateMarketMarginRatio(
24742483
market,
24752484
proposedBaseAssetAmount.abs(),
24762485
marginCategory,
2477-
this.getUserAccount().maxMarginRatio,
2478-
this.isHighLeverageMode(marginCategory) || enteringHighLeverage
2486+
enteringHighLeverage === false
2487+
? Math.max(market.marginRatioInitial, userCustomMargin)
2488+
: userCustomMargin,
2489+
this.isHighLeverageMode(marginCategory) || enteringHighLeverage === true
24792490
);
24802491

24812492
const marginRatioQuotePrecision = new BN(marginRatio)
@@ -2631,7 +2642,7 @@ export class User {
26312642
targetMarketIndex: number,
26322643
tradeSide: PositionDirection,
26332644
isLp = false,
2634-
enterHighLeverageMode = false
2645+
enterHighLeverageMode = undefined
26352646
): { tradeSize: BN; oppositeSideTradeSize: BN } {
26362647
let tradeSize = ZERO;
26372648
let oppositeSideTradeSize = ZERO;

0 commit comments

Comments
 (0)