@drift-labs/sdk 2.96.0-beta.0 → 2.96.0-beta.2
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/grpcAccountSubscriber.d.ts +16 -0
- package/lib/accounts/grpcAccountSubscriber.js +155 -0
- package/lib/accounts/grpcDriftClientAccountSubscriber.d.ts +13 -0
- package/lib/accounts/grpcDriftClientAccountSubscriber.js +96 -0
- package/lib/accounts/grpcInsuranceFundStakeAccountSubscriber.d.ts +10 -0
- package/lib/accounts/grpcInsuranceFundStakeAccountSubscriber.js +30 -0
- package/lib/accounts/grpcProgramAccountSubscriber.d.ts +19 -0
- package/lib/accounts/grpcProgramAccountSubscriber.js +161 -0
- package/lib/accounts/grpcUserAccountSubscriber.d.ts +10 -0
- package/lib/accounts/grpcUserAccountSubscriber.js +28 -0
- package/lib/accounts/grpcUserStatsAccountSubscriber.d.ts +10 -0
- package/lib/accounts/grpcUserStatsAccountSubscriber.js +28 -0
- package/lib/accounts/types.d.ts +8 -0
- package/lib/accounts/webSocketAccountSubscriber.d.ts +1 -1
- package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +3 -3
- package/lib/accounts/webSocketProgramAccountSubscriber.d.ts +1 -1
- package/lib/driftClient.js +35 -14
- package/lib/driftClientConfig.d.ts +6 -0
- package/lib/events/eventSubscriber.d.ts +7 -0
- package/lib/events/eventSubscriber.js +69 -32
- package/lib/events/eventsServerLogProvider.d.ts +21 -0
- package/lib/events/eventsServerLogProvider.js +121 -0
- package/lib/events/pollingLogProvider.js +1 -1
- package/lib/events/types.d.ts +12 -5
- package/lib/events/types.js +5 -1
- package/lib/events/webSocketLogProvider.js +2 -2
- package/lib/orderSubscriber/OrderSubscriber.d.ts +2 -1
- package/lib/orderSubscriber/OrderSubscriber.js +19 -4
- package/lib/orderSubscriber/grpcSubscription.d.ts +25 -0
- package/lib/orderSubscriber/grpcSubscription.js +68 -0
- package/lib/orderSubscriber/types.d.ts +9 -0
- package/lib/user.js +11 -4
- package/lib/userConfig.d.ts +6 -1
- package/lib/userMap/grpcSubscription.d.ts +26 -0
- package/lib/userMap/grpcSubscription.js +42 -0
- package/lib/userMap/userMap.js +14 -0
- package/lib/userMap/userMapConfig.d.ts +7 -0
- package/lib/userStatsConfig.d.ts +6 -0
- package/package.json +3 -1
- package/src/accounts/grpcAccountSubscriber.ts +158 -0
- package/src/accounts/grpcDriftClientAccountSubscriber.ts +196 -0
- package/src/accounts/grpcInsuranceFundStakeAccountSubscriber.ts +62 -0
- package/src/accounts/grpcProgramAccountSubscriber.ts +181 -0
- package/src/accounts/grpcUserAccountSubscriber.ts +48 -0
- package/src/accounts/grpcUserStatsAccountSubscriber.ts +51 -0
- package/src/accounts/types.ts +9 -0
- package/src/accounts/webSocketAccountSubscriber.ts +1 -1
- package/src/accounts/webSocketDriftClientAccountSubscriber.ts +3 -3
- package/src/accounts/webSocketProgramAccountSubscriber.ts +1 -1
- package/src/driftClient.ts +28 -0
- package/src/driftClientConfig.ts +7 -0
- package/src/events/eventSubscriber.ts +125 -54
- package/src/events/eventsServerLogProvider.ts +152 -0
- package/src/events/pollingLogProvider.ts +1 -1
- package/src/events/types.ts +29 -6
- package/src/events/webSocketLogProvider.ts +4 -4
- package/src/orderSubscriber/OrderSubscriber.ts +15 -1
- package/src/orderSubscriber/grpcSubscription.ts +126 -0
- package/src/orderSubscriber/types.ts +10 -0
- package/src/user.ts +11 -0
- package/src/userConfig.ts +7 -1
- package/src/userMap/grpcSubscription.ts +83 -0
- package/src/userMap/userMap.ts +17 -1
- package/src/userMap/userMapConfig.ts +8 -0
- package/src/userStatsConfig.ts +7 -0
package/src/user.ts
CHANGED
|
@@ -98,6 +98,7 @@ import {
|
|
|
98
98
|
calculatePerpFuelBonus,
|
|
99
99
|
calculateInsuranceFuelBonus,
|
|
100
100
|
} from './math/fuel';
|
|
101
|
+
import { grpcUserAccountSubscriber } from './accounts/grpcUserAccountSubscriber';
|
|
101
102
|
|
|
102
103
|
export class User {
|
|
103
104
|
driftClient: DriftClient;
|
|
@@ -128,6 +129,16 @@ export class User {
|
|
|
128
129
|
);
|
|
129
130
|
} else if (config.accountSubscription?.type === 'custom') {
|
|
130
131
|
this.accountSubscriber = config.accountSubscription.userAccountSubscriber;
|
|
132
|
+
} else if (config.accountSubscription?.type === 'grpc') {
|
|
133
|
+
this.accountSubscriber = new grpcUserAccountSubscriber(
|
|
134
|
+
config.accountSubscription.configs,
|
|
135
|
+
config.driftClient.program,
|
|
136
|
+
config.userAccountPublicKey,
|
|
137
|
+
{
|
|
138
|
+
resubTimeoutMs: config.accountSubscription?.resubTimeoutMs,
|
|
139
|
+
logResubMessages: config.accountSubscription?.logResubMessages,
|
|
140
|
+
}
|
|
141
|
+
);
|
|
131
142
|
} else {
|
|
132
143
|
this.accountSubscriber = new WebSocketUserAccountSubscriber(
|
|
133
144
|
config.driftClient.program,
|
package/src/userConfig.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { DriftClient } from './driftClient';
|
|
2
2
|
import { Commitment, PublicKey } from '@solana/web3.js';
|
|
3
3
|
import { BulkAccountLoader } from './accounts/bulkAccountLoader';
|
|
4
|
-
import { UserAccountSubscriber } from './accounts/types';
|
|
4
|
+
import { GrpcConfigs, UserAccountSubscriber } from './accounts/types';
|
|
5
5
|
|
|
6
6
|
export type UserConfig = {
|
|
7
7
|
accountSubscription?: UserSubscriptionConfig;
|
|
@@ -23,4 +23,10 @@ export type UserSubscriptionConfig =
|
|
|
23
23
|
| {
|
|
24
24
|
type: 'custom';
|
|
25
25
|
userAccountSubscriber: UserAccountSubscriber;
|
|
26
|
+
}
|
|
27
|
+
| {
|
|
28
|
+
type: 'grpc';
|
|
29
|
+
resubTimeoutMs?: number;
|
|
30
|
+
logResubMessages?: boolean;
|
|
31
|
+
configs: GrpcConfigs;
|
|
26
32
|
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { UserMap } from './userMap';
|
|
2
|
+
import { getNonIdleUserFilter, getUserFilter } from '../memcmp';
|
|
3
|
+
import { WebSocketProgramAccountSubscriber } from '../accounts/webSocketProgramAccountSubscriber';
|
|
4
|
+
import { UserAccount } from '../types';
|
|
5
|
+
import { Commitment, Context, PublicKey } from '@solana/web3.js';
|
|
6
|
+
import { GrpcConfigs, ResubOpts } from '../accounts/types';
|
|
7
|
+
import { grpcProgramAccountSubscriber } from '../accounts/grpcProgramAccountSubscriber';
|
|
8
|
+
|
|
9
|
+
export class grpcSubscription {
|
|
10
|
+
private configs: GrpcConfigs;
|
|
11
|
+
private userMap: UserMap;
|
|
12
|
+
private commitment: Commitment;
|
|
13
|
+
private skipInitialLoad: boolean;
|
|
14
|
+
private resubOpts?: ResubOpts;
|
|
15
|
+
private includeIdle?: boolean;
|
|
16
|
+
private decodeFn: (name: string, data: Buffer) => UserAccount;
|
|
17
|
+
|
|
18
|
+
private subscriber: WebSocketProgramAccountSubscriber<UserAccount>;
|
|
19
|
+
|
|
20
|
+
constructor({
|
|
21
|
+
configs,
|
|
22
|
+
userMap,
|
|
23
|
+
commitment,
|
|
24
|
+
skipInitialLoad = false,
|
|
25
|
+
resubOpts,
|
|
26
|
+
includeIdle = false,
|
|
27
|
+
decodeFn,
|
|
28
|
+
}: {
|
|
29
|
+
configs: GrpcConfigs;
|
|
30
|
+
userMap: UserMap;
|
|
31
|
+
commitment: Commitment;
|
|
32
|
+
skipInitialLoad?: boolean;
|
|
33
|
+
resubOpts?: ResubOpts;
|
|
34
|
+
includeIdle?: boolean;
|
|
35
|
+
decodeFn: (name: string, data: Buffer) => UserAccount;
|
|
36
|
+
}) {
|
|
37
|
+
this.userMap = userMap;
|
|
38
|
+
this.commitment = commitment;
|
|
39
|
+
this.skipInitialLoad = skipInitialLoad;
|
|
40
|
+
this.resubOpts = resubOpts;
|
|
41
|
+
this.includeIdle = includeIdle || false;
|
|
42
|
+
this.decodeFn = decodeFn;
|
|
43
|
+
this.configs = configs;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public async subscribe(): Promise<void> {
|
|
47
|
+
if (!this.subscriber) {
|
|
48
|
+
const filters = [getUserFilter()];
|
|
49
|
+
if (!this.includeIdle) {
|
|
50
|
+
filters.push(getNonIdleUserFilter());
|
|
51
|
+
}
|
|
52
|
+
this.subscriber = new grpcProgramAccountSubscriber<UserAccount>(
|
|
53
|
+
this.configs,
|
|
54
|
+
'UserMap',
|
|
55
|
+
'User',
|
|
56
|
+
this.userMap.driftClient.program,
|
|
57
|
+
this.decodeFn,
|
|
58
|
+
{
|
|
59
|
+
filters,
|
|
60
|
+
commitment: this.commitment,
|
|
61
|
+
},
|
|
62
|
+
this.resubOpts
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
await this.subscriber.subscribe(
|
|
67
|
+
(accountId: PublicKey, account: UserAccount, context: Context) => {
|
|
68
|
+
const userKey = accountId.toBase58();
|
|
69
|
+
this.userMap.updateUserAccount(userKey, account, context.slot);
|
|
70
|
+
}
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
if (!this.skipInitialLoad) {
|
|
74
|
+
await this.userMap.sync();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public async unsubscribe(): Promise<void> {
|
|
79
|
+
if (!this.subscriber) return;
|
|
80
|
+
await this.subscriber.unsubscribe();
|
|
81
|
+
this.subscriber = undefined;
|
|
82
|
+
}
|
|
83
|
+
}
|
package/src/userMap/userMap.ts
CHANGED
|
@@ -36,6 +36,7 @@ import {
|
|
|
36
36
|
import { WebsocketSubscription } from './WebsocketSubscription';
|
|
37
37
|
import { PollingSubscription } from './PollingSubscription';
|
|
38
38
|
import { decodeUser } from '../decode/user';
|
|
39
|
+
import { grpcSubscription } from './grpcSubscription';
|
|
39
40
|
|
|
40
41
|
const MAX_USER_ACCOUNT_SIZE_BYTES = 4376;
|
|
41
42
|
|
|
@@ -75,7 +76,10 @@ export class UserMap implements UserMapInterface {
|
|
|
75
76
|
private includeIdle: boolean;
|
|
76
77
|
private disableSyncOnTotalAccountsChange: boolean;
|
|
77
78
|
private lastNumberOfSubAccounts: BN;
|
|
78
|
-
private subscription:
|
|
79
|
+
private subscription:
|
|
80
|
+
| PollingSubscription
|
|
81
|
+
| WebsocketSubscription
|
|
82
|
+
| grpcSubscription;
|
|
79
83
|
private stateAccountUpdateCallback = async (state: StateAccount) => {
|
|
80
84
|
if (!state.numberOfSubAccounts.eq(this.lastNumberOfSubAccounts)) {
|
|
81
85
|
await this.sync();
|
|
@@ -122,6 +126,18 @@ export class UserMap implements UserMapInterface {
|
|
|
122
126
|
frequency: config.subscriptionConfig.frequency,
|
|
123
127
|
skipInitialLoad: config.skipInitialLoad,
|
|
124
128
|
});
|
|
129
|
+
} else if (config.subscriptionConfig.type === 'grpc') {
|
|
130
|
+
this.subscription = new grpcSubscription({
|
|
131
|
+
configs: config.subscriptionConfig.configs,
|
|
132
|
+
userMap: this,
|
|
133
|
+
commitment: this.commitment,
|
|
134
|
+
resubOpts: {
|
|
135
|
+
resubTimeoutMs: config.subscriptionConfig.resubTimeoutMs,
|
|
136
|
+
logResubMessages: config.subscriptionConfig.logResubMessages,
|
|
137
|
+
},
|
|
138
|
+
skipInitialLoad: config.skipInitialLoad,
|
|
139
|
+
decodeFn,
|
|
140
|
+
});
|
|
125
141
|
} else {
|
|
126
142
|
this.subscription = new WebsocketSubscription({
|
|
127
143
|
userMap: this,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Commitment, Connection } from '@solana/web3.js';
|
|
2
2
|
import { DriftClient } from '../driftClient';
|
|
3
|
+
import { GrpcConfigs } from '../accounts/types';
|
|
3
4
|
|
|
4
5
|
// passed into UserMap.getUniqueAuthorities to filter users
|
|
5
6
|
export type UserAccountFilterCriteria = {
|
|
@@ -27,6 +28,13 @@ export type UserMapConfig = {
|
|
|
27
28
|
frequency: number;
|
|
28
29
|
commitment?: Commitment;
|
|
29
30
|
}
|
|
31
|
+
| {
|
|
32
|
+
type: 'grpc';
|
|
33
|
+
configs: GrpcConfigs;
|
|
34
|
+
resubTimeoutMs?: number;
|
|
35
|
+
logResubMessages?: boolean;
|
|
36
|
+
commitment?: Commitment;
|
|
37
|
+
}
|
|
30
38
|
| {
|
|
31
39
|
type: 'websocket';
|
|
32
40
|
resubTimeoutMs?: number;
|
package/src/userStatsConfig.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { DriftClient } from './driftClient';
|
|
2
2
|
import { Commitment, PublicKey } from '@solana/web3.js';
|
|
3
3
|
import { BulkAccountLoader } from './accounts/bulkAccountLoader';
|
|
4
|
+
import { GrpcConfigs } from './accounts/types';
|
|
4
5
|
|
|
5
6
|
export type UserStatsConfig = {
|
|
6
7
|
accountSubscription?: UserStatsSubscriptionConfig;
|
|
@@ -21,4 +22,10 @@ export type UserStatsSubscriptionConfig =
|
|
|
21
22
|
}
|
|
22
23
|
| {
|
|
23
24
|
type: 'custom';
|
|
25
|
+
}
|
|
26
|
+
| {
|
|
27
|
+
type: 'grpc';
|
|
28
|
+
resubTimeoutMs?: number;
|
|
29
|
+
logResubMessages?: boolean;
|
|
30
|
+
configs: GrpcConfigs;
|
|
24
31
|
};
|