@drift-labs/sdk 2.10.0-beta.2 → 2.10.0-beta.4

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.
@@ -10,6 +10,7 @@ export interface AccountSubscriber<T> {
10
10
  subscribe(onChange: (data: T) => void): Promise<void>;
11
11
  fetch(): Promise<void>;
12
12
  unsubscribe(): Promise<void>;
13
+ setData(userAccount: T): void;
13
14
  }
14
15
  export declare class NotSubscribedError extends Error {
15
16
  name: string;
@@ -47,7 +48,7 @@ export interface UserAccountEvents {
47
48
  export interface UserAccountSubscriber {
48
49
  eventEmitter: StrictEventEmitter<EventEmitter, UserAccountEvents>;
49
50
  isSubscribed: boolean;
50
- subscribe(): Promise<boolean>;
51
+ subscribe(userAccount?: UserAccount): Promise<boolean>;
51
52
  fetch(): Promise<void>;
52
53
  unsubscribe(): Promise<void>;
53
54
  getUserAccountAndSlot(): DataAndSlot<UserAccount>;
@@ -106,7 +107,7 @@ export interface UserStatsAccountEvents {
106
107
  export interface UserStatsAccountSubscriber {
107
108
  eventEmitter: StrictEventEmitter<EventEmitter, UserStatsAccountEvents>;
108
109
  isSubscribed: boolean;
109
- subscribe(): Promise<boolean>;
110
+ subscribe(userStatsAccount?: UserStatsAccount): Promise<boolean>;
110
111
  fetch(): Promise<void>;
111
112
  unsubscribe(): Promise<void>;
112
113
  getUserStatsAccountAndSlot(): DataAndSlot<UserStatsAccount>;
@@ -13,6 +13,7 @@ export declare class WebSocketAccountSubscriber<T> implements AccountSubscriber<
13
13
  listenerId?: number;
14
14
  constructor(accountName: string, program: Program, accountPublicKey: PublicKey, decodeBuffer?: (buffer: Buffer) => T);
15
15
  subscribe(onChange: (data: T) => void): Promise<void>;
16
+ setData(data: T): void;
16
17
  fetch(): Promise<void>;
17
18
  handleRpcResponse(context: Context, accountInfo?: AccountInfo<Buffer>): void;
18
19
  decodeBuffer(buffer: Buffer): T;
@@ -14,11 +14,22 @@ class WebSocketAccountSubscriber {
14
14
  return;
15
15
  }
16
16
  this.onChange = onChange;
17
- await this.fetch();
17
+ if (!this.dataAndSlot) {
18
+ await this.fetch();
19
+ }
18
20
  this.listenerId = this.program.provider.connection.onAccountChange(this.accountPublicKey, (accountInfo, context) => {
19
21
  this.handleRpcResponse(context, accountInfo);
20
22
  }, this.program.provider.opts.commitment);
21
23
  }
24
+ setData(data) {
25
+ if (this.dataAndSlot) {
26
+ return;
27
+ }
28
+ this.dataAndSlot = {
29
+ data,
30
+ slot: undefined,
31
+ };
32
+ }
22
33
  async fetch() {
23
34
  const rpcResponse = await this.program.provider.connection.getAccountInfoAndContext(this.accountPublicKey, this.program.provider.opts.commitment);
24
35
  this.handleRpcResponse(rpcResponse.context, rpcResponse === null || rpcResponse === void 0 ? void 0 : rpcResponse.value);
@@ -12,7 +12,7 @@ export declare class WebSocketUserAccountSubscriber implements UserAccountSubscr
12
12
  userAccountPublicKey: PublicKey;
13
13
  userDataAccountSubscriber: AccountSubscriber<UserAccount>;
14
14
  constructor(program: Program, userAccountPublicKey: PublicKey);
15
- subscribe(): Promise<boolean>;
15
+ subscribe(userAccount?: UserAccount): Promise<boolean>;
16
16
  fetch(): Promise<void>;
17
17
  unsubscribe(): Promise<void>;
18
18
  assertIsSubscribed(): void;
@@ -11,11 +11,14 @@ class WebSocketUserAccountSubscriber {
11
11
  this.userAccountPublicKey = userAccountPublicKey;
12
12
  this.eventEmitter = new events_1.EventEmitter();
13
13
  }
14
- async subscribe() {
14
+ async subscribe(userAccount) {
15
15
  if (this.isSubscribed) {
16
16
  return true;
17
17
  }
18
18
  this.userDataAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('user', this.program, this.userAccountPublicKey);
19
+ if (userAccount) {
20
+ this.userDataAccountSubscriber.setData(userAccount);
21
+ }
19
22
  await this.userDataAccountSubscriber.subscribe((data) => {
20
23
  this.eventEmitter.emit('userAccountUpdate', data);
21
24
  this.eventEmitter.emit('update');
@@ -12,7 +12,7 @@ export declare class WebSocketUserStatsAccountSubscriber implements UserStatsAcc
12
12
  userStatsAccountPublicKey: PublicKey;
13
13
  userStatsAccountSubscriber: AccountSubscriber<UserStatsAccount>;
14
14
  constructor(program: Program, userStatsAccountPublicKey: PublicKey);
15
- subscribe(): Promise<boolean>;
15
+ subscribe(userStatsAccount?: UserStatsAccount): Promise<boolean>;
16
16
  fetch(): Promise<void>;
17
17
  unsubscribe(): Promise<void>;
18
18
  assertIsSubscribed(): void;
@@ -11,11 +11,14 @@ class WebSocketUserStatsAccountSubscriber {
11
11
  this.userStatsAccountPublicKey = userStatsAccountPublicKey;
12
12
  this.eventEmitter = new events_1.EventEmitter();
13
13
  }
14
- async subscribe() {
14
+ async subscribe(userStatsAccount) {
15
15
  if (this.isSubscribed) {
16
16
  return true;
17
17
  }
18
18
  this.userStatsAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('userStats', this.program, this.userStatsAccountPublicKey);
19
+ if (userStatsAccount) {
20
+ this.userStatsAccountSubscriber.setData(userStatsAccount);
21
+ }
19
22
  await this.userStatsAccountSubscriber.subscribe((data) => {
20
23
  this.eventEmitter.emit('userStatsAccountUpdate', data);
21
24
  this.eventEmitter.emit('update');
@@ -19,7 +19,7 @@ export declare type DriftClientConfig = {
19
19
  userStats?: boolean;
20
20
  authority?: PublicKey;
21
21
  };
22
- declare type DriftClientSubscriptionConfig = {
22
+ export declare type DriftClientSubscriptionConfig = {
23
23
  type: 'websocket';
24
24
  } | {
25
25
  type: 'polling';
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.10.0-beta.2",
2
+ "version": "2.10.0-beta.4",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
package/lib/user.d.ts CHANGED
@@ -22,7 +22,7 @@ export declare class User {
22
22
  * Subscribe to User state accounts
23
23
  * @returns SusbcriptionSuccess result
24
24
  */
25
- subscribe(): Promise<boolean>;
25
+ subscribe(userAccount?: UserAccount): Promise<boolean>;
26
26
  /**
27
27
  * Forces the accountSubscriber to fetch account updates from rpc
28
28
  */
package/lib/user.js CHANGED
@@ -35,8 +35,8 @@ class User {
35
35
  * Subscribe to User state accounts
36
36
  * @returns SusbcriptionSuccess result
37
37
  */
38
- async subscribe() {
39
- this.isSubscribed = await this.accountSubscriber.subscribe();
38
+ async subscribe(userAccount) {
39
+ this.isSubscribed = await this.accountSubscriber.subscribe(userAccount);
40
40
  return this.isSubscribed;
41
41
  }
42
42
  /**
@@ -11,6 +11,7 @@ class UserMap {
11
11
  }
12
12
  async fetchAllUsers() {
13
13
  const userArray = [];
14
+ const userAccountArray = [];
14
15
  const programUserAccounts = (await this.driftClient.program.account.user.all());
15
16
  for (const programUserAccount of programUserAccounts) {
16
17
  if (this.userMap.has(programUserAccount.publicKey.toString())) {
@@ -22,10 +23,14 @@ class UserMap {
22
23
  accountSubscription: this.accountSubscription,
23
24
  });
24
25
  userArray.push(user);
26
+ userAccountArray.push(programUserAccount.account);
25
27
  }
26
28
  if (this.accountSubscription.type === 'polling') {
27
29
  await (0, __1.bulkPollingUserSubscribe)(userArray, this.accountSubscription.accountLoader);
28
30
  }
31
+ else {
32
+ await Promise.all(userArray.map((user, i) => user.subscribe(userAccountArray[i])));
33
+ }
29
34
  for (const user of userArray) {
30
35
  this.userMap.set(user.getUserAccountPublicKey().toString(), user);
31
36
  }
@@ -14,6 +14,7 @@ class UserStatsMap {
14
14
  }
15
15
  async fetchAllUserStats() {
16
16
  const userStatArray = [];
17
+ const userStatsAccountArray = [];
17
18
  const programUserAccounts = (await this.driftClient.program.account.userStats.all());
18
19
  for (const programUserAccount of programUserAccounts) {
19
20
  const userStat = programUserAccount.account;
@@ -26,10 +27,14 @@ class UserStatsMap {
26
27
  accountSubscription: this.accountSubscription,
27
28
  });
28
29
  userStatArray.push(chUserStat);
30
+ userStatsAccountArray.push(userStat);
29
31
  }
30
32
  if (this.accountSubscription.type === 'polling') {
31
33
  await (0, __1.bulkPollingUserStatsSubscribe)(userStatArray, this.accountSubscription.accountLoader);
32
34
  }
35
+ else {
36
+ await Promise.all(userStatArray.map((userStat, i) => userStat.subscribe(userStatsAccountArray[i])));
37
+ }
33
38
  for (const userStat of userStatArray) {
34
39
  this.userStatsMap.set(userStat.getAccount().authority.toString(), userStat);
35
40
  }
@@ -9,7 +9,7 @@ export declare class UserStats {
9
9
  accountSubscriber: UserStatsAccountSubscriber;
10
10
  isSubscribed: boolean;
11
11
  constructor(config: UserStatsConfig);
12
- subscribe(): Promise<boolean>;
12
+ subscribe(userStatsAccount?: UserStatsAccount): Promise<boolean>;
13
13
  fetchAccounts(): Promise<void>;
14
14
  unsubscribe(): Promise<void>;
15
15
  getAccountAndSlot(): DataAndSlot<UserStatsAccount>;
package/lib/userStats.js CHANGED
@@ -17,8 +17,8 @@ class UserStats {
17
17
  this.accountSubscriber = new webSocketUserStatsAccountSubsriber_1.WebSocketUserStatsAccountSubscriber(config.driftClient.program, config.userStatsAccountPublicKey);
18
18
  }
19
19
  }
20
- async subscribe() {
21
- this.isSubscribed = await this.accountSubscriber.subscribe();
20
+ async subscribe(userStatsAccount) {
21
+ this.isSubscribed = await this.accountSubscriber.subscribe(userStatsAccount);
22
22
  return this.isSubscribed;
23
23
  }
24
24
  async fetchAccounts() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.10.0-beta.2",
3
+ "version": "2.10.0-beta.4",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -17,6 +17,8 @@ export interface AccountSubscriber<T> {
17
17
  subscribe(onChange: (data: T) => void): Promise<void>;
18
18
  fetch(): Promise<void>;
19
19
  unsubscribe(): Promise<void>;
20
+
21
+ setData(userAccount: T): void;
20
22
  }
21
23
 
22
24
  export class NotSubscribedError extends Error {
@@ -69,7 +71,7 @@ export interface UserAccountSubscriber {
69
71
  eventEmitter: StrictEventEmitter<EventEmitter, UserAccountEvents>;
70
72
  isSubscribed: boolean;
71
73
 
72
- subscribe(): Promise<boolean>;
74
+ subscribe(userAccount?: UserAccount): Promise<boolean>;
73
75
  fetch(): Promise<void>;
74
76
  unsubscribe(): Promise<void>;
75
77
 
@@ -144,7 +146,7 @@ export interface UserStatsAccountSubscriber {
144
146
  eventEmitter: StrictEventEmitter<EventEmitter, UserStatsAccountEvents>;
145
147
  isSubscribed: boolean;
146
148
 
147
- subscribe(): Promise<boolean>;
149
+ subscribe(userStatsAccount?: UserStatsAccount): Promise<boolean>;
148
150
  fetch(): Promise<void>;
149
151
  unsubscribe(): Promise<void>;
150
152
 
@@ -32,7 +32,9 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
32
32
  }
33
33
 
34
34
  this.onChange = onChange;
35
- await this.fetch();
35
+ if (!this.dataAndSlot) {
36
+ await this.fetch();
37
+ }
36
38
 
37
39
  this.listenerId = this.program.provider.connection.onAccountChange(
38
40
  this.accountPublicKey,
@@ -43,6 +45,17 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
43
45
  );
44
46
  }
45
47
 
48
+ setData(data: T): void {
49
+ if (this.dataAndSlot) {
50
+ return;
51
+ }
52
+
53
+ this.dataAndSlot = {
54
+ data,
55
+ slot: undefined,
56
+ };
57
+ }
58
+
46
59
  async fetch(): Promise<void> {
47
60
  const rpcResponse =
48
61
  await this.program.provider.connection.getAccountInfoAndContext(
@@ -27,7 +27,7 @@ export class WebSocketUserAccountSubscriber implements UserAccountSubscriber {
27
27
  this.eventEmitter = new EventEmitter();
28
28
  }
29
29
 
30
- async subscribe(): Promise<boolean> {
30
+ async subscribe(userAccount?: UserAccount): Promise<boolean> {
31
31
  if (this.isSubscribed) {
32
32
  return true;
33
33
  }
@@ -37,6 +37,11 @@ export class WebSocketUserAccountSubscriber implements UserAccountSubscriber {
37
37
  this.program,
38
38
  this.userAccountPublicKey
39
39
  );
40
+
41
+ if (userAccount) {
42
+ this.userDataAccountSubscriber.setData(userAccount);
43
+ }
44
+
40
45
  await this.userDataAccountSubscriber.subscribe((data: UserAccount) => {
41
46
  this.eventEmitter.emit('userAccountUpdate', data);
42
47
  this.eventEmitter.emit('update');
@@ -29,7 +29,7 @@ export class WebSocketUserStatsAccountSubscriber
29
29
  this.eventEmitter = new EventEmitter();
30
30
  }
31
31
 
32
- async subscribe(): Promise<boolean> {
32
+ async subscribe(userStatsAccount?: UserStatsAccount): Promise<boolean> {
33
33
  if (this.isSubscribed) {
34
34
  return true;
35
35
  }
@@ -39,6 +39,11 @@ export class WebSocketUserStatsAccountSubscriber
39
39
  this.program,
40
40
  this.userStatsAccountPublicKey
41
41
  );
42
+
43
+ if (userStatsAccount) {
44
+ this.userStatsAccountSubscriber.setData(userStatsAccount);
45
+ }
46
+
42
47
  await this.userStatsAccountSubscriber.subscribe(
43
48
  (data: UserStatsAccount) => {
44
49
  this.eventEmitter.emit('userStatsAccountUpdate', data);
@@ -21,7 +21,7 @@ export type DriftClientConfig = {
21
21
  authority?: PublicKey; // explicitly pass an authority if signer is delegate
22
22
  };
23
23
 
24
- type DriftClientSubscriptionConfig =
24
+ export type DriftClientSubscriptionConfig =
25
25
  | {
26
26
  type: 'websocket';
27
27
  }
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.10.0-beta.2",
2
+ "version": "2.10.0-beta.4",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
package/src/user.ts CHANGED
@@ -103,8 +103,8 @@ export class User {
103
103
  * Subscribe to User state accounts
104
104
  * @returns SusbcriptionSuccess result
105
105
  */
106
- public async subscribe(): Promise<boolean> {
107
- this.isSubscribed = await this.accountSubscriber.subscribe();
106
+ public async subscribe(userAccount?: UserAccount): Promise<boolean> {
107
+ this.isSubscribed = await this.accountSubscriber.subscribe(userAccount);
108
108
  return this.isSubscribed;
109
109
  }
110
110
 
@@ -44,6 +44,7 @@ export class UserMap implements UserMapInterface {
44
44
 
45
45
  public async fetchAllUsers() {
46
46
  const userArray: User[] = [];
47
+ const userAccountArray: UserAccount[] = [];
47
48
 
48
49
  const programUserAccounts =
49
50
  (await this.driftClient.program.account.user.all()) as ProgramAccount<UserAccount>[];
@@ -58,6 +59,7 @@ export class UserMap implements UserMapInterface {
58
59
  accountSubscription: this.accountSubscription,
59
60
  });
60
61
  userArray.push(user);
62
+ userAccountArray.push(programUserAccount.account);
61
63
  }
62
64
 
63
65
  if (this.accountSubscription.type === 'polling') {
@@ -65,6 +67,10 @@ export class UserMap implements UserMapInterface {
65
67
  userArray,
66
68
  this.accountSubscription.accountLoader
67
69
  );
70
+ } else {
71
+ await Promise.all(
72
+ userArray.map((user, i) => user.subscribe(userAccountArray[i]))
73
+ );
68
74
  }
69
75
 
70
76
  for (const user of userArray) {
@@ -39,6 +39,7 @@ export class UserStatsMap {
39
39
 
40
40
  public async fetchAllUserStats() {
41
41
  const userStatArray: UserStats[] = [];
42
+ const userStatsAccountArray: UserStatsAccount[] = [];
42
43
 
43
44
  const programUserAccounts =
44
45
  (await this.driftClient.program.account.userStats.all()) as ProgramAccount<UserStatsAccount>[];
@@ -58,6 +59,7 @@ export class UserStatsMap {
58
59
  accountSubscription: this.accountSubscription,
59
60
  });
60
61
  userStatArray.push(chUserStat);
62
+ userStatsAccountArray.push(userStat);
61
63
  }
62
64
 
63
65
  if (this.accountSubscription.type === 'polling') {
@@ -65,6 +67,12 @@ export class UserStatsMap {
65
67
  userStatArray,
66
68
  this.accountSubscription.accountLoader
67
69
  );
70
+ } else {
71
+ await Promise.all(
72
+ userStatArray.map((userStat, i) =>
73
+ userStat.subscribe(userStatsAccountArray[i])
74
+ )
75
+ );
68
76
  }
69
77
 
70
78
  for (const userStat of userStatArray) {
package/src/userStats.ts CHANGED
@@ -33,8 +33,12 @@ export class UserStats {
33
33
  }
34
34
  }
35
35
 
36
- public async subscribe(): Promise<boolean> {
37
- this.isSubscribed = await this.accountSubscriber.subscribe();
36
+ public async subscribe(
37
+ userStatsAccount?: UserStatsAccount
38
+ ): Promise<boolean> {
39
+ this.isSubscribed = await this.accountSubscriber.subscribe(
40
+ userStatsAccount
41
+ );
38
42
  return this.isSubscribed;
39
43
  }
40
44