@drift-labs/sdk 2.108.0-beta.16 → 2.108.0-beta.18

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.108.0-beta.16
1
+ 2.108.0-beta.18
@@ -119,7 +119,7 @@ export declare class User {
119
119
  /**
120
120
  * @returns The maintenance margin requirement in USDC. : QUOTE_PRECISION
121
121
  */
122
- getMaintenanceMarginRequirement(): BN;
122
+ getMaintenanceMarginRequirement(liquidationBuffer?: BN): BN;
123
123
  getActivePerpPositionsForUserAccount(userAccount: UserAccount): PerpPosition[];
124
124
  getActivePerpPositions(): PerpPosition[];
125
125
  getActivePerpPositionsAndSlot(): DataAndSlot<PerpPosition[]>;
@@ -130,7 +130,7 @@ export declare class User {
130
130
  * calculates unrealized position price pnl
131
131
  * @returns : Precision QUOTE_PRECISION
132
132
  */
133
- getUnrealizedPNL(withFunding?: boolean, marketIndex?: number, withWeightMarginCategory?: MarginCategory, strict?: boolean): BN;
133
+ getUnrealizedPNL(withFunding?: boolean, marketIndex?: number, withWeightMarginCategory?: MarginCategory, strict?: boolean, liquidationBuffer?: BN): BN;
134
134
  /**
135
135
  * calculates unrealized funding payment pnl
136
136
  * @returns : Precision QUOTE_PRECISION
@@ -158,7 +158,8 @@ export declare class User {
158
158
  * calculates TotalCollateral: collateral + unrealized pnl
159
159
  * @returns : Precision QUOTE_PRECISION
160
160
  */
161
- getTotalCollateral(marginCategory?: MarginCategory, strict?: boolean, includeOpenOrders?: boolean): BN;
161
+ getTotalCollateral(marginCategory?: MarginCategory, strict?: boolean, includeOpenOrders?: boolean, liquidationBuffer?: BN): BN;
162
+ getLiquidationBuffer(): BN | undefined;
162
163
  /**
163
164
  * calculates User Health by comparing total collateral and maint. margin requirement
164
165
  * @returns : number (value from [0, 100])
@@ -457,12 +457,7 @@ class User {
457
457
  /**
458
458
  * @returns The maintenance margin requirement in USDC. : QUOTE_PRECISION
459
459
  */
460
- getMaintenanceMarginRequirement() {
461
- // if user being liq'd, can continue to be liq'd until total collateral above the margin requirement plus buffer
462
- let liquidationBuffer = undefined;
463
- if (this.isBeingLiquidated()) {
464
- liquidationBuffer = new _1.BN(this.driftClient.getStateAccount().liquidationMarginBufferRatio);
465
- }
460
+ getMaintenanceMarginRequirement(liquidationBuffer) {
466
461
  return this.getMarginRequirement('Maintenance', liquidationBuffer);
467
462
  }
468
463
  getActivePerpPositionsForUserAccount(userAccount) {
@@ -502,7 +497,7 @@ class User {
502
497
  * calculates unrealized position price pnl
503
498
  * @returns : Precision QUOTE_PRECISION
504
499
  */
505
- getUnrealizedPNL(withFunding, marketIndex, withWeightMarginCategory, strict = false) {
500
+ getUnrealizedPNL(withFunding, marketIndex, withWeightMarginCategory, strict = false, liquidationBuffer) {
506
501
  return this.getActivePerpPositions()
507
502
  .filter((pos) => marketIndex !== undefined ? pos.marketIndex === marketIndex : true)
508
503
  .reduce((unrealizedPnl, perpPosition) => {
@@ -533,6 +528,9 @@ class User {
533
528
  .mul((0, _1.calculateUnrealizedAssetWeight)(market, quoteSpotMarket, positionUnrealizedPnl, withWeightMarginCategory, oraclePriceData))
534
529
  .div(new _1.BN(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION));
535
530
  }
531
+ if (liquidationBuffer && positionUnrealizedPnl.lt(numericConstants_1.ZERO)) {
532
+ positionUnrealizedPnl = positionUnrealizedPnl.add(positionUnrealizedPnl.mul(liquidationBuffer).div(numericConstants_1.MARGIN_PRECISION));
533
+ }
536
534
  }
537
535
  return unrealizedPnl.add(positionUnrealizedPnl);
538
536
  }, numericConstants_1.ZERO);
@@ -731,8 +729,16 @@ class User {
731
729
  * calculates TotalCollateral: collateral + unrealized pnl
732
730
  * @returns : Precision QUOTE_PRECISION
733
731
  */
734
- getTotalCollateral(marginCategory = 'Initial', strict = false, includeOpenOrders = true) {
735
- return this.getSpotMarketAssetValue(undefined, marginCategory, includeOpenOrders, strict).add(this.getUnrealizedPNL(true, undefined, marginCategory, strict));
732
+ getTotalCollateral(marginCategory = 'Initial', strict = false, includeOpenOrders = true, liquidationBuffer) {
733
+ return this.getSpotMarketAssetValue(undefined, marginCategory, includeOpenOrders, strict).add(this.getUnrealizedPNL(true, undefined, marginCategory, strict, liquidationBuffer));
734
+ }
735
+ getLiquidationBuffer() {
736
+ // if user being liq'd, can continue to be liq'd until total collateral above the margin requirement plus buffer
737
+ let liquidationBuffer = undefined;
738
+ if (this.isBeingLiquidated()) {
739
+ liquidationBuffer = new _1.BN(this.driftClient.getStateAccount().liquidationMarginBufferRatio);
740
+ }
741
+ return liquidationBuffer;
736
742
  }
737
743
  /**
738
744
  * calculates User Health by comparing total collateral and maint. margin requirement
@@ -1105,8 +1111,9 @@ class User {
1105
1111
  return netAssetValue.mul(numericConstants_1.TEN_THOUSAND).div(totalLiabilityValue);
1106
1112
  }
1107
1113
  canBeLiquidated() {
1108
- const totalCollateral = this.getTotalCollateral('Maintenance');
1109
- const marginRequirement = this.getMaintenanceMarginRequirement();
1114
+ const liquidationBuffer = this.getLiquidationBuffer();
1115
+ const totalCollateral = this.getTotalCollateral('Maintenance', undefined, undefined, liquidationBuffer);
1116
+ const marginRequirement = this.getMaintenanceMarginRequirement(liquidationBuffer);
1110
1117
  const canBeLiquidated = totalCollateral.lt(marginRequirement);
1111
1118
  return {
1112
1119
  canBeLiquidated,
@@ -119,7 +119,7 @@ export declare class User {
119
119
  /**
120
120
  * @returns The maintenance margin requirement in USDC. : QUOTE_PRECISION
121
121
  */
122
- getMaintenanceMarginRequirement(): BN;
122
+ getMaintenanceMarginRequirement(liquidationBuffer?: BN): BN;
123
123
  getActivePerpPositionsForUserAccount(userAccount: UserAccount): PerpPosition[];
124
124
  getActivePerpPositions(): PerpPosition[];
125
125
  getActivePerpPositionsAndSlot(): DataAndSlot<PerpPosition[]>;
@@ -130,7 +130,7 @@ export declare class User {
130
130
  * calculates unrealized position price pnl
131
131
  * @returns : Precision QUOTE_PRECISION
132
132
  */
133
- getUnrealizedPNL(withFunding?: boolean, marketIndex?: number, withWeightMarginCategory?: MarginCategory, strict?: boolean): BN;
133
+ getUnrealizedPNL(withFunding?: boolean, marketIndex?: number, withWeightMarginCategory?: MarginCategory, strict?: boolean, liquidationBuffer?: BN): BN;
134
134
  /**
135
135
  * calculates unrealized funding payment pnl
136
136
  * @returns : Precision QUOTE_PRECISION
@@ -158,7 +158,8 @@ export declare class User {
158
158
  * calculates TotalCollateral: collateral + unrealized pnl
159
159
  * @returns : Precision QUOTE_PRECISION
160
160
  */
161
- getTotalCollateral(marginCategory?: MarginCategory, strict?: boolean, includeOpenOrders?: boolean): BN;
161
+ getTotalCollateral(marginCategory?: MarginCategory, strict?: boolean, includeOpenOrders?: boolean, liquidationBuffer?: BN): BN;
162
+ getLiquidationBuffer(): BN | undefined;
162
163
  /**
163
164
  * calculates User Health by comparing total collateral and maint. margin requirement
164
165
  * @returns : number (value from [0, 100])
package/lib/node/user.js CHANGED
@@ -457,12 +457,7 @@ class User {
457
457
  /**
458
458
  * @returns The maintenance margin requirement in USDC. : QUOTE_PRECISION
459
459
  */
460
- getMaintenanceMarginRequirement() {
461
- // if user being liq'd, can continue to be liq'd until total collateral above the margin requirement plus buffer
462
- let liquidationBuffer = undefined;
463
- if (this.isBeingLiquidated()) {
464
- liquidationBuffer = new _1.BN(this.driftClient.getStateAccount().liquidationMarginBufferRatio);
465
- }
460
+ getMaintenanceMarginRequirement(liquidationBuffer) {
466
461
  return this.getMarginRequirement('Maintenance', liquidationBuffer);
467
462
  }
468
463
  getActivePerpPositionsForUserAccount(userAccount) {
@@ -502,7 +497,7 @@ class User {
502
497
  * calculates unrealized position price pnl
503
498
  * @returns : Precision QUOTE_PRECISION
504
499
  */
505
- getUnrealizedPNL(withFunding, marketIndex, withWeightMarginCategory, strict = false) {
500
+ getUnrealizedPNL(withFunding, marketIndex, withWeightMarginCategory, strict = false, liquidationBuffer) {
506
501
  return this.getActivePerpPositions()
507
502
  .filter((pos) => marketIndex !== undefined ? pos.marketIndex === marketIndex : true)
508
503
  .reduce((unrealizedPnl, perpPosition) => {
@@ -533,6 +528,9 @@ class User {
533
528
  .mul((0, _1.calculateUnrealizedAssetWeight)(market, quoteSpotMarket, positionUnrealizedPnl, withWeightMarginCategory, oraclePriceData))
534
529
  .div(new _1.BN(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION));
535
530
  }
531
+ if (liquidationBuffer && positionUnrealizedPnl.lt(numericConstants_1.ZERO)) {
532
+ positionUnrealizedPnl = positionUnrealizedPnl.add(positionUnrealizedPnl.mul(liquidationBuffer).div(numericConstants_1.MARGIN_PRECISION));
533
+ }
536
534
  }
537
535
  return unrealizedPnl.add(positionUnrealizedPnl);
538
536
  }, numericConstants_1.ZERO);
@@ -731,8 +729,16 @@ class User {
731
729
  * calculates TotalCollateral: collateral + unrealized pnl
732
730
  * @returns : Precision QUOTE_PRECISION
733
731
  */
734
- getTotalCollateral(marginCategory = 'Initial', strict = false, includeOpenOrders = true) {
735
- return this.getSpotMarketAssetValue(undefined, marginCategory, includeOpenOrders, strict).add(this.getUnrealizedPNL(true, undefined, marginCategory, strict));
732
+ getTotalCollateral(marginCategory = 'Initial', strict = false, includeOpenOrders = true, liquidationBuffer) {
733
+ return this.getSpotMarketAssetValue(undefined, marginCategory, includeOpenOrders, strict).add(this.getUnrealizedPNL(true, undefined, marginCategory, strict, liquidationBuffer));
734
+ }
735
+ getLiquidationBuffer() {
736
+ // if user being liq'd, can continue to be liq'd until total collateral above the margin requirement plus buffer
737
+ let liquidationBuffer = undefined;
738
+ if (this.isBeingLiquidated()) {
739
+ liquidationBuffer = new _1.BN(this.driftClient.getStateAccount().liquidationMarginBufferRatio);
740
+ }
741
+ return liquidationBuffer;
736
742
  }
737
743
  /**
738
744
  * calculates User Health by comparing total collateral and maint. margin requirement
@@ -1105,8 +1111,9 @@ class User {
1105
1111
  return netAssetValue.mul(numericConstants_1.TEN_THOUSAND).div(totalLiabilityValue);
1106
1112
  }
1107
1113
  canBeLiquidated() {
1108
- const totalCollateral = this.getTotalCollateral('Maintenance');
1109
- const marginRequirement = this.getMaintenanceMarginRequirement();
1114
+ const liquidationBuffer = this.getLiquidationBuffer();
1115
+ const totalCollateral = this.getTotalCollateral('Maintenance', undefined, undefined, liquidationBuffer);
1116
+ const marginRequirement = this.getMaintenanceMarginRequirement(liquidationBuffer);
1110
1117
  const canBeLiquidated = totalCollateral.lt(marginRequirement);
1111
1118
  return {
1112
1119
  canBeLiquidated,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.108.0-beta.16",
3
+ "version": "2.108.0-beta.18",
4
4
  "main": "lib/node/index.js",
5
5
  "types": "lib/node/index.d.ts",
6
6
  "browser": "./lib/browser/index.js",
package/src/user.ts CHANGED
@@ -743,15 +743,7 @@ export class User {
743
743
  /**
744
744
  * @returns The maintenance margin requirement in USDC. : QUOTE_PRECISION
745
745
  */
746
- public getMaintenanceMarginRequirement(): BN {
747
- // if user being liq'd, can continue to be liq'd until total collateral above the margin requirement plus buffer
748
- let liquidationBuffer = undefined;
749
- if (this.isBeingLiquidated()) {
750
- liquidationBuffer = new BN(
751
- this.driftClient.getStateAccount().liquidationMarginBufferRatio
752
- );
753
- }
754
-
746
+ public getMaintenanceMarginRequirement(liquidationBuffer?: BN): BN {
755
747
  return this.getMarginRequirement('Maintenance', liquidationBuffer);
756
748
  }
757
749
 
@@ -813,7 +805,8 @@ export class User {
813
805
  withFunding?: boolean,
814
806
  marketIndex?: number,
815
807
  withWeightMarginCategory?: MarginCategory,
816
- strict = false
808
+ strict = false,
809
+ liquidationBuffer?: BN
817
810
  ): BN {
818
811
  return this.getActivePerpPositions()
819
812
  .filter((pos) =>
@@ -882,6 +875,12 @@ export class User {
882
875
  )
883
876
  .div(new BN(SPOT_MARKET_WEIGHT_PRECISION));
884
877
  }
878
+
879
+ if (liquidationBuffer && positionUnrealizedPnl.lt(ZERO)) {
880
+ positionUnrealizedPnl = positionUnrealizedPnl.add(
881
+ positionUnrealizedPnl.mul(liquidationBuffer).div(MARGIN_PRECISION)
882
+ );
883
+ }
885
884
  }
886
885
 
887
886
  return unrealizedPnl.add(positionUnrealizedPnl);
@@ -1386,14 +1385,34 @@ export class User {
1386
1385
  public getTotalCollateral(
1387
1386
  marginCategory: MarginCategory = 'Initial',
1388
1387
  strict = false,
1389
- includeOpenOrders = true
1388
+ includeOpenOrders = true,
1389
+ liquidationBuffer?: BN
1390
1390
  ): BN {
1391
1391
  return this.getSpotMarketAssetValue(
1392
1392
  undefined,
1393
1393
  marginCategory,
1394
1394
  includeOpenOrders,
1395
1395
  strict
1396
- ).add(this.getUnrealizedPNL(true, undefined, marginCategory, strict));
1396
+ ).add(
1397
+ this.getUnrealizedPNL(
1398
+ true,
1399
+ undefined,
1400
+ marginCategory,
1401
+ strict,
1402
+ liquidationBuffer
1403
+ )
1404
+ );
1405
+ }
1406
+
1407
+ public getLiquidationBuffer(): BN | undefined {
1408
+ // if user being liq'd, can continue to be liq'd until total collateral above the margin requirement plus buffer
1409
+ let liquidationBuffer = undefined;
1410
+ if (this.isBeingLiquidated()) {
1411
+ liquidationBuffer = new BN(
1412
+ this.driftClient.getStateAccount().liquidationMarginBufferRatio
1413
+ );
1414
+ }
1415
+ return liquidationBuffer;
1397
1416
  }
1398
1417
 
1399
1418
  /**
@@ -2106,9 +2125,17 @@ export class User {
2106
2125
  marginRequirement: BN;
2107
2126
  totalCollateral: BN;
2108
2127
  } {
2109
- const totalCollateral = this.getTotalCollateral('Maintenance');
2128
+ const liquidationBuffer = this.getLiquidationBuffer();
2129
+
2130
+ const totalCollateral = this.getTotalCollateral(
2131
+ 'Maintenance',
2132
+ undefined,
2133
+ undefined,
2134
+ liquidationBuffer
2135
+ );
2110
2136
 
2111
- const marginRequirement = this.getMaintenanceMarginRequirement();
2137
+ const marginRequirement =
2138
+ this.getMaintenanceMarginRequirement(liquidationBuffer);
2112
2139
  const canBeLiquidated = totalCollateral.lt(marginRequirement);
2113
2140
 
2114
2141
  return {