@drift-labs/sdk 2.49.0-beta.7 → 2.49.0-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.49.0-beta.7
1
+ 2.49.0-beta.8
@@ -0,0 +1,29 @@
1
+ /// <reference types="node" />
2
+ import { DataAndSlot, InsuranceFundStakeAccountEvents, InsuranceFundStakeAccountSubscriber } from './types';
3
+ import { Program } from '@coral-xyz/anchor';
4
+ import StrictEventEmitter from 'strict-event-emitter-types';
5
+ import { EventEmitter } from 'events';
6
+ import { PublicKey } from '@solana/web3.js';
7
+ import { BulkAccountLoader } from './bulkAccountLoader';
8
+ import { InsuranceFundStake } from '../types';
9
+ export declare class PollingInsuranceFundStakeAccountSubscriber implements InsuranceFundStakeAccountSubscriber {
10
+ isSubscribed: boolean;
11
+ program: Program;
12
+ eventEmitter: StrictEventEmitter<EventEmitter, InsuranceFundStakeAccountEvents>;
13
+ insuranceFundStakeAccountPublicKey: PublicKey;
14
+ accountLoader: BulkAccountLoader;
15
+ callbackId?: string;
16
+ errorCallbackId?: string;
17
+ insuranceFundStakeAccountAndSlot?: DataAndSlot<InsuranceFundStake>;
18
+ constructor(program: Program, publicKey: PublicKey, accountLoader: BulkAccountLoader);
19
+ subscribe(insuranceFundStake?: InsuranceFundStake): Promise<boolean>;
20
+ addToAccountLoader(): Promise<void>;
21
+ fetchIfUnloaded(): Promise<void>;
22
+ fetch(): Promise<void>;
23
+ doesAccountExist(): boolean;
24
+ unsubscribe(): Promise<void>;
25
+ assertIsSubscribed(): void;
26
+ getInsuranceFundStakeAccountAndSlot(): DataAndSlot<InsuranceFundStake>;
27
+ didSubscriptionSucceed(): boolean;
28
+ updateData(insuranceFundStake: InsuranceFundStake, slot: number): void;
29
+ }
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PollingInsuranceFundStakeAccountSubscriber = void 0;
4
+ const types_1 = require("./types");
5
+ const events_1 = require("events");
6
+ class PollingInsuranceFundStakeAccountSubscriber {
7
+ constructor(program, publicKey, accountLoader) {
8
+ this.isSubscribed = false;
9
+ this.program = program;
10
+ this.insuranceFundStakeAccountPublicKey = publicKey;
11
+ this.accountLoader = accountLoader;
12
+ this.eventEmitter = new events_1.EventEmitter();
13
+ }
14
+ async subscribe(insuranceFundStake) {
15
+ if (this.isSubscribed) {
16
+ return true;
17
+ }
18
+ if (insuranceFundStake) {
19
+ this.insuranceFundStakeAccountAndSlot = {
20
+ data: insuranceFundStake,
21
+ slot: undefined,
22
+ };
23
+ }
24
+ await this.addToAccountLoader();
25
+ await this.fetchIfUnloaded();
26
+ if (this.doesAccountExist()) {
27
+ this.eventEmitter.emit('update');
28
+ }
29
+ this.isSubscribed = true;
30
+ return true;
31
+ }
32
+ async addToAccountLoader() {
33
+ if (this.callbackId) {
34
+ return;
35
+ }
36
+ this.callbackId = await this.accountLoader.addAccount(this.insuranceFundStakeAccountPublicKey, (buffer, slot) => {
37
+ if (!buffer) {
38
+ return;
39
+ }
40
+ if (this.insuranceFundStakeAccountAndSlot &&
41
+ this.insuranceFundStakeAccountAndSlot.slot > slot) {
42
+ return;
43
+ }
44
+ const account = this.program.account.user.coder.accounts.decode('InsuranceFundStake', buffer);
45
+ this.insuranceFundStakeAccountAndSlot = { data: account, slot };
46
+ this.eventEmitter.emit('insuranceFundStakeAccountUpdate', account);
47
+ this.eventEmitter.emit('update');
48
+ });
49
+ this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => {
50
+ this.eventEmitter.emit('error', error);
51
+ });
52
+ }
53
+ async fetchIfUnloaded() {
54
+ if (this.insuranceFundStakeAccountAndSlot === undefined) {
55
+ await this.fetch();
56
+ }
57
+ }
58
+ async fetch() {
59
+ var _a, _b;
60
+ try {
61
+ const dataAndContext = await this.program.account.insuranceFundStake.fetchAndContext(this.insuranceFundStakeAccountPublicKey, this.accountLoader.commitment);
62
+ if (dataAndContext.context.slot >
63
+ ((_b = (_a = this.insuranceFundStakeAccountAndSlot) === null || _a === void 0 ? void 0 : _a.slot) !== null && _b !== void 0 ? _b : 0)) {
64
+ this.insuranceFundStakeAccountAndSlot = {
65
+ data: dataAndContext.data,
66
+ slot: dataAndContext.context.slot,
67
+ };
68
+ }
69
+ }
70
+ catch (e) {
71
+ console.log(`PollingInsuranceFundStakeAccountSubscriber.fetch() InsuranceFundStake does not exist: ${e.message}`);
72
+ }
73
+ }
74
+ doesAccountExist() {
75
+ return this.insuranceFundStakeAccountAndSlot !== undefined;
76
+ }
77
+ async unsubscribe() {
78
+ if (!this.isSubscribed) {
79
+ return;
80
+ }
81
+ this.accountLoader.removeAccount(this.insuranceFundStakeAccountPublicKey, this.callbackId);
82
+ this.callbackId = undefined;
83
+ this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
84
+ this.errorCallbackId = undefined;
85
+ this.isSubscribed = false;
86
+ }
87
+ assertIsSubscribed() {
88
+ if (!this.isSubscribed) {
89
+ throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
90
+ }
91
+ }
92
+ getInsuranceFundStakeAccountAndSlot() {
93
+ this.assertIsSubscribed();
94
+ return this.insuranceFundStakeAccountAndSlot;
95
+ }
96
+ didSubscriptionSucceed() {
97
+ return !!this.insuranceFundStakeAccountAndSlot;
98
+ }
99
+ updateData(insuranceFundStake, slot) {
100
+ if (!this.insuranceFundStakeAccountAndSlot ||
101
+ this.insuranceFundStakeAccountAndSlot.slot < slot) {
102
+ this.insuranceFundStakeAccountAndSlot = {
103
+ data: insuranceFundStake,
104
+ slot,
105
+ };
106
+ this.eventEmitter.emit('insuranceFundStakeAccountUpdate', insuranceFundStake);
107
+ this.eventEmitter.emit('update');
108
+ }
109
+ }
110
+ }
111
+ exports.PollingInsuranceFundStakeAccountSubscriber = PollingInsuranceFundStakeAccountSubscriber;
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
- import { SpotMarketAccount, PerpMarketAccount, OracleSource, StateAccount, UserAccount, UserStatsAccount } from '../types';
3
+ import { SpotMarketAccount, PerpMarketAccount, OracleSource, StateAccount, UserAccount, UserStatsAccount, InsuranceFundStake } from '../types';
4
4
  import StrictEventEmitter from 'strict-event-emitter-types';
5
5
  import { EventEmitter } from 'events';
6
6
  import { Context, PublicKey } from '@solana/web3.js';
@@ -73,6 +73,19 @@ export interface TokenAccountSubscriber {
73
73
  unsubscribe(): Promise<void>;
74
74
  getTokenAccountAndSlot(): DataAndSlot<Account>;
75
75
  }
76
+ export interface InsuranceFundStakeAccountSubscriber {
77
+ eventEmitter: StrictEventEmitter<EventEmitter, InsuranceFundStakeAccountEvents>;
78
+ isSubscribed: boolean;
79
+ subscribe(): Promise<boolean>;
80
+ fetch(): Promise<void>;
81
+ unsubscribe(): Promise<void>;
82
+ getInsuranceFundStakeAccountAndSlot(): DataAndSlot<InsuranceFundStake>;
83
+ }
84
+ export interface InsuranceFundStakeAccountEvents {
85
+ insuranceFundStakeAccountUpdate: (payload: InsuranceFundStake) => void;
86
+ update: void;
87
+ error: (e: Error) => void;
88
+ }
76
89
  export interface OracleEvents {
77
90
  oracleUpdate: (payload: OraclePriceData) => void;
78
91
  update: void;
@@ -0,0 +1,23 @@
1
+ /// <reference types="node" />
2
+ import { DataAndSlot, AccountSubscriber, InsuranceFundStakeAccountEvents, InsuranceFundStakeAccountSubscriber } from './types';
3
+ import { Program } from '@coral-xyz/anchor';
4
+ import StrictEventEmitter from 'strict-event-emitter-types';
5
+ import { EventEmitter } from 'events';
6
+ import { Commitment, PublicKey } from '@solana/web3.js';
7
+ import { InsuranceFundStake } from '../types';
8
+ export declare class WebSocketInsuranceFundStakeAccountSubscriber implements InsuranceFundStakeAccountSubscriber {
9
+ isSubscribed: boolean;
10
+ reconnectTimeoutMs?: number;
11
+ commitment?: Commitment;
12
+ program: Program;
13
+ eventEmitter: StrictEventEmitter<EventEmitter, InsuranceFundStakeAccountEvents>;
14
+ insuranceFundStakeAccountPublicKey: PublicKey;
15
+ insuranceFundStakeDataAccountSubscriber: AccountSubscriber<InsuranceFundStake>;
16
+ constructor(program: Program, insuranceFundStakeAccountPublicKey: PublicKey, reconnectTimeoutMs?: number, commitment?: Commitment);
17
+ subscribe(insuranceFundStakeAccount?: InsuranceFundStake): Promise<boolean>;
18
+ fetch(): Promise<void>;
19
+ unsubscribe(): Promise<void>;
20
+ assertIsSubscribed(): void;
21
+ getInsuranceFundStakeAccountAndSlot(): DataAndSlot<InsuranceFundStake>;
22
+ updateData(insuranceFundStake: InsuranceFundStake, slot: number): void;
23
+ }
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WebSocketInsuranceFundStakeAccountSubscriber = void 0;
4
+ const types_1 = require("./types");
5
+ const events_1 = require("events");
6
+ const webSocketAccountSubscriber_1 = require("./webSocketAccountSubscriber");
7
+ class WebSocketInsuranceFundStakeAccountSubscriber {
8
+ constructor(program, insuranceFundStakeAccountPublicKey, reconnectTimeoutMs, commitment) {
9
+ this.isSubscribed = false;
10
+ this.program = program;
11
+ this.insuranceFundStakeAccountPublicKey =
12
+ insuranceFundStakeAccountPublicKey;
13
+ this.eventEmitter = new events_1.EventEmitter();
14
+ this.reconnectTimeoutMs = reconnectTimeoutMs;
15
+ this.commitment = commitment;
16
+ }
17
+ async subscribe(insuranceFundStakeAccount) {
18
+ if (this.isSubscribed) {
19
+ return true;
20
+ }
21
+ this.insuranceFundStakeDataAccountSubscriber =
22
+ new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('insuranceFundStake', this.program, this.insuranceFundStakeAccountPublicKey, undefined, this.reconnectTimeoutMs, this.commitment);
23
+ if (insuranceFundStakeAccount) {
24
+ this.insuranceFundStakeDataAccountSubscriber.setData(insuranceFundStakeAccount);
25
+ }
26
+ await this.insuranceFundStakeDataAccountSubscriber.subscribe((data) => {
27
+ this.eventEmitter.emit('insuranceFundStakeAccountUpdate', data);
28
+ this.eventEmitter.emit('update');
29
+ });
30
+ this.eventEmitter.emit('update');
31
+ this.isSubscribed = true;
32
+ return true;
33
+ }
34
+ async fetch() {
35
+ await Promise.all([this.insuranceFundStakeDataAccountSubscriber.fetch()]);
36
+ }
37
+ async unsubscribe() {
38
+ if (!this.isSubscribed) {
39
+ return;
40
+ }
41
+ await Promise.all([
42
+ this.insuranceFundStakeDataAccountSubscriber.unsubscribe(),
43
+ ]);
44
+ this.isSubscribed = false;
45
+ }
46
+ assertIsSubscribed() {
47
+ if (!this.isSubscribed) {
48
+ throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
49
+ }
50
+ }
51
+ getInsuranceFundStakeAccountAndSlot() {
52
+ this.assertIsSubscribed();
53
+ return this.insuranceFundStakeDataAccountSubscriber.dataAndSlot;
54
+ }
55
+ updateData(insuranceFundStake, slot) {
56
+ var _a;
57
+ const currentDataSlot = ((_a = this.insuranceFundStakeDataAccountSubscriber.dataAndSlot) === null || _a === void 0 ? void 0 : _a.slot) || 0;
58
+ if (currentDataSlot <= slot) {
59
+ this.insuranceFundStakeDataAccountSubscriber.setData(insuranceFundStake, slot);
60
+ this.eventEmitter.emit('insuranceFundStakeAccountUpdate', insuranceFundStake);
61
+ this.eventEmitter.emit('update');
62
+ }
63
+ }
64
+ }
65
+ exports.WebSocketInsuranceFundStakeAccountSubscriber = WebSocketInsuranceFundStakeAccountSubscriber;
package/lib/index.d.ts CHANGED
@@ -9,6 +9,7 @@ export * from './types';
9
9
  export * from './constants/perpMarkets';
10
10
  export * from './accounts/fetch';
11
11
  export * from './accounts/webSocketDriftClientAccountSubscriber';
12
+ export * from './accounts/webSocketInsuranceFundStakeAccountSubscriber';
12
13
  export * from './accounts/bulkAccountLoader';
13
14
  export * from './accounts/bulkUserSubscription';
14
15
  export * from './accounts/bulkUserStatsSubscription';
@@ -17,6 +18,7 @@ export * from './accounts/pollingOracleAccountSubscriber';
17
18
  export * from './accounts/pollingTokenAccountSubscriber';
18
19
  export * from './accounts/pollingUserAccountSubscriber';
19
20
  export * from './accounts/pollingUserStatsAccountSubscriber';
21
+ export * from './accounts/pollingInsuranceFundStakeAccountSubscriber';
20
22
  export * from './accounts/mockUserAccountSubscriber';
21
23
  export * from './accounts/types';
22
24
  export * from './addresses/pda';
package/lib/index.js CHANGED
@@ -32,6 +32,7 @@ __exportStar(require("./types"), exports);
32
32
  __exportStar(require("./constants/perpMarkets"), exports);
33
33
  __exportStar(require("./accounts/fetch"), exports);
34
34
  __exportStar(require("./accounts/webSocketDriftClientAccountSubscriber"), exports);
35
+ __exportStar(require("./accounts/webSocketInsuranceFundStakeAccountSubscriber"), exports);
35
36
  __exportStar(require("./accounts/bulkAccountLoader"), exports);
36
37
  __exportStar(require("./accounts/bulkUserSubscription"), exports);
37
38
  __exportStar(require("./accounts/bulkUserStatsSubscription"), exports);
@@ -40,6 +41,7 @@ __exportStar(require("./accounts/pollingOracleAccountSubscriber"), exports);
40
41
  __exportStar(require("./accounts/pollingTokenAccountSubscriber"), exports);
41
42
  __exportStar(require("./accounts/pollingUserAccountSubscriber"), exports);
42
43
  __exportStar(require("./accounts/pollingUserStatsAccountSubscriber"), exports);
44
+ __exportStar(require("./accounts/pollingInsuranceFundStakeAccountSubscriber"), exports);
43
45
  __exportStar(require("./accounts/mockUserAccountSubscriber"), exports);
44
46
  __exportStar(require("./accounts/types"), exports);
45
47
  __exportStar(require("./addresses/pda"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.49.0-beta.7",
3
+ "version": "2.49.0-beta.8",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -0,0 +1,186 @@
1
+ import {
2
+ DataAndSlot,
3
+ NotSubscribedError,
4
+ InsuranceFundStakeAccountEvents,
5
+ InsuranceFundStakeAccountSubscriber,
6
+ } from './types';
7
+ import { Program } from '@coral-xyz/anchor';
8
+ import StrictEventEmitter from 'strict-event-emitter-types';
9
+ import { EventEmitter } from 'events';
10
+ import { PublicKey } from '@solana/web3.js';
11
+ import { BulkAccountLoader } from './bulkAccountLoader';
12
+ import { InsuranceFundStake } from '../types';
13
+
14
+ export class PollingInsuranceFundStakeAccountSubscriber
15
+ implements InsuranceFundStakeAccountSubscriber
16
+ {
17
+ isSubscribed: boolean;
18
+ program: Program;
19
+ eventEmitter: StrictEventEmitter<
20
+ EventEmitter,
21
+ InsuranceFundStakeAccountEvents
22
+ >;
23
+ insuranceFundStakeAccountPublicKey: PublicKey;
24
+
25
+ accountLoader: BulkAccountLoader;
26
+ callbackId?: string;
27
+ errorCallbackId?: string;
28
+
29
+ insuranceFundStakeAccountAndSlot?: DataAndSlot<InsuranceFundStake>;
30
+
31
+ public constructor(
32
+ program: Program,
33
+ publicKey: PublicKey,
34
+ accountLoader: BulkAccountLoader
35
+ ) {
36
+ this.isSubscribed = false;
37
+ this.program = program;
38
+ this.insuranceFundStakeAccountPublicKey = publicKey;
39
+ this.accountLoader = accountLoader;
40
+ this.eventEmitter = new EventEmitter();
41
+ }
42
+
43
+ async subscribe(insuranceFundStake?: InsuranceFundStake): Promise<boolean> {
44
+ if (this.isSubscribed) {
45
+ return true;
46
+ }
47
+
48
+ if (insuranceFundStake) {
49
+ this.insuranceFundStakeAccountAndSlot = {
50
+ data: insuranceFundStake,
51
+ slot: undefined,
52
+ };
53
+ }
54
+
55
+ await this.addToAccountLoader();
56
+
57
+ await this.fetchIfUnloaded();
58
+ if (this.doesAccountExist()) {
59
+ this.eventEmitter.emit('update');
60
+ }
61
+
62
+ this.isSubscribed = true;
63
+ return true;
64
+ }
65
+
66
+ async addToAccountLoader(): Promise<void> {
67
+ if (this.callbackId) {
68
+ return;
69
+ }
70
+
71
+ this.callbackId = await this.accountLoader.addAccount(
72
+ this.insuranceFundStakeAccountPublicKey,
73
+ (buffer, slot: number) => {
74
+ if (!buffer) {
75
+ return;
76
+ }
77
+
78
+ if (
79
+ this.insuranceFundStakeAccountAndSlot &&
80
+ this.insuranceFundStakeAccountAndSlot.slot > slot
81
+ ) {
82
+ return;
83
+ }
84
+
85
+ const account = this.program.account.user.coder.accounts.decode(
86
+ 'InsuranceFundStake',
87
+ buffer
88
+ );
89
+ this.insuranceFundStakeAccountAndSlot = { data: account, slot };
90
+ this.eventEmitter.emit('insuranceFundStakeAccountUpdate', account);
91
+ this.eventEmitter.emit('update');
92
+ }
93
+ );
94
+
95
+ this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => {
96
+ this.eventEmitter.emit('error', error);
97
+ });
98
+ }
99
+
100
+ async fetchIfUnloaded(): Promise<void> {
101
+ if (this.insuranceFundStakeAccountAndSlot === undefined) {
102
+ await this.fetch();
103
+ }
104
+ }
105
+
106
+ async fetch(): Promise<void> {
107
+ try {
108
+ const dataAndContext =
109
+ await this.program.account.insuranceFundStake.fetchAndContext(
110
+ this.insuranceFundStakeAccountPublicKey,
111
+ this.accountLoader.commitment
112
+ );
113
+ if (
114
+ dataAndContext.context.slot >
115
+ (this.insuranceFundStakeAccountAndSlot?.slot ?? 0)
116
+ ) {
117
+ this.insuranceFundStakeAccountAndSlot = {
118
+ data: dataAndContext.data as InsuranceFundStake,
119
+ slot: dataAndContext.context.slot,
120
+ };
121
+ }
122
+ } catch (e) {
123
+ console.log(
124
+ `PollingInsuranceFundStakeAccountSubscriber.fetch() InsuranceFundStake does not exist: ${e.message}`
125
+ );
126
+ }
127
+ }
128
+
129
+ doesAccountExist(): boolean {
130
+ return this.insuranceFundStakeAccountAndSlot !== undefined;
131
+ }
132
+
133
+ async unsubscribe(): Promise<void> {
134
+ if (!this.isSubscribed) {
135
+ return;
136
+ }
137
+
138
+ this.accountLoader.removeAccount(
139
+ this.insuranceFundStakeAccountPublicKey,
140
+ this.callbackId
141
+ );
142
+ this.callbackId = undefined;
143
+
144
+ this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
145
+ this.errorCallbackId = undefined;
146
+
147
+ this.isSubscribed = false;
148
+ }
149
+
150
+ assertIsSubscribed(): void {
151
+ if (!this.isSubscribed) {
152
+ throw new NotSubscribedError(
153
+ 'You must call `subscribe` before using this function'
154
+ );
155
+ }
156
+ }
157
+
158
+ public getInsuranceFundStakeAccountAndSlot(): DataAndSlot<InsuranceFundStake> {
159
+ this.assertIsSubscribed();
160
+ return this.insuranceFundStakeAccountAndSlot;
161
+ }
162
+
163
+ didSubscriptionSucceed(): boolean {
164
+ return !!this.insuranceFundStakeAccountAndSlot;
165
+ }
166
+
167
+ public updateData(
168
+ insuranceFundStake: InsuranceFundStake,
169
+ slot: number
170
+ ): void {
171
+ if (
172
+ !this.insuranceFundStakeAccountAndSlot ||
173
+ this.insuranceFundStakeAccountAndSlot.slot < slot
174
+ ) {
175
+ this.insuranceFundStakeAccountAndSlot = {
176
+ data: insuranceFundStake,
177
+ slot,
178
+ };
179
+ this.eventEmitter.emit(
180
+ 'insuranceFundStakeAccountUpdate',
181
+ insuranceFundStake
182
+ );
183
+ this.eventEmitter.emit('update');
184
+ }
185
+ }
186
+ }
@@ -5,6 +5,7 @@ import {
5
5
  StateAccount,
6
6
  UserAccount,
7
7
  UserStatsAccount,
8
+ InsuranceFundStake,
8
9
  } from '../types';
9
10
  import StrictEventEmitter from 'strict-event-emitter-types';
10
11
  import { EventEmitter } from 'events';
@@ -105,6 +106,26 @@ export interface TokenAccountSubscriber {
105
106
  getTokenAccountAndSlot(): DataAndSlot<Account>;
106
107
  }
107
108
 
109
+ export interface InsuranceFundStakeAccountSubscriber {
110
+ eventEmitter: StrictEventEmitter<
111
+ EventEmitter,
112
+ InsuranceFundStakeAccountEvents
113
+ >;
114
+ isSubscribed: boolean;
115
+
116
+ subscribe(): Promise<boolean>;
117
+ fetch(): Promise<void>;
118
+ unsubscribe(): Promise<void>;
119
+
120
+ getInsuranceFundStakeAccountAndSlot(): DataAndSlot<InsuranceFundStake>;
121
+ }
122
+
123
+ export interface InsuranceFundStakeAccountEvents {
124
+ insuranceFundStakeAccountUpdate: (payload: InsuranceFundStake) => void;
125
+ update: void;
126
+ error: (e: Error) => void;
127
+ }
128
+
108
129
  export interface OracleEvents {
109
130
  oracleUpdate: (payload: OraclePriceData) => void;
110
131
  update: void;
@@ -0,0 +1,127 @@
1
+ import {
2
+ DataAndSlot,
3
+ AccountSubscriber,
4
+ NotSubscribedError,
5
+ InsuranceFundStakeAccountEvents,
6
+ InsuranceFundStakeAccountSubscriber,
7
+ } from './types';
8
+ import { Program } from '@coral-xyz/anchor';
9
+ import StrictEventEmitter from 'strict-event-emitter-types';
10
+ import { EventEmitter } from 'events';
11
+ import { Commitment, PublicKey } from '@solana/web3.js';
12
+ import { WebSocketAccountSubscriber } from './webSocketAccountSubscriber';
13
+ import { InsuranceFundStake } from '../types';
14
+
15
+ export class WebSocketInsuranceFundStakeAccountSubscriber
16
+ implements InsuranceFundStakeAccountSubscriber
17
+ {
18
+ isSubscribed: boolean;
19
+ reconnectTimeoutMs?: number;
20
+ commitment?: Commitment;
21
+ program: Program;
22
+ eventEmitter: StrictEventEmitter<
23
+ EventEmitter,
24
+ InsuranceFundStakeAccountEvents
25
+ >;
26
+ insuranceFundStakeAccountPublicKey: PublicKey;
27
+
28
+ insuranceFundStakeDataAccountSubscriber: AccountSubscriber<InsuranceFundStake>;
29
+
30
+ public constructor(
31
+ program: Program,
32
+ insuranceFundStakeAccountPublicKey: PublicKey,
33
+ reconnectTimeoutMs?: number,
34
+ commitment?: Commitment
35
+ ) {
36
+ this.isSubscribed = false;
37
+ this.program = program;
38
+ this.insuranceFundStakeAccountPublicKey =
39
+ insuranceFundStakeAccountPublicKey;
40
+ this.eventEmitter = new EventEmitter();
41
+ this.reconnectTimeoutMs = reconnectTimeoutMs;
42
+ this.commitment = commitment;
43
+ }
44
+
45
+ async subscribe(
46
+ insuranceFundStakeAccount?: InsuranceFundStake
47
+ ): Promise<boolean> {
48
+ if (this.isSubscribed) {
49
+ return true;
50
+ }
51
+
52
+ this.insuranceFundStakeDataAccountSubscriber =
53
+ new WebSocketAccountSubscriber(
54
+ 'insuranceFundStake',
55
+ this.program,
56
+ this.insuranceFundStakeAccountPublicKey,
57
+ undefined,
58
+ this.reconnectTimeoutMs,
59
+ this.commitment
60
+ );
61
+
62
+ if (insuranceFundStakeAccount) {
63
+ this.insuranceFundStakeDataAccountSubscriber.setData(
64
+ insuranceFundStakeAccount
65
+ );
66
+ }
67
+
68
+ await this.insuranceFundStakeDataAccountSubscriber.subscribe(
69
+ (data: InsuranceFundStake) => {
70
+ this.eventEmitter.emit('insuranceFundStakeAccountUpdate', data);
71
+ this.eventEmitter.emit('update');
72
+ }
73
+ );
74
+
75
+ this.eventEmitter.emit('update');
76
+ this.isSubscribed = true;
77
+ return true;
78
+ }
79
+
80
+ async fetch(): Promise<void> {
81
+ await Promise.all([this.insuranceFundStakeDataAccountSubscriber.fetch()]);
82
+ }
83
+
84
+ async unsubscribe(): Promise<void> {
85
+ if (!this.isSubscribed) {
86
+ return;
87
+ }
88
+
89
+ await Promise.all([
90
+ this.insuranceFundStakeDataAccountSubscriber.unsubscribe(),
91
+ ]);
92
+
93
+ this.isSubscribed = false;
94
+ }
95
+
96
+ assertIsSubscribed(): void {
97
+ if (!this.isSubscribed) {
98
+ throw new NotSubscribedError(
99
+ 'You must call `subscribe` before using this function'
100
+ );
101
+ }
102
+ }
103
+
104
+ public getInsuranceFundStakeAccountAndSlot(): DataAndSlot<InsuranceFundStake> {
105
+ this.assertIsSubscribed();
106
+ return this.insuranceFundStakeDataAccountSubscriber.dataAndSlot;
107
+ }
108
+
109
+ public updateData(
110
+ insuranceFundStake: InsuranceFundStake,
111
+ slot: number
112
+ ): void {
113
+ const currentDataSlot =
114
+ this.insuranceFundStakeDataAccountSubscriber.dataAndSlot?.slot || 0;
115
+ if (currentDataSlot <= slot) {
116
+ this.insuranceFundStakeDataAccountSubscriber.setData(
117
+ insuranceFundStake,
118
+ slot
119
+ );
120
+ this.eventEmitter.emit(
121
+ 'insuranceFundStakeAccountUpdate',
122
+ insuranceFundStake
123
+ );
124
+ this.eventEmitter.emit('update');
125
+ }
126
+ }
127
+ }
package/src/index.ts CHANGED
@@ -10,6 +10,7 @@ export * from './types';
10
10
  export * from './constants/perpMarkets';
11
11
  export * from './accounts/fetch';
12
12
  export * from './accounts/webSocketDriftClientAccountSubscriber';
13
+ export * from './accounts/webSocketInsuranceFundStakeAccountSubscriber';
13
14
  export * from './accounts/bulkAccountLoader';
14
15
  export * from './accounts/bulkUserSubscription';
15
16
  export * from './accounts/bulkUserStatsSubscription';
@@ -18,6 +19,7 @@ export * from './accounts/pollingOracleAccountSubscriber';
18
19
  export * from './accounts/pollingTokenAccountSubscriber';
19
20
  export * from './accounts/pollingUserAccountSubscriber';
20
21
  export * from './accounts/pollingUserStatsAccountSubscriber';
22
+ export * from './accounts/pollingInsuranceFundStakeAccountSubscriber';
21
23
  export * from './accounts/mockUserAccountSubscriber';
22
24
  export * from './accounts/types';
23
25
  export * from './addresses/pda';