@drift-labs/sdk 2.142.0-beta.1 → 2.142.0-beta.3

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 (41) hide show
  1. package/VERSION +1 -1
  2. package/bun.lock +25 -10
  3. package/lib/browser/accounts/grpcAccountSubscriber.d.ts +2 -1
  4. package/lib/browser/accounts/grpcAccountSubscriber.js +4 -2
  5. package/lib/browser/accounts/grpcDriftClientAccountSubscriber.d.ts +1 -1
  6. package/lib/browser/accounts/grpcDriftClientAccountSubscriber.js +3 -3
  7. package/lib/browser/accounts/grpcDriftClientAccountSubscriberV2.d.ts +22 -0
  8. package/lib/browser/accounts/grpcDriftClientAccountSubscriberV2.js +194 -0
  9. package/lib/browser/accounts/grpcMultiAccountSubscriber.d.ts +33 -0
  10. package/lib/browser/accounts/grpcMultiAccountSubscriber.js +278 -0
  11. package/lib/browser/driftClient.js +11 -10
  12. package/lib/browser/driftClientConfig.d.ts +3 -0
  13. package/lib/node/accounts/grpcAccountSubscriber.d.ts +2 -1
  14. package/lib/node/accounts/grpcAccountSubscriber.d.ts.map +1 -1
  15. package/lib/node/accounts/grpcAccountSubscriber.js +4 -2
  16. package/lib/node/accounts/grpcDriftClientAccountSubscriber.d.ts +1 -1
  17. package/lib/node/accounts/grpcDriftClientAccountSubscriber.js +3 -3
  18. package/lib/node/accounts/grpcDriftClientAccountSubscriberV2.d.ts +23 -0
  19. package/lib/node/accounts/grpcDriftClientAccountSubscriberV2.d.ts.map +1 -0
  20. package/lib/node/accounts/grpcDriftClientAccountSubscriberV2.js +194 -0
  21. package/lib/node/accounts/grpcMultiAccountSubscriber.d.ts +34 -0
  22. package/lib/node/accounts/grpcMultiAccountSubscriber.d.ts.map +1 -0
  23. package/lib/node/accounts/grpcMultiAccountSubscriber.js +278 -0
  24. package/lib/node/driftClient.d.ts.map +1 -1
  25. package/lib/node/driftClient.js +11 -10
  26. package/lib/node/driftClientConfig.d.ts +3 -0
  27. package/lib/node/driftClientConfig.d.ts.map +1 -1
  28. package/lib/node/isomorphic/grpc.d.ts +5 -3
  29. package/lib/node/isomorphic/grpc.js +1 -3
  30. package/lib/node/isomorphic/grpc.node.d.ts +5 -3
  31. package/lib/node/isomorphic/grpc.node.d.ts.map +1 -1
  32. package/lib/node/isomorphic/grpc.node.js +1 -3
  33. package/package.json +3 -3
  34. package/scripts/client-test.ts +133 -0
  35. package/src/accounts/grpcAccountSubscriber.ts +9 -6
  36. package/src/accounts/grpcDriftClientAccountSubscriber.ts +1 -1
  37. package/src/accounts/grpcDriftClientAccountSubscriberV2.ts +358 -0
  38. package/src/accounts/grpcMultiAccountSubscriber.ts +338 -0
  39. package/src/driftClient.ts +5 -2
  40. package/src/driftClientConfig.ts +13 -0
  41. package/src/isomorphic/grpc.node.ts +11 -7
@@ -0,0 +1,338 @@
1
+ import { Program } from '@coral-xyz/anchor';
2
+ import { Context, PublicKey } from '@solana/web3.js';
3
+ import * as Buffer from 'buffer';
4
+ import bs58 from 'bs58';
5
+
6
+ import {
7
+ Client,
8
+ ClientDuplexStream,
9
+ CommitmentLevel,
10
+ SubscribeRequest,
11
+ SubscribeUpdate,
12
+ createClient,
13
+ } from '../isomorphic/grpc';
14
+ import { DataAndSlot, GrpcConfigs, ResubOpts } from './types';
15
+
16
+ interface AccountInfoLike {
17
+ owner: PublicKey;
18
+ lamports: number;
19
+ data: Buffer;
20
+ executable: boolean;
21
+ rentEpoch: number;
22
+ }
23
+
24
+ export class grpcMultiAccountSubscriber<T> {
25
+ private client: Client;
26
+ private stream: ClientDuplexStream<SubscribeRequest, SubscribeUpdate>;
27
+ private commitmentLevel: CommitmentLevel;
28
+ private program: Program;
29
+ private accountName: string;
30
+ private decodeBufferFn?: (buffer: Buffer, pubkey?: string) => T;
31
+ private resubOpts?: ResubOpts;
32
+ private onUnsubscribe?: () => Promise<void>;
33
+
34
+ public listenerId?: number;
35
+ public isUnsubscribing = false;
36
+ private timeoutId?: ReturnType<typeof setTimeout>;
37
+ private receivingData = false;
38
+
39
+ private subscribedAccounts = new Set<string>();
40
+ private onChangeMap = new Map<
41
+ string,
42
+ (data: T, context: Context, buffer: Buffer) => void
43
+ >();
44
+
45
+ private dataMap = new Map<string, DataAndSlot<T>>();
46
+
47
+ private constructor(
48
+ client: Client,
49
+ commitmentLevel: CommitmentLevel,
50
+ accountName: string,
51
+ program: Program,
52
+ decodeBuffer?: (buffer: Buffer, pubkey?: string) => T,
53
+ resubOpts?: ResubOpts,
54
+ onUnsubscribe?: () => Promise<void>
55
+ ) {
56
+ this.client = client;
57
+ this.commitmentLevel = commitmentLevel;
58
+ this.accountName = accountName;
59
+ this.program = program;
60
+ this.decodeBufferFn = decodeBuffer;
61
+ this.resubOpts = resubOpts;
62
+ this.onUnsubscribe = onUnsubscribe;
63
+ }
64
+
65
+ public static async create<U>(
66
+ grpcConfigs: GrpcConfigs,
67
+ accountName: string,
68
+ program: Program,
69
+ decodeBuffer?: (buffer: Buffer, pubkey?: string) => U,
70
+ resubOpts?: ResubOpts,
71
+ clientProp?: Client,
72
+ onUnsubscribe?: () => Promise<void>
73
+ ): Promise<grpcMultiAccountSubscriber<U>> {
74
+ const client = clientProp
75
+ ? clientProp
76
+ : await createClient(
77
+ grpcConfigs.endpoint,
78
+ grpcConfigs.token,
79
+ grpcConfigs.channelOptions ?? {}
80
+ );
81
+ const commitmentLevel =
82
+ // @ts-ignore :: isomorphic exported enum fails typescript but will work at runtime
83
+ grpcConfigs.commitmentLevel ?? CommitmentLevel.CONFIRMED;
84
+
85
+ return new grpcMultiAccountSubscriber(
86
+ client,
87
+ commitmentLevel,
88
+ accountName,
89
+ program,
90
+ decodeBuffer,
91
+ resubOpts,
92
+ onUnsubscribe
93
+ );
94
+ }
95
+
96
+ setAccountData(accountPubkey: PublicKey, data: T, slot?: number): void {
97
+ this.dataMap.set(accountPubkey.toBase58(), { data, slot });
98
+ }
99
+
100
+ getAccountData(accountPubkey: PublicKey): DataAndSlot<T> | undefined {
101
+ return this.dataMap.get(accountPubkey.toBase58());
102
+ }
103
+
104
+ async subscribe(
105
+ accounts: PublicKey[],
106
+ onChange: (
107
+ accountId: PublicKey,
108
+ data: T,
109
+ context: Context,
110
+ buffer: Buffer
111
+ ) => void
112
+ ): Promise<void> {
113
+ if (this.listenerId != null || this.isUnsubscribing) {
114
+ return;
115
+ }
116
+
117
+ // Track accounts and single onChange for all
118
+ for (const pk of accounts) {
119
+ const key = pk.toBase58();
120
+ this.subscribedAccounts.add(key);
121
+ this.onChangeMap.set(key, (data, ctx, buffer) =>
122
+ onChange(new PublicKey(key), data, ctx, buffer)
123
+ );
124
+ }
125
+
126
+ this.stream =
127
+ (await this.client.subscribe()) as unknown as typeof this.stream;
128
+ const request: SubscribeRequest = {
129
+ slots: {},
130
+ accounts: {
131
+ account: {
132
+ account: accounts.map((a) => a.toBase58()),
133
+ owner: [],
134
+ filters: [],
135
+ },
136
+ },
137
+ transactions: {},
138
+ blocks: {},
139
+ blocksMeta: {},
140
+ accountsDataSlice: [],
141
+ commitment: this.commitmentLevel,
142
+ entry: {},
143
+ transactionsStatus: {},
144
+ };
145
+
146
+ this.stream.on('data', (chunk: SubscribeUpdate) => {
147
+ if (!chunk.account) {
148
+ return;
149
+ }
150
+ const slot = Number(chunk.account.slot);
151
+ const accountPubkeyBytes = chunk.account.account.pubkey;
152
+ const accountPubkey = bs58.encode(
153
+ accountPubkeyBytes as unknown as Uint8Array
154
+ );
155
+ if (!accountPubkey || !this.subscribedAccounts.has(accountPubkey)) {
156
+ return;
157
+ }
158
+ const accountInfo: AccountInfoLike = {
159
+ owner: new PublicKey(chunk.account.account.owner),
160
+ lamports: Number(chunk.account.account.lamports),
161
+ data: Buffer.Buffer.from(chunk.account.account.data),
162
+ executable: chunk.account.account.executable,
163
+ rentEpoch: Number(chunk.account.account.rentEpoch),
164
+ };
165
+
166
+ const context = { slot } as Context;
167
+ const buffer = accountInfo.data;
168
+ const data = this.decodeBufferFn
169
+ ? this.decodeBufferFn(buffer, accountPubkey)
170
+ : this.program.account[this.accountName].coder.accounts.decode(
171
+ this.capitalize(this.accountName),
172
+ buffer
173
+ );
174
+
175
+ const handler = this.onChangeMap.get(accountPubkey);
176
+ if (handler) {
177
+ if (this.resubOpts?.resubTimeoutMs) {
178
+ this.receivingData = true;
179
+ clearTimeout(this.timeoutId);
180
+ handler(data, context, buffer);
181
+ this.setTimeout();
182
+ } else {
183
+ handler(data, context, buffer);
184
+ }
185
+ }
186
+ });
187
+
188
+ return new Promise<void>((resolve, reject) => {
189
+ this.stream.write(request, (err) => {
190
+ if (err === null || err === undefined) {
191
+ this.listenerId = 1;
192
+ if (this.resubOpts?.resubTimeoutMs) {
193
+ this.receivingData = true;
194
+ this.setTimeout();
195
+ }
196
+ resolve();
197
+ } else {
198
+ reject(err);
199
+ }
200
+ });
201
+ }).catch((reason) => {
202
+ console.error(reason);
203
+ throw reason;
204
+ });
205
+ }
206
+
207
+ async addAccounts(accounts: PublicKey[]): Promise<void> {
208
+ for (const pk of accounts) {
209
+ this.subscribedAccounts.add(pk.toBase58());
210
+ }
211
+ const request: SubscribeRequest = {
212
+ slots: {},
213
+ accounts: {
214
+ account: {
215
+ account: Array.from(this.subscribedAccounts.values()),
216
+ owner: [],
217
+ filters: [],
218
+ },
219
+ },
220
+ transactions: {},
221
+ blocks: {},
222
+ blocksMeta: {},
223
+ accountsDataSlice: [],
224
+ commitment: this.commitmentLevel,
225
+ entry: {},
226
+ transactionsStatus: {},
227
+ };
228
+
229
+ await new Promise<void>((resolve, reject) => {
230
+ this.stream.write(request, (err) => {
231
+ if (err === null || err === undefined) {
232
+ resolve();
233
+ } else {
234
+ reject(err);
235
+ }
236
+ });
237
+ });
238
+ }
239
+
240
+ async removeAccounts(accounts: PublicKey[]): Promise<void> {
241
+ for (const pk of accounts) {
242
+ const k = pk.toBase58();
243
+ this.subscribedAccounts.delete(k);
244
+ this.onChangeMap.delete(k);
245
+ }
246
+ const request: SubscribeRequest = {
247
+ slots: {},
248
+ accounts: {
249
+ account: {
250
+ account: Array.from(this.subscribedAccounts.values()),
251
+ owner: [],
252
+ filters: [],
253
+ },
254
+ },
255
+ transactions: {},
256
+ blocks: {},
257
+ blocksMeta: {},
258
+ accountsDataSlice: [],
259
+ commitment: this.commitmentLevel,
260
+ entry: {},
261
+ transactionsStatus: {},
262
+ };
263
+
264
+ await new Promise<void>((resolve, reject) => {
265
+ this.stream.write(request, (err) => {
266
+ if (err === null || err === undefined) {
267
+ resolve();
268
+ } else {
269
+ reject(err);
270
+ }
271
+ });
272
+ });
273
+ }
274
+
275
+ async unsubscribe(): Promise<void> {
276
+ this.isUnsubscribing = true;
277
+ clearTimeout(this.timeoutId);
278
+ this.timeoutId = undefined;
279
+
280
+ if (this.listenerId != null) {
281
+ const promise = new Promise<void>((resolve, reject) => {
282
+ const request: SubscribeRequest = {
283
+ slots: {},
284
+ accounts: {},
285
+ transactions: {},
286
+ blocks: {},
287
+ blocksMeta: {},
288
+ accountsDataSlice: [],
289
+ entry: {},
290
+ transactionsStatus: {},
291
+ };
292
+ this.stream.write(request, (err) => {
293
+ if (err === null || err === undefined) {
294
+ this.listenerId = undefined;
295
+ this.isUnsubscribing = false;
296
+ resolve();
297
+ } else {
298
+ reject(err);
299
+ }
300
+ });
301
+ }).catch((reason) => {
302
+ console.error(reason);
303
+ throw reason;
304
+ });
305
+ return promise;
306
+ } else {
307
+ this.isUnsubscribing = false;
308
+ }
309
+
310
+ if (this.onUnsubscribe) {
311
+ try {
312
+ await this.onUnsubscribe();
313
+ } catch (e) {
314
+ console.error(e);
315
+ }
316
+ }
317
+ }
318
+
319
+ private setTimeout(): void {
320
+ this.timeoutId = setTimeout(
321
+ async () => {
322
+ if (this.isUnsubscribing) {
323
+ return;
324
+ }
325
+ if (this.receivingData) {
326
+ await this.unsubscribe();
327
+ this.receivingData = false;
328
+ }
329
+ },
330
+ this.resubOpts?.resubTimeoutMs
331
+ );
332
+ }
333
+
334
+ private capitalize(value: string): string {
335
+ if (!value) return value;
336
+ return value.charAt(0).toUpperCase() + value.slice(1);
337
+ }
338
+ }
@@ -189,7 +189,7 @@ import {
189
189
  } from './tx/utils';
190
190
  import pythSolanaReceiverIdl from './idl/pyth_solana_receiver.json';
191
191
  import { asV0Tx, PullFeed, AnchorUtils } from '@switchboard-xyz/on-demand';
192
- import { gprcDriftClientAccountSubscriber } from './accounts/grpcDriftClientAccountSubscriber';
192
+ import { grpcDriftClientAccountSubscriber } from './accounts/grpcDriftClientAccountSubscriber';
193
193
  import nacl from 'tweetnacl';
194
194
  import { Slothash } from './slot/SlothashSubscriber';
195
195
  import { getOracleId } from './oracles/oracleId';
@@ -434,7 +434,10 @@ export class DriftClient {
434
434
  delistedMarketSetting
435
435
  );
436
436
  } else if (config.accountSubscription?.type === 'grpc') {
437
- this.accountSubscriber = new gprcDriftClientAccountSubscriber(
437
+ const accountSubscriberClass =
438
+ config.accountSubscription?.driftClientAccountSubscriber ??
439
+ grpcDriftClientAccountSubscriber;
440
+ this.accountSubscriber = new accountSubscriberClass(
438
441
  config.accountSubscription.grpcConfigs,
439
442
  this.program,
440
443
  config.perpMarketIndexes ?? [],
@@ -22,6 +22,8 @@ import { WebSocketAccountSubscriberV2 } from './accounts/webSocketAccountSubscri
22
22
  import { WebSocketProgramAccountSubscriber } from './accounts/webSocketProgramAccountSubscriber';
23
23
  import { WebSocketDriftClientAccountSubscriberV2 } from './accounts/webSocketDriftClientAccountSubscriberV2';
24
24
  import { WebSocketDriftClientAccountSubscriber } from './accounts/webSocketDriftClientAccountSubscriber';
25
+ import { grpcDriftClientAccountSubscriberV2 } from './accounts/grpcDriftClientAccountSubscriberV2';
26
+ import { grpcDriftClientAccountSubscriber } from './accounts/grpcDriftClientAccountSubscriber';
25
27
 
26
28
  export type DriftClientConfig = {
27
29
  connection: Connection;
@@ -60,6 +62,17 @@ export type DriftClientSubscriptionConfig =
60
62
  grpcConfigs: GrpcConfigs;
61
63
  resubTimeoutMs?: number;
62
64
  logResubMessages?: boolean;
65
+ driftClientAccountSubscriber?: new (
66
+ grpcConfigs: GrpcConfigs,
67
+ program: Program,
68
+ perpMarketIndexes: number[],
69
+ spotMarketIndexes: number[],
70
+ oracleInfos: OracleInfo[],
71
+ shouldFindAllMarketsAndOracles: boolean,
72
+ delistedMarketSetting: DelistedMarketSetting
73
+ ) =>
74
+ | grpcDriftClientAccountSubscriberV2
75
+ | grpcDriftClientAccountSubscriber;
63
76
  }
64
77
  | {
65
78
  type: 'websocket';
@@ -4,30 +4,34 @@ import type {
4
4
  SubscribeUpdate,
5
5
  } from '@triton-one/yellowstone-grpc';
6
6
  import { CommitmentLevel } from '@triton-one/yellowstone-grpc';
7
- import { ClientDuplexStream, ChannelOptions } from '@grpc/grpc-js';
7
+ import type { ClientDuplexStream, ChannelOptions } from '@grpc/grpc-js';
8
8
 
9
9
  import {
10
10
  CommitmentLevel as LaserCommitmentLevel,
11
11
  subscribe as LaserSubscribe,
12
+ CompressionAlgorithms,
13
+ } from 'helius-laserstream';
14
+ import type {
12
15
  LaserstreamConfig,
13
16
  SubscribeRequest as LaserSubscribeRequest,
14
17
  SubscribeUpdate as LaserSubscribeUpdate,
15
- CompressionAlgorithms,
16
18
  } from 'helius-laserstream';
17
19
 
18
20
  export {
19
- ClientDuplexStream,
20
- ChannelOptions,
21
- SubscribeRequest,
22
- SubscribeUpdate,
23
21
  CommitmentLevel,
24
22
  Client,
25
23
  LaserSubscribe,
26
24
  LaserCommitmentLevel,
25
+ CompressionAlgorithms,
26
+ };
27
+ export type {
28
+ ClientDuplexStream,
29
+ ChannelOptions,
30
+ SubscribeRequest,
31
+ SubscribeUpdate,
27
32
  LaserstreamConfig,
28
33
  LaserSubscribeRequest,
29
34
  LaserSubscribeUpdate,
30
- CompressionAlgorithms,
31
35
  };
32
36
 
33
37
  // Export a function to create a new Client instance