@drift-labs/sdk 2.97.0-beta.6 → 2.97.0-beta.8

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.
package/VERSION CHANGED
@@ -1 +1 @@
1
- 2.97.0-beta.6
1
+ 2.97.0-beta.8
@@ -26,3 +26,4 @@ export declare function getProtocolIfSharesTransferConfigPublicKey(programId: Pu
26
26
  export declare function getPrelaunchOraclePublicKey(programId: PublicKey, marketIndex: number): PublicKey;
27
27
  export declare function getPythPullOraclePublicKey(progarmId: PublicKey, feedId: Uint8Array): PublicKey;
28
28
  export declare function getTokenProgramForSpotMarket(spotMarketAccount: SpotMarketAccount): PublicKey;
29
+ export declare function getHighLeverageModeConfigPublicKey(programId: PublicKey): PublicKey;
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.getTokenProgramForSpotMarket = exports.getPythPullOraclePublicKey = exports.getPrelaunchOraclePublicKey = exports.getProtocolIfSharesTransferConfigPublicKey = exports.getReferrerNamePublicKeySync = exports.getOpenbookV2FulfillmentConfigPublicKey = exports.getPhoenixFulfillmentConfigPublicKey = exports.getSerumFulfillmentConfigPublicKey = exports.getSerumSignerPublicKey = exports.getSerumOpenOrdersPublicKey = exports.getDriftSignerPublicKey = exports.getInsuranceFundStakeAccountPublicKey = exports.getInsuranceFundVaultPublicKey = exports.getSpotMarketVaultPublicKey = exports.getSpotMarketPublicKeySync = exports.getSpotMarketPublicKey = exports.getPerpMarketPublicKeySync = exports.getPerpMarketPublicKey = exports.getUserStatsAccountPublicKey = exports.getUserAccountPublicKeySync = exports.getUserAccountPublicKey = exports.getUserAccountPublicKeyAndNonce = exports.getDriftStateAccountPublicKey = exports.getDriftStateAccountPublicKeyAndNonce = void 0;
26
+ exports.getHighLeverageModeConfigPublicKey = exports.getTokenProgramForSpotMarket = exports.getPythPullOraclePublicKey = exports.getPrelaunchOraclePublicKey = exports.getProtocolIfSharesTransferConfigPublicKey = exports.getReferrerNamePublicKeySync = exports.getOpenbookV2FulfillmentConfigPublicKey = exports.getPhoenixFulfillmentConfigPublicKey = exports.getSerumFulfillmentConfigPublicKey = exports.getSerumSignerPublicKey = exports.getSerumOpenOrdersPublicKey = exports.getDriftSignerPublicKey = exports.getInsuranceFundStakeAccountPublicKey = exports.getInsuranceFundVaultPublicKey = exports.getSpotMarketVaultPublicKey = exports.getSpotMarketPublicKeySync = exports.getSpotMarketPublicKey = exports.getPerpMarketPublicKeySync = exports.getPerpMarketPublicKey = exports.getUserStatsAccountPublicKey = exports.getUserAccountPublicKeySync = exports.getUserAccountPublicKey = exports.getUserAccountPublicKeyAndNonce = exports.getDriftStateAccountPublicKey = exports.getDriftStateAccountPublicKeyAndNonce = void 0;
27
27
  const web3_js_1 = require("@solana/web3.js");
28
28
  const anchor = __importStar(require("@coral-xyz/anchor"));
29
29
  const spl_token_1 = require("@solana/spl-token");
@@ -180,3 +180,7 @@ function getTokenProgramForSpotMarket(spotMarketAccount) {
180
180
  return spl_token_1.TOKEN_PROGRAM_ID;
181
181
  }
182
182
  exports.getTokenProgramForSpotMarket = getTokenProgramForSpotMarket;
183
+ function getHighLeverageModeConfigPublicKey(programId) {
184
+ return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from(anchor.utils.bytes.utf8.encode('high_leverage_mode_config'))], programId)[0];
185
+ }
186
+ exports.getHighLeverageModeConfigPublicKey = getHighLeverageModeConfigPublicKey;
@@ -55,6 +55,8 @@ export declare class AdminClient extends DriftClient {
55
55
  getUpdatePerpMarketTargetBaseAssetAmountPerLpIx(perpMarketIndex: number, targetBaseAssetAmountPerLP: number): Promise<TransactionInstruction>;
56
56
  updatePerpMarketMarginRatio(perpMarketIndex: number, marginRatioInitial: number, marginRatioMaintenance: number): Promise<TransactionSignature>;
57
57
  getUpdatePerpMarketMarginRatioIx(perpMarketIndex: number, marginRatioInitial: number, marginRatioMaintenance: number): Promise<TransactionInstruction>;
58
+ updatePerpMarketHighLeverageMarginRatio(perpMarketIndex: number, marginRatioInitial: number, marginRatioMaintenance: number): Promise<TransactionSignature>;
59
+ getUpdatePerpMarketHighLeverageMarginRatioIx(perpMarketIndex: number, marginRatioInitial: number, marginRatioMaintenance: number): Promise<TransactionInstruction>;
58
60
  updatePerpMarketImfFactor(perpMarketIndex: number, imfFactor: number, unrealizedPnlImfFactor: number): Promise<TransactionSignature>;
59
61
  getUpdatePerpMarketImfFactorIx(perpMarketIndex: number, imfFactor: number, unrealizedPnlImfFactor: number): Promise<TransactionInstruction>;
60
62
  updatePerpMarketBaseSpread(perpMarketIndex: number, baseSpread: number): Promise<TransactionSignature>;
@@ -193,4 +195,8 @@ export declare class AdminClient extends DriftClient {
193
195
  getInitUserFuelIx(user: PublicKey, authority: PublicKey, fuelBonusDeposits?: number, fuelBonusBorrows?: number, fuelBonusTaker?: number, fuelBonusMaker?: number, fuelBonusInsurance?: number): Promise<TransactionInstruction>;
194
196
  initializePythPullOracle(feedId: string): Promise<TransactionSignature>;
195
197
  getInitializePythPullOracleIx(feedId: string): Promise<TransactionInstruction>;
198
+ initializeHighLeverageModeConfig(maxUsers: number): Promise<TransactionSignature>;
199
+ getInitializeHighLeverageModeConfigIx(maxUsers: number): Promise<TransactionInstruction>;
200
+ updateUpdateHighLeverageModeConfig(maxUsers: number, reduceOnly: boolean): Promise<TransactionSignature>;
201
+ getUpdateHighLeverageModeConfigIx(maxUsers: number, reduceOnly: boolean): Promise<TransactionInstruction>;
196
202
  }
@@ -561,6 +561,23 @@ class AdminClient extends driftClient_1.DriftClient {
561
561
  },
562
562
  });
563
563
  }
564
+ async updatePerpMarketHighLeverageMarginRatio(perpMarketIndex, marginRatioInitial, marginRatioMaintenance) {
565
+ const updatePerpMarketHighLeverageMarginRatioIx = await this.getUpdatePerpMarketHighLeverageMarginRatioIx(perpMarketIndex, marginRatioInitial, marginRatioMaintenance);
566
+ const tx = await this.buildTransaction(updatePerpMarketHighLeverageMarginRatioIx);
567
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
568
+ return txSig;
569
+ }
570
+ async getUpdatePerpMarketHighLeverageMarginRatioIx(perpMarketIndex, marginRatioInitial, marginRatioMaintenance) {
571
+ return await this.program.instruction.updatePerpMarketHighLeverageMarginRatio(marginRatioInitial, marginRatioMaintenance, {
572
+ accounts: {
573
+ admin: this.isSubscribed
574
+ ? this.getStateAccount().admin
575
+ : this.wallet.publicKey,
576
+ state: await this.getStatePublicKey(),
577
+ perpMarket: await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex),
578
+ },
579
+ });
580
+ }
564
581
  async updatePerpMarketImfFactor(perpMarketIndex, imfFactor, unrealizedPnlImfFactor) {
565
582
  const updatePerpMarketImfFactorIx = await this.getUpdatePerpMarketImfFactorIx(perpMarketIndex, imfFactor, unrealizedPnlImfFactor);
566
583
  const tx = await this.buildTransaction(updatePerpMarketImfFactorIx);
@@ -1769,5 +1786,41 @@ class AdminClient extends driftClient_1.DriftClient {
1769
1786
  },
1770
1787
  });
1771
1788
  }
1789
+ async initializeHighLeverageModeConfig(maxUsers) {
1790
+ const initializeHighLeverageModeConfigIx = await this.getInitializeHighLeverageModeConfigIx(maxUsers);
1791
+ const tx = await this.buildTransaction(initializeHighLeverageModeConfigIx);
1792
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
1793
+ return txSig;
1794
+ }
1795
+ async getInitializeHighLeverageModeConfigIx(maxUsers) {
1796
+ return await this.program.instruction.initializeHighLeverageModeConfig(maxUsers, {
1797
+ accounts: {
1798
+ admin: this.isSubscribed
1799
+ ? this.getStateAccount().admin
1800
+ : this.wallet.publicKey,
1801
+ state: await this.getStatePublicKey(),
1802
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY,
1803
+ systemProgram: anchor.web3.SystemProgram.programId,
1804
+ highLeverageModeConfig: (0, pda_1.getHighLeverageModeConfigPublicKey)(this.program.programId),
1805
+ },
1806
+ });
1807
+ }
1808
+ async updateUpdateHighLeverageModeConfig(maxUsers, reduceOnly) {
1809
+ const updateHighLeverageModeConfigIx = await this.getUpdateHighLeverageModeConfigIx(maxUsers, reduceOnly);
1810
+ const tx = await this.buildTransaction(updateHighLeverageModeConfigIx);
1811
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
1812
+ return txSig;
1813
+ }
1814
+ async getUpdateHighLeverageModeConfigIx(maxUsers, reduceOnly) {
1815
+ return await this.program.instruction.updateHighLeverageModeConfig(maxUsers, reduceOnly, {
1816
+ accounts: {
1817
+ admin: this.isSubscribed
1818
+ ? this.getStateAccount().admin
1819
+ : this.wallet.publicKey,
1820
+ state: await this.getStatePublicKey(),
1821
+ highLeverageModeConfig: (0, pda_1.getHighLeverageModeConfigPublicKey)(this.program.programId),
1822
+ },
1823
+ });
1824
+ }
1772
1825
  }
1773
1826
  exports.AdminClient = AdminClient;
@@ -789,6 +789,10 @@ export declare class DriftClient {
789
789
  getPostSwitchboardOnDemandUpdateAtomicIx(feed: PublicKey, numSignatures?: number): Promise<TransactionInstruction | undefined>;
790
790
  postSwitchboardOnDemandUpdate(feed: PublicKey, numSignatures?: number): Promise<TransactionSignature>;
791
791
  private getBuildEncodedVaaIxs;
792
+ enableUserHighLeverageMode(subAccountId: number, txParams?: TxParams): Promise<TransactionSignature>;
793
+ getEnableHighLeverageModeIx(subAccountId: number): Promise<anchor.web3.TransactionInstruction>;
794
+ disableUserHighLeverageMode(user: PublicKey, txParams?: TxParams): Promise<TransactionSignature>;
795
+ getDisableHighLeverageModeIx(user: PublicKey): Promise<anchor.web3.TransactionInstruction>;
792
796
  private handleSignedTransaction;
793
797
  private handlePreSignedTransaction;
794
798
  private isVersionedTransaction;
@@ -4254,6 +4254,36 @@ class DriftClient {
4254
4254
  .instruction());
4255
4255
  return [postIxs, encodedVaaKeypair];
4256
4256
  }
4257
+ async enableUserHighLeverageMode(subAccountId, txParams) {
4258
+ const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getEnableHighLeverageModeIx(subAccountId), txParams), [], this.opts);
4259
+ return txSig;
4260
+ }
4261
+ async getEnableHighLeverageModeIx(subAccountId) {
4262
+ const ix = await this.program.instruction.enableHighLeverageMode(subAccountId, {
4263
+ accounts: {
4264
+ state: await this.getStatePublicKey(),
4265
+ user: (0, pda_1.getUserAccountPublicKeySync)(this.program.programId, this.wallet.publicKey, subAccountId),
4266
+ authority: this.wallet.publicKey,
4267
+ highLeverageModeConfig: (0, pda_1.getHighLeverageModeConfigPublicKey)(this.program.programId),
4268
+ },
4269
+ });
4270
+ return ix;
4271
+ }
4272
+ async disableUserHighLeverageMode(user, txParams) {
4273
+ const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getDisableHighLeverageModeIx(user), txParams), [], this.opts);
4274
+ return txSig;
4275
+ }
4276
+ async getDisableHighLeverageModeIx(user) {
4277
+ const ix = await this.program.instruction.disableHighLeverageMode({
4278
+ accounts: {
4279
+ state: await this.getStatePublicKey(),
4280
+ user,
4281
+ authority: this.wallet.publicKey,
4282
+ highLeverageModeConfig: (0, pda_1.getHighLeverageModeConfigPublicKey)(this.program.programId),
4283
+ },
4284
+ });
4285
+ return ix;
4286
+ }
4257
4287
  handleSignedTransaction(signedTxs) {
4258
4288
  if (this.enableMetricsEvents && this.metricsEventEmitter) {
4259
4289
  this.metricsEventEmitter.emit('txSigned', signedTxs);
@@ -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,32 @@
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
+ },
1501
1558
  {
1502
1559
  "name": "updateUserFuelBonus",
1503
1560
  "accounts": [
@@ -3987,6 +4044,36 @@
3987
4044
  }
3988
4045
  ]
3989
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
+ },
3990
4077
  {
3991
4078
  "name": "updatePerpMarketFundingPeriod",
3992
4079
  "accounts": [
@@ -5952,6 +6039,72 @@
5952
6039
  }
5953
6040
  }
5954
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
+ ]
5955
6108
  }
5956
6109
  ],
5957
6110
  "accounts": [
@@ -6158,6 +6311,35 @@
6158
6311
  ]
6159
6312
  }
6160
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
+ },
6161
6343
  {
6162
6344
  "name": "InsuranceFundStake",
6163
6345
  "type": {
@@ -6546,12 +6728,24 @@
6546
6728
  ],
6547
6729
  "type": "u8"
6548
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
+ },
6549
6743
  {
6550
6744
  "name": "padding",
6551
6745
  "type": {
6552
6746
  "array": [
6553
6747
  "u8",
6554
- 43
6748
+ 38
6555
6749
  ]
6556
6750
  }
6557
6751
  }
@@ -7401,12 +7595,18 @@
7401
7595
  ],
7402
7596
  "type": "bool"
7403
7597
  },
7598
+ {
7599
+ "name": "marginMode",
7600
+ "type": {
7601
+ "defined": "MarginMode"
7602
+ }
7603
+ },
7404
7604
  {
7405
7605
  "name": "padding1",
7406
7606
  "type": {
7407
7607
  "array": [
7408
7608
  "u8",
7409
- 5
7609
+ 4
7410
7610
  ]
7411
7611
  }
7412
7612
  },
@@ -10659,6 +10859,20 @@
10659
10859
  }
10660
10860
  ]
10661
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
+ }
10662
10876
  }
10663
10877
  ],
10664
10878
  "events": [
@@ -13152,6 +13366,11 @@
13152
13366
  "code": 6289,
13153
13367
  "name": "PlaceAndTakeOrderSuccessConditionFailed",
13154
13368
  "msg": "Place and take order success condition failed"
13369
+ },
13370
+ {
13371
+ "code": 6290,
13372
+ "name": "InvalidHighLeverageModeConfig",
13373
+ "msg": "Invalid High Leverage Mode Config"
13155
13374
  }
13156
13375
  ],
13157
13376
  "metadata": {
@@ -28,12 +28,12 @@ export declare function calculatePerpLiabilityValue(baseAssetAmount: BN, oracleP
28
28
  * @param baseSize
29
29
  * @returns
30
30
  */
31
- export declare function calculateMarginUSDCRequiredForTrade(driftClient: DriftClient, targetMarketIndex: number, baseSize: BN, userMaxMarginRatio?: number): BN;
31
+ export declare function calculateMarginUSDCRequiredForTrade(driftClient: DriftClient, targetMarketIndex: number, baseSize: BN, userMaxMarginRatio?: number, userHighLeverageMode?: boolean): BN;
32
32
  /**
33
33
  * Similar to calculatetMarginUSDCRequiredForTrade, but calculates how much of a given collateral is required to cover the margin requirements for a given trade. Basically does the same thing as getMarginUSDCRequiredForTrade but also accounts for asset weight of the selected collateral.
34
34
  *
35
35
  * Returns collateral required in the precision of the target collateral market.
36
36
  */
37
- export declare function calculateCollateralDepositRequiredForTrade(driftClient: DriftClient, targetMarketIndex: number, baseSize: BN, collateralIndex: number, userMaxMarginRatio?: number): BN;
37
+ export declare function calculateCollateralDepositRequiredForTrade(driftClient: DriftClient, targetMarketIndex: number, baseSize: BN, collateralIndex: number, userMaxMarginRatio?: number, userHighLeverageMode?: boolean): BN;
38
38
  export declare function calculateCollateralValueOfDeposit(driftClient: DriftClient, collateralIndex: number, baseSize: BN): BN;
39
39
  export declare function calculateLiquidationPrice(freeCollateral: BN, freeCollateralDelta: BN, oraclePrice: BN): BN;
@@ -123,11 +123,11 @@ exports.calculatePerpLiabilityValue = calculatePerpLiabilityValue;
123
123
  * @param baseSize
124
124
  * @returns
125
125
  */
126
- function calculateMarginUSDCRequiredForTrade(driftClient, targetMarketIndex, baseSize, userMaxMarginRatio) {
126
+ function calculateMarginUSDCRequiredForTrade(driftClient, targetMarketIndex, baseSize, userMaxMarginRatio, userHighLeverageMode) {
127
127
  const targetMarket = driftClient.getPerpMarketAccount(targetMarketIndex);
128
128
  const oracleData = driftClient.getOracleDataForPerpMarket(targetMarket.marketIndex);
129
129
  const perpLiabilityValue = calculatePerpLiabilityValue(baseSize, oracleData.price, (0, types_1.isVariant)(targetMarket.contractType, 'prediction'));
130
- const marginRequired = new anchor_1.BN((0, __1.calculateMarketMarginRatio)(targetMarket, baseSize.abs(), 'Initial', userMaxMarginRatio))
130
+ const marginRequired = new anchor_1.BN((0, __1.calculateMarketMarginRatio)(targetMarket, baseSize.abs(), 'Initial', userMaxMarginRatio, userHighLeverageMode))
131
131
  .mul(perpLiabilityValue)
132
132
  .div(numericConstants_1.MARGIN_PRECISION);
133
133
  return marginRequired;
@@ -138,8 +138,8 @@ exports.calculateMarginUSDCRequiredForTrade = calculateMarginUSDCRequiredForTrad
138
138
  *
139
139
  * Returns collateral required in the precision of the target collateral market.
140
140
  */
141
- function calculateCollateralDepositRequiredForTrade(driftClient, targetMarketIndex, baseSize, collateralIndex, userMaxMarginRatio) {
142
- const marginRequiredUsdc = calculateMarginUSDCRequiredForTrade(driftClient, targetMarketIndex, baseSize, userMaxMarginRatio);
141
+ function calculateCollateralDepositRequiredForTrade(driftClient, targetMarketIndex, baseSize, collateralIndex, userMaxMarginRatio, userHighLeverageMode) {
142
+ const marginRequiredUsdc = calculateMarginUSDCRequiredForTrade(driftClient, targetMarketIndex, baseSize, userMaxMarginRatio, userHighLeverageMode);
143
143
  const collateralMarket = driftClient.getSpotMarketAccount(collateralIndex);
144
144
  const collateralOracleData = driftClient.getOracleDataForSpotMarket(collateralIndex);
145
145
  const scaledAssetWeight = (0, __1.calculateScaledInitialAssetWeight)(collateralMarket, collateralOracleData.price);
@@ -27,7 +27,7 @@ export declare function calculateAskPrice(market: PerpMarketAccount, oraclePrice
27
27
  export declare function calculateNewMarketAfterTrade(baseAssetAmount: BN, direction: PositionDirection, market: PerpMarketAccount): PerpMarketAccount;
28
28
  export declare function calculateOracleReserveSpread(market: PerpMarketAccount, oraclePriceData: OraclePriceData): BN;
29
29
  export declare function calculateOracleSpread(price: BN, oraclePriceData: OraclePriceData): BN;
30
- export declare function calculateMarketMarginRatio(market: PerpMarketAccount, size: BN, marginCategory: MarginCategory, customMarginRatio?: number): number;
30
+ export declare function calculateMarketMarginRatio(market: PerpMarketAccount, size: BN, marginCategory: MarginCategory, customMarginRatio?: number, userHighLeverageMode?: boolean): number;
31
31
  export declare function calculateUnrealizedAssetWeight(market: PerpMarketAccount, quoteSpotMarket: SpotMarketAccount, unrealizedPnl: BN, marginCategory: MarginCategory, oraclePriceData: OraclePriceData): BN;
32
32
  export declare function calculateMarketAvailablePNL(perpMarket: PerpMarketAccount, spotMarket: SpotMarketAccount): BN;
33
33
  export declare function calculateMarketMaxAvailableInsurance(perpMarket: PerpMarketAccount, spotMarket: SpotMarketAccount): BN;
@@ -60,16 +60,28 @@ function calculateOracleSpread(price, oraclePriceData) {
60
60
  return price.sub(oraclePriceData.price);
61
61
  }
62
62
  exports.calculateOracleSpread = calculateOracleSpread;
63
- function calculateMarketMarginRatio(market, size, marginCategory, customMarginRatio = 0) {
63
+ function calculateMarketMarginRatio(market, size, marginCategory, customMarginRatio = 0, userHighLeverageMode = false) {
64
+ let marginRationInitial;
65
+ let marginRatioMaintenance;
66
+ if (userHighLeverageMode &&
67
+ market.highLeverageMarginRatioInitial > 0 &&
68
+ market.highLeverageMarginRatioMaintenance) {
69
+ marginRationInitial = market.highLeverageMarginRatioInitial;
70
+ marginRatioMaintenance = market.highLeverageMarginRatioMaintenance;
71
+ }
72
+ else {
73
+ marginRationInitial = market.marginRatioInitial;
74
+ marginRatioMaintenance = market.marginRatioMaintenance;
75
+ }
64
76
  let marginRatio;
65
77
  switch (marginCategory) {
66
78
  case 'Initial': {
67
79
  // use lowest leverage between max allowed and optional user custom max
68
- marginRatio = Math.max((0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(market.marginRatioInitial), numericConstants_1.MARGIN_PRECISION).toNumber(), customMarginRatio);
80
+ marginRatio = Math.max((0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(marginRationInitial), numericConstants_1.MARGIN_PRECISION).toNumber(), customMarginRatio);
69
81
  break;
70
82
  }
71
83
  case 'Maintenance': {
72
- marginRatio = (0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(market.marginRatioMaintenance), numericConstants_1.MARGIN_PRECISION).toNumber();
84
+ marginRatio = (0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(marginRatioMaintenance), numericConstants_1.MARGIN_PRECISION).toNumber();
73
85
  break;
74
86
  }
75
87
  }
package/lib/types.d.ts CHANGED
@@ -71,6 +71,14 @@ export declare enum UserStatus {
71
71
  REDUCE_ONLY = 4,
72
72
  ADVANCED_LP = 8
73
73
  }
74
+ export declare class MarginMode {
75
+ static readonly DEFAULT: {
76
+ default: {};
77
+ };
78
+ static readonly HIGH_LEVERAGE: {
79
+ highLeverage: {};
80
+ };
81
+ }
74
82
  export declare class ContractType {
75
83
  static readonly PERPETUAL: {
76
84
  perpetual: {};
@@ -753,6 +761,8 @@ export type PerpMarketAccount = {
753
761
  fuelBoostTaker: number;
754
762
  fuelBoostMaker: number;
755
763
  fuelBoostPosition: number;
764
+ highLeverageMarginRatioInitial: number;
765
+ highLeverageMarginRatioMaintenance: number;
756
766
  };
757
767
  export type HistoricalOracleData = {
758
768
  lastOraclePrice: BN;
@@ -1004,6 +1014,7 @@ export type UserAccount = {
1004
1014
  openAuctions: number;
1005
1015
  hasOpenAuction: boolean;
1006
1016
  lastFuelBonusUpdateTs: number;
1017
+ marginMode: MarginMode;
1007
1018
  };
1008
1019
  export type SpotPosition = {
1009
1020
  marketIndex: number;
package/lib/types.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SwapReduceOnly = exports.PlaceAndTakeOrderSuccessCondition = exports.DefaultOrderParams = exports.ModifyOrderPolicy = exports.PostOnlyParams = exports.LiquidationType = exports.LPAction = exports.TradeSide = exports.getVariant = exports.isOneOfVariant = exports.isVariant = exports.SettlePnlMode = exports.StakeAction = exports.SpotFulfillmentConfigStatus = exports.SettlePnlExplanation = exports.DepositExplanation = exports.SpotFulfillmentStatus = exports.SpotFulfillmentType = exports.OrderTriggerCondition = exports.OrderActionExplanation = exports.OrderAction = exports.OrderStatus = exports.MarketType = exports.OrderType = exports.OracleSource = exports.DepositDirection = exports.PositionDirection = exports.SpotBalanceType = exports.SwapDirection = exports.AssetTier = exports.ContractTier = exports.ContractType = exports.UserStatus = exports.InsuranceFundOperation = exports.SpotOperation = exports.PerpOperation = exports.MarketStatus = exports.ExchangeStatus = void 0;
3
+ exports.SwapReduceOnly = exports.PlaceAndTakeOrderSuccessCondition = exports.DefaultOrderParams = exports.ModifyOrderPolicy = exports.PostOnlyParams = exports.LiquidationType = exports.LPAction = exports.TradeSide = exports.getVariant = exports.isOneOfVariant = exports.isVariant = exports.SettlePnlMode = exports.StakeAction = exports.SpotFulfillmentConfigStatus = exports.SettlePnlExplanation = exports.DepositExplanation = exports.SpotFulfillmentStatus = exports.SpotFulfillmentType = exports.OrderTriggerCondition = exports.OrderActionExplanation = exports.OrderAction = exports.OrderStatus = exports.MarketType = exports.OrderType = exports.OracleSource = exports.DepositDirection = exports.PositionDirection = exports.SpotBalanceType = exports.SwapDirection = exports.AssetTier = exports.ContractTier = exports.ContractType = exports.MarginMode = exports.UserStatus = exports.InsuranceFundOperation = exports.SpotOperation = exports.PerpOperation = exports.MarketStatus = exports.ExchangeStatus = void 0;
4
4
  const _1 = require(".");
5
5
  // # Utility Types / Enums / Constants
6
6
  var ExchangeStatus;
@@ -58,6 +58,11 @@ var UserStatus;
58
58
  UserStatus[UserStatus["REDUCE_ONLY"] = 4] = "REDUCE_ONLY";
59
59
  UserStatus[UserStatus["ADVANCED_LP"] = 8] = "ADVANCED_LP";
60
60
  })(UserStatus = exports.UserStatus || (exports.UserStatus = {}));
61
+ class MarginMode {
62
+ }
63
+ exports.MarginMode = MarginMode;
64
+ MarginMode.DEFAULT = { default: {} };
65
+ MarginMode.HIGH_LEVERAGE = { highLeverage: {} };
61
66
  class ContractType {
62
67
  }
63
68
  exports.ContractType = ContractType;
package/lib/user.d.ts CHANGED
@@ -247,6 +247,7 @@ export declare class User {
247
247
  isBeingLiquidated(): boolean;
248
248
  hasStatus(status: UserStatus): boolean;
249
249
  isBankrupt(): boolean;
250
+ isHighLeverageMode(): boolean;
250
251
  /**
251
252
  * Checks if any user position cumulative funding differs from respective market cumulative funding
252
253
  * @returns