@drift-labs/sdk 2.82.0-beta.4 → 2.82.0-beta.6

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.
Files changed (57) hide show
  1. package/VERSION +1 -1
  2. package/lib/accounts/types.d.ts +4 -0
  3. package/lib/accounts/webSocketAccountSubscriber.d.ts +3 -3
  4. package/lib/accounts/webSocketAccountSubscriber.js +15 -8
  5. package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +3 -3
  6. package/lib/accounts/webSocketDriftClientAccountSubscriber.js +5 -5
  7. package/lib/accounts/webSocketInsuranceFundStakeAccountSubscriber.d.ts +2 -2
  8. package/lib/accounts/webSocketInsuranceFundStakeAccountSubscriber.js +5 -3
  9. package/lib/accounts/webSocketProgramAccountSubscriber.d.ts +3 -3
  10. package/lib/accounts/webSocketProgramAccountSubscriber.js +15 -9
  11. package/lib/accounts/webSocketUserAccountSubscriber.d.ts +3 -3
  12. package/lib/accounts/webSocketUserAccountSubscriber.js +3 -3
  13. package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +3 -3
  14. package/lib/accounts/webSocketUserStatsAccountSubsriber.js +3 -3
  15. package/lib/auctionSubscriber/auctionSubscriber.d.ts +2 -2
  16. package/lib/auctionSubscriber/auctionSubscriber.js +3 -3
  17. package/lib/auctionSubscriber/types.d.ts +1 -0
  18. package/lib/driftClient.d.ts +10 -0
  19. package/lib/driftClient.js +48 -8
  20. package/lib/driftClientConfig.d.ts +1 -0
  21. package/lib/orderSubscriber/OrderSubscriber.js +6 -3
  22. package/lib/orderSubscriber/WebsocketSubscription.d.ts +4 -3
  23. package/lib/orderSubscriber/WebsocketSubscription.js +3 -3
  24. package/lib/orderSubscriber/types.d.ts +1 -0
  25. package/lib/tx/utils.js +5 -1
  26. package/lib/user.d.ts +0 -10
  27. package/lib/user.js +6 -29
  28. package/lib/userConfig.d.ts +1 -0
  29. package/lib/userMap/WebsocketSubscription.d.ts +4 -3
  30. package/lib/userMap/WebsocketSubscription.js +3 -3
  31. package/lib/userMap/userMap.js +4 -1
  32. package/lib/userMap/userMapConfig.d.ts +1 -0
  33. package/lib/userStats.js +6 -3
  34. package/lib/userStatsConfig.d.ts +1 -0
  35. package/package.json +1 -1
  36. package/src/accounts/types.ts +5 -0
  37. package/src/accounts/webSocketAccountSubscriber.ts +34 -22
  38. package/src/accounts/webSocketDriftClientAccountSubscriber.ts +7 -6
  39. package/src/accounts/webSocketInsuranceFundStakeAccountSubscriber.ts +6 -4
  40. package/src/accounts/webSocketProgramAccountSubscriber.ts +32 -22
  41. package/src/accounts/webSocketUserAccountSubscriber.ts +5 -4
  42. package/src/accounts/webSocketUserStatsAccountSubsriber.ts +5 -4
  43. package/src/auctionSubscriber/auctionSubscriber.ts +10 -4
  44. package/src/auctionSubscriber/types.ts +1 -0
  45. package/src/driftClient.ts +48 -1
  46. package/src/driftClientConfig.ts +1 -0
  47. package/src/orderSubscriber/OrderSubscriber.ts +4 -1
  48. package/src/orderSubscriber/WebsocketSubscription.ts +6 -5
  49. package/src/orderSubscriber/types.ts +1 -0
  50. package/src/tx/utils.ts +5 -1
  51. package/src/user.ts +7 -32
  52. package/src/userConfig.ts +1 -0
  53. package/src/userMap/WebsocketSubscription.ts +6 -5
  54. package/src/userMap/userMap.ts +4 -1
  55. package/src/userMap/userMapConfig.ts +1 -0
  56. package/src/userStats.ts +4 -1
  57. package/src/userStatsConfig.ts +1 -0
package/VERSION CHANGED
@@ -1 +1 @@
1
- 2.82.0-beta.4
1
+ 2.82.0-beta.6
@@ -126,6 +126,10 @@ export type DataAndSlot<T> = {
126
126
  data: T;
127
127
  slot: number;
128
128
  };
129
+ export type ResubOpts = {
130
+ resubTimeoutMs?: number;
131
+ logResubMessages?: boolean;
132
+ };
129
133
  export interface UserStatsAccountEvents {
130
134
  userStatsAccountUpdate: (payload: UserStatsAccount) => void;
131
135
  update: void;
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
- import { DataAndSlot, BufferAndSlot, AccountSubscriber } from './types';
3
+ import { DataAndSlot, BufferAndSlot, AccountSubscriber, ResubOpts } from './types';
4
4
  import { Program } from '@coral-xyz/anchor';
5
5
  import { AccountInfo, Commitment, Context, PublicKey } from '@solana/web3.js';
6
6
  export declare class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
@@ -12,12 +12,12 @@ export declare class WebSocketAccountSubscriber<T> implements AccountSubscriber<
12
12
  decodeBufferFn: (buffer: Buffer) => T;
13
13
  onChange: (data: T) => void;
14
14
  listenerId?: number;
15
- resubTimeoutMs?: number;
15
+ resubOpts?: ResubOpts;
16
16
  commitment?: Commitment;
17
17
  isUnsubscribing: boolean;
18
18
  timeoutId?: NodeJS.Timeout;
19
19
  receivingData: boolean;
20
- constructor(accountName: string, program: Program, accountPublicKey: PublicKey, decodeBuffer?: (buffer: Buffer) => T, resubTimeoutMs?: number, commitment?: Commitment);
20
+ constructor(accountName: string, program: Program, accountPublicKey: PublicKey, decodeBuffer?: (buffer: Buffer) => T, resubOpts?: ResubOpts, commitment?: Commitment);
21
21
  subscribe(onChange: (data: T) => void): Promise<void>;
22
22
  setData(data: T, slot?: number): void;
23
23
  private setTimeout;
@@ -3,14 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.WebSocketAccountSubscriber = void 0;
4
4
  const utils_1 = require("./utils");
5
5
  class WebSocketAccountSubscriber {
6
- constructor(accountName, program, accountPublicKey, decodeBuffer, resubTimeoutMs, commitment) {
6
+ constructor(accountName, program, accountPublicKey, decodeBuffer, resubOpts, commitment) {
7
+ var _a;
7
8
  this.isUnsubscribing = false;
8
9
  this.accountName = accountName;
9
10
  this.program = program;
10
11
  this.accountPublicKey = accountPublicKey;
11
12
  this.decodeBufferFn = decodeBuffer;
12
- this.resubTimeoutMs = resubTimeoutMs;
13
- if (this.resubTimeoutMs < 1000) {
13
+ this.resubOpts = resubOpts;
14
+ if (((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs) < 1000) {
14
15
  console.log('resubTimeoutMs should be at least 1000ms to avoid spamming resub');
15
16
  }
16
17
  this.receivingData = false;
@@ -18,6 +19,7 @@ class WebSocketAccountSubscriber {
18
19
  commitment !== null && commitment !== void 0 ? commitment : this.program.provider.opts.commitment;
19
20
  }
20
21
  async subscribe(onChange) {
22
+ var _a;
21
23
  if (this.listenerId != null || this.isUnsubscribing) {
22
24
  return;
23
25
  }
@@ -26,7 +28,8 @@ class WebSocketAccountSubscriber {
26
28
  await this.fetch();
27
29
  }
28
30
  this.listenerId = this.program.provider.connection.onAccountChange(this.accountPublicKey, (accountInfo, context) => {
29
- if (this.resubTimeoutMs) {
31
+ var _a;
32
+ if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs) {
30
33
  this.receivingData = true;
31
34
  clearTimeout(this.timeoutId);
32
35
  this.handleRpcResponse(context, accountInfo);
@@ -36,7 +39,7 @@ class WebSocketAccountSubscriber {
36
39
  this.handleRpcResponse(context, accountInfo);
37
40
  }
38
41
  }, this.commitment);
39
- if (this.resubTimeoutMs) {
42
+ if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs) {
40
43
  this.receivingData = true;
41
44
  this.setTimeout();
42
45
  }
@@ -52,21 +55,25 @@ class WebSocketAccountSubscriber {
52
55
  };
53
56
  }
54
57
  setTimeout() {
58
+ var _a;
55
59
  if (!this.onChange) {
56
60
  throw new Error('onChange callback function must be set');
57
61
  }
58
62
  this.timeoutId = setTimeout(async () => {
63
+ var _a;
59
64
  if (this.isUnsubscribing) {
60
65
  // If we are in the process of unsubscribing, do not attempt to resubscribe
61
66
  return;
62
67
  }
63
68
  if (this.receivingData) {
64
- console.log(`No ws data from ${this.accountName} in ${this.resubTimeoutMs}ms, resubscribing`);
69
+ if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.logResubMessages) {
70
+ console.log(`No ws data from ${this.accountName} in ${this.resubOpts.resubTimeoutMs}ms, resubscribing`);
71
+ }
65
72
  await this.unsubscribe(true);
66
73
  this.receivingData = false;
67
74
  await this.subscribe(this.onChange);
68
75
  }
69
- }, this.resubTimeoutMs);
76
+ }, (_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs);
70
77
  }
71
78
  async fetch() {
72
79
  const rpcResponse = await this.program.provider.connection.getAccountInfoAndContext(this.accountPublicKey, this.program.provider.opts.commitment);
@@ -120,7 +127,7 @@ class WebSocketAccountSubscriber {
120
127
  }
121
128
  unsubscribe(onResub = false) {
122
129
  if (!onResub) {
123
- this.resubTimeoutMs = undefined;
130
+ this.resubOpts.resubTimeoutMs = undefined;
124
131
  }
125
132
  this.isUnsubscribing = true;
126
133
  clearTimeout(this.timeoutId);
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import { DriftClientAccountSubscriber, DriftClientAccountEvents, DataAndSlot } from './types';
2
+ import { DriftClientAccountSubscriber, DriftClientAccountEvents, DataAndSlot, ResubOpts } from './types';
3
3
  import { AccountSubscriber } from './types';
4
4
  import { SpotMarketAccount, PerpMarketAccount, StateAccount } from '../types';
5
5
  import { Program } from '@coral-xyz/anchor';
@@ -16,7 +16,7 @@ export declare class WebSocketDriftClientAccountSubscriber implements DriftClien
16
16
  spotMarketIndexes: number[];
17
17
  oracleInfos: OracleInfo[];
18
18
  oracleClientCache: OracleClientCache;
19
- resubTimeoutMs?: number;
19
+ resubOpts?: ResubOpts;
20
20
  shouldFindAllMarketsAndOracles: boolean;
21
21
  eventEmitter: StrictEventEmitter<EventEmitter, DriftClientAccountEvents>;
22
22
  stateAccountSubscriber?: AccountSubscriber<StateAccount>;
@@ -28,7 +28,7 @@ export declare class WebSocketDriftClientAccountSubscriber implements DriftClien
28
28
  private isSubscribing;
29
29
  private subscriptionPromise;
30
30
  private subscriptionPromiseResolver;
31
- constructor(program: Program, perpMarketIndexes: number[], spotMarketIndexes: number[], oracleInfos: OracleInfo[], shouldFindAllMarketsAndOracles: boolean, resubTimeoutMs?: number, commitment?: Commitment);
31
+ constructor(program: Program, perpMarketIndexes: number[], spotMarketIndexes: number[], oracleInfos: OracleInfo[], shouldFindAllMarketsAndOracles: boolean, resubOpts?: ResubOpts, commitment?: Commitment);
32
32
  subscribe(): Promise<boolean>;
33
33
  subscribeToPerpMarketAccounts(): Promise<boolean>;
34
34
  subscribeToPerpMarketAccount(marketIndex: number): Promise<boolean>;
@@ -10,7 +10,7 @@ const oracleClientCache_1 = require("../oracles/oracleClientCache");
10
10
  const quoteAssetOracleClient_1 = require("../oracles/quoteAssetOracleClient");
11
11
  const config_1 = require("../config");
12
12
  class WebSocketDriftClientAccountSubscriber {
13
- constructor(program, perpMarketIndexes, spotMarketIndexes, oracleInfos, shouldFindAllMarketsAndOracles, resubTimeoutMs, commitment) {
13
+ constructor(program, perpMarketIndexes, spotMarketIndexes, oracleInfos, shouldFindAllMarketsAndOracles, resubOpts, commitment) {
14
14
  this.oracleClientCache = new oracleClientCache_1.OracleClientCache();
15
15
  this.perpMarketAccountSubscribers = new Map();
16
16
  this.perpOracleMap = new Map();
@@ -25,7 +25,7 @@ class WebSocketDriftClientAccountSubscriber {
25
25
  this.spotMarketIndexes = spotMarketIndexes;
26
26
  this.oracleInfos = oracleInfos;
27
27
  this.shouldFindAllMarketsAndOracles = shouldFindAllMarketsAndOracles;
28
- this.resubTimeoutMs = resubTimeoutMs;
28
+ this.resubOpts = resubOpts;
29
29
  this.commitment = commitment;
30
30
  }
31
31
  async subscribe() {
@@ -74,7 +74,7 @@ class WebSocketDriftClientAccountSubscriber {
74
74
  }
75
75
  async subscribeToPerpMarketAccount(marketIndex) {
76
76
  const perpMarketPublicKey = await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, marketIndex);
77
- const accountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('perpMarket', this.program, perpMarketPublicKey, undefined, this.resubTimeoutMs, this.commitment);
77
+ const accountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('perpMarket', this.program, perpMarketPublicKey, undefined, this.resubOpts, this.commitment);
78
78
  await accountSubscriber.subscribe((data) => {
79
79
  this.eventEmitter.emit('perpMarketAccountUpdate', data);
80
80
  this.eventEmitter.emit('update');
@@ -90,7 +90,7 @@ class WebSocketDriftClientAccountSubscriber {
90
90
  }
91
91
  async subscribeToSpotMarketAccount(marketIndex) {
92
92
  const marketPublicKey = await (0, pda_1.getSpotMarketPublicKey)(this.program.programId, marketIndex);
93
- const accountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('spotMarket', this.program, marketPublicKey, undefined, this.resubTimeoutMs, this.commitment);
93
+ const accountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('spotMarket', this.program, marketPublicKey, undefined, this.resubOpts, this.commitment);
94
94
  await accountSubscriber.subscribe((data) => {
95
95
  this.eventEmitter.emit('spotMarketAccountUpdate', data);
96
96
  this.eventEmitter.emit('update');
@@ -110,7 +110,7 @@ class WebSocketDriftClientAccountSubscriber {
110
110
  const client = this.oracleClientCache.get(oracleInfo.source, this.program.provider.connection, this.program);
111
111
  const accountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('oracle', this.program, oracleInfo.publicKey, (buffer) => {
112
112
  return client.getOraclePriceDataFromBuffer(buffer);
113
- }, this.resubTimeoutMs, this.commitment);
113
+ }, this.resubOpts, this.commitment);
114
114
  await accountSubscriber.subscribe((data) => {
115
115
  this.eventEmitter.emit('oraclePriceUpdate', oracleInfo.publicKey, data);
116
116
  this.eventEmitter.emit('update');
@@ -7,13 +7,13 @@ import { Commitment, PublicKey } from '@solana/web3.js';
7
7
  import { InsuranceFundStake } from '../types';
8
8
  export declare class WebSocketInsuranceFundStakeAccountSubscriber implements InsuranceFundStakeAccountSubscriber {
9
9
  isSubscribed: boolean;
10
- reconnectTimeoutMs?: number;
10
+ resubTimeoutMs?: number;
11
11
  commitment?: Commitment;
12
12
  program: Program;
13
13
  eventEmitter: StrictEventEmitter<EventEmitter, InsuranceFundStakeAccountEvents>;
14
14
  insuranceFundStakeAccountPublicKey: PublicKey;
15
15
  insuranceFundStakeDataAccountSubscriber: AccountSubscriber<InsuranceFundStake>;
16
- constructor(program: Program, insuranceFundStakeAccountPublicKey: PublicKey, reconnectTimeoutMs?: number, commitment?: Commitment);
16
+ constructor(program: Program, insuranceFundStakeAccountPublicKey: PublicKey, resubTimeoutMs?: number, commitment?: Commitment);
17
17
  subscribe(insuranceFundStakeAccount?: InsuranceFundStake): Promise<boolean>;
18
18
  fetch(): Promise<void>;
19
19
  unsubscribe(): Promise<void>;
@@ -5,13 +5,13 @@ const types_1 = require("./types");
5
5
  const events_1 = require("events");
6
6
  const webSocketAccountSubscriber_1 = require("./webSocketAccountSubscriber");
7
7
  class WebSocketInsuranceFundStakeAccountSubscriber {
8
- constructor(program, insuranceFundStakeAccountPublicKey, reconnectTimeoutMs, commitment) {
8
+ constructor(program, insuranceFundStakeAccountPublicKey, resubTimeoutMs, commitment) {
9
9
  this.isSubscribed = false;
10
10
  this.program = program;
11
11
  this.insuranceFundStakeAccountPublicKey =
12
12
  insuranceFundStakeAccountPublicKey;
13
13
  this.eventEmitter = new events_1.EventEmitter();
14
- this.reconnectTimeoutMs = reconnectTimeoutMs;
14
+ this.resubTimeoutMs = resubTimeoutMs;
15
15
  this.commitment = commitment;
16
16
  }
17
17
  async subscribe(insuranceFundStakeAccount) {
@@ -19,7 +19,9 @@ class WebSocketInsuranceFundStakeAccountSubscriber {
19
19
  return true;
20
20
  }
21
21
  this.insuranceFundStakeDataAccountSubscriber =
22
- new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('insuranceFundStake', this.program, this.insuranceFundStakeAccountPublicKey, undefined, this.reconnectTimeoutMs, this.commitment);
22
+ new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('insuranceFundStake', this.program, this.insuranceFundStakeAccountPublicKey, undefined, {
23
+ resubTimeoutMs: this.resubTimeoutMs,
24
+ }, this.commitment);
23
25
  if (insuranceFundStakeAccount) {
24
26
  this.insuranceFundStakeDataAccountSubscriber.setData(insuranceFundStakeAccount);
25
27
  }
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
- import { DataAndSlot, BufferAndSlot, ProgramAccountSubscriber } from './types';
3
+ import { DataAndSlot, BufferAndSlot, ProgramAccountSubscriber, ResubOpts } from './types';
4
4
  import { Program } from '@coral-xyz/anchor';
5
5
  import { Commitment, Context, KeyedAccountInfo, MemcmpFilter, PublicKey } from '@solana/web3.js';
6
6
  export declare class WebSocketProgramAccountSubscriber<T> implements ProgramAccountSubscriber<T> {
@@ -14,7 +14,7 @@ export declare class WebSocketProgramAccountSubscriber<T> implements ProgramAcco
14
14
  decodeBuffer: (accountName: string, ix: Buffer) => T;
15
15
  onChange: (accountId: PublicKey, data: T, context: Context, buffer: Buffer) => void;
16
16
  listenerId?: number;
17
- resubTimeoutMs?: number;
17
+ resubOpts?: ResubOpts;
18
18
  isUnsubscribing: boolean;
19
19
  timeoutId?: NodeJS.Timeout;
20
20
  options: {
@@ -25,7 +25,7 @@ export declare class WebSocketProgramAccountSubscriber<T> implements ProgramAcco
25
25
  constructor(subscriptionName: string, accountDiscriminator: string, program: Program, decodeBufferFn: (accountName: string, ix: Buffer) => T, options?: {
26
26
  filters: MemcmpFilter[];
27
27
  commitment?: Commitment;
28
- }, resubTimeoutMs?: number);
28
+ }, resubOpts?: ResubOpts);
29
29
  subscribe(onChange: (accountId: PublicKey, data: T, context: Context, buffer: Buffer) => void): Promise<void>;
30
30
  private setTimeout;
31
31
  handleRpcResponse(context: Context, keyedAccountInfo: KeyedAccountInfo): void;
@@ -4,28 +4,30 @@ exports.WebSocketProgramAccountSubscriber = void 0;
4
4
  class WebSocketProgramAccountSubscriber {
5
5
  constructor(subscriptionName, accountDiscriminator, program, decodeBufferFn, options = {
6
6
  filters: [],
7
- }, resubTimeoutMs) {
7
+ }, resubOpts) {
8
+ var _a;
8
9
  this.isUnsubscribing = false;
9
10
  this.receivingData = false;
10
11
  this.subscriptionName = subscriptionName;
11
12
  this.accountDiscriminator = accountDiscriminator;
12
13
  this.program = program;
13
14
  this.decodeBuffer = decodeBufferFn;
14
- this.resubTimeoutMs = resubTimeoutMs;
15
- if (this.resubTimeoutMs < 1000) {
15
+ this.resubOpts = resubOpts;
16
+ if (((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs) < 1000) {
16
17
  console.log('resubTimeoutMs should be at least 1000ms to avoid spamming resub');
17
18
  }
18
19
  this.options = options;
19
20
  this.receivingData = false;
20
21
  }
21
22
  async subscribe(onChange) {
22
- var _a;
23
+ var _a, _b;
23
24
  if (this.listenerId != null || this.isUnsubscribing) {
24
25
  return;
25
26
  }
26
27
  this.onChange = onChange;
27
28
  this.listenerId = this.program.provider.connection.onProgramAccountChange(this.program.programId, (keyedAccountInfo, context) => {
28
- if (this.resubTimeoutMs) {
29
+ var _a;
30
+ if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs) {
29
31
  this.receivingData = true;
30
32
  clearTimeout(this.timeoutId);
31
33
  this.handleRpcResponse(context, keyedAccountInfo);
@@ -35,27 +37,31 @@ class WebSocketProgramAccountSubscriber {
35
37
  this.handleRpcResponse(context, keyedAccountInfo);
36
38
  }
37
39
  }, (_a = this.options.commitment) !== null && _a !== void 0 ? _a : this.program.provider.opts.commitment, this.options.filters);
38
- if (this.resubTimeoutMs) {
40
+ if ((_b = this.resubOpts) === null || _b === void 0 ? void 0 : _b.resubTimeoutMs) {
39
41
  this.receivingData = true;
40
42
  this.setTimeout();
41
43
  }
42
44
  }
43
45
  setTimeout() {
46
+ var _a;
44
47
  if (!this.onChange) {
45
48
  throw new Error('onChange callback function must be set');
46
49
  }
47
50
  this.timeoutId = setTimeout(async () => {
51
+ var _a, _b;
48
52
  if (this.isUnsubscribing) {
49
53
  // If we are in the process of unsubscribing, do not attempt to resubscribe
50
54
  return;
51
55
  }
52
56
  if (this.receivingData) {
53
- console.log(`No ws data from ${this.subscriptionName} in ${this.resubTimeoutMs}ms, resubscribing`);
57
+ if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.logResubMessages) {
58
+ console.log(`No ws data from ${this.subscriptionName} in ${(_b = this.resubOpts) === null || _b === void 0 ? void 0 : _b.resubTimeoutMs}ms, resubscribing`);
59
+ }
54
60
  await this.unsubscribe(true);
55
61
  this.receivingData = false;
56
62
  await this.subscribe(this.onChange);
57
63
  }
58
- }, this.resubTimeoutMs);
64
+ }, (_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs);
59
65
  }
60
66
  handleRpcResponse(context, keyedAccountInfo) {
61
67
  const newSlot = context.slot;
@@ -99,7 +105,7 @@ class WebSocketProgramAccountSubscriber {
99
105
  }
100
106
  unsubscribe(onResub = false) {
101
107
  if (!onResub) {
102
- this.resubTimeoutMs = undefined;
108
+ this.resubOpts.resubTimeoutMs = undefined;
103
109
  }
104
110
  this.isUnsubscribing = true;
105
111
  clearTimeout(this.timeoutId);
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import { DataAndSlot, AccountSubscriber, UserAccountEvents, UserAccountSubscriber } from './types';
2
+ import { DataAndSlot, AccountSubscriber, UserAccountEvents, UserAccountSubscriber, ResubOpts } from './types';
3
3
  import { Program } from '@coral-xyz/anchor';
4
4
  import StrictEventEmitter from 'strict-event-emitter-types';
5
5
  import { EventEmitter } from 'events';
@@ -7,13 +7,13 @@ import { Commitment, PublicKey } from '@solana/web3.js';
7
7
  import { UserAccount } from '../types';
8
8
  export declare class WebSocketUserAccountSubscriber implements UserAccountSubscriber {
9
9
  isSubscribed: boolean;
10
- reconnectTimeoutMs?: number;
10
+ resubOpts?: ResubOpts;
11
11
  commitment?: Commitment;
12
12
  program: Program;
13
13
  eventEmitter: StrictEventEmitter<EventEmitter, UserAccountEvents>;
14
14
  userAccountPublicKey: PublicKey;
15
15
  userDataAccountSubscriber: AccountSubscriber<UserAccount>;
16
- constructor(program: Program, userAccountPublicKey: PublicKey, reconnectTimeoutMs?: number, commitment?: Commitment);
16
+ constructor(program: Program, userAccountPublicKey: PublicKey, resubOpts?: ResubOpts, commitment?: Commitment);
17
17
  subscribe(userAccount?: UserAccount): Promise<boolean>;
18
18
  fetch(): Promise<void>;
19
19
  unsubscribe(): Promise<void>;
@@ -5,19 +5,19 @@ const types_1 = require("./types");
5
5
  const events_1 = require("events");
6
6
  const webSocketAccountSubscriber_1 = require("./webSocketAccountSubscriber");
7
7
  class WebSocketUserAccountSubscriber {
8
- constructor(program, userAccountPublicKey, reconnectTimeoutMs, commitment) {
8
+ constructor(program, userAccountPublicKey, resubOpts, commitment) {
9
9
  this.isSubscribed = false;
10
10
  this.program = program;
11
+ this.resubOpts = resubOpts;
11
12
  this.userAccountPublicKey = userAccountPublicKey;
12
13
  this.eventEmitter = new events_1.EventEmitter();
13
- this.reconnectTimeoutMs = reconnectTimeoutMs;
14
14
  this.commitment = commitment;
15
15
  }
16
16
  async subscribe(userAccount) {
17
17
  if (this.isSubscribed) {
18
18
  return true;
19
19
  }
20
- this.userDataAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('user', this.program, this.userAccountPublicKey, undefined, this.reconnectTimeoutMs, this.commitment);
20
+ this.userDataAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('user', this.program, this.userAccountPublicKey, undefined, this.resubOpts, this.commitment);
21
21
  if (userAccount) {
22
22
  this.userDataAccountSubscriber.setData(userAccount);
23
23
  }
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import { DataAndSlot, AccountSubscriber, UserStatsAccountSubscriber, UserStatsAccountEvents } from './types';
2
+ import { DataAndSlot, AccountSubscriber, UserStatsAccountSubscriber, UserStatsAccountEvents, ResubOpts } from './types';
3
3
  import { Program } from '@coral-xyz/anchor';
4
4
  import StrictEventEmitter from 'strict-event-emitter-types';
5
5
  import { EventEmitter } from 'events';
@@ -7,13 +7,13 @@ import { Commitment, PublicKey } from '@solana/web3.js';
7
7
  import { UserStatsAccount } from '../types';
8
8
  export declare class WebSocketUserStatsAccountSubscriber implements UserStatsAccountSubscriber {
9
9
  isSubscribed: boolean;
10
- reconnectTimeoutMs?: number;
10
+ resubOpts?: ResubOpts;
11
11
  commitment?: Commitment;
12
12
  program: Program;
13
13
  eventEmitter: StrictEventEmitter<EventEmitter, UserStatsAccountEvents>;
14
14
  userStatsAccountPublicKey: PublicKey;
15
15
  userStatsAccountSubscriber: AccountSubscriber<UserStatsAccount>;
16
- constructor(program: Program, userStatsAccountPublicKey: PublicKey, reconnectTimeoutMs?: number, commitment?: Commitment);
16
+ constructor(program: Program, userStatsAccountPublicKey: PublicKey, resubOpts?: ResubOpts, commitment?: Commitment);
17
17
  subscribe(userStatsAccount?: UserStatsAccount): Promise<boolean>;
18
18
  fetch(): Promise<void>;
19
19
  unsubscribe(): Promise<void>;
@@ -5,19 +5,19 @@ const types_1 = require("./types");
5
5
  const events_1 = require("events");
6
6
  const webSocketAccountSubscriber_1 = require("./webSocketAccountSubscriber");
7
7
  class WebSocketUserStatsAccountSubscriber {
8
- constructor(program, userStatsAccountPublicKey, reconnectTimeoutMs, commitment) {
8
+ constructor(program, userStatsAccountPublicKey, resubOpts, commitment) {
9
9
  this.isSubscribed = false;
10
10
  this.program = program;
11
11
  this.userStatsAccountPublicKey = userStatsAccountPublicKey;
12
12
  this.eventEmitter = new events_1.EventEmitter();
13
- this.reconnectTimeoutMs = reconnectTimeoutMs;
13
+ this.resubOpts = resubOpts;
14
14
  this.commitment = commitment;
15
15
  }
16
16
  async subscribe(userStatsAccount) {
17
17
  if (this.isSubscribed) {
18
18
  return true;
19
19
  }
20
- this.userStatsAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('userStats', this.program, this.userStatsAccountPublicKey, undefined, this.reconnectTimeoutMs, this.commitment);
20
+ this.userStatsAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('userStats', this.program, this.userStatsAccountPublicKey, undefined, this.resubOpts, this.commitment);
21
21
  if (userStatsAccount) {
22
22
  this.userStatsAccountSubscriber.setData(userStatsAccount);
23
23
  }
@@ -5,10 +5,10 @@ import { EventEmitter } from 'events';
5
5
  export declare class AuctionSubscriber {
6
6
  private driftClient;
7
7
  private opts;
8
- private resubTimeoutMs?;
8
+ private resubOpts?;
9
9
  eventEmitter: StrictEventEmitter<EventEmitter, AuctionSubscriberEvents>;
10
10
  private subscriber;
11
- constructor({ driftClient, opts, resubTimeoutMs }: AuctionSubscriberConfig);
11
+ constructor({ driftClient, opts, resubTimeoutMs, logResubMessages, }: AuctionSubscriberConfig);
12
12
  subscribe(): Promise<void>;
13
13
  unsubscribe(): Promise<void>;
14
14
  }
@@ -5,18 +5,18 @@ const memcmp_1 = require("../memcmp");
5
5
  const events_1 = require("events");
6
6
  const webSocketProgramAccountSubscriber_1 = require("../accounts/webSocketProgramAccountSubscriber");
7
7
  class AuctionSubscriber {
8
- constructor({ driftClient, opts, resubTimeoutMs }) {
8
+ constructor({ driftClient, opts, resubTimeoutMs, logResubMessages, }) {
9
9
  this.driftClient = driftClient;
10
10
  this.opts = opts || this.driftClient.opts;
11
11
  this.eventEmitter = new events_1.EventEmitter();
12
- this.resubTimeoutMs = resubTimeoutMs;
12
+ this.resubOpts = { resubTimeoutMs, logResubMessages };
13
13
  }
14
14
  async subscribe() {
15
15
  if (!this.subscriber) {
16
16
  this.subscriber = new webSocketProgramAccountSubscriber_1.WebSocketProgramAccountSubscriber('AuctionSubscriber', 'User', this.driftClient.program, this.driftClient.program.account.user.coder.accounts.decode.bind(this.driftClient.program.account.user.coder.accounts), {
17
17
  filters: [(0, memcmp_1.getUserFilter)(), (0, memcmp_1.getUserWithAuctionFilter)()],
18
18
  commitment: this.opts.commitment,
19
- }, this.resubTimeoutMs);
19
+ }, this.resubOpts);
20
20
  }
21
21
  await this.subscriber.subscribe((accountId, data, context) => {
22
22
  this.eventEmitter.emit('onAccountUpdate', data, accountId, context.slot);
@@ -5,6 +5,7 @@ export type AuctionSubscriberConfig = {
5
5
  driftClient: DriftClient;
6
6
  opts?: ConfirmOptions;
7
7
  resubTimeoutMs?: number;
8
+ logResubMessages?: boolean;
8
9
  };
9
10
  export interface AuctionSubscriberEvents {
10
11
  onAccountUpdate: (account: UserAccount, pubkey: PublicKey, slot: number) => void;
@@ -657,6 +657,16 @@ export declare class DriftClient {
657
657
  getDepositIntoSpotMarketRevenuePoolIx(marketIndex: number, amount: BN, userTokenAccountPublicKey: PublicKey): Promise<TransactionInstruction>;
658
658
  depositIntoSpotMarketRevenuePool(marketIndex: number, amount: BN, userTokenAccountPublicKey: PublicKey): Promise<TransactionSignature>;
659
659
  getPerpMarketExtendedInfo(marketIndex: number): PerpMarketExtendedInfo;
660
+ /**
661
+ * Calculates taker / maker fee (as a percentage, e.g. .001 = 10 basis points) for particular marketType
662
+ * @param marketType
663
+ * @param positionMarketIndex
664
+ * @returns : {takerFee: number, makerFee: number} Precision None
665
+ */
666
+ getMarketFees(marketType: MarketType, marketIndex?: number, user?: User): {
667
+ takerFee: number;
668
+ makerFee: number;
669
+ };
660
670
  /**
661
671
  * Returns the market index and type for a given market name
662
672
  * E.g. "SOL-PERP" -> { marketIndex: 0, marketType: MarketType.PERP }
@@ -70,7 +70,7 @@ class DriftClient {
70
70
  this._isSubscribed = val;
71
71
  }
72
72
  constructor(config) {
73
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2;
73
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5;
74
74
  this.users = new Map();
75
75
  this._isSubscribed = false;
76
76
  this.perpMarketLastSlotCache = new Map();
@@ -125,12 +125,14 @@ class DriftClient {
125
125
  this.userAccountSubscriptionConfig = {
126
126
  type: 'websocket',
127
127
  resubTimeoutMs: (_p = config.accountSubscription) === null || _p === void 0 ? void 0 : _p.resubTimeoutMs,
128
- commitment: (_q = config.accountSubscription) === null || _q === void 0 ? void 0 : _q.commitment,
128
+ logResubMessages: (_q = config.accountSubscription) === null || _q === void 0 ? void 0 : _q.logResubMessages,
129
+ commitment: (_r = config.accountSubscription) === null || _r === void 0 ? void 0 : _r.commitment,
129
130
  };
130
131
  this.userStatsAccountSubscriptionConfig = {
131
132
  type: 'websocket',
132
- resubTimeoutMs: (_r = config.accountSubscription) === null || _r === void 0 ? void 0 : _r.resubTimeoutMs,
133
- commitment: (_s = config.accountSubscription) === null || _s === void 0 ? void 0 : _s.commitment,
133
+ resubTimeoutMs: (_s = config.accountSubscription) === null || _s === void 0 ? void 0 : _s.resubTimeoutMs,
134
+ logResubMessages: (_t = config.accountSubscription) === null || _t === void 0 ? void 0 : _t.logResubMessages,
135
+ commitment: (_u = config.accountSubscription) === null || _u === void 0 ? void 0 : _u.commitment,
134
136
  };
135
137
  }
136
138
  if (config.userStats) {
@@ -147,11 +149,14 @@ class DriftClient {
147
149
  const noMarketsAndOraclesSpecified = config.perpMarketIndexes === undefined &&
148
150
  config.spotMarketIndexes === undefined &&
149
151
  config.oracleInfos === undefined;
150
- if (((_t = config.accountSubscription) === null || _t === void 0 ? void 0 : _t.type) === 'polling') {
151
- this.accountSubscriber = new pollingDriftClientAccountSubscriber_1.PollingDriftClientAccountSubscriber(this.program, config.accountSubscription.accountLoader, (_u = config.perpMarketIndexes) !== null && _u !== void 0 ? _u : [], (_v = config.spotMarketIndexes) !== null && _v !== void 0 ? _v : [], (_w = config.oracleInfos) !== null && _w !== void 0 ? _w : [], noMarketsAndOraclesSpecified);
152
+ if (((_v = config.accountSubscription) === null || _v === void 0 ? void 0 : _v.type) === 'polling') {
153
+ this.accountSubscriber = new pollingDriftClientAccountSubscriber_1.PollingDriftClientAccountSubscriber(this.program, config.accountSubscription.accountLoader, (_w = config.perpMarketIndexes) !== null && _w !== void 0 ? _w : [], (_x = config.spotMarketIndexes) !== null && _x !== void 0 ? _x : [], (_y = config.oracleInfos) !== null && _y !== void 0 ? _y : [], noMarketsAndOraclesSpecified);
152
154
  }
153
155
  else {
154
- this.accountSubscriber = new webSocketDriftClientAccountSubscriber_1.WebSocketDriftClientAccountSubscriber(this.program, (_x = config.perpMarketIndexes) !== null && _x !== void 0 ? _x : [], (_y = config.spotMarketIndexes) !== null && _y !== void 0 ? _y : [], (_z = config.oracleInfos) !== null && _z !== void 0 ? _z : [], noMarketsAndOraclesSpecified, (_0 = config.accountSubscription) === null || _0 === void 0 ? void 0 : _0.resubTimeoutMs, (_1 = config.accountSubscription) === null || _1 === void 0 ? void 0 : _1.commitment);
156
+ this.accountSubscriber = new webSocketDriftClientAccountSubscriber_1.WebSocketDriftClientAccountSubscriber(this.program, (_z = config.perpMarketIndexes) !== null && _z !== void 0 ? _z : [], (_0 = config.spotMarketIndexes) !== null && _0 !== void 0 ? _0 : [], (_1 = config.oracleInfos) !== null && _1 !== void 0 ? _1 : [], noMarketsAndOraclesSpecified, {
157
+ resubTimeoutMs: (_2 = config.accountSubscription) === null || _2 === void 0 ? void 0 : _2.resubTimeoutMs,
158
+ logResubMessages: (_3 = config.accountSubscription) === null || _3 === void 0 ? void 0 : _3.logResubMessages,
159
+ }, (_4 = config.accountSubscription) === null || _4 === void 0 ? void 0 : _4.commitment);
155
160
  }
156
161
  this.eventEmitter = this.accountSubscriber.eventEmitter;
157
162
  if (config.enableMetricsEvents) {
@@ -159,7 +164,7 @@ class DriftClient {
159
164
  this.metricsEventEmitter = new events_1.EventEmitter();
160
165
  }
161
166
  this.txSender =
162
- (_2 = config.txSender) !== null && _2 !== void 0 ? _2 : new retryTxSender_1.RetryTxSender({
167
+ (_5 = config.txSender) !== null && _5 !== void 0 ? _5 : new retryTxSender_1.RetryTxSender({
163
168
  connection: this.connection,
164
169
  wallet: this.wallet,
165
170
  opts: this.opts,
@@ -3548,6 +3553,41 @@ class DriftClient {
3548
3553
  };
3549
3554
  return extendedInfo;
3550
3555
  }
3556
+ /**
3557
+ * Calculates taker / maker fee (as a percentage, e.g. .001 = 10 basis points) for particular marketType
3558
+ * @param marketType
3559
+ * @param positionMarketIndex
3560
+ * @returns : {takerFee: number, makerFee: number} Precision None
3561
+ */
3562
+ getMarketFees(marketType, marketIndex, user) {
3563
+ let feeTier;
3564
+ if (user) {
3565
+ feeTier = user.getUserFeeTier(marketType);
3566
+ }
3567
+ else {
3568
+ const state = this.getStateAccount();
3569
+ feeTier = (0, types_1.isVariant)(marketType, 'perp')
3570
+ ? state.perpFeeStructure.feeTiers[0]
3571
+ : state.spotFeeStructure.feeTiers[0];
3572
+ }
3573
+ let takerFee = feeTier.feeNumerator / feeTier.feeDenominator;
3574
+ let makerFee = feeTier.makerRebateNumerator / feeTier.makerRebateDenominator;
3575
+ if (marketIndex !== undefined) {
3576
+ let marketAccount = null;
3577
+ if ((0, types_1.isVariant)(marketType, 'perp')) {
3578
+ marketAccount = this.getPerpMarketAccount(marketIndex);
3579
+ }
3580
+ else {
3581
+ marketAccount = this.getSpotMarketAccount(marketIndex);
3582
+ }
3583
+ takerFee += (takerFee * marketAccount.feeAdjustment) / 100;
3584
+ makerFee += (makerFee * marketAccount.feeAdjustment) / 100;
3585
+ }
3586
+ return {
3587
+ takerFee,
3588
+ makerFee,
3589
+ };
3590
+ }
3551
3591
  /**
3552
3592
  * Returns the market index and type for a given market name
3553
3593
  * E.g. "SOL-PERP" -> { marketIndex: 0, marketType: MarketType.PERP }
@@ -30,6 +30,7 @@ export type DriftClientConfig = {
30
30
  export type DriftClientSubscriptionConfig = {
31
31
  type: 'websocket';
32
32
  resubTimeoutMs?: number;
33
+ logResubMessages?: boolean;
33
34
  commitment?: Commitment;
34
35
  } | {
35
36
  type: 'polling';
@@ -12,7 +12,7 @@ const index_1 = require("../index");
12
12
  const user_1 = require("../decode/user");
13
13
  class OrderSubscriber {
14
14
  constructor(config) {
15
- var _a;
15
+ var _a, _b, _c;
16
16
  this.usersAccounts = new Map();
17
17
  this.driftClient = config.driftClient;
18
18
  this.commitment = config.subscriptionConfig.commitment || 'processed';
@@ -27,12 +27,15 @@ class OrderSubscriber {
27
27
  orderSubscriber: this,
28
28
  commitment: this.commitment,
29
29
  skipInitialLoad: config.subscriptionConfig.skipInitialLoad,
30
- resubTimeoutMs: config.subscriptionConfig.resubTimeoutMs,
30
+ resubOpts: {
31
+ resubTimeoutMs: (_a = config.subscriptionConfig) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs,
32
+ logResubMessages: (_b = config.subscriptionConfig) === null || _b === void 0 ? void 0 : _b.logResubMessages,
33
+ },
31
34
  resyncIntervalMs: config.subscriptionConfig.resyncIntervalMs,
32
35
  decoded: config.decodeData,
33
36
  });
34
37
  }
35
- if ((_a = config.fastDecode) !== null && _a !== void 0 ? _a : true) {
38
+ if ((_c = config.fastDecode) !== null && _c !== void 0 ? _c : true) {
36
39
  this.decodeFn = (name, data) => (0, user_1.decodeUser)(data);
37
40
  }
38
41
  else {