@drift-labs/sdk 2.97.0-beta.0 → 2.97.0-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1283,6 +1283,37 @@
1283
1283
  ],
1284
1284
  "args": []
1285
1285
  },
1286
+ {
1287
+ "name": "enableUserHighLeverageMode",
1288
+ "accounts": [
1289
+ {
1290
+ "name": "state",
1291
+ "isMut": false,
1292
+ "isSigner": false
1293
+ },
1294
+ {
1295
+ "name": "user",
1296
+ "isMut": true,
1297
+ "isSigner": false
1298
+ },
1299
+ {
1300
+ "name": "authority",
1301
+ "isMut": false,
1302
+ "isSigner": true
1303
+ },
1304
+ {
1305
+ "name": "highLeverageModeConfig",
1306
+ "isMut": true,
1307
+ "isSigner": false
1308
+ }
1309
+ ],
1310
+ "args": [
1311
+ {
1312
+ "name": "subAccountId",
1313
+ "type": "u16"
1314
+ }
1315
+ ]
1316
+ },
1286
1317
  {
1287
1318
  "name": "fillPerpOrder",
1288
1319
  "accounts": [
@@ -1498,6 +1529,58 @@
1498
1529
  ],
1499
1530
  "args": []
1500
1531
  },
1532
+ {
1533
+ "name": "disableUserHighLeverageMode",
1534
+ "accounts": [
1535
+ {
1536
+ "name": "state",
1537
+ "isMut": false,
1538
+ "isSigner": false
1539
+ },
1540
+ {
1541
+ "name": "authority",
1542
+ "isMut": false,
1543
+ "isSigner": true
1544
+ },
1545
+ {
1546
+ "name": "user",
1547
+ "isMut": true,
1548
+ "isSigner": false
1549
+ },
1550
+ {
1551
+ "name": "highLeverageModeConfig",
1552
+ "isMut": true,
1553
+ "isSigner": false
1554
+ }
1555
+ ],
1556
+ "args": []
1557
+ },
1558
+ {
1559
+ "name": "updateUserFuelBonus",
1560
+ "accounts": [
1561
+ {
1562
+ "name": "state",
1563
+ "isMut": false,
1564
+ "isSigner": false
1565
+ },
1566
+ {
1567
+ "name": "authority",
1568
+ "isMut": false,
1569
+ "isSigner": true
1570
+ },
1571
+ {
1572
+ "name": "user",
1573
+ "isMut": true,
1574
+ "isSigner": false
1575
+ },
1576
+ {
1577
+ "name": "userStats",
1578
+ "isMut": true,
1579
+ "isSigner": false
1580
+ }
1581
+ ],
1582
+ "args": []
1583
+ },
1501
1584
  {
1502
1585
  "name": "updateUserOpenOrdersCount",
1503
1586
  "accounts": [
@@ -3961,6 +4044,36 @@
3961
4044
  }
3962
4045
  ]
3963
4046
  },
4047
+ {
4048
+ "name": "updatePerpMarketHighLeverageMarginRatio",
4049
+ "accounts": [
4050
+ {
4051
+ "name": "admin",
4052
+ "isMut": false,
4053
+ "isSigner": true
4054
+ },
4055
+ {
4056
+ "name": "state",
4057
+ "isMut": false,
4058
+ "isSigner": false
4059
+ },
4060
+ {
4061
+ "name": "perpMarket",
4062
+ "isMut": true,
4063
+ "isSigner": false
4064
+ }
4065
+ ],
4066
+ "args": [
4067
+ {
4068
+ "name": "marginRatioInitial",
4069
+ "type": "u16"
4070
+ },
4071
+ {
4072
+ "name": "marginRatioMaintenance",
4073
+ "type": "u16"
4074
+ }
4075
+ ]
4076
+ },
3964
4077
  {
3965
4078
  "name": "updatePerpMarketFundingPeriod",
3966
4079
  "accounts": [
@@ -5926,6 +6039,72 @@
5926
6039
  }
5927
6040
  }
5928
6041
  ]
6042
+ },
6043
+ {
6044
+ "name": "initializeHighLeverageModeConfig",
6045
+ "accounts": [
6046
+ {
6047
+ "name": "admin",
6048
+ "isMut": true,
6049
+ "isSigner": true
6050
+ },
6051
+ {
6052
+ "name": "highLeverageModeConfig",
6053
+ "isMut": true,
6054
+ "isSigner": false
6055
+ },
6056
+ {
6057
+ "name": "state",
6058
+ "isMut": false,
6059
+ "isSigner": false
6060
+ },
6061
+ {
6062
+ "name": "rent",
6063
+ "isMut": false,
6064
+ "isSigner": false
6065
+ },
6066
+ {
6067
+ "name": "systemProgram",
6068
+ "isMut": false,
6069
+ "isSigner": false
6070
+ }
6071
+ ],
6072
+ "args": [
6073
+ {
6074
+ "name": "maxUsers",
6075
+ "type": "u32"
6076
+ }
6077
+ ]
6078
+ },
6079
+ {
6080
+ "name": "updateHighLeverageModeConfig",
6081
+ "accounts": [
6082
+ {
6083
+ "name": "admin",
6084
+ "isMut": true,
6085
+ "isSigner": true
6086
+ },
6087
+ {
6088
+ "name": "highLeverageModeConfig",
6089
+ "isMut": true,
6090
+ "isSigner": false
6091
+ },
6092
+ {
6093
+ "name": "state",
6094
+ "isMut": false,
6095
+ "isSigner": false
6096
+ }
6097
+ ],
6098
+ "args": [
6099
+ {
6100
+ "name": "maxUsers",
6101
+ "type": "u32"
6102
+ },
6103
+ {
6104
+ "name": "reduceOnly",
6105
+ "type": "bool"
6106
+ }
6107
+ ]
5929
6108
  }
5930
6109
  ],
5931
6110
  "accounts": [
@@ -6132,6 +6311,35 @@
6132
6311
  ]
6133
6312
  }
6134
6313
  },
6314
+ {
6315
+ "name": "HighLeverageModeConfig",
6316
+ "type": {
6317
+ "kind": "struct",
6318
+ "fields": [
6319
+ {
6320
+ "name": "maxUsers",
6321
+ "type": "u32"
6322
+ },
6323
+ {
6324
+ "name": "currentUsers",
6325
+ "type": "u32"
6326
+ },
6327
+ {
6328
+ "name": "reduceOnly",
6329
+ "type": "u8"
6330
+ },
6331
+ {
6332
+ "name": "padding",
6333
+ "type": {
6334
+ "array": [
6335
+ "u8",
6336
+ 31
6337
+ ]
6338
+ }
6339
+ }
6340
+ ]
6341
+ }
6342
+ },
6135
6343
  {
6136
6344
  "name": "InsuranceFundStake",
6137
6345
  "type": {
@@ -6520,12 +6728,24 @@
6520
6728
  ],
6521
6729
  "type": "u8"
6522
6730
  },
6731
+ {
6732
+ "name": "padding1",
6733
+ "type": "u8"
6734
+ },
6735
+ {
6736
+ "name": "highLeverageMarginRatioInitial",
6737
+ "type": "u16"
6738
+ },
6739
+ {
6740
+ "name": "highLeverageMarginRatioMaintenance",
6741
+ "type": "u16"
6742
+ },
6523
6743
  {
6524
6744
  "name": "padding",
6525
6745
  "type": {
6526
6746
  "array": [
6527
6747
  "u8",
6528
- 43
6748
+ 38
6529
6749
  ]
6530
6750
  }
6531
6751
  }
@@ -7375,12 +7595,18 @@
7375
7595
  ],
7376
7596
  "type": "bool"
7377
7597
  },
7598
+ {
7599
+ "name": "marginMode",
7600
+ "type": {
7601
+ "defined": "MarginMode"
7602
+ }
7603
+ },
7378
7604
  {
7379
7605
  "name": "padding1",
7380
7606
  "type": {
7381
7607
  "array": [
7382
7608
  "u8",
7383
- 5
7609
+ 4
7384
7610
  ]
7385
7611
  }
7386
7612
  },
@@ -10633,6 +10859,20 @@
10633
10859
  }
10634
10860
  ]
10635
10861
  }
10862
+ },
10863
+ {
10864
+ "name": "MarginMode",
10865
+ "type": {
10866
+ "kind": "enum",
10867
+ "variants": [
10868
+ {
10869
+ "name": "Default"
10870
+ },
10871
+ {
10872
+ "name": "HighLeverage"
10873
+ }
10874
+ ]
10875
+ }
10636
10876
  }
10637
10877
  ],
10638
10878
  "events": [
@@ -13126,6 +13366,11 @@
13126
13366
  "code": 6289,
13127
13367
  "name": "PlaceAndTakeOrderSuccessConditionFailed",
13128
13368
  "msg": "Place and take order success condition failed"
13369
+ },
13370
+ {
13371
+ "code": 6290,
13372
+ "name": "InvalidHighLeverageModeConfig",
13373
+ "msg": "Invalid High Leverage Mode Config"
13129
13374
  }
13130
13375
  ],
13131
13376
  "metadata": {
@@ -214,7 +214,8 @@ export function calculateMarginUSDCRequiredForTrade(
214
214
  driftClient: DriftClient,
215
215
  targetMarketIndex: number,
216
216
  baseSize: BN,
217
- userMaxMarginRatio?: number
217
+ userMaxMarginRatio?: number,
218
+ userHighLeverageMode?: boolean
218
219
  ): BN {
219
220
  const targetMarket = driftClient.getPerpMarketAccount(targetMarketIndex);
220
221
  const oracleData = driftClient.getOracleDataForPerpMarket(
@@ -232,7 +233,8 @@ export function calculateMarginUSDCRequiredForTrade(
232
233
  targetMarket,
233
234
  baseSize.abs(),
234
235
  'Initial',
235
- userMaxMarginRatio
236
+ userMaxMarginRatio,
237
+ userHighLeverageMode
236
238
  )
237
239
  )
238
240
  .mul(perpLiabilityValue)
@@ -251,13 +253,15 @@ export function calculateCollateralDepositRequiredForTrade(
251
253
  targetMarketIndex: number,
252
254
  baseSize: BN,
253
255
  collateralIndex: number,
254
- userMaxMarginRatio?: number
256
+ userMaxMarginRatio?: number,
257
+ userHighLeverageMode?: boolean
255
258
  ): BN {
256
259
  const marginRequiredUsdc = calculateMarginUSDCRequiredForTrade(
257
260
  driftClient,
258
261
  targetMarketIndex,
259
262
  baseSize,
260
- userMaxMarginRatio
263
+ userMaxMarginRatio,
264
+ userHighLeverageMode
261
265
  );
262
266
 
263
267
  const collateralMarket = driftClient.getSpotMarketAccount(collateralIndex);
@@ -130,8 +130,24 @@ export function calculateMarketMarginRatio(
130
130
  market: PerpMarketAccount,
131
131
  size: BN,
132
132
  marginCategory: MarginCategory,
133
- customMarginRatio = 0
133
+ customMarginRatio = 0,
134
+ userHighLeverageMode = false
134
135
  ): number {
136
+ let marginRationInitial;
137
+ let marginRatioMaintenance;
138
+
139
+ if (
140
+ userHighLeverageMode &&
141
+ market.highLeverageMarginRatioInitial > 0 &&
142
+ market.highLeverageMarginRatioMaintenance
143
+ ) {
144
+ marginRationInitial = market.highLeverageMarginRatioInitial;
145
+ marginRatioMaintenance = market.highLeverageMarginRatioMaintenance;
146
+ } else {
147
+ marginRationInitial = market.marginRatioInitial;
148
+ marginRatioMaintenance = market.marginRatioMaintenance;
149
+ }
150
+
135
151
  let marginRatio;
136
152
  switch (marginCategory) {
137
153
  case 'Initial': {
@@ -140,7 +156,7 @@ export function calculateMarketMarginRatio(
140
156
  calculateSizePremiumLiabilityWeight(
141
157
  size,
142
158
  new BN(market.imfFactor),
143
- new BN(market.marginRatioInitial),
159
+ new BN(marginRationInitial),
144
160
  MARGIN_PRECISION
145
161
  ).toNumber(),
146
162
  customMarginRatio
@@ -151,7 +167,7 @@ export function calculateMarketMarginRatio(
151
167
  marginRatio = calculateSizePremiumLiabilityWeight(
152
168
  size,
153
169
  new BN(market.imfFactor),
154
- new BN(market.marginRatioMaintenance),
170
+ new BN(marginRatioMaintenance),
155
171
  MARGIN_PRECISION
156
172
  ).toNumber();
157
173
  break;
package/src/types.ts CHANGED
@@ -68,6 +68,11 @@ export enum UserStatus {
68
68
  ADVANCED_LP = 8,
69
69
  }
70
70
 
71
+ export class MarginMode {
72
+ static readonly DEFAULT = { default: {} };
73
+ static readonly HIGH_LEVERAGE = { highLeverage: {} };
74
+ }
75
+
71
76
  export class ContractType {
72
77
  static readonly PERPETUAL = { perpetual: {} };
73
78
  static readonly FUTURE = { future: {} };
@@ -661,6 +666,9 @@ export type PerpMarketAccount = {
661
666
  fuelBoostTaker: number;
662
667
  fuelBoostMaker: number;
663
668
  fuelBoostPosition: number;
669
+
670
+ highLeverageMarginRatioInitial: number;
671
+ highLeverageMarginRatioMaintenance: number;
664
672
  };
665
673
 
666
674
  export type HistoricalOracleData = {
@@ -950,6 +958,7 @@ export type UserAccount = {
950
958
  openAuctions: number;
951
959
  hasOpenAuction: boolean;
952
960
  lastFuelBonusUpdateTs: number;
961
+ marginMode: MarginMode;
953
962
  };
954
963
 
955
964
  export type SpotPosition = {
package/src/user.ts CHANGED
@@ -676,7 +676,8 @@ export class User {
676
676
  this.driftClient.getPerpMarketAccount(marketIndex),
677
677
  baseAssetAmount,
678
678
  'Initial',
679
- this.getUserAccount().maxMarginRatio
679
+ this.getUserAccount().maxMarginRatio,
680
+ this.isHighLeverageMode()
680
681
  );
681
682
 
682
683
  return freeCollateral.mul(MARGIN_PRECISION).div(new BN(marginRatio));
@@ -1507,7 +1508,8 @@ export class User {
1507
1508
  market,
1508
1509
  baseAssetAmount.abs(),
1509
1510
  marginCategory,
1510
- this.getUserAccount().maxMarginRatio
1511
+ this.getUserAccount().maxMarginRatio,
1512
+ this.isHighLeverageMode()
1511
1513
  )
1512
1514
  );
1513
1515
 
@@ -1972,7 +1974,8 @@ export class User {
1972
1974
  market,
1973
1975
  maxSize,
1974
1976
  marginCategory,
1975
- this.getUserAccount().maxMarginRatio
1977
+ this.getUserAccount().maxMarginRatio,
1978
+ this.isHighLeverageMode()
1976
1979
  );
1977
1980
 
1978
1981
  // use more fesible size since imf factor activated
@@ -1993,7 +1996,8 @@ export class User {
1993
1996
  market,
1994
1997
  targetSize,
1995
1998
  marginCategory,
1996
- this.getUserAccount().maxMarginRatio
1999
+ this.getUserAccount().maxMarginRatio,
2000
+ this.isHighLeverageMode()
1997
2001
  );
1998
2002
  attempts += 1;
1999
2003
  }
@@ -2154,6 +2158,10 @@ export class User {
2154
2158
  return (this.getUserAccount().status & UserStatus.BANKRUPT) > 0;
2155
2159
  }
2156
2160
 
2161
+ public isHighLeverageMode(): boolean {
2162
+ return isVariant(this.getUserAccount().marginMode, 'highLeverage');
2163
+ }
2164
+
2157
2165
  /**
2158
2166
  * Checks if any user position cumulative funding differs from respective market cumulative funding
2159
2167
  * @returns
@@ -2443,7 +2451,9 @@ export class User {
2443
2451
  const marginRatio = calculateMarketMarginRatio(
2444
2452
  market,
2445
2453
  baseAssetAmount.abs(),
2446
- 'Maintenance'
2454
+ 'Maintenance',
2455
+ this.getUserAccount().maxMarginRatio,
2456
+ this.isHighLeverageMode()
2447
2457
  );
2448
2458
 
2449
2459
  return liabilityValue.mul(new BN(marginRatio)).div(MARGIN_PRECISION);
@@ -2488,7 +2498,8 @@ export class User {
2488
2498
  market,
2489
2499
  proposedBaseAssetAmount.abs(),
2490
2500
  marginCategory,
2491
- this.getUserAccount().maxMarginRatio
2501
+ this.getUserAccount().maxMarginRatio,
2502
+ this.isHighLeverageMode()
2492
2503
  );
2493
2504
  const marginRatioQuotePrecision = new BN(marginRatio)
2494
2505
  .mul(QUOTE_PRECISION)
@@ -2622,7 +2633,8 @@ export class User {
2622
2633
  targetMarketIndex,
2623
2634
  baseSize,
2624
2635
  collateralIndex,
2625
- this.getUserAccount().maxMarginRatio
2636
+ this.getUserAccount().maxMarginRatio,
2637
+ false // assume user cant be high leverage if they havent created user account ?
2626
2638
  );
2627
2639
  }
2628
2640
 
@@ -3771,7 +3783,8 @@ export class User {
3771
3783
  perpMarket,
3772
3784
  worstCaseBaseAmount.abs(),
3773
3785
  marginCategory,
3774
- this.getUserAccount().maxMarginRatio
3786
+ this.getUserAccount().maxMarginRatio,
3787
+ this.isHighLeverageMode()
3775
3788
  )
3776
3789
  );
3777
3790