@drift-labs/sdk 2.142.0-beta.14 → 2.142.0-beta.15
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 +1 -1
- package/lib/browser/accounts/grpcDriftClientAccountSubscriberV2.d.ts +1 -0
- package/lib/browser/accounts/grpcDriftClientAccountSubscriberV2.js +55 -60
- package/lib/browser/accounts/grpcMultiAccountSubscriber.d.ts +4 -3
- package/lib/browser/accounts/grpcMultiAccountSubscriber.js +31 -17
- package/lib/node/accounts/grpcDriftClientAccountSubscriberV2.d.ts +1 -0
- package/lib/node/accounts/grpcDriftClientAccountSubscriberV2.d.ts.map +1 -1
- package/lib/node/accounts/grpcDriftClientAccountSubscriberV2.js +55 -60
- package/lib/node/accounts/grpcMultiAccountSubscriber.d.ts +4 -3
- package/lib/node/accounts/grpcMultiAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/grpcMultiAccountSubscriber.js +31 -17
- package/package.json +1 -1
- package/scripts/client-test.ts +123 -162
- package/src/accounts/grpcDriftClientAccountSubscriberV2.ts +69 -61
- package/src/accounts/grpcMultiAccountSubscriber.ts +53 -28
|
@@ -36,13 +36,17 @@ function commitmentLevelToCommitment(
|
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
export class grpcMultiAccountSubscriber<T> {
|
|
39
|
+
export class grpcMultiAccountSubscriber<T, U = undefined> {
|
|
40
40
|
private client: Client;
|
|
41
41
|
private stream: ClientDuplexStream<SubscribeRequest, SubscribeUpdate>;
|
|
42
42
|
private commitmentLevel: CommitmentLevel;
|
|
43
43
|
private program: Program;
|
|
44
44
|
private accountName: string;
|
|
45
|
-
private decodeBufferFn?: (
|
|
45
|
+
private decodeBufferFn?: (
|
|
46
|
+
buffer: Buffer,
|
|
47
|
+
pubkey?: string,
|
|
48
|
+
accountProps?: U
|
|
49
|
+
) => T;
|
|
46
50
|
private resubOpts?: ResubOpts;
|
|
47
51
|
private onUnsubscribe?: () => Promise<void>;
|
|
48
52
|
|
|
@@ -54,10 +58,11 @@ export class grpcMultiAccountSubscriber<T> {
|
|
|
54
58
|
private subscribedAccounts = new Set<string>();
|
|
55
59
|
private onChangeMap = new Map<
|
|
56
60
|
string,
|
|
57
|
-
(data: T, context: Context, buffer: Buffer) => void
|
|
61
|
+
(data: T, context: Context, buffer: Buffer, accountProps: U) => void
|
|
58
62
|
>();
|
|
59
63
|
|
|
60
64
|
private dataMap = new Map<string, DataAndSlot<T>>();
|
|
65
|
+
private accountPropsMap = new Map<string, U | Array<U>>();
|
|
61
66
|
|
|
62
67
|
private constructor(
|
|
63
68
|
client: Client,
|
|
@@ -66,7 +71,8 @@ export class grpcMultiAccountSubscriber<T> {
|
|
|
66
71
|
program: Program,
|
|
67
72
|
decodeBuffer?: (buffer: Buffer, pubkey?: string) => T,
|
|
68
73
|
resubOpts?: ResubOpts,
|
|
69
|
-
onUnsubscribe?: () => Promise<void
|
|
74
|
+
onUnsubscribe?: () => Promise<void>,
|
|
75
|
+
accountPropsMap?: Map<string, U | Array<U>>
|
|
70
76
|
) {
|
|
71
77
|
this.client = client;
|
|
72
78
|
this.commitmentLevel = commitmentLevel;
|
|
@@ -75,17 +81,19 @@ export class grpcMultiAccountSubscriber<T> {
|
|
|
75
81
|
this.decodeBufferFn = decodeBuffer;
|
|
76
82
|
this.resubOpts = resubOpts;
|
|
77
83
|
this.onUnsubscribe = onUnsubscribe;
|
|
84
|
+
this.accountPropsMap = accountPropsMap;
|
|
78
85
|
}
|
|
79
86
|
|
|
80
|
-
public static async create<U>(
|
|
87
|
+
public static async create<T, U = undefined>(
|
|
81
88
|
grpcConfigs: GrpcConfigs,
|
|
82
89
|
accountName: string,
|
|
83
90
|
program: Program,
|
|
84
|
-
decodeBuffer?: (buffer: Buffer, pubkey?: string) =>
|
|
91
|
+
decodeBuffer?: (buffer: Buffer, pubkey?: string, accountProps?: U) => T,
|
|
85
92
|
resubOpts?: ResubOpts,
|
|
86
93
|
clientProp?: Client,
|
|
87
|
-
onUnsubscribe?: () => Promise<void
|
|
88
|
-
|
|
94
|
+
onUnsubscribe?: () => Promise<void>,
|
|
95
|
+
accountPropsMap?: Map<string, U | Array<U>>
|
|
96
|
+
): Promise<grpcMultiAccountSubscriber<T, U>> {
|
|
89
97
|
const client = clientProp
|
|
90
98
|
? clientProp
|
|
91
99
|
: await createClient(
|
|
@@ -104,7 +112,8 @@ export class grpcMultiAccountSubscriber<T> {
|
|
|
104
112
|
program,
|
|
105
113
|
decodeBuffer,
|
|
106
114
|
resubOpts,
|
|
107
|
-
onUnsubscribe
|
|
115
|
+
onUnsubscribe,
|
|
116
|
+
accountPropsMap
|
|
108
117
|
);
|
|
109
118
|
}
|
|
110
119
|
|
|
@@ -176,7 +185,8 @@ export class grpcMultiAccountSubscriber<T> {
|
|
|
176
185
|
accountId: PublicKey,
|
|
177
186
|
data: T,
|
|
178
187
|
context: Context,
|
|
179
|
-
buffer: Buffer
|
|
188
|
+
buffer: Buffer,
|
|
189
|
+
accountProps: U
|
|
180
190
|
) => void
|
|
181
191
|
): Promise<void> {
|
|
182
192
|
if (this.listenerId != null || this.isUnsubscribing) {
|
|
@@ -187,9 +197,9 @@ export class grpcMultiAccountSubscriber<T> {
|
|
|
187
197
|
for (const pk of accounts) {
|
|
188
198
|
const key = pk.toBase58();
|
|
189
199
|
this.subscribedAccounts.add(key);
|
|
190
|
-
this.onChangeMap.set(key, (data, ctx, buffer) => {
|
|
200
|
+
this.onChangeMap.set(key, (data, ctx, buffer, accountProps) => {
|
|
191
201
|
this.setAccountData(key, data, ctx.slot);
|
|
192
|
-
onChange(new PublicKey(key), data, ctx, buffer);
|
|
202
|
+
onChange(new PublicKey(key), data, ctx, buffer, accountProps);
|
|
193
203
|
});
|
|
194
204
|
}
|
|
195
205
|
|
|
@@ -235,23 +245,38 @@ export class grpcMultiAccountSubscriber<T> {
|
|
|
235
245
|
|
|
236
246
|
const context = { slot } as Context;
|
|
237
247
|
const buffer = accountInfo.data;
|
|
238
|
-
const
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
248
|
+
const accountProps = this.accountPropsMap?.get(accountPubkey);
|
|
249
|
+
|
|
250
|
+
const handleDataBuffer = (
|
|
251
|
+
context: Context,
|
|
252
|
+
buffer: Buffer,
|
|
253
|
+
accountProps: U
|
|
254
|
+
) => {
|
|
255
|
+
const data = this.decodeBufferFn
|
|
256
|
+
? this.decodeBufferFn(buffer, accountPubkey, accountProps)
|
|
257
|
+
: this.program.account[this.accountName].coder.accounts.decode(
|
|
258
|
+
this.capitalize(this.accountName),
|
|
259
|
+
buffer
|
|
260
|
+
);
|
|
261
|
+
const handler = this.onChangeMap.get(accountPubkey);
|
|
262
|
+
if (handler) {
|
|
263
|
+
if (this.resubOpts?.resubTimeoutMs) {
|
|
264
|
+
this.receivingData = true;
|
|
265
|
+
clearTimeout(this.timeoutId);
|
|
266
|
+
handler(data, context, buffer, accountProps);
|
|
267
|
+
this.setTimeout();
|
|
268
|
+
} else {
|
|
269
|
+
handler(data, context, buffer, accountProps);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
if (Array.isArray(accountProps)) {
|
|
275
|
+
for (const props of accountProps) {
|
|
276
|
+
handleDataBuffer(context, buffer, props);
|
|
254
277
|
}
|
|
278
|
+
} else {
|
|
279
|
+
handleDataBuffer(context, buffer, accountProps);
|
|
255
280
|
}
|
|
256
281
|
});
|
|
257
282
|
|