@drift-labs/sdk 2.37.1-beta.9 → 2.38.1-beta.0

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.9
1
+ 2.38.1-beta.0
@@ -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) {
@@ -30,7 +30,7 @@ class EventSubscriber {
30
30
  this.logProvider = new webSocketLogProvider_1.WebSocketLogProvider(this.connection, this.address, this.options.commitment);
31
31
  }
32
32
  else {
33
- this.logProvider = new pollingLogProvider_1.PollingLogProvider(this.connection, this.address, options.commitment, this.options.logProviderConfig.frequency);
33
+ this.logProvider = new pollingLogProvider_1.PollingLogProvider(this.connection, this.address, options.commitment, this.options.logProviderConfig.frequency, this.options.logProviderConfig.batchSize);
34
34
  }
35
35
  }
36
36
  async subscribe() {
@@ -14,7 +14,7 @@ type FetchLogsResponse = {
14
14
  transactionLogs: Log[];
15
15
  mostRecentBlockTime: number | undefined;
16
16
  };
17
- export declare function fetchLogs(connection: Connection, address: PublicKey, finality: Finality, beforeTx?: TransactionSignature, untilTx?: TransactionSignature, limit?: number): Promise<FetchLogsResponse>;
17
+ export declare function fetchLogs(connection: Connection, address: PublicKey, finality: Finality, beforeTx?: TransactionSignature, untilTx?: TransactionSignature, limit?: number, batchSize?: number): Promise<FetchLogsResponse>;
18
18
  export declare function fetchTransactionLogs(connection: Connection, signatures: TransactionSignature[], finality: Finality): Promise<Log[]>;
19
19
  export declare class LogParser {
20
20
  private program;
@@ -9,7 +9,7 @@ function mapTransactionResponseToLog(transaction) {
9
9
  logs: transaction.meta.logMessages,
10
10
  };
11
11
  }
12
- async function fetchLogs(connection, address, finality, beforeTx, untilTx, limit) {
12
+ async function fetchLogs(connection, address, finality, beforeTx, untilTx, limit, batchSize = 25) {
13
13
  const signatures = await connection.getSignaturesForAddress(address, {
14
14
  before: beforeTx,
15
15
  until: untilTx,
@@ -20,7 +20,7 @@ async function fetchLogs(connection, address, finality, beforeTx, untilTx, limit
20
20
  if (filteredSignatures.length === 0) {
21
21
  return undefined;
22
22
  }
23
- const chunkedSignatures = chunk(filteredSignatures, 100);
23
+ const chunkedSignatures = chunk(filteredSignatures, batchSize);
24
24
  const transactionLogs = (await Promise.all(chunkedSignatures.map(async (chunk) => {
25
25
  return await fetchTransactionLogs(connection, chunk.map((confirmedSignature) => confirmedSignature.signature), finality);
26
26
  }))).flat();
@@ -4,12 +4,13 @@ export declare class PollingLogProvider implements LogProvider {
4
4
  private connection;
5
5
  private address;
6
6
  private frequency;
7
+ private batchSize?;
7
8
  private finality;
8
9
  private intervalId;
9
10
  private mostRecentSeenTx?;
10
11
  private mutex;
11
12
  private firstFetch;
12
- constructor(connection: Connection, address: PublicKey, commitment: Commitment, frequency?: number);
13
+ constructor(connection: Connection, address: PublicKey, commitment: Commitment, frequency?: number, batchSize?: number);
13
14
  subscribe(callback: logProviderCallback, skipHistory?: boolean): boolean;
14
15
  isSubscribed(): boolean;
15
16
  unsubscribe(): Promise<boolean>;
@@ -3,10 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PollingLogProvider = void 0;
4
4
  const fetchLogs_1 = require("./fetchLogs");
5
5
  class PollingLogProvider {
6
- constructor(connection, address, commitment, frequency = 15 * 1000) {
6
+ constructor(connection, address, commitment, frequency = 15 * 1000, batchSize) {
7
7
  this.connection = connection;
8
8
  this.address = address;
9
9
  this.frequency = frequency;
10
+ this.batchSize = batchSize;
10
11
  this.firstFetch = true;
11
12
  this.finality = commitment === 'finalized' ? 'finalized' : 'confirmed';
12
13
  }
@@ -22,7 +23,7 @@ class PollingLogProvider {
22
23
  try {
23
24
  const response = await (0, fetchLogs_1.fetchLogs)(this.connection, this.address, this.finality, undefined, this.mostRecentSeenTx,
24
25
  // If skipping history, only fetch one log back, not the maximum amount available
25
- skipHistory && this.firstFetch ? 1 : undefined);
26
+ skipHistory && this.firstFetch ? 1 : undefined, this.batchSize);
26
27
  if (response === undefined) {
27
28
  return;
28
29
  }
@@ -57,5 +57,6 @@ export type WebSocketLogProviderConfig = {
57
57
  export type PollingLogProviderConfig = {
58
58
  type: 'polling';
59
59
  frequency: number;
60
+ batchSize?: number;
60
61
  };
61
62
  export type LogProviderConfig = WebSocketLogProviderConfig | PollingLogProviderConfig;
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.37.0",
2
+ "version": "2.38.0",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
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
@@ -1817,7 +1807,7 @@ class User {
1817
1807
  healthComponents.perpPnl.push({
1818
1808
  marketIndex: perpMarket.marketIndex,
1819
1809
  size: positionUnrealizedPnl,
1820
- value: wegithedPnlValue,
1810
+ value: pnlValue,
1821
1811
  weight: pnlWeight,
1822
1812
  weightedValue: wegithedPnlValue,
1823
1813
  });
@@ -1858,7 +1848,7 @@ class User {
1858
1848
  healthComponents.deposits.push({
1859
1849
  marketIndex: spotMarketAccount.marketIndex,
1860
1850
  size: worstCaseTokenAmount,
1861
- value: weightedValue,
1851
+ value: baseAssetValue,
1862
1852
  weight: weight,
1863
1853
  weightedValue: weightedValue,
1864
1854
  });
@@ -1892,7 +1882,7 @@ class User {
1892
1882
  healthComponents.deposits.push({
1893
1883
  marketIndex: spotMarketAccount.marketIndex,
1894
1884
  size: netQuoteValue,
1895
- value: weightedValue,
1885
+ value: baseAssetValue,
1896
1886
  weight: weight,
1897
1887
  weightedValue: weightedValue,
1898
1888
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.37.1-beta.9",
3
+ "version": "2.38.1-beta.0",
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
 
@@ -63,7 +63,8 @@ export class EventSubscriber {
63
63
  this.connection,
64
64
  this.address,
65
65
  options.commitment,
66
- this.options.logProviderConfig.frequency
66
+ this.options.logProviderConfig.frequency,
67
+ this.options.logProviderConfig.batchSize
67
68
  );
68
69
  }
69
70
  }
@@ -36,7 +36,8 @@ export async function fetchLogs(
36
36
  finality: Finality,
37
37
  beforeTx?: TransactionSignature,
38
38
  untilTx?: TransactionSignature,
39
- limit?: number
39
+ limit?: number,
40
+ batchSize = 25
40
41
  ): Promise<FetchLogsResponse> {
41
42
  const signatures = await connection.getSignaturesForAddress(
42
43
  address,
@@ -60,7 +61,7 @@ export async function fetchLogs(
60
61
  return undefined;
61
62
  }
62
63
 
63
- const chunkedSignatures = chunk(filteredSignatures, 100);
64
+ const chunkedSignatures = chunk(filteredSignatures, batchSize);
64
65
 
65
66
  const transactionLogs = (
66
67
  await Promise.all(
@@ -19,7 +19,8 @@ export class PollingLogProvider implements LogProvider {
19
19
  private connection: Connection,
20
20
  private address: PublicKey,
21
21
  commitment: Commitment,
22
- private frequency = 15 * 1000
22
+ private frequency = 15 * 1000,
23
+ private batchSize?: number
23
24
  ) {
24
25
  this.finality = commitment === 'finalized' ? 'finalized' : 'confirmed';
25
26
  }
@@ -46,7 +47,8 @@ export class PollingLogProvider implements LogProvider {
46
47
  undefined,
47
48
  this.mostRecentSeenTx,
48
49
  // If skipping history, only fetch one log back, not the maximum amount available
49
- skipHistory && this.firstFetch ? 1 : undefined
50
+ skipHistory && this.firstFetch ? 1 : undefined,
51
+ this.batchSize
50
52
  );
51
53
 
52
54
  if (response === undefined) {
@@ -137,6 +137,7 @@ export type WebSocketLogProviderConfig = {
137
137
  export type PollingLogProviderConfig = {
138
138
  type: 'polling';
139
139
  frequency: number;
140
+ batchSize?: number;
140
141
  };
141
142
 
142
143
  export type LogProviderConfig =
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.37.0",
2
+ "version": "2.38.0",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
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
  /**
@@ -3271,7 +3267,7 @@ export class User {
3271
3267
  healthComponents.perpPnl.push({
3272
3268
  marketIndex: perpMarket.marketIndex,
3273
3269
  size: positionUnrealizedPnl,
3274
- value: wegithedPnlValue,
3270
+ value: pnlValue,
3275
3271
  weight: pnlWeight,
3276
3272
  weightedValue: wegithedPnlValue,
3277
3273
  });
@@ -3347,7 +3343,7 @@ export class User {
3347
3343
  healthComponents.deposits.push({
3348
3344
  marketIndex: spotMarketAccount.marketIndex,
3349
3345
  size: worstCaseTokenAmount,
3350
- value: weightedValue,
3346
+ value: baseAssetValue,
3351
3347
  weight: weight,
3352
3348
  weightedValue: weightedValue,
3353
3349
  });
@@ -3398,7 +3394,7 @@ export class User {
3398
3394
  healthComponents.deposits.push({
3399
3395
  marketIndex: spotMarketAccount.marketIndex,
3400
3396
  size: netQuoteValue,
3401
- value: weightedValue,
3397
+ value: baseAssetValue,
3402
3398
  weight: weight,
3403
3399
  weightedValue: weightedValue,
3404
3400
  });