@drift-labs/sdk 2.37.1-beta.7 → 2.37.1-beta.8

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.37.1-beta.7
1
+ 2.37.1-beta.8
@@ -1,11 +1,14 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import { DriftClient } from '../driftClient';
3
4
  import { UserAccount } from '../types';
4
5
  import { Buffer } from 'buffer';
5
6
  import { DLOB } from '../dlob/DLOB';
6
- import { OrderSubscriberConfig } from './types';
7
+ import { OrderSubscriberConfig, OrderSubscriberEvents } from './types';
7
8
  import { PollingSubscription } from './PollingSubscription';
8
9
  import { WebsocketSubscription } from './WebsocketSubscription';
10
+ import StrictEventEmitter from 'strict-event-emitter-types';
11
+ import { EventEmitter } from 'events';
9
12
  export declare class OrderSubscriber {
10
13
  driftClient: DriftClient;
11
14
  usersAccounts: Map<string, {
@@ -13,6 +16,7 @@ export declare class OrderSubscriber {
13
16
  userAccount: UserAccount;
14
17
  }>;
15
18
  subscription: PollingSubscription | WebsocketSubscription;
19
+ eventEmitter: StrictEventEmitter<EventEmitter, OrderSubscriberEvents>;
16
20
  fetchPromise?: Promise<void>;
17
21
  fetchPromiseResolver: () => void;
18
22
  constructor(config: OrderSubscriberConfig);
@@ -7,6 +7,7 @@ const buffer_1 = require("buffer");
7
7
  const DLOB_1 = require("../dlob/DLOB");
8
8
  const PollingSubscription_1 = require("./PollingSubscription");
9
9
  const WebsocketSubscription_1 = require("./WebsocketSubscription");
10
+ const events_1 = require("events");
10
11
  class OrderSubscriber {
11
12
  constructor(config) {
12
13
  this.usersAccounts = new Map();
@@ -23,6 +24,7 @@ class OrderSubscriber {
23
24
  skipInitialLoad: config.subscriptionConfig.skipInitialLoad,
24
25
  });
25
26
  }
27
+ this.eventEmitter = new events_1.EventEmitter();
26
28
  }
27
29
  async subscribe() {
28
30
  await this.subscription.subscribe();
@@ -75,6 +77,14 @@ class OrderSubscriber {
75
77
  const slotAndUserAccount = this.usersAccounts.get(key);
76
78
  if (!slotAndUserAccount || slotAndUserAccount.slot < slot) {
77
79
  const userAccount = this.driftClient.program.account.user.coder.accounts.decode('User', buffer);
80
+ const newOrders = userAccount.orders.filter((order) => {
81
+ var _a;
82
+ return order.slot.toNumber() > ((_a = slotAndUserAccount === null || slotAndUserAccount === void 0 ? void 0 : slotAndUserAccount.slot) !== null && _a !== void 0 ? _a : 0) &&
83
+ order.slot.toNumber() <= slot;
84
+ });
85
+ if (newOrders.length > 0) {
86
+ this.eventEmitter.emit('onUpdate', userAccount, newOrders, new web3_js_1.PublicKey(key), slot);
87
+ }
78
88
  if (userAccount.hasOpenOrder) {
79
89
  this.usersAccounts.set(key, { slot, userAccount });
80
90
  }
@@ -1,3 +1,5 @@
1
+ import { PublicKey } from '@solana/web3.js';
2
+ import { Order, UserAccount } from '../types';
1
3
  import { DriftClient } from '../driftClient';
2
4
  export type OrderSubscriberConfig = {
3
5
  driftClient: DriftClient;
@@ -9,3 +11,6 @@ export type OrderSubscriberConfig = {
9
11
  skipInitialLoad?: boolean;
10
12
  };
11
13
  };
14
+ export interface OrderSubscriberEvents {
15
+ onUpdate: (account: UserAccount, updatedOrders: Order[], pubkey: PublicKey, slot: number) => void;
16
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.37.1-beta.7",
3
+ "version": "2.37.1-beta.8",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -4,14 +4,17 @@ import { getUserFilter, getUserWithOrderFilter } from '../memcmp';
4
4
  import { PublicKey, RpcResponseAndContext } from '@solana/web3.js';
5
5
  import { Buffer } from 'buffer';
6
6
  import { DLOB } from '../dlob/DLOB';
7
- import { OrderSubscriberConfig } from './types';
7
+ import { OrderSubscriberConfig, OrderSubscriberEvents } from './types';
8
8
  import { PollingSubscription } from './PollingSubscription';
9
9
  import { WebsocketSubscription } from './WebsocketSubscription';
10
+ import StrictEventEmitter from 'strict-event-emitter-types';
11
+ import { EventEmitter } from 'events';
10
12
 
11
13
  export class OrderSubscriber {
12
14
  driftClient: DriftClient;
13
15
  usersAccounts = new Map<string, { slot: number; userAccount: UserAccount }>();
14
16
  subscription: PollingSubscription | WebsocketSubscription;
17
+ eventEmitter: StrictEventEmitter<EventEmitter, OrderSubscriberEvents>;
15
18
 
16
19
  fetchPromise?: Promise<void>;
17
20
  fetchPromiseResolver: () => void;
@@ -29,6 +32,7 @@ export class OrderSubscriber {
29
32
  skipInitialLoad: config.subscriptionConfig.skipInitialLoad,
30
33
  });
31
34
  }
35
+ this.eventEmitter = new EventEmitter();
32
36
  }
33
37
 
34
38
  public async subscribe(): Promise<void> {
@@ -106,7 +110,20 @@ export class OrderSubscriber {
106
110
  'User',
107
111
  buffer
108
112
  ) as UserAccount;
109
-
113
+ const newOrders = userAccount.orders.filter(
114
+ (order) =>
115
+ order.slot.toNumber() > (slotAndUserAccount?.slot ?? 0) &&
116
+ order.slot.toNumber() <= slot
117
+ );
118
+ if (newOrders.length > 0) {
119
+ this.eventEmitter.emit(
120
+ 'onUpdate',
121
+ userAccount,
122
+ newOrders,
123
+ new PublicKey(key),
124
+ slot
125
+ );
126
+ }
110
127
  if (userAccount.hasOpenOrder) {
111
128
  this.usersAccounts.set(key, { slot, userAccount });
112
129
  } else {
@@ -1,3 +1,5 @@
1
+ import { PublicKey } from '@solana/web3.js';
2
+ import { Order, UserAccount } from '../types';
1
3
  import { DriftClient } from '../driftClient';
2
4
 
3
5
  export type OrderSubscriberConfig = {
@@ -12,3 +14,12 @@ export type OrderSubscriberConfig = {
12
14
  skipInitialLoad?: boolean;
13
15
  };
14
16
  };
17
+
18
+ export interface OrderSubscriberEvents {
19
+ onUpdate: (
20
+ account: UserAccount,
21
+ updatedOrders: Order[],
22
+ pubkey: PublicKey,
23
+ slot: number
24
+ ) => void;
25
+ }