@drift-labs/sdk 2.31.1-beta.2 → 2.31.1-beta.3
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/accounts/mockUserAccountSubscriber.d.ts +23 -0
- package/lib/accounts/mockUserAccountSubscriber.js +31 -0
- package/lib/driftClient.d.ts +2 -0
- package/lib/driftClient.js +19 -10
- package/lib/idl/drift.json +1 -1
- package/lib/user.js +4 -1
- package/lib/userConfig.d.ts +4 -0
- package/package.json +1 -1
- package/src/accounts/mockUserAccountSubscriber.ts +53 -0
- package/src/driftClient.ts +20 -10
- package/src/idl/drift.json +1 -1
- package/src/user.ts +2 -0
- package/src/userConfig.ts +5 -0
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.31.1-beta.
|
|
1
|
+
2.31.1-beta.3
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { DataAndSlot, UserAccountEvents, UserAccountSubscriber } from './types';
|
|
3
|
+
import { PublicKey } from '@solana/web3.js';
|
|
4
|
+
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
5
|
+
import { EventEmitter } from 'events';
|
|
6
|
+
import { UserAccount } from '../types';
|
|
7
|
+
export declare class MockUserAccountSubscriber implements UserAccountSubscriber {
|
|
8
|
+
isSubscribed: boolean;
|
|
9
|
+
eventEmitter: StrictEventEmitter<EventEmitter, UserAccountEvents>;
|
|
10
|
+
userAccountPublicKey: PublicKey;
|
|
11
|
+
callbackId?: string;
|
|
12
|
+
errorCallbackId?: string;
|
|
13
|
+
user: DataAndSlot<UserAccount>;
|
|
14
|
+
constructor(userAccountPublicKey: PublicKey, data: UserAccount, slot: number);
|
|
15
|
+
subscribe(_userAccount?: UserAccount): Promise<boolean>;
|
|
16
|
+
addToAccountLoader(): Promise<void>;
|
|
17
|
+
fetch(): Promise<void>;
|
|
18
|
+
doesAccountExist(): boolean;
|
|
19
|
+
unsubscribe(): Promise<void>;
|
|
20
|
+
assertIsSubscribed(): void;
|
|
21
|
+
getUserAccountAndSlot(): DataAndSlot<UserAccount>;
|
|
22
|
+
updateData(userAccount: UserAccount, slot: number): void;
|
|
23
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MockUserAccountSubscriber = void 0;
|
|
4
|
+
const events_1 = require("events");
|
|
5
|
+
class MockUserAccountSubscriber {
|
|
6
|
+
constructor(userAccountPublicKey, data, slot) {
|
|
7
|
+
this.isSubscribed = true;
|
|
8
|
+
this.eventEmitter = new events_1.EventEmitter();
|
|
9
|
+
this.userAccountPublicKey = userAccountPublicKey;
|
|
10
|
+
this.user = { data, slot };
|
|
11
|
+
}
|
|
12
|
+
async subscribe(_userAccount) {
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
async addToAccountLoader() { }
|
|
16
|
+
async fetch() { }
|
|
17
|
+
doesAccountExist() {
|
|
18
|
+
return this.user !== undefined;
|
|
19
|
+
}
|
|
20
|
+
async unsubscribe() { }
|
|
21
|
+
assertIsSubscribed() { }
|
|
22
|
+
getUserAccountAndSlot() {
|
|
23
|
+
return this.user;
|
|
24
|
+
}
|
|
25
|
+
updateData(userAccount, slot) {
|
|
26
|
+
this.user = { data: userAccount, slot };
|
|
27
|
+
this.eventEmitter.emit('userAccountUpdate', userAccount);
|
|
28
|
+
this.eventEmitter.emit('update');
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.MockUserAccountSubscriber = MockUserAccountSubscriber;
|
package/lib/driftClient.d.ts
CHANGED
|
@@ -14,6 +14,7 @@ import { User } from './user';
|
|
|
14
14
|
import { UserSubscriptionConfig } from './userConfig';
|
|
15
15
|
import { UserStats } from './userStats';
|
|
16
16
|
import { JupiterClient, Route, SwapMode } from './jupiter/jupiterClient';
|
|
17
|
+
import { UserStatsSubscriptionConfig } from './userStatsConfig';
|
|
17
18
|
type RemainingAccountParams = {
|
|
18
19
|
userAccounts: UserAccount[];
|
|
19
20
|
writablePerpMarketIndexes?: number[];
|
|
@@ -36,6 +37,7 @@ export declare class DriftClient {
|
|
|
36
37
|
userStats?: UserStats;
|
|
37
38
|
activeSubAccountId: number;
|
|
38
39
|
userAccountSubscriptionConfig: UserSubscriptionConfig;
|
|
40
|
+
userStatsAccountSubscriptionConfig: UserStatsSubscriptionConfig;
|
|
39
41
|
accountSubscriber: DriftClientAccountSubscriber;
|
|
40
42
|
eventEmitter: StrictEventEmitter<EventEmitter, DriftClientAccountEvents>;
|
|
41
43
|
_isSubscribed: boolean;
|
package/lib/driftClient.js
CHANGED
|
@@ -93,15 +93,24 @@ class DriftClient {
|
|
|
93
93
|
? new Map([[this.authority.toString(), config.subAccountIds]])
|
|
94
94
|
: new Map();
|
|
95
95
|
this.includeDelegates = (_f = config.includeDelegates) !== null && _f !== void 0 ? _f : false;
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
:
|
|
103
|
-
|
|
104
|
-
|
|
96
|
+
if (((_g = config.accountSubscription) === null || _g === void 0 ? void 0 : _g.type) === 'polling') {
|
|
97
|
+
this.userAccountSubscriptionConfig = {
|
|
98
|
+
type: 'polling',
|
|
99
|
+
accountLoader: config.accountSubscription.accountLoader,
|
|
100
|
+
};
|
|
101
|
+
this.userStatsAccountSubscriptionConfig = {
|
|
102
|
+
type: 'polling',
|
|
103
|
+
accountLoader: config.accountSubscription.accountLoader,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
this.userAccountSubscriptionConfig = {
|
|
108
|
+
type: 'websocket',
|
|
109
|
+
};
|
|
110
|
+
this.userStatsAccountSubscriptionConfig = {
|
|
111
|
+
type: 'websocket',
|
|
112
|
+
};
|
|
113
|
+
}
|
|
105
114
|
if (config.userStats) {
|
|
106
115
|
this.userStats = new userStats_1.UserStats({
|
|
107
116
|
driftClient: this,
|
|
@@ -304,7 +313,7 @@ class DriftClient {
|
|
|
304
313
|
this.userStats = new userStats_1.UserStats({
|
|
305
314
|
driftClient: this,
|
|
306
315
|
userStatsAccountPublicKey: this.getUserStatsAccountPublicKey(),
|
|
307
|
-
accountSubscription: this.
|
|
316
|
+
accountSubscription: this.userStatsAccountSubscriptionConfig,
|
|
308
317
|
});
|
|
309
318
|
await this.userStats.subscribe();
|
|
310
319
|
}
|
package/lib/idl/drift.json
CHANGED
package/lib/user.js
CHANGED
|
@@ -20,13 +20,16 @@ class User {
|
|
|
20
20
|
this._isSubscribed = val;
|
|
21
21
|
}
|
|
22
22
|
constructor(config) {
|
|
23
|
-
var _a;
|
|
23
|
+
var _a, _b;
|
|
24
24
|
this._isSubscribed = false;
|
|
25
25
|
this.driftClient = config.driftClient;
|
|
26
26
|
this.userAccountPublicKey = config.userAccountPublicKey;
|
|
27
27
|
if (((_a = config.accountSubscription) === null || _a === void 0 ? void 0 : _a.type) === 'polling') {
|
|
28
28
|
this.accountSubscriber = new pollingUserAccountSubscriber_1.PollingUserAccountSubscriber(config.driftClient.program, config.userAccountPublicKey, config.accountSubscription.accountLoader);
|
|
29
29
|
}
|
|
30
|
+
else if (((_b = config.accountSubscription) === null || _b === void 0 ? void 0 : _b.type) === 'custom') {
|
|
31
|
+
this.accountSubscriber = config.accountSubscription.userAccountSubscriber;
|
|
32
|
+
}
|
|
30
33
|
else {
|
|
31
34
|
this.accountSubscriber = new webSocketUserAccountSubscriber_1.WebSocketUserAccountSubscriber(config.driftClient.program, config.userAccountPublicKey);
|
|
32
35
|
}
|
package/lib/userConfig.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { DriftClient } from './driftClient';
|
|
2
2
|
import { PublicKey } from '@solana/web3.js';
|
|
3
3
|
import { BulkAccountLoader } from './accounts/bulkAccountLoader';
|
|
4
|
+
import { UserAccountSubscriber } from './accounts/types';
|
|
4
5
|
export type UserConfig = {
|
|
5
6
|
accountSubscription?: UserSubscriptionConfig;
|
|
6
7
|
driftClient: DriftClient;
|
|
@@ -11,4 +12,7 @@ export type UserSubscriptionConfig = {
|
|
|
11
12
|
} | {
|
|
12
13
|
type: 'polling';
|
|
13
14
|
accountLoader: BulkAccountLoader;
|
|
15
|
+
} | {
|
|
16
|
+
type: 'custom';
|
|
17
|
+
userAccountSubscriber: UserAccountSubscriber;
|
|
14
18
|
};
|
package/package.json
CHANGED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { DataAndSlot, UserAccountEvents, UserAccountSubscriber } from './types';
|
|
2
|
+
import { PublicKey } from '@solana/web3.js';
|
|
3
|
+
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
4
|
+
import { EventEmitter } from 'events';
|
|
5
|
+
import { UserAccount } from '../types';
|
|
6
|
+
|
|
7
|
+
export class MockUserAccountSubscriber implements UserAccountSubscriber {
|
|
8
|
+
isSubscribed: boolean;
|
|
9
|
+
eventEmitter: StrictEventEmitter<EventEmitter, UserAccountEvents>;
|
|
10
|
+
userAccountPublicKey: PublicKey;
|
|
11
|
+
|
|
12
|
+
callbackId?: string;
|
|
13
|
+
errorCallbackId?: string;
|
|
14
|
+
|
|
15
|
+
user: DataAndSlot<UserAccount>;
|
|
16
|
+
|
|
17
|
+
public constructor(
|
|
18
|
+
userAccountPublicKey: PublicKey,
|
|
19
|
+
data: UserAccount,
|
|
20
|
+
slot: number
|
|
21
|
+
) {
|
|
22
|
+
this.isSubscribed = true;
|
|
23
|
+
this.eventEmitter = new EventEmitter();
|
|
24
|
+
this.userAccountPublicKey = userAccountPublicKey;
|
|
25
|
+
this.user = { data, slot };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async subscribe(_userAccount?: UserAccount): Promise<boolean> {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async addToAccountLoader(): Promise<void> {}
|
|
33
|
+
|
|
34
|
+
async fetch(): Promise<void> {}
|
|
35
|
+
|
|
36
|
+
doesAccountExist(): boolean {
|
|
37
|
+
return this.user !== undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async unsubscribe(): Promise<void> {}
|
|
41
|
+
|
|
42
|
+
assertIsSubscribed(): void {}
|
|
43
|
+
|
|
44
|
+
public getUserAccountAndSlot(): DataAndSlot<UserAccount> {
|
|
45
|
+
return this.user;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public updateData(userAccount: UserAccount, slot: number): void {
|
|
49
|
+
this.user = { data: userAccount, slot };
|
|
50
|
+
this.eventEmitter.emit('userAccountUpdate', userAccount);
|
|
51
|
+
this.eventEmitter.emit('update');
|
|
52
|
+
}
|
|
53
|
+
}
|
package/src/driftClient.ts
CHANGED
|
@@ -115,6 +115,7 @@ import { fetchUserStatsAccount } from './accounts/fetch';
|
|
|
115
115
|
import { castNumberToSpotPrecision } from './math/spotMarket';
|
|
116
116
|
import { JupiterClient, Route, SwapMode } from './jupiter/jupiterClient';
|
|
117
117
|
import { getNonIdleUserFilter } from './memcmp';
|
|
118
|
+
import { UserStatsSubscriptionConfig } from './userStatsConfig';
|
|
118
119
|
|
|
119
120
|
type RemainingAccountParams = {
|
|
120
121
|
userAccounts: UserAccount[];
|
|
@@ -139,6 +140,7 @@ export class DriftClient {
|
|
|
139
140
|
userStats?: UserStats;
|
|
140
141
|
activeSubAccountId: number;
|
|
141
142
|
userAccountSubscriptionConfig: UserSubscriptionConfig;
|
|
143
|
+
userStatsAccountSubscriptionConfig: UserStatsSubscriptionConfig;
|
|
142
144
|
accountSubscriber: DriftClientAccountSubscriber;
|
|
143
145
|
eventEmitter: StrictEventEmitter<EventEmitter, DriftClientAccountEvents>;
|
|
144
146
|
_isSubscribed = false;
|
|
@@ -206,15 +208,23 @@ export class DriftClient {
|
|
|
206
208
|
: new Map<string, number[]>();
|
|
207
209
|
|
|
208
210
|
this.includeDelegates = config.includeDelegates ?? false;
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
:
|
|
216
|
-
|
|
217
|
-
|
|
211
|
+
if (config.accountSubscription?.type === 'polling') {
|
|
212
|
+
this.userAccountSubscriptionConfig = {
|
|
213
|
+
type: 'polling',
|
|
214
|
+
accountLoader: config.accountSubscription.accountLoader,
|
|
215
|
+
};
|
|
216
|
+
this.userStatsAccountSubscriptionConfig = {
|
|
217
|
+
type: 'polling',
|
|
218
|
+
accountLoader: config.accountSubscription.accountLoader,
|
|
219
|
+
};
|
|
220
|
+
} else {
|
|
221
|
+
this.userAccountSubscriptionConfig = {
|
|
222
|
+
type: 'websocket',
|
|
223
|
+
};
|
|
224
|
+
this.userStatsAccountSubscriptionConfig = {
|
|
225
|
+
type: 'websocket',
|
|
226
|
+
};
|
|
227
|
+
}
|
|
218
228
|
|
|
219
229
|
if (config.userStats) {
|
|
220
230
|
this.userStats = new UserStats({
|
|
@@ -542,7 +552,7 @@ export class DriftClient {
|
|
|
542
552
|
this.userStats = new UserStats({
|
|
543
553
|
driftClient: this,
|
|
544
554
|
userStatsAccountPublicKey: this.getUserStatsAccountPublicKey(),
|
|
545
|
-
accountSubscription: this.
|
|
555
|
+
accountSubscription: this.userStatsAccountSubscriptionConfig,
|
|
546
556
|
});
|
|
547
557
|
|
|
548
558
|
await this.userStats.subscribe();
|
package/src/idl/drift.json
CHANGED
package/src/user.ts
CHANGED
|
@@ -97,6 +97,8 @@ export class User {
|
|
|
97
97
|
config.userAccountPublicKey,
|
|
98
98
|
config.accountSubscription.accountLoader
|
|
99
99
|
);
|
|
100
|
+
} else if (config.accountSubscription?.type === 'custom') {
|
|
101
|
+
this.accountSubscriber = config.accountSubscription.userAccountSubscriber;
|
|
100
102
|
} else {
|
|
101
103
|
this.accountSubscriber = new WebSocketUserAccountSubscriber(
|
|
102
104
|
config.driftClient.program,
|
package/src/userConfig.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { DriftClient } from './driftClient';
|
|
2
2
|
import { PublicKey } from '@solana/web3.js';
|
|
3
3
|
import { BulkAccountLoader } from './accounts/bulkAccountLoader';
|
|
4
|
+
import { UserAccountSubscriber } from './accounts/types';
|
|
4
5
|
|
|
5
6
|
export type UserConfig = {
|
|
6
7
|
accountSubscription?: UserSubscriptionConfig;
|
|
@@ -15,4 +16,8 @@ export type UserSubscriptionConfig =
|
|
|
15
16
|
| {
|
|
16
17
|
type: 'polling';
|
|
17
18
|
accountLoader: BulkAccountLoader;
|
|
19
|
+
}
|
|
20
|
+
| {
|
|
21
|
+
type: 'custom';
|
|
22
|
+
userAccountSubscriber: UserAccountSubscriber;
|
|
18
23
|
};
|