@drift-labs/sdk 2.142.0-beta.12 → 2.142.0-beta.14

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.142.0-beta.12
1
+ 2.142.0-beta.14
@@ -1,24 +1,64 @@
1
- import { WebSocketDriftClientAccountSubscriber } from './webSocketDriftClientAccountSubscriber';
2
- import { OracleInfo } from '../oracles/types';
1
+ /// <reference types="node" />
2
+ import StrictEventEmitter from 'strict-event-emitter-types';
3
+ import { EventEmitter } from 'events';
4
+ import { OracleInfo, OraclePriceData } from '../oracles/types';
3
5
  import { Program } from '@coral-xyz/anchor';
4
- import { DataAndSlot, DelistedMarketSetting, GrpcConfigs, ResubOpts } from './types';
5
- import { PerpMarketAccount, SpotMarketAccount } from '../types';
6
- export declare class grpcDriftClientAccountSubscriberV2 extends WebSocketDriftClientAccountSubscriber {
6
+ import { PublicKey } from '@solana/web3.js';
7
+ import { AccountSubscriber, DataAndSlot, DelistedMarketSetting, DriftClientAccountEvents, DriftClientAccountSubscriber, GrpcConfigs, ResubOpts } from './types';
8
+ import { PerpMarketAccount, SpotMarketAccount, StateAccount } from '../types';
9
+ import { OracleClientCache } from '../oracles/oracleClientCache';
10
+ export declare class grpcDriftClientAccountSubscriberV2 implements DriftClientAccountSubscriber {
7
11
  private grpcConfigs;
8
12
  private perpMarketsSubscriber?;
9
13
  private spotMarketsSubscriber?;
10
14
  private oracleMultiSubscriber?;
11
15
  private perpMarketIndexToAccountPubkeyMap;
12
16
  private spotMarketIndexToAccountPubkeyMap;
17
+ private delistedMarketSetting;
18
+ eventEmitter: StrictEventEmitter<EventEmitter, DriftClientAccountEvents>;
19
+ isSubscribed: boolean;
20
+ isSubscribing: boolean;
21
+ program: Program;
22
+ perpMarketIndexes: number[];
23
+ spotMarketIndexes: number[];
24
+ shouldFindAllMarketsAndOracles: boolean;
25
+ oracleInfos: OracleInfo[];
26
+ initialPerpMarketAccountData: Map<number, PerpMarketAccount>;
27
+ initialSpotMarketAccountData: Map<number, SpotMarketAccount>;
28
+ initialOraclePriceData: Map<string, OraclePriceData>;
29
+ perpOracleMap: Map<number, PublicKey>;
30
+ perpOracleStringMap: Map<number, string>;
31
+ spotOracleMap: Map<number, PublicKey>;
32
+ spotOracleStringMap: Map<number, string>;
33
+ stateAccountSubscriber?: AccountSubscriber<StateAccount>;
34
+ oracleClientCache: OracleClientCache;
35
+ private resubOpts?;
36
+ private subscriptionPromise;
37
+ protected subscriptionPromiseResolver: (val: boolean) => void;
13
38
  constructor(grpcConfigs: GrpcConfigs, program: Program, perpMarketIndexes: number[], spotMarketIndexes: number[], oracleInfos: OracleInfo[], shouldFindAllMarketsAndOracles: boolean, delistedMarketSetting: DelistedMarketSetting, resubOpts?: ResubOpts);
39
+ chunks: <T>(array: readonly T[], size: number) => T[][];
40
+ setInitialData(): Promise<void>;
41
+ addPerpMarket(_marketIndex: number): Promise<boolean>;
42
+ addSpotMarket(_marketIndex: number): Promise<boolean>;
43
+ addOracle(oracleInfo: OracleInfo): Promise<boolean>;
14
44
  subscribe(): Promise<boolean>;
45
+ fetch(): Promise<void>;
46
+ private assertIsSubscribed;
47
+ getStateAccountAndSlot(): DataAndSlot<StateAccount>;
48
+ getMarketAccountsAndSlots(): DataAndSlot<PerpMarketAccount>[];
49
+ getSpotMarketAccountsAndSlots(): DataAndSlot<SpotMarketAccount>[];
15
50
  getMarketAccountAndSlot(marketIndex: number): DataAndSlot<PerpMarketAccount> | undefined;
16
51
  getSpotMarketAccountAndSlot(marketIndex: number): DataAndSlot<SpotMarketAccount> | undefined;
52
+ getOraclePriceDataAndSlot(oracleId: string): DataAndSlot<OraclePriceData> | undefined;
53
+ getOraclePriceDataAndSlotForPerpMarket(marketIndex: number): DataAndSlot<OraclePriceData> | undefined;
54
+ getOraclePriceDataAndSlotForSpotMarket(marketIndex: number): DataAndSlot<OraclePriceData> | undefined;
17
55
  setPerpOracleMap(): Promise<void>;
18
56
  setSpotOracleMap(): Promise<void>;
19
57
  subscribeToPerpMarketAccounts(): Promise<boolean>;
20
58
  subscribeToSpotMarketAccounts(): Promise<boolean>;
21
59
  subscribeToOracles(): Promise<boolean>;
60
+ handleDelistedMarkets(): Promise<void>;
61
+ removeInitialData(): void;
22
62
  unsubscribeFromOracles(): Promise<void>;
23
63
  unsubscribe(): Promise<void>;
24
64
  }
@@ -1,19 +1,114 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.grpcDriftClientAccountSubscriberV2 = void 0;
4
- const webSocketDriftClientAccountSubscriber_1 = require("./webSocketDriftClientAccountSubscriber");
4
+ const events_1 = require("events");
5
5
  const web3_js_1 = require("@solana/web3.js");
6
6
  const config_1 = require("../config");
7
7
  const pda_1 = require("../addresses/pda");
8
+ const types_1 = require("./types");
8
9
  const grpcAccountSubscriber_1 = require("./grpcAccountSubscriber");
9
10
  const grpcMultiAccountSubscriber_1 = require("./grpcMultiAccountSubscriber");
10
11
  const oracleId_1 = require("../oracles/oracleId");
11
- class grpcDriftClientAccountSubscriberV2 extends webSocketDriftClientAccountSubscriber_1.WebSocketDriftClientAccountSubscriber {
12
+ const oracleClientCache_1 = require("../oracles/oracleClientCache");
13
+ const utils_1 = require("./utils");
14
+ class grpcDriftClientAccountSubscriberV2 {
12
15
  constructor(grpcConfigs, program, perpMarketIndexes, spotMarketIndexes, oracleInfos, shouldFindAllMarketsAndOracles, delistedMarketSetting, resubOpts) {
13
- super(program, perpMarketIndexes, spotMarketIndexes, oracleInfos, shouldFindAllMarketsAndOracles, delistedMarketSetting, resubOpts);
14
16
  this.perpMarketIndexToAccountPubkeyMap = new Map();
15
17
  this.spotMarketIndexToAccountPubkeyMap = new Map();
18
+ this.perpOracleMap = new Map();
19
+ this.perpOracleStringMap = new Map();
20
+ this.spotOracleMap = new Map();
21
+ this.spotOracleStringMap = new Map();
22
+ this.oracleClientCache = new oracleClientCache_1.OracleClientCache();
23
+ this.chunks = (array, size) => {
24
+ return new Array(Math.ceil(array.length / size))
25
+ .fill(null)
26
+ .map((_, index) => index * size)
27
+ .map((begin) => array.slice(begin, begin + size));
28
+ };
29
+ this.eventEmitter = new events_1.EventEmitter();
30
+ this.isSubscribed = false;
31
+ this.isSubscribing = false;
32
+ this.program = program;
33
+ this.perpMarketIndexes = perpMarketIndexes;
34
+ this.spotMarketIndexes = spotMarketIndexes;
35
+ this.shouldFindAllMarketsAndOracles = shouldFindAllMarketsAndOracles;
36
+ this.oracleInfos = oracleInfos;
37
+ this.initialPerpMarketAccountData = new Map();
38
+ this.initialSpotMarketAccountData = new Map();
39
+ this.initialOraclePriceData = new Map();
40
+ this.perpOracleMap = new Map();
41
+ this.perpOracleStringMap = new Map();
42
+ this.spotOracleMap = new Map();
43
+ this.spotOracleStringMap = new Map();
16
44
  this.grpcConfigs = grpcConfigs;
45
+ this.resubOpts = resubOpts;
46
+ this.delistedMarketSetting = delistedMarketSetting;
47
+ }
48
+ async setInitialData() {
49
+ const connection = this.program.provider.connection;
50
+ if (!this.initialPerpMarketAccountData ||
51
+ this.initialPerpMarketAccountData.size === 0) {
52
+ const perpMarketPublicKeys = this.perpMarketIndexes.map((marketIndex) => (0, pda_1.getPerpMarketPublicKeySync)(this.program.programId, marketIndex));
53
+ const perpMarketPublicKeysChunks = this.chunks(perpMarketPublicKeys, 75);
54
+ const perpMarketAccountInfos = (await Promise.all(perpMarketPublicKeysChunks.map((perpMarketPublicKeysChunk) => connection.getMultipleAccountsInfo(perpMarketPublicKeysChunk)))).flat();
55
+ this.initialPerpMarketAccountData = new Map(perpMarketAccountInfos
56
+ .filter((accountInfo) => !!accountInfo)
57
+ .map((accountInfo) => {
58
+ const perpMarket = this.program.coder.accounts.decode('PerpMarket', accountInfo.data);
59
+ return [perpMarket.marketIndex, perpMarket];
60
+ }));
61
+ }
62
+ if (!this.initialSpotMarketAccountData ||
63
+ this.initialSpotMarketAccountData.size === 0) {
64
+ const spotMarketPublicKeys = this.spotMarketIndexes.map((marketIndex) => (0, pda_1.getSpotMarketPublicKeySync)(this.program.programId, marketIndex));
65
+ const spotMarketPublicKeysChunks = this.chunks(spotMarketPublicKeys, 75);
66
+ const spotMarketAccountInfos = (await Promise.all(spotMarketPublicKeysChunks.map((spotMarketPublicKeysChunk) => connection.getMultipleAccountsInfo(spotMarketPublicKeysChunk)))).flat();
67
+ this.initialSpotMarketAccountData = new Map(spotMarketAccountInfos
68
+ .filter((accountInfo) => !!accountInfo)
69
+ .map((accountInfo) => {
70
+ const spotMarket = this.program.coder.accounts.decode('SpotMarket', accountInfo.data);
71
+ return [spotMarket.marketIndex, spotMarket];
72
+ }));
73
+ }
74
+ const oracleAccountPubkeyChunks = this.chunks(this.oracleInfos.map((oracleInfo) => oracleInfo.publicKey), 75);
75
+ const oracleAccountInfos = (await Promise.all(oracleAccountPubkeyChunks.map((oracleAccountPublicKeysChunk) => connection.getMultipleAccountsInfo(oracleAccountPublicKeysChunk)))).flat();
76
+ this.initialOraclePriceData = new Map(this.oracleInfos.reduce((result, oracleInfo, i) => {
77
+ if (!oracleAccountInfos[i]) {
78
+ return result;
79
+ }
80
+ const oracleClient = this.oracleClientCache.get(oracleInfo.source, connection, this.program);
81
+ const oraclePriceData = oracleClient.getOraclePriceDataFromBuffer(oracleAccountInfos[i].data);
82
+ result.push([
83
+ (0, oracleId_1.getOracleId)(oracleInfo.publicKey, oracleInfo.source),
84
+ oraclePriceData,
85
+ ]);
86
+ return result;
87
+ }, []));
88
+ }
89
+ async addPerpMarket(_marketIndex) {
90
+ if (!this.perpMarketIndexes.includes(_marketIndex)) {
91
+ this.perpMarketIndexes = this.perpMarketIndexes.concat(_marketIndex);
92
+ }
93
+ return true;
94
+ }
95
+ async addSpotMarket(_marketIndex) {
96
+ return true;
97
+ }
98
+ async addOracle(oracleInfo) {
99
+ if (oracleInfo.publicKey.equals(web3_js_1.PublicKey.default)) {
100
+ return true;
101
+ }
102
+ const exists = this.oracleInfos.some((o) => o.source === oracleInfo.source &&
103
+ o.publicKey.equals(oracleInfo.publicKey));
104
+ if (!exists) {
105
+ this.oracleInfos = this.oracleInfos.concat(oracleInfo);
106
+ }
107
+ if (this.oracleMultiSubscriber) {
108
+ await this.unsubscribeFromOracles();
109
+ await this.subscribeToOracles();
110
+ }
111
+ return true;
17
112
  }
18
113
  async subscribe() {
19
114
  if (this.isSubscribed) {
@@ -61,6 +156,32 @@ class grpcDriftClientAccountSubscriberV2 extends webSocketDriftClientAccountSubs
61
156
  this.removeInitialData();
62
157
  return true;
63
158
  }
159
+ async fetch() {
160
+ var _a, _b, _c, _d;
161
+ await ((_a = this.stateAccountSubscriber) === null || _a === void 0 ? void 0 : _a.fetch());
162
+ await ((_b = this.perpMarketsSubscriber) === null || _b === void 0 ? void 0 : _b.fetch());
163
+ await ((_c = this.spotMarketsSubscriber) === null || _c === void 0 ? void 0 : _c.fetch());
164
+ await ((_d = this.oracleMultiSubscriber) === null || _d === void 0 ? void 0 : _d.fetch());
165
+ }
166
+ assertIsSubscribed() {
167
+ if (!this.isSubscribed) {
168
+ throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
169
+ }
170
+ }
171
+ getStateAccountAndSlot() {
172
+ this.assertIsSubscribed();
173
+ return this.stateAccountSubscriber.dataAndSlot;
174
+ }
175
+ getMarketAccountsAndSlots() {
176
+ var _a, _b;
177
+ const map = (_a = this.perpMarketsSubscriber) === null || _a === void 0 ? void 0 : _a.getAccountDataMap();
178
+ return Array.from((_b = map === null || map === void 0 ? void 0 : map.values()) !== null && _b !== void 0 ? _b : []);
179
+ }
180
+ getSpotMarketAccountsAndSlots() {
181
+ var _a, _b;
182
+ const map = (_a = this.spotMarketsSubscriber) === null || _a === void 0 ? void 0 : _a.getAccountDataMap();
183
+ return Array.from((_b = map === null || map === void 0 ? void 0 : map.values()) !== null && _b !== void 0 ? _b : []);
184
+ }
64
185
  getMarketAccountAndSlot(marketIndex) {
65
186
  var _a;
66
187
  return (_a = this.perpMarketsSubscriber) === null || _a === void 0 ? void 0 : _a.getAccountData(this.perpMarketIndexToAccountPubkeyMap.get(marketIndex));
@@ -69,8 +190,40 @@ class grpcDriftClientAccountSubscriberV2 extends webSocketDriftClientAccountSubs
69
190
  var _a;
70
191
  return (_a = this.spotMarketsSubscriber) === null || _a === void 0 ? void 0 : _a.getAccountData(this.spotMarketIndexToAccountPubkeyMap.get(marketIndex));
71
192
  }
72
- async setPerpOracleMap() {
193
+ getOraclePriceDataAndSlot(oracleId) {
73
194
  var _a;
195
+ this.assertIsSubscribed();
196
+ const { publicKey } = (0, oracleId_1.getPublicKeyAndSourceFromOracleId)(oracleId);
197
+ return (_a = this.oracleMultiSubscriber) === null || _a === void 0 ? void 0 : _a.getAccountData(publicKey.toBase58());
198
+ }
199
+ getOraclePriceDataAndSlotForPerpMarket(marketIndex) {
200
+ const perpMarketAccount = this.getMarketAccountAndSlot(marketIndex);
201
+ const oracle = this.perpOracleMap.get(marketIndex);
202
+ const oracleId = this.perpOracleStringMap.get(marketIndex);
203
+ if (!perpMarketAccount || !oracleId) {
204
+ return undefined;
205
+ }
206
+ if (!perpMarketAccount.data.amm.oracle.equals(oracle)) {
207
+ // If the oracle has changed, we need to update the oracle map in background
208
+ this.setPerpOracleMap();
209
+ }
210
+ return this.getOraclePriceDataAndSlot(oracleId);
211
+ }
212
+ getOraclePriceDataAndSlotForSpotMarket(marketIndex) {
213
+ const spotMarketAccount = this.getSpotMarketAccountAndSlot(marketIndex);
214
+ const oracle = this.spotOracleMap.get(marketIndex);
215
+ const oracleId = this.spotOracleStringMap.get(marketIndex);
216
+ if (!spotMarketAccount || !oracleId) {
217
+ return undefined;
218
+ }
219
+ if (!spotMarketAccount.data.oracle.equals(oracle)) {
220
+ // If the oracle has changed, we need to update the oracle map in background
221
+ this.setSpotOracleMap();
222
+ }
223
+ return this.getOraclePriceDataAndSlot(oracleId);
224
+ }
225
+ async setPerpOracleMap() {
226
+ var _a, _b;
74
227
  const perpMarketsMap = (_a = this.perpMarketsSubscriber) === null || _a === void 0 ? void 0 : _a.getAccountDataMap();
75
228
  const perpMarkets = Array.from(perpMarketsMap.values());
76
229
  const addOraclePromises = [];
@@ -82,7 +235,7 @@ class grpcDriftClientAccountSubscriberV2 extends webSocketDriftClientAccountSubs
82
235
  const perpMarketIndex = perpMarketAccount.marketIndex;
83
236
  const oracle = perpMarketAccount.amm.oracle;
84
237
  const oracleId = (0, oracleId_1.getOracleId)(oracle, perpMarket.data.amm.oracleSource);
85
- if (!this.oracleSubscribers.has(oracleId)) {
238
+ if (!((_b = this.oracleMultiSubscriber) === null || _b === void 0 ? void 0 : _b.getAccountDataMap().has(oracleId))) {
86
239
  addOraclePromises.push(this.addOracle({
87
240
  publicKey: oracle,
88
241
  source: perpMarket.data.amm.oracleSource,
@@ -94,7 +247,7 @@ class grpcDriftClientAccountSubscriberV2 extends webSocketDriftClientAccountSubs
94
247
  await Promise.all(addOraclePromises);
95
248
  }
96
249
  async setSpotOracleMap() {
97
- var _a;
250
+ var _a, _b;
98
251
  const spotMarketsMap = (_a = this.spotMarketsSubscriber) === null || _a === void 0 ? void 0 : _a.getAccountDataMap();
99
252
  const spotMarkets = Array.from(spotMarketsMap.values());
100
253
  const addOraclePromises = [];
@@ -106,7 +259,7 @@ class grpcDriftClientAccountSubscriberV2 extends webSocketDriftClientAccountSubs
106
259
  const spotMarketIndex = spotMarketAccount.marketIndex;
107
260
  const oracle = spotMarketAccount.oracle;
108
261
  const oracleId = (0, oracleId_1.getOracleId)(oracle, spotMarketAccount.oracleSource);
109
- if (!this.oracleSubscribers.has(oracleId)) {
262
+ if (!((_b = this.oracleMultiSubscriber) === null || _b === void 0 ? void 0 : _b.getAccountDataMap().has(oracleId))) {
110
263
  addOraclePromises.push(this.addOracle({
111
264
  publicKey: oracle,
112
265
  source: spotMarketAccount.oracleSource,
@@ -233,19 +386,45 @@ class grpcDriftClientAccountSubscriberV2 extends webSocketDriftClientAccountSubs
233
386
  });
234
387
  return true;
235
388
  }
389
+ async handleDelistedMarkets() {
390
+ var _a, _b;
391
+ if (this.delistedMarketSetting === types_1.DelistedMarketSetting.Subscribe) {
392
+ return;
393
+ }
394
+ const { perpMarketIndexes, oracles } = (0, utils_1.findDelistedPerpMarketsAndOracles)(Array.from(((_a = this.perpMarketsSubscriber) === null || _a === void 0 ? void 0 : _a.getAccountDataMap().values()) || []), Array.from(((_b = this.spotMarketsSubscriber) === null || _b === void 0 ? void 0 : _b.getAccountDataMap().values()) || []));
395
+ for (const perpMarketIndex of perpMarketIndexes) {
396
+ await this.perpMarketsSubscriber.removeAccounts([
397
+ new web3_js_1.PublicKey(this.perpMarketIndexToAccountPubkeyMap.get(perpMarketIndex) || ''),
398
+ ]);
399
+ if (this.delistedMarketSetting === types_1.DelistedMarketSetting.Discard) {
400
+ this.perpMarketIndexToAccountPubkeyMap.delete(perpMarketIndex);
401
+ }
402
+ }
403
+ for (const oracle of oracles) {
404
+ await this.oracleMultiSubscriber.removeAccounts([oracle.publicKey]);
405
+ }
406
+ }
407
+ removeInitialData() {
408
+ this.initialPerpMarketAccountData = new Map();
409
+ this.initialSpotMarketAccountData = new Map();
410
+ this.initialOraclePriceData = new Map();
411
+ }
236
412
  async unsubscribeFromOracles() {
237
413
  if (this.oracleMultiSubscriber) {
238
414
  await this.oracleMultiSubscriber.unsubscribe();
239
415
  this.oracleMultiSubscriber = undefined;
240
416
  return;
241
417
  }
242
- await super.unsubscribeFromOracles();
243
418
  }
244
419
  async unsubscribe() {
420
+ var _a, _b;
245
421
  if (this.isSubscribed) {
246
422
  return;
247
423
  }
248
424
  await this.stateAccountSubscriber.unsubscribe();
425
+ await this.unsubscribeFromOracles();
426
+ await ((_a = this.perpMarketsSubscriber) === null || _a === void 0 ? void 0 : _a.unsubscribe());
427
+ await ((_b = this.spotMarketsSubscriber) === null || _b === void 0 ? void 0 : _b.unsubscribe());
249
428
  }
250
429
  }
251
430
  exports.grpcDriftClientAccountSubscriberV2 = grpcDriftClientAccountSubscriberV2;
@@ -25,6 +25,7 @@ export declare class grpcMultiAccountSubscriber<T> {
25
25
  setAccountData(accountPubkey: string, data: T, slot?: number): void;
26
26
  getAccountData(accountPubkey: string): DataAndSlot<T> | undefined;
27
27
  getAccountDataMap(): Map<string, DataAndSlot<T>>;
28
+ fetch(): Promise<void>;
28
29
  subscribe(accounts: PublicKey[], onChange: (accountId: PublicKey, data: T, context: Context, buffer: Buffer) => void): Promise<void>;
29
30
  addAccounts(accounts: PublicKey[]): Promise<void>;
30
31
  removeAccounts(accounts: PublicKey[]): Promise<void>;
@@ -31,6 +31,18 @@ const web3_js_1 = require("@solana/web3.js");
31
31
  const Buffer = __importStar(require("buffer"));
32
32
  const bs58_1 = __importDefault(require("bs58"));
33
33
  const grpc_1 = require("../isomorphic/grpc");
34
+ function commitmentLevelToCommitment(commitmentLevel) {
35
+ switch (commitmentLevel) {
36
+ case grpc_1.CommitmentLevel.PROCESSED:
37
+ return 'processed';
38
+ case grpc_1.CommitmentLevel.CONFIRMED:
39
+ return 'confirmed';
40
+ case grpc_1.CommitmentLevel.FINALIZED:
41
+ return 'finalized';
42
+ default:
43
+ return 'confirmed';
44
+ }
45
+ }
34
46
  class grpcMultiAccountSubscriber {
35
47
  constructor(client, commitmentLevel, accountName, program, decodeBuffer, resubOpts, onUnsubscribe) {
36
48
  this.isUnsubscribing = false;
@@ -65,6 +77,40 @@ class grpcMultiAccountSubscriber {
65
77
  getAccountDataMap() {
66
78
  return this.dataMap;
67
79
  }
80
+ async fetch() {
81
+ var _a;
82
+ try {
83
+ // Chunk account IDs into groups of 100 (getMultipleAccounts limit)
84
+ const chunkSize = 100;
85
+ const chunks = [];
86
+ const accountIds = Array.from(this.subscribedAccounts.values());
87
+ for (let i = 0; i < accountIds.length; i += chunkSize) {
88
+ chunks.push(accountIds.slice(i, i + chunkSize));
89
+ }
90
+ // Process all chunks concurrently
91
+ await Promise.all(chunks.map(async (chunk) => {
92
+ const accountAddresses = chunk.map((accountId) => new web3_js_1.PublicKey(accountId));
93
+ const rpcResponseAndContext = await this.program.provider.connection.getMultipleAccountsInfoAndContext(accountAddresses, {
94
+ commitment: commitmentLevelToCommitment(this.commitmentLevel),
95
+ });
96
+ const rpcResponse = rpcResponseAndContext.value;
97
+ const currentSlot = rpcResponseAndContext.context.slot;
98
+ for (let i = 0; i < chunk.length; i++) {
99
+ const accountId = chunk[i];
100
+ const accountInfo = rpcResponse[i];
101
+ if (accountInfo) {
102
+ const perpMarket = this.program.coder.accounts.decode('PerpMarket', accountInfo.data);
103
+ this.setAccountData(accountId, perpMarket, currentSlot);
104
+ }
105
+ }
106
+ }));
107
+ }
108
+ catch (error) {
109
+ if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.logResubMessages) {
110
+ console.log(`[${this.accountName}] grpcMultiAccountSubscriber error fetching accounts:`, error);
111
+ }
112
+ }
113
+ }
68
114
  async subscribe(accounts, onChange) {
69
115
  if (this.listenerId != null || this.isUnsubscribing) {
70
116
  return;
@@ -246,4 +246,6 @@ export declare class AdminClient extends DriftClient {
246
246
  getUpdateFeatureBitFlagsBuilderCodesIx(enable: boolean): Promise<TransactionInstruction>;
247
247
  updateFeatureBitFlagsBuilderReferral(enable: boolean): Promise<TransactionSignature>;
248
248
  getUpdateFeatureBitFlagsBuilderReferralIx(enable: boolean): Promise<TransactionInstruction>;
249
+ adminDisableUpdatePerpBidAskTwap(authority: PublicKey, disable: boolean): Promise<TransactionSignature>;
250
+ getAdminDisableUpdatePerpBidAskTwapIx(authority: PublicKey, disable: boolean): Promise<TransactionInstruction>;
249
251
  }
@@ -2248,5 +2248,22 @@ class AdminClient extends driftClient_1.DriftClient {
2248
2248
  },
2249
2249
  });
2250
2250
  }
2251
+ async adminDisableUpdatePerpBidAskTwap(authority, disable) {
2252
+ const disableBidAskTwapUpdateIx = await this.getAdminDisableUpdatePerpBidAskTwapIx(authority, disable);
2253
+ const tx = await this.buildTransaction(disableBidAskTwapUpdateIx);
2254
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
2255
+ return txSig;
2256
+ }
2257
+ async getAdminDisableUpdatePerpBidAskTwapIx(authority, disable) {
2258
+ return await this.program.instruction.adminDisableUpdatePerpBidAskTwap(disable, {
2259
+ accounts: {
2260
+ admin: this.useHotWalletAdmin
2261
+ ? this.wallet.publicKey
2262
+ : this.getStateAccount().admin,
2263
+ state: await this.getStatePublicKey(),
2264
+ userStats: (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, authority),
2265
+ },
2266
+ });
2267
+ }
2251
2268
  }
2252
2269
  exports.AdminClient = AdminClient;
@@ -1,24 +1,64 @@
1
- import { WebSocketDriftClientAccountSubscriber } from './webSocketDriftClientAccountSubscriber';
2
- import { OracleInfo } from '../oracles/types';
1
+ /// <reference types="node" />
2
+ import StrictEventEmitter from 'strict-event-emitter-types';
3
+ import { EventEmitter } from 'events';
4
+ import { OracleInfo, OraclePriceData } from '../oracles/types';
3
5
  import { Program } from '@coral-xyz/anchor';
4
- import { DataAndSlot, DelistedMarketSetting, GrpcConfigs, ResubOpts } from './types';
5
- import { PerpMarketAccount, SpotMarketAccount } from '../types';
6
- export declare class grpcDriftClientAccountSubscriberV2 extends WebSocketDriftClientAccountSubscriber {
6
+ import { PublicKey } from '@solana/web3.js';
7
+ import { AccountSubscriber, DataAndSlot, DelistedMarketSetting, DriftClientAccountEvents, DriftClientAccountSubscriber, GrpcConfigs, ResubOpts } from './types';
8
+ import { PerpMarketAccount, SpotMarketAccount, StateAccount } from '../types';
9
+ import { OracleClientCache } from '../oracles/oracleClientCache';
10
+ export declare class grpcDriftClientAccountSubscriberV2 implements DriftClientAccountSubscriber {
7
11
  private grpcConfigs;
8
12
  private perpMarketsSubscriber?;
9
13
  private spotMarketsSubscriber?;
10
14
  private oracleMultiSubscriber?;
11
15
  private perpMarketIndexToAccountPubkeyMap;
12
16
  private spotMarketIndexToAccountPubkeyMap;
17
+ private delistedMarketSetting;
18
+ eventEmitter: StrictEventEmitter<EventEmitter, DriftClientAccountEvents>;
19
+ isSubscribed: boolean;
20
+ isSubscribing: boolean;
21
+ program: Program;
22
+ perpMarketIndexes: number[];
23
+ spotMarketIndexes: number[];
24
+ shouldFindAllMarketsAndOracles: boolean;
25
+ oracleInfos: OracleInfo[];
26
+ initialPerpMarketAccountData: Map<number, PerpMarketAccount>;
27
+ initialSpotMarketAccountData: Map<number, SpotMarketAccount>;
28
+ initialOraclePriceData: Map<string, OraclePriceData>;
29
+ perpOracleMap: Map<number, PublicKey>;
30
+ perpOracleStringMap: Map<number, string>;
31
+ spotOracleMap: Map<number, PublicKey>;
32
+ spotOracleStringMap: Map<number, string>;
33
+ stateAccountSubscriber?: AccountSubscriber<StateAccount>;
34
+ oracleClientCache: OracleClientCache;
35
+ private resubOpts?;
36
+ private subscriptionPromise;
37
+ protected subscriptionPromiseResolver: (val: boolean) => void;
13
38
  constructor(grpcConfigs: GrpcConfigs, program: Program, perpMarketIndexes: number[], spotMarketIndexes: number[], oracleInfos: OracleInfo[], shouldFindAllMarketsAndOracles: boolean, delistedMarketSetting: DelistedMarketSetting, resubOpts?: ResubOpts);
39
+ chunks: <T>(array: readonly T[], size: number) => T[][];
40
+ setInitialData(): Promise<void>;
41
+ addPerpMarket(_marketIndex: number): Promise<boolean>;
42
+ addSpotMarket(_marketIndex: number): Promise<boolean>;
43
+ addOracle(oracleInfo: OracleInfo): Promise<boolean>;
14
44
  subscribe(): Promise<boolean>;
45
+ fetch(): Promise<void>;
46
+ private assertIsSubscribed;
47
+ getStateAccountAndSlot(): DataAndSlot<StateAccount>;
48
+ getMarketAccountsAndSlots(): DataAndSlot<PerpMarketAccount>[];
49
+ getSpotMarketAccountsAndSlots(): DataAndSlot<SpotMarketAccount>[];
15
50
  getMarketAccountAndSlot(marketIndex: number): DataAndSlot<PerpMarketAccount> | undefined;
16
51
  getSpotMarketAccountAndSlot(marketIndex: number): DataAndSlot<SpotMarketAccount> | undefined;
52
+ getOraclePriceDataAndSlot(oracleId: string): DataAndSlot<OraclePriceData> | undefined;
53
+ getOraclePriceDataAndSlotForPerpMarket(marketIndex: number): DataAndSlot<OraclePriceData> | undefined;
54
+ getOraclePriceDataAndSlotForSpotMarket(marketIndex: number): DataAndSlot<OraclePriceData> | undefined;
17
55
  setPerpOracleMap(): Promise<void>;
18
56
  setSpotOracleMap(): Promise<void>;
19
57
  subscribeToPerpMarketAccounts(): Promise<boolean>;
20
58
  subscribeToSpotMarketAccounts(): Promise<boolean>;
21
59
  subscribeToOracles(): Promise<boolean>;
60
+ handleDelistedMarkets(): Promise<void>;
61
+ removeInitialData(): void;
22
62
  unsubscribeFromOracles(): Promise<void>;
23
63
  unsubscribe(): Promise<void>;
24
64
  }
@@ -1 +1 @@
1
- {"version":3,"file":"grpcDriftClientAccountSubscriberV2.d.ts","sourceRoot":"","sources":["../../../src/accounts/grpcDriftClientAccountSubscriberV2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qCAAqC,EAAE,MAAM,yCAAyC,CAAC;AAChG,OAAO,EAAE,UAAU,EAAmB,MAAM,kBAAkB,CAAC;AAC/D,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAQ5C,OAAO,EACN,WAAW,EACX,qBAAqB,EACrB,WAAW,EACX,SAAS,EACT,MAAM,SAAS,CAAC;AAGjB,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAgB,MAAM,UAAU,CAAC;AAM9E,qBAAa,kCAAmC,SAAQ,qCAAqC;IAC5F,OAAO,CAAC,WAAW,CAAc;IACjC,OAAO,CAAC,qBAAqB,CAAC,CAAgD;IAC9E,OAAO,CAAC,qBAAqB,CAAC,CAAgD;IAC9E,OAAO,CAAC,qBAAqB,CAAC,CAA8C;IAC5E,OAAO,CAAC,iCAAiC,CAA6B;IACtE,OAAO,CAAC,iCAAiC,CAA6B;gBAGrE,WAAW,EAAE,WAAW,EACxB,OAAO,EAAE,OAAO,EAChB,iBAAiB,EAAE,MAAM,EAAE,EAC3B,iBAAiB,EAAE,MAAM,EAAE,EAC3B,WAAW,EAAE,UAAU,EAAE,EACzB,8BAA8B,EAA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