@drift-labs/sdk 2.65.0-beta.0 → 2.65.0-beta.10

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.
@@ -395,7 +395,7 @@ class DriftClient {
395
395
  * Adds and subscribes to users based on params set by the constructor or by updateWallet.
396
396
  */
397
397
  async addAndSubscribeToUsers() {
398
- var _a, _b, _c, _d, _e, _f, _g, _h;
398
+ var _a, _b, _c, _d, _e, _f, _g;
399
399
  // save the rpc calls if driftclient is initialized without a real wallet
400
400
  if (this.skipLoadUsers)
401
401
  return true;
@@ -412,19 +412,27 @@ class DriftClient {
412
412
  }
413
413
  }
414
414
  else {
415
- const userAccounts = (_c = (await this.getUserAccountsForAuthority(this.wallet.publicKey))) !== null && _c !== void 0 ? _c : [];
415
+ let userAccounts = [];
416
416
  let delegatedAccounts = [];
417
+ const userAccountsPromise = this.getUserAccountsForAuthority(this.wallet.publicKey);
417
418
  if (this.includeDelegates) {
418
- delegatedAccounts =
419
- (_d = (await this.getUserAccountsForDelegate(this.wallet.publicKey))) !== null && _d !== void 0 ? _d : [];
419
+ const delegatedAccountsPromise = this.getUserAccountsForDelegate(this.wallet.publicKey);
420
+ [userAccounts, delegatedAccounts] = await Promise.all([
421
+ userAccountsPromise,
422
+ delegatedAccountsPromise,
423
+ ]);
424
+ !userAccounts && (userAccounts = []);
425
+ !delegatedAccounts && (delegatedAccounts = []);
420
426
  }
421
- for (const account of userAccounts.concat(delegatedAccounts)) {
422
- result =
423
- result &&
424
- (await this.addUser(account.subAccountId, account.authority, account));
427
+ else {
428
+ userAccounts = (_c = (await userAccountsPromise)) !== null && _c !== void 0 ? _c : [];
425
429
  }
430
+ const allAccounts = userAccounts.concat(delegatedAccounts);
431
+ const addAllAccountsPromise = allAccounts.map((acc) => this.addUser(acc.subAccountId, acc.authority, acc));
432
+ const addAllAccountsResults = await Promise.all(addAllAccountsPromise);
433
+ result = addAllAccountsResults.every((res) => !!res);
426
434
  if (this.activeSubAccountId == undefined) {
427
- this.switchActiveUser((_f = (_e = userAccounts.concat(delegatedAccounts)[0]) === null || _e === void 0 ? void 0 : _e.subAccountId) !== null && _f !== void 0 ? _f : 0, (_h = (_g = userAccounts.concat(delegatedAccounts)[0]) === null || _g === void 0 ? void 0 : _g.authority) !== null && _h !== void 0 ? _h : this.authority);
435
+ this.switchActiveUser((_e = (_d = userAccounts.concat(delegatedAccounts)[0]) === null || _d === void 0 ? void 0 : _d.subAccountId) !== null && _e !== void 0 ? _e : 0, (_g = (_f = userAccounts.concat(delegatedAccounts)[0]) === null || _f === void 0 ? void 0 : _f.authority) !== null && _g !== void 0 ? _g : this.authority);
428
436
  }
429
437
  }
430
438
  return result;
@@ -54,8 +54,11 @@ class JupiterClient {
54
54
  onlyDirectRoutes: onlyDirectRoutes.toString(),
55
55
  maxAccounts: maxAccounts.toString(),
56
56
  ...(excludeDexes && { excludeDexes: excludeDexes.join(',') }),
57
- }).toString();
58
- const quote = await (await (0, node_fetch_1.default)(`${this.url}/v6/quote?${params}`)).json();
57
+ });
58
+ if (swapMode === 'ExactOut') {
59
+ params.delete('maxAccounts');
60
+ }
61
+ const quote = await (await (0, node_fetch_1.default)(`${this.url}/v6/quote?${params.toString()}`)).json();
59
62
  return quote;
60
63
  }
61
64
  /**
@@ -1,16 +1,21 @@
1
- import { User, DriftClient, UserAccount, OrderRecord, WrappedEvent, DLOB, UserSubscriptionConfig } from '..';
1
+ import { User, DriftClient, UserAccount, OrderRecord, WrappedEvent, DLOB, UserSubscriptionConfig, DataAndSlot } from '..';
2
2
  import { PublicKey } from '@solana/web3.js';
3
3
  import { UserAccountFilterCriteria as UserFilterCriteria, UserMapConfig } from './userMapConfig';
4
4
  export interface UserMapInterface {
5
5
  subscribe(): Promise<void>;
6
6
  unsubscribe(): Promise<void>;
7
- addPubkey(userAccountPublicKey: PublicKey): Promise<void>;
7
+ addPubkey(userAccountPublicKey: PublicKey, userAccount?: UserAccount, slot?: number, accountSubscription?: UserSubscriptionConfig): Promise<void>;
8
8
  has(key: string): boolean;
9
9
  get(key: string): User | undefined;
10
- mustGet(key: string): Promise<User>;
10
+ getWithSlot(key: string): DataAndSlot<User> | undefined;
11
+ mustGet(key: string, accountSubscription?: UserSubscriptionConfig): Promise<User>;
12
+ mustGetWithSlot(key: string, accountSubscription?: UserSubscriptionConfig): Promise<DataAndSlot<User>>;
11
13
  getUserAuthority(key: string): PublicKey | undefined;
12
14
  updateWithOrderRecord(record: OrderRecord): Promise<void>;
13
15
  values(): IterableIterator<User>;
16
+ valuesWithSlot(): IterableIterator<DataAndSlot<User>>;
17
+ entries(): IterableIterator<[string, User]>;
18
+ entriesWithSlot(): IterableIterator<[string, DataAndSlot<User>]>;
14
19
  }
15
20
  export declare class UserMap implements UserMapInterface {
16
21
  private userMap;
@@ -39,12 +44,14 @@ export declare class UserMap implements UserMapInterface {
39
44
  * @returns user User | undefined
40
45
  */
41
46
  get(key: string): User | undefined;
47
+ getWithSlot(key: string): DataAndSlot<User> | undefined;
42
48
  /**
43
49
  * gets the User for a particular userAccountPublicKey, if no User exists, new one is created
44
50
  * @param key userAccountPublicKey to get User for
45
51
  * @returns User
46
52
  */
47
53
  mustGet(key: string, accountSubscription?: UserSubscriptionConfig): Promise<User>;
54
+ mustGetWithSlot(key: string, accountSubscription?: UserSubscriptionConfig): Promise<DataAndSlot<User>>;
48
55
  /**
49
56
  * gets the Authority for a particular userAccountPublicKey, if no User exists, undefined is returned
50
57
  * @param key userAccountPublicKey to get User for
@@ -60,6 +67,9 @@ export declare class UserMap implements UserMapInterface {
60
67
  updateWithOrderRecord(record: OrderRecord): Promise<void>;
61
68
  updateWithEventRecord(record: WrappedEvent<any>): Promise<void>;
62
69
  values(): IterableIterator<User>;
70
+ valuesWithSlot(): IterableIterator<DataAndSlot<User>>;
71
+ entries(): IterableIterator<[string, User]>;
72
+ entriesWithSlot(): IterableIterator<[string, DataAndSlot<User>]>;
63
73
  size(): number;
64
74
  /**
65
75
  * Returns a unique list of authorities for all users in the UserMap that meet the filter criteria
@@ -73,16 +73,21 @@ class UserMap {
73
73
  await this.subscription.subscribe();
74
74
  }
75
75
  async addPubkey(userAccountPublicKey, userAccount, slot, accountSubscription) {
76
+ var _a;
76
77
  const user = new __1.User({
77
78
  driftClient: this.driftClient,
78
79
  userAccountPublicKey,
79
80
  accountSubscription: accountSubscription !== null && accountSubscription !== void 0 ? accountSubscription : {
80
81
  type: 'custom',
82
+ // OneShotUserAccountSubscriber used here so we don't load up the RPC with AccountSubscribes
81
83
  userAccountSubscriber: new __1.OneShotUserAccountSubscriber(this.driftClient.program, userAccountPublicKey, userAccount, slot, this.commitment),
82
84
  },
83
85
  });
84
86
  await user.subscribe(userAccount);
85
- this.userMap.set(userAccountPublicKey.toString(), user);
87
+ this.userMap.set(userAccountPublicKey.toString(), {
88
+ data: user,
89
+ slot: slot !== null && slot !== void 0 ? slot : (_a = user.getUserAccountAndSlot()) === null || _a === void 0 ? void 0 : _a.slot,
90
+ });
86
91
  }
87
92
  has(key) {
88
93
  return this.userMap.has(key);
@@ -93,6 +98,10 @@ class UserMap {
93
98
  * @returns user User | undefined
94
99
  */
95
100
  get(key) {
101
+ var _a;
102
+ return (_a = this.userMap.get(key)) === null || _a === void 0 ? void 0 : _a.data;
103
+ }
104
+ getWithSlot(key) {
96
105
  return this.userMap.get(key);
97
106
  }
98
107
  /**
@@ -104,8 +113,13 @@ class UserMap {
104
113
  if (!this.has(key)) {
105
114
  await this.addPubkey(new web3_js_1.PublicKey(key), undefined, undefined, accountSubscription);
106
115
  }
107
- const user = this.userMap.get(key);
108
- return user;
116
+ return this.userMap.get(key).data;
117
+ }
118
+ async mustGetWithSlot(key, accountSubscription) {
119
+ if (!this.has(key)) {
120
+ await this.addPubkey(new web3_js_1.PublicKey(key), undefined, undefined, accountSubscription);
121
+ }
122
+ return this.userMap.get(key);
109
123
  }
110
124
  /**
111
125
  * gets the Authority for a particular userAccountPublicKey, if no User exists, undefined is returned
@@ -113,11 +127,11 @@ class UserMap {
113
127
  * @returns authority PublicKey | undefined
114
128
  */
115
129
  getUserAuthority(key) {
116
- const chUser = this.userMap.get(key);
117
- if (!chUser) {
130
+ const user = this.userMap.get(key);
131
+ if (!user) {
118
132
  return undefined;
119
133
  }
120
- return chUser.getUserAccount().authority;
134
+ return user.data.getUserAccount().authority;
121
135
  }
122
136
  /**
123
137
  * implements the {@link DLOBSource} interface
@@ -174,9 +188,22 @@ class UserMap {
174
188
  await this.mustGet(lpRecord.user.toString());
175
189
  }
176
190
  }
177
- values() {
191
+ *values() {
192
+ for (const dataAndSlot of this.userMap.values()) {
193
+ yield dataAndSlot.data;
194
+ }
195
+ }
196
+ valuesWithSlot() {
178
197
  return this.userMap.values();
179
198
  }
199
+ *entries() {
200
+ for (const [key, dataAndSlot] of this.userMap.entries()) {
201
+ yield [key, dataAndSlot.data];
202
+ }
203
+ }
204
+ entriesWithSlot() {
205
+ return this.userMap.entries();
206
+ }
180
207
  size() {
181
208
  return this.userMap.size;
182
209
  }
@@ -186,7 +213,7 @@ class UserMap {
186
213
  * @returns
187
214
  */
188
215
  getUniqueAuthorities(filterCriteria) {
189
- const usersMeetingCriteria = Array.from(this.userMap.values()).filter((user) => {
216
+ const usersMeetingCriteria = Array.from(this.values()).filter((user) => {
190
217
  let pass = true;
191
218
  if (filterCriteria && filterCriteria.hasOpenOrders) {
192
219
  pass = pass && user.getUserAccount().hasOpenOrder;
@@ -233,17 +260,17 @@ class UserMap {
233
260
  for (const [key, buffer] of programAccountBufferMap.entries()) {
234
261
  if (!this.has(key)) {
235
262
  const userAccount = this.decode('User', buffer);
236
- await this.addPubkey(new web3_js_1.PublicKey(key), userAccount);
237
- this.userMap.get(key).accountSubscriber.updateData(userAccount, slot);
263
+ await this.addPubkey(new web3_js_1.PublicKey(key), userAccount, slot);
264
+ this.get(key).accountSubscriber.updateData(userAccount, slot);
238
265
  }
239
266
  else {
240
267
  const userAccount = this.decode('User', buffer);
241
- this.userMap.get(key).accountSubscriber.updateData(userAccount, slot);
268
+ this.get(key).accountSubscriber.updateData(userAccount, slot);
242
269
  }
243
270
  // give event loop a chance to breathe
244
271
  await new Promise((resolve) => setTimeout(resolve, 0));
245
272
  }
246
- for (const [key, user] of this.userMap.entries()) {
273
+ for (const [key, user] of this.entries()) {
247
274
  if (!programAccountBufferMap.has(key)) {
248
275
  await user.unsubscribe();
249
276
  this.userMap.delete(key);
@@ -263,7 +290,7 @@ class UserMap {
263
290
  }
264
291
  async unsubscribe() {
265
292
  await this.subscription.unsubscribe();
266
- for (const [key, user] of this.userMap.entries()) {
293
+ for (const [key, user] of this.entries()) {
267
294
  await user.unsubscribe();
268
295
  this.userMap.delete(key);
269
296
  }
@@ -276,12 +303,16 @@ class UserMap {
276
303
  }
277
304
  async updateUserAccount(key, userAccount, slot) {
278
305
  this.updateLatestSlot(slot);
279
- if (!this.userMap.has(key)) {
306
+ if (!this.has(key)) {
280
307
  this.addPubkey(new web3_js_1.PublicKey(key), userAccount, slot);
281
308
  }
282
309
  else {
283
- const user = this.userMap.get(key);
310
+ const user = this.get(key);
284
311
  user.accountSubscriber.updateData(userAccount, slot);
312
+ this.userMap.set(key, {
313
+ data: user,
314
+ slot,
315
+ });
285
316
  }
286
317
  }
287
318
  updateLatestSlot(slot) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.65.0-beta.0",
3
+ "version": "2.65.0-beta.10",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",