@drift-labs/sdk 2.97.0-beta.2 → 2.97.0-beta.21

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.
@@ -443,10 +443,14 @@
443
443
  }
444
444
  },
445
445
  {
446
- "name": "queue"
446
+ "name": "queue",
447
+ "relations": [
448
+ "oracle"
449
+ ]
447
450
  },
448
451
  {
449
- "name": "authority"
452
+ "name": "authority",
453
+ "signer": true
450
454
  },
451
455
  {
452
456
  "name": "program_state",
@@ -503,6 +507,9 @@
503
507
  },
504
508
  {
505
509
  "name": "stake_pool"
510
+ },
511
+ {
512
+ "name": "delegation_group"
506
513
  }
507
514
  ],
508
515
  "args": [
@@ -955,10 +962,7 @@
955
962
  },
956
963
  {
957
964
  "name": "authority",
958
- "signer": true,
959
- "relations": [
960
- "queue"
961
- ]
965
+ "signer": true
962
966
  },
963
967
  {
964
968
  "name": "program_authority"
@@ -1038,6 +1042,13 @@
1038
1042
  {
1039
1043
  "name": "oracle",
1040
1044
  "writable": true
1045
+ },
1046
+ {
1047
+ "name": "authority",
1048
+ "signer": true
1049
+ },
1050
+ {
1051
+ "name": "state"
1041
1052
  }
1042
1053
  ],
1043
1054
  "args": [
@@ -1294,10 +1305,7 @@
1294
1305
  },
1295
1306
  {
1296
1307
  "name": "authority",
1297
- "signer": true,
1298
- "relations": [
1299
- "queue"
1300
- ]
1308
+ "signer": true
1301
1309
  },
1302
1310
  {
1303
1311
  "name": "program_authority"
@@ -1336,10 +1344,7 @@
1336
1344
  },
1337
1345
  {
1338
1346
  "name": "authority",
1339
- "signer": true,
1340
- "relations": [
1341
- "queue"
1342
- ]
1347
+ "signer": true
1343
1348
  },
1344
1349
  {
1345
1350
  "name": "state"
@@ -1387,6 +1392,13 @@
1387
1392
  {
1388
1393
  "name": "recent_slothashes",
1389
1394
  "address": "SysvarS1otHashes111111111111111111111111111"
1395
+ },
1396
+ {
1397
+ "name": "authority",
1398
+ "signer": true,
1399
+ "relations": [
1400
+ "randomness"
1401
+ ]
1390
1402
  }
1391
1403
  ],
1392
1404
  "args": [
@@ -1860,6 +1872,19 @@
1860
1872
  }
1861
1873
  ],
1862
1874
  "events": [
1875
+ {
1876
+ "name": "CostWhitelistEvent",
1877
+ "discriminator": [
1878
+ 56,
1879
+ 107,
1880
+ 191,
1881
+ 127,
1882
+ 116,
1883
+ 6,
1884
+ 138,
1885
+ 149
1886
+ ]
1887
+ },
1863
1888
  {
1864
1889
  "name": "GarbageCollectionEvent",
1865
1890
  "discriminator": [
@@ -2235,9 +2260,84 @@
2235
2260
  {
2236
2261
  "code": 6047,
2237
2262
  "name": "InvalidInstruction"
2263
+ },
2264
+ {
2265
+ "code": 6048,
2266
+ "name": "OracleAlreadyVerified"
2267
+ },
2268
+ {
2269
+ "code": 6049,
2270
+ "name": "GuardianNotVerified"
2271
+ },
2272
+ {
2273
+ "code": 6050,
2274
+ "name": "InvalidConstraint"
2275
+ },
2276
+ {
2277
+ "code": 6051,
2278
+ "name": "InvalidDelegationGroup"
2279
+ },
2280
+ {
2281
+ "code": 6052,
2282
+ "name": "OracleKeyNotFound"
2283
+ },
2284
+ {
2285
+ "code": 6053,
2286
+ "name": "GuardianReregisterAttempt"
2238
2287
  }
2239
2288
  ],
2240
2289
  "types": [
2290
+ {
2291
+ "name": "CompactResult",
2292
+ "serialization": "bytemuck",
2293
+ "repr": {
2294
+ "kind": "c"
2295
+ },
2296
+ "type": {
2297
+ "kind": "struct",
2298
+ "fields": [
2299
+ {
2300
+ "name": "std_dev",
2301
+ "docs": [
2302
+ "The standard deviation of the submissions needed for quorom size"
2303
+ ],
2304
+ "type": "f32"
2305
+ },
2306
+ {
2307
+ "name": "mean",
2308
+ "docs": [
2309
+ "The mean of the submissions needed for quorom size"
2310
+ ],
2311
+ "type": "f32"
2312
+ },
2313
+ {
2314
+ "name": "slot",
2315
+ "docs": [
2316
+ "The slot at which this value was signed."
2317
+ ],
2318
+ "type": "u64"
2319
+ }
2320
+ ]
2321
+ }
2322
+ },
2323
+ {
2324
+ "name": "CostWhitelistEvent",
2325
+ "type": {
2326
+ "kind": "struct",
2327
+ "fields": [
2328
+ {
2329
+ "name": "feeds",
2330
+ "type": {
2331
+ "vec": "pubkey"
2332
+ }
2333
+ },
2334
+ {
2335
+ "name": "reward",
2336
+ "type": "u32"
2337
+ }
2338
+ ]
2339
+ }
2340
+ },
2241
2341
  {
2242
2342
  "name": "CurrentResult",
2243
2343
  "serialization": "bytemuck",
@@ -2384,7 +2484,7 @@
2384
2484
  }
2385
2485
  },
2386
2486
  {
2387
- "name": "idx",
2487
+ "name": "_reserved1",
2388
2488
  "type": "u32"
2389
2489
  },
2390
2490
  {
@@ -2437,12 +2537,7 @@
2437
2537
  "name": "GuardianUnregisterParams",
2438
2538
  "type": {
2439
2539
  "kind": "struct",
2440
- "fields": [
2441
- {
2442
- "name": "idx",
2443
- "type": "u32"
2444
- }
2445
- ]
2540
+ "fields": []
2446
2541
  }
2447
2542
  },
2448
2543
  {
@@ -2948,7 +3043,7 @@
2948
3043
  "kind": "struct",
2949
3044
  "fields": [
2950
3045
  {
2951
- "name": "recent_slot",
3046
+ "name": "_reserved1",
2952
3047
  "type": "u64"
2953
3048
  }
2954
3049
  ]
@@ -3068,14 +3163,18 @@
3068
3163
  }
3069
3164
  },
3070
3165
  {
3071
- "name": "_padding1",
3166
+ "name": "padding1",
3072
3167
  "type": {
3073
3168
  "array": [
3074
3169
  "u8",
3075
- 3
3170
+ 2
3076
3171
  ]
3077
3172
  }
3078
3173
  },
3174
+ {
3175
+ "name": "historical_result_idx",
3176
+ "type": "u8"
3177
+ },
3079
3178
  {
3080
3179
  "name": "min_sample_size",
3081
3180
  "type": "u8"
@@ -3089,7 +3188,7 @@
3089
3188
  "type": "u64"
3090
3189
  },
3091
3190
  {
3092
- "name": "ipfs_hash",
3191
+ "name": "_reserved1",
3093
3192
  "type": {
3094
3193
  "array": [
3095
3194
  "u8",
@@ -3110,38 +3209,51 @@
3110
3209
  "type": "u32"
3111
3210
  },
3112
3211
  {
3113
- "name": "_ebuf4",
3212
+ "name": "padding2",
3114
3213
  "type": {
3115
3214
  "array": [
3116
3215
  "u8",
3117
- 20
3216
+ 12
3118
3217
  ]
3119
3218
  }
3120
3219
  },
3121
3220
  {
3122
- "name": "_ebuf3",
3221
+ "name": "historical_results",
3222
+ "type": {
3223
+ "array": [
3224
+ {
3225
+ "defined": {
3226
+ "name": "CompactResult"
3227
+ }
3228
+ },
3229
+ 32
3230
+ ]
3231
+ }
3232
+ },
3233
+ {
3234
+ "name": "_ebuf4",
3123
3235
  "type": {
3124
3236
  "array": [
3125
3237
  "u8",
3126
- 24
3238
+ 8
3127
3239
  ]
3128
3240
  }
3129
3241
  },
3130
3242
  {
3131
- "name": "_ebuf2",
3243
+ "name": "_ebuf3",
3132
3244
  "type": {
3133
3245
  "array": [
3134
3246
  "u8",
3135
- 256
3247
+ 24
3136
3248
  ]
3137
3249
  }
3138
3250
  },
3139
3251
  {
3140
- "name": "_ebuf1",
3252
+ "name": "_ebuf2",
3141
3253
  "type": {
3142
3254
  "array": [
3143
3255
  "u8",
3144
- 512
3256
+ 256
3145
3257
  ]
3146
3258
  }
3147
3259
  }
@@ -4237,12 +4349,43 @@
4237
4349
  "name": "lut_slot",
4238
4350
  "type": "u64"
4239
4351
  },
4352
+ {
4353
+ "name": "base_reward",
4354
+ "type": "u32"
4355
+ },
4356
+ {
4357
+ "name": "_ebuf6",
4358
+ "type": {
4359
+ "array": [
4360
+ "u8",
4361
+ 28
4362
+ ]
4363
+ }
4364
+ },
4365
+ {
4366
+ "name": "_ebuf5",
4367
+ "type": {
4368
+ "array": [
4369
+ "u8",
4370
+ 32
4371
+ ]
4372
+ }
4373
+ },
4374
+ {
4375
+ "name": "_ebuf4",
4376
+ "type": {
4377
+ "array": [
4378
+ "u8",
4379
+ 64
4380
+ ]
4381
+ }
4382
+ },
4240
4383
  {
4241
4384
  "name": "_ebuf3",
4242
4385
  "type": {
4243
4386
  "array": [
4244
4387
  "u8",
4245
- 256
4388
+ 128
4246
4389
  ]
4247
4390
  }
4248
4391
  },
@@ -4256,11 +4399,14 @@
4256
4399
  }
4257
4400
  },
4258
4401
  {
4259
- "name": "_ebuf1",
4402
+ "name": "cost_whitelist",
4403
+ "docs": [
4404
+ "Cost whitelist by authority"
4405
+ ],
4260
4406
  "type": {
4261
4407
  "array": [
4262
- "u8",
4263
- 1024
4408
+ "pubkey",
4409
+ 32
4264
4410
  ]
4265
4411
  }
4266
4412
  }
@@ -4346,6 +4492,18 @@
4346
4492
  {
4347
4493
  "name": "subsidy_amount",
4348
4494
  "type": "u32"
4495
+ },
4496
+ {
4497
+ "name": "base_reward",
4498
+ "type": "u32"
4499
+ },
4500
+ {
4501
+ "name": "add_cost_wl",
4502
+ "type": "pubkey"
4503
+ },
4504
+ {
4505
+ "name": "rm_cost_wl",
4506
+ "type": "pubkey"
4349
4507
  }
4350
4508
  ]
4351
4509
  }
@@ -187,20 +187,20 @@ export function calculateWorstCasePerpLiabilityValue(
187
187
 
188
188
  export function calculatePerpLiabilityValue(
189
189
  baseAssetAmount: BN,
190
- oraclePrice: BN,
190
+ price: BN,
191
191
  isPredictionMarket: boolean
192
192
  ): BN {
193
193
  if (isPredictionMarket) {
194
194
  if (baseAssetAmount.gt(ZERO)) {
195
- return baseAssetAmount.mul(oraclePrice).div(BASE_PRECISION);
195
+ return baseAssetAmount.mul(price).div(BASE_PRECISION);
196
196
  } else {
197
197
  return baseAssetAmount
198
198
  .abs()
199
- .mul(MAX_PREDICTION_PRICE.sub(oraclePrice))
199
+ .mul(MAX_PREDICTION_PRICE.sub(price))
200
200
  .div(BASE_PRECISION);
201
201
  }
202
202
  } else {
203
- return baseAssetAmount.abs().mul(oraclePrice).div(BASE_PRECISION);
203
+ return baseAssetAmount.abs().mul(price).div(BASE_PRECISION);
204
204
  }
205
205
  }
206
206
 
@@ -214,16 +214,19 @@ export function calculateMarginUSDCRequiredForTrade(
214
214
  driftClient: DriftClient,
215
215
  targetMarketIndex: number,
216
216
  baseSize: BN,
217
- userMaxMarginRatio?: number
217
+ userMaxMarginRatio?: number,
218
+ userHighLeverageMode?: boolean,
219
+ entryPrice?: BN
218
220
  ): BN {
219
221
  const targetMarket = driftClient.getPerpMarketAccount(targetMarketIndex);
220
- const oracleData = driftClient.getOracleDataForPerpMarket(
221
- targetMarket.marketIndex
222
- );
222
+
223
+ const price =
224
+ entryPrice ??
225
+ driftClient.getOracleDataForPerpMarket(targetMarket.marketIndex).price;
223
226
 
224
227
  const perpLiabilityValue = calculatePerpLiabilityValue(
225
228
  baseSize,
226
- oracleData.price,
229
+ price,
227
230
  isVariant(targetMarket.contractType, 'prediction')
228
231
  );
229
232
 
@@ -232,7 +235,8 @@ export function calculateMarginUSDCRequiredForTrade(
232
235
  targetMarket,
233
236
  baseSize.abs(),
234
237
  'Initial',
235
- userMaxMarginRatio
238
+ userMaxMarginRatio,
239
+ userHighLeverageMode
236
240
  )
237
241
  )
238
242
  .mul(perpLiabilityValue)
@@ -251,13 +255,17 @@ export function calculateCollateralDepositRequiredForTrade(
251
255
  targetMarketIndex: number,
252
256
  baseSize: BN,
253
257
  collateralIndex: number,
254
- userMaxMarginRatio?: number
258
+ userMaxMarginRatio?: number,
259
+ userHighLeverageMode?: boolean,
260
+ estEntryPrice?: BN
255
261
  ): BN {
256
262
  const marginRequiredUsdc = calculateMarginUSDCRequiredForTrade(
257
263
  driftClient,
258
264
  targetMarketIndex,
259
265
  baseSize,
260
- userMaxMarginRatio
266
+ userMaxMarginRatio,
267
+ userHighLeverageMode,
268
+ estEntryPrice
261
269
  );
262
270
 
263
271
  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 = {
@@ -906,7 +914,7 @@ export type UserStatsAccount = {
906
914
  current_epoch_referrer_reward: BN;
907
915
  };
908
916
  referrer: PublicKey;
909
- isReferrer: boolean;
917
+ referrerStatus: boolean;
910
918
  authority: PublicKey;
911
919
  ifStakedQuoteAssetAmount: BN;
912
920
 
@@ -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 = {
@@ -1093,6 +1102,11 @@ export type ReferrerInfo = {
1093
1102
  referrerStats: PublicKey;
1094
1103
  };
1095
1104
 
1105
+ export enum ReferrerStatus {
1106
+ IsReferrer = 1,
1107
+ IsReferred = 2,
1108
+ }
1109
+
1096
1110
  export enum PlaceAndTakeOrderSuccessCondition {
1097
1111
  PartialFill = 1,
1098
1112
  FullFill = 2,
@@ -1301,3 +1315,9 @@ export type SignedTxData = {
1301
1315
  lastValidBlockHeight?: number;
1302
1316
  blockHash: string;
1303
1317
  };
1318
+
1319
+ export type HighLeverageModeConfig = {
1320
+ maxUsers: number;
1321
+ currentUsers: number;
1322
+ reduceOnly: boolean;
1323
+ };
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)
@@ -2602,13 +2613,16 @@ export class User {
2602
2613
 
2603
2614
  public getMarginUSDCRequiredForTrade(
2604
2615
  targetMarketIndex: number,
2605
- baseSize: BN
2616
+ baseSize: BN,
2617
+ estEntryPrice?: BN
2606
2618
  ): BN {
2607
2619
  return calculateMarginUSDCRequiredForTrade(
2608
2620
  this.driftClient,
2609
2621
  targetMarketIndex,
2610
2622
  baseSize,
2611
- this.getUserAccount().maxMarginRatio
2623
+ this.getUserAccount().maxMarginRatio,
2624
+ undefined,
2625
+ estEntryPrice
2612
2626
  );
2613
2627
  }
2614
2628
 
@@ -2622,7 +2636,8 @@ export class User {
2622
2636
  targetMarketIndex,
2623
2637
  baseSize,
2624
2638
  collateralIndex,
2625
- this.getUserAccount().maxMarginRatio
2639
+ this.getUserAccount().maxMarginRatio,
2640
+ false // assume user cant be high leverage if they havent created user account ?
2626
2641
  );
2627
2642
  }
2628
2643
 
@@ -3771,7 +3786,8 @@ export class User {
3771
3786
  perpMarket,
3772
3787
  worstCaseBaseAmount.abs(),
3773
3788
  marginCategory,
3774
- this.getUserAccount().maxMarginRatio
3789
+ this.getUserAccount().maxMarginRatio,
3790
+ this.isHighLeverageMode()
3775
3791
  )
3776
3792
  );
3777
3793