@drift-labs/sdk 2.37.1-beta.10 → 2.37.1-beta.12

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.37.1-beta.10
1
+ 2.37.1-beta.12
@@ -168,7 +168,18 @@ class BulkAccountLoader {
168
168
  }
169
169
  handleAccountCallbacks(accountToLoad, buffer, slot) {
170
170
  for (const [_, callback] of accountToLoad.callbacks) {
171
- callback(buffer, slot);
171
+ try {
172
+ callback(buffer, slot);
173
+ }
174
+ catch (e) {
175
+ console.log('Bulk account load: error in account callback');
176
+ console.log('accounto to load', accountToLoad.publicKey.toString());
177
+ console.log('buffer', buffer.toString('base64'));
178
+ for (const callback of accountToLoad.callbacks.values()) {
179
+ console.log('account to load cb', callback);
180
+ }
181
+ throw e;
182
+ }
172
183
  }
173
184
  }
174
185
  getBufferAndSlot(publicKey) {
package/lib/user.d.ts CHANGED
@@ -101,10 +101,10 @@ export declare class User {
101
101
  getPerpBuyingPower(marketIndex: number, collateralBuffer?: any): BN;
102
102
  getPerpBuyingPowerFromFreeCollateralAndBaseAssetAmount(marketIndex: number, freeCollateral: BN, baseAssetAmount: BN): BN;
103
103
  /**
104
- * calculates Free Collateral = Total collateral - initial margin requirement
104
+ * calculates Free Collateral = Total collateral - margin requirement
105
105
  * @returns : Precision QUOTE_PRECISION
106
106
  */
107
- getFreeCollateral(): BN;
107
+ getFreeCollateral(marginCategory?: MarginCategory): BN;
108
108
  /**
109
109
  * @returns The margin requirement of a certain type (Initial or Maintenance) in USDC. : QUOTE_PRECISION
110
110
  */
package/lib/user.js CHANGED
@@ -414,13 +414,15 @@ class User {
414
414
  return freeCollateral.mul(numericConstants_1.MARGIN_PRECISION).div(new _1.BN(marginRatio));
415
415
  }
416
416
  /**
417
- * calculates Free Collateral = Total collateral - initial margin requirement
417
+ * calculates Free Collateral = Total collateral - margin requirement
418
418
  * @returns : Precision QUOTE_PRECISION
419
419
  */
420
- getFreeCollateral() {
421
- const totalCollateral = this.getTotalCollateral('Initial', true);
422
- const initialMarginRequirement = this.getInitialMarginRequirement();
423
- const freeCollateral = totalCollateral.sub(initialMarginRequirement);
420
+ getFreeCollateral(marginCategory = 'Initial') {
421
+ const totalCollateral = this.getTotalCollateral(marginCategory, true);
422
+ const marginRequirement = marginCategory === 'Initial'
423
+ ? this.getInitialMarginRequirement()
424
+ : this.getMaintenanceMarginRequirement();
425
+ const freeCollateral = totalCollateral.sub(marginRequirement);
424
426
  return freeCollateral.gte(numericConstants_1.ZERO) ? freeCollateral : numericConstants_1.ZERO;
425
427
  }
426
428
  /**
@@ -686,15 +688,8 @@ class User {
686
688
  health = 0;
687
689
  }
688
690
  else {
689
- const healthP1 = Math.max(0, (1 - maintenanceMarginReq.toNumber() / totalCollateral.toNumber()) *
690
- 100) + 1;
691
- health = Math.min(1, Math.log(healthP1) / Math.log(100)) * 100;
692
- if (health > 1) {
693
- health = Math.round(health);
694
- }
695
- else {
696
- health = Math.round(health * 100) / 100;
697
- }
691
+ health = Math.round(Math.min(100, Math.max(0, (1 - maintenanceMarginReq.toNumber() / totalCollateral.toNumber()) *
692
+ 100)));
698
693
  }
699
694
  return health;
700
695
  }
@@ -759,12 +754,7 @@ class User {
759
754
  */
760
755
  getPerpMarketLiabilityValue(marketIndex, marginCategory, liquidationBuffer, includeOpenOrders, strict = false) {
761
756
  const perpPosition = this.getPerpPosition(marketIndex);
762
- if (!perpPosition) {
763
- return numericConstants_1.ZERO;
764
- }
765
- else {
766
- return this.calculateWeightedPerpPositionValue(perpPosition, marginCategory, liquidationBuffer, includeOpenOrders, strict);
767
- }
757
+ return this.calculateWeightedPerpPositionValue(perpPosition, marginCategory, liquidationBuffer, includeOpenOrders, strict);
768
758
  }
769
759
  /**
770
760
  * calculates sum of position value across all positions in margin system
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.37.1-beta.10",
3
+ "version": "2.37.1-beta.12",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -229,7 +229,17 @@ export class BulkAccountLoader {
229
229
  slot: number
230
230
  ): void {
231
231
  for (const [_, callback] of accountToLoad.callbacks) {
232
- callback(buffer, slot);
232
+ try {
233
+ callback(buffer, slot);
234
+ } catch (e) {
235
+ console.log('Bulk account load: error in account callback');
236
+ console.log('accounto to load', accountToLoad.publicKey.toString());
237
+ console.log('buffer', buffer.toString('base64'));
238
+ for (const callback of accountToLoad.callbacks.values()) {
239
+ console.log('account to load cb', callback);
240
+ }
241
+ throw e;
242
+ }
233
243
  }
234
244
  }
235
245
 
package/src/user.ts CHANGED
@@ -636,13 +636,16 @@ export class User {
636
636
  }
637
637
 
638
638
  /**
639
- * calculates Free Collateral = Total collateral - initial margin requirement
639
+ * calculates Free Collateral = Total collateral - margin requirement
640
640
  * @returns : Precision QUOTE_PRECISION
641
641
  */
642
- public getFreeCollateral(): BN {
643
- const totalCollateral = this.getTotalCollateral('Initial', true);
644
- const initialMarginRequirement = this.getInitialMarginRequirement();
645
- const freeCollateral = totalCollateral.sub(initialMarginRequirement);
642
+ public getFreeCollateral(marginCategory: MarginCategory = 'Initial'): BN {
643
+ const totalCollateral = this.getTotalCollateral(marginCategory, true);
644
+ const marginRequirement =
645
+ marginCategory === 'Initial'
646
+ ? this.getInitialMarginRequirement()
647
+ : this.getMaintenanceMarginRequirement();
648
+ const freeCollateral = totalCollateral.sub(marginRequirement);
646
649
  return freeCollateral.gte(ZERO) ? freeCollateral : ZERO;
647
650
  }
648
651
 
@@ -1222,19 +1225,16 @@ export class User {
1222
1225
  } else if (totalCollateral.lte(ZERO)) {
1223
1226
  health = 0;
1224
1227
  } else {
1225
- const healthP1 =
1226
- Math.max(
1227
- 0,
1228
- (1 - maintenanceMarginReq.toNumber() / totalCollateral.toNumber()) *
1229
- 100
1230
- ) + 1;
1231
-
1232
- health = Math.min(1, Math.log(healthP1) / Math.log(100)) * 100;
1233
- if (health > 1) {
1234
- health = Math.round(health);
1235
- } else {
1236
- health = Math.round(health * 100) / 100;
1237
- }
1228
+ health = Math.round(
1229
+ Math.min(
1230
+ 100,
1231
+ Math.max(
1232
+ 0,
1233
+ (1 - maintenanceMarginReq.toNumber() / totalCollateral.toNumber()) *
1234
+ 100
1235
+ )
1236
+ )
1237
+ );
1238
1238
  }
1239
1239
 
1240
1240
  return health;
@@ -1359,17 +1359,13 @@ export class User {
1359
1359
  strict = false
1360
1360
  ): BN {
1361
1361
  const perpPosition = this.getPerpPosition(marketIndex);
1362
- if (!perpPosition) {
1363
- return ZERO;
1364
- } else {
1365
- return this.calculateWeightedPerpPositionValue(
1366
- perpPosition,
1367
- marginCategory,
1368
- liquidationBuffer,
1369
- includeOpenOrders,
1370
- strict
1371
- );
1372
- }
1362
+ return this.calculateWeightedPerpPositionValue(
1363
+ perpPosition,
1364
+ marginCategory,
1365
+ liquidationBuffer,
1366
+ includeOpenOrders,
1367
+ strict
1368
+ );
1373
1369
  }
1374
1370
 
1375
1371
  /**