@drift-labs/sdk 2.96.0-beta.0 → 2.96.0-beta.1
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/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/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/lib/driftClient.js
CHANGED
|
@@ -68,6 +68,7 @@ const utils_2 = require("./tx/utils");
|
|
|
68
68
|
const pyth_solana_receiver_json_1 = __importDefault(require("./idl/pyth_solana_receiver.json"));
|
|
69
69
|
const on_demand_1 = require("@switchboard-xyz/on-demand");
|
|
70
70
|
const switchboard_on_demand_30_json_1 = __importDefault(require("./idl/switchboard_on_demand_30.json"));
|
|
71
|
+
const grpcDriftClientAccountSubscriber_1 = require("./accounts/grpcDriftClientAccountSubscriber");
|
|
71
72
|
/**
|
|
72
73
|
* # DriftClient
|
|
73
74
|
* This class is the main way to interact with Drift Protocol. It allows you to subscribe to the various accounts where the Market's state is stored, as well as: opening positions, liquidating, settling funding, depositing & withdrawing, and more.
|
|
@@ -80,7 +81,7 @@ class DriftClient {
|
|
|
80
81
|
this._isSubscribed = val;
|
|
81
82
|
}
|
|
82
83
|
constructor(config) {
|
|
83
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6;
|
|
84
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19;
|
|
84
85
|
this.users = new Map();
|
|
85
86
|
this._isSubscribed = false;
|
|
86
87
|
this.perpMarketLastSlotCache = new Map();
|
|
@@ -143,18 +144,32 @@ class DriftClient {
|
|
|
143
144
|
accountLoader: config.accountSubscription.accountLoader,
|
|
144
145
|
};
|
|
145
146
|
}
|
|
147
|
+
else if (((_q = config.accountSubscription) === null || _q === void 0 ? void 0 : _q.type) === 'grpc') {
|
|
148
|
+
this.userAccountSubscriptionConfig = {
|
|
149
|
+
type: 'grpc',
|
|
150
|
+
resubTimeoutMs: (_r = config.accountSubscription) === null || _r === void 0 ? void 0 : _r.resubTimeoutMs,
|
|
151
|
+
logResubMessages: (_s = config.accountSubscription) === null || _s === void 0 ? void 0 : _s.logResubMessages,
|
|
152
|
+
configs: (_t = config.accountSubscription) === null || _t === void 0 ? void 0 : _t.configs,
|
|
153
|
+
};
|
|
154
|
+
this.userStatsAccountSubscriptionConfig = {
|
|
155
|
+
type: 'grpc',
|
|
156
|
+
resubTimeoutMs: (_u = config.accountSubscription) === null || _u === void 0 ? void 0 : _u.resubTimeoutMs,
|
|
157
|
+
logResubMessages: (_v = config.accountSubscription) === null || _v === void 0 ? void 0 : _v.logResubMessages,
|
|
158
|
+
configs: (_w = config.accountSubscription) === null || _w === void 0 ? void 0 : _w.configs,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
146
161
|
else {
|
|
147
162
|
this.userAccountSubscriptionConfig = {
|
|
148
163
|
type: 'websocket',
|
|
149
|
-
resubTimeoutMs: (
|
|
150
|
-
logResubMessages: (
|
|
151
|
-
commitment: (
|
|
164
|
+
resubTimeoutMs: (_x = config.accountSubscription) === null || _x === void 0 ? void 0 : _x.resubTimeoutMs,
|
|
165
|
+
logResubMessages: (_y = config.accountSubscription) === null || _y === void 0 ? void 0 : _y.logResubMessages,
|
|
166
|
+
commitment: (_z = config.accountSubscription) === null || _z === void 0 ? void 0 : _z.commitment,
|
|
152
167
|
};
|
|
153
168
|
this.userStatsAccountSubscriptionConfig = {
|
|
154
169
|
type: 'websocket',
|
|
155
|
-
resubTimeoutMs: (
|
|
156
|
-
logResubMessages: (
|
|
157
|
-
commitment: (
|
|
170
|
+
resubTimeoutMs: (_0 = config.accountSubscription) === null || _0 === void 0 ? void 0 : _0.resubTimeoutMs,
|
|
171
|
+
logResubMessages: (_1 = config.accountSubscription) === null || _1 === void 0 ? void 0 : _1.logResubMessages,
|
|
172
|
+
commitment: (_2 = config.accountSubscription) === null || _2 === void 0 ? void 0 : _2.commitment,
|
|
158
173
|
};
|
|
159
174
|
}
|
|
160
175
|
if (config.userStats) {
|
|
@@ -171,14 +186,20 @@ class DriftClient {
|
|
|
171
186
|
const noMarketsAndOraclesSpecified = config.perpMarketIndexes === undefined &&
|
|
172
187
|
config.spotMarketIndexes === undefined &&
|
|
173
188
|
config.oracleInfos === undefined;
|
|
174
|
-
if (((
|
|
175
|
-
this.accountSubscriber = new pollingDriftClientAccountSubscriber_1.PollingDriftClientAccountSubscriber(this.program, config.accountSubscription.accountLoader, (
|
|
189
|
+
if (((_3 = config.accountSubscription) === null || _3 === void 0 ? void 0 : _3.type) === 'polling') {
|
|
190
|
+
this.accountSubscriber = new pollingDriftClientAccountSubscriber_1.PollingDriftClientAccountSubscriber(this.program, config.accountSubscription.accountLoader, (_4 = config.perpMarketIndexes) !== null && _4 !== void 0 ? _4 : [], (_5 = config.spotMarketIndexes) !== null && _5 !== void 0 ? _5 : [], (_6 = config.oracleInfos) !== null && _6 !== void 0 ? _6 : [], noMarketsAndOraclesSpecified);
|
|
191
|
+
}
|
|
192
|
+
else if (((_7 = config.accountSubscription) === null || _7 === void 0 ? void 0 : _7.type) === 'grpc') {
|
|
193
|
+
this.accountSubscriber = new grpcDriftClientAccountSubscriber_1.gprcDriftClientAccountSubscriber(config.accountSubscription.configs, this.program, (_8 = config.perpMarketIndexes) !== null && _8 !== void 0 ? _8 : [], (_9 = config.spotMarketIndexes) !== null && _9 !== void 0 ? _9 : [], (_10 = config.oracleInfos) !== null && _10 !== void 0 ? _10 : [], noMarketsAndOraclesSpecified, {
|
|
194
|
+
resubTimeoutMs: (_11 = config.accountSubscription) === null || _11 === void 0 ? void 0 : _11.resubTimeoutMs,
|
|
195
|
+
logResubMessages: (_12 = config.accountSubscription) === null || _12 === void 0 ? void 0 : _12.logResubMessages,
|
|
196
|
+
});
|
|
176
197
|
}
|
|
177
198
|
else {
|
|
178
|
-
this.accountSubscriber = new webSocketDriftClientAccountSubscriber_1.WebSocketDriftClientAccountSubscriber(this.program, (
|
|
179
|
-
resubTimeoutMs: (
|
|
180
|
-
logResubMessages: (
|
|
181
|
-
}, (
|
|
199
|
+
this.accountSubscriber = new webSocketDriftClientAccountSubscriber_1.WebSocketDriftClientAccountSubscriber(this.program, (_13 = config.perpMarketIndexes) !== null && _13 !== void 0 ? _13 : [], (_14 = config.spotMarketIndexes) !== null && _14 !== void 0 ? _14 : [], (_15 = config.oracleInfos) !== null && _15 !== void 0 ? _15 : [], noMarketsAndOraclesSpecified, {
|
|
200
|
+
resubTimeoutMs: (_16 = config.accountSubscription) === null || _16 === void 0 ? void 0 : _16.resubTimeoutMs,
|
|
201
|
+
logResubMessages: (_17 = config.accountSubscription) === null || _17 === void 0 ? void 0 : _17.logResubMessages,
|
|
202
|
+
}, (_18 = config.accountSubscription) === null || _18 === void 0 ? void 0 : _18.commitment);
|
|
182
203
|
}
|
|
183
204
|
this.eventEmitter = this.accountSubscriber.eventEmitter;
|
|
184
205
|
this.metricsEventEmitter = new events_1.EventEmitter();
|
|
@@ -186,7 +207,7 @@ class DriftClient {
|
|
|
186
207
|
this.enableMetricsEvents = true;
|
|
187
208
|
}
|
|
188
209
|
this.txSender =
|
|
189
|
-
(
|
|
210
|
+
(_19 = config.txSender) !== null && _19 !== void 0 ? _19 : new retryTxSender_1.RetryTxSender({
|
|
190
211
|
connection: this.connection,
|
|
191
212
|
wallet: this.wallet,
|
|
192
213
|
opts: this.opts,
|
|
@@ -5,6 +5,7 @@ import { BulkAccountLoader } from './accounts/bulkAccountLoader';
|
|
|
5
5
|
import { DriftEnv } from './config';
|
|
6
6
|
import { TxSender } from './tx/types';
|
|
7
7
|
import { TxHandler, TxHandlerConfig } from './tx/txHandler';
|
|
8
|
+
import { GrpcConfigs } from './accounts/types';
|
|
8
9
|
export type DriftClientConfig = {
|
|
9
10
|
connection: Connection;
|
|
10
11
|
wallet: IWallet;
|
|
@@ -38,4 +39,9 @@ export type DriftClientSubscriptionConfig = {
|
|
|
38
39
|
} | {
|
|
39
40
|
type: 'polling';
|
|
40
41
|
accountLoader: BulkAccountLoader;
|
|
42
|
+
} | {
|
|
43
|
+
type: 'grpc';
|
|
44
|
+
configs: GrpcConfigs;
|
|
45
|
+
resubTimeoutMs?: number;
|
|
46
|
+
logResubMessages?: boolean;
|
|
41
47
|
};
|
|
@@ -10,13 +10,14 @@ import { PollingSubscription } from './PollingSubscription';
|
|
|
10
10
|
import { WebsocketSubscription } from './WebsocketSubscription';
|
|
11
11
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
12
12
|
import { EventEmitter } from 'events';
|
|
13
|
+
import { grpcSubscription } from './grpcSubscription';
|
|
13
14
|
export declare class OrderSubscriber {
|
|
14
15
|
driftClient: DriftClient;
|
|
15
16
|
usersAccounts: Map<string, {
|
|
16
17
|
slot: number;
|
|
17
18
|
userAccount: UserAccount;
|
|
18
19
|
}>;
|
|
19
|
-
subscription: PollingSubscription | WebsocketSubscription;
|
|
20
|
+
subscription: PollingSubscription | WebsocketSubscription | grpcSubscription;
|
|
20
21
|
commitment: Commitment;
|
|
21
22
|
eventEmitter: StrictEventEmitter<EventEmitter, OrderSubscriberEvents>;
|
|
22
23
|
fetchPromise?: Promise<void>;
|
|
@@ -10,9 +10,10 @@ const WebsocketSubscription_1 = require("./WebsocketSubscription");
|
|
|
10
10
|
const events_1 = require("events");
|
|
11
11
|
const index_1 = require("../index");
|
|
12
12
|
const user_1 = require("../decode/user");
|
|
13
|
+
const grpcSubscription_1 = require("./grpcSubscription");
|
|
13
14
|
class OrderSubscriber {
|
|
14
15
|
constructor(config) {
|
|
15
|
-
var _a, _b, _c;
|
|
16
|
+
var _a, _b, _c, _d, _e;
|
|
16
17
|
this.usersAccounts = new Map();
|
|
17
18
|
this.driftClient = config.driftClient;
|
|
18
19
|
this.commitment = config.subscriptionConfig.commitment || 'processed';
|
|
@@ -22,8 +23,9 @@ class OrderSubscriber {
|
|
|
22
23
|
frequency: config.subscriptionConfig.frequency,
|
|
23
24
|
});
|
|
24
25
|
}
|
|
25
|
-
else {
|
|
26
|
-
this.subscription = new
|
|
26
|
+
else if (config.subscriptionConfig.type === 'grpc') {
|
|
27
|
+
this.subscription = new grpcSubscription_1.grpcSubscription({
|
|
28
|
+
grpcConfigs: config.subscriptionConfig.configs,
|
|
27
29
|
orderSubscriber: this,
|
|
28
30
|
commitment: this.commitment,
|
|
29
31
|
skipInitialLoad: config.subscriptionConfig.skipInitialLoad,
|
|
@@ -35,7 +37,20 @@ class OrderSubscriber {
|
|
|
35
37
|
decoded: config.decodeData,
|
|
36
38
|
});
|
|
37
39
|
}
|
|
38
|
-
|
|
40
|
+
else {
|
|
41
|
+
this.subscription = new WebsocketSubscription_1.WebsocketSubscription({
|
|
42
|
+
orderSubscriber: this,
|
|
43
|
+
commitment: this.commitment,
|
|
44
|
+
skipInitialLoad: config.subscriptionConfig.skipInitialLoad,
|
|
45
|
+
resubOpts: {
|
|
46
|
+
resubTimeoutMs: (_c = config.subscriptionConfig) === null || _c === void 0 ? void 0 : _c.resubTimeoutMs,
|
|
47
|
+
logResubMessages: (_d = config.subscriptionConfig) === null || _d === void 0 ? void 0 : _d.logResubMessages,
|
|
48
|
+
},
|
|
49
|
+
resyncIntervalMs: config.subscriptionConfig.resyncIntervalMs,
|
|
50
|
+
decoded: config.decodeData,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
if ((_e = config.fastDecode) !== null && _e !== void 0 ? _e : true) {
|
|
39
54
|
this.decodeFn = (name, data) => (0, user_1.decodeUser)(data);
|
|
40
55
|
}
|
|
41
56
|
else {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Commitment } from '@solana/web3.js';
|
|
2
|
+
import { OrderSubscriber } from './OrderSubscriber';
|
|
3
|
+
import { GrpcConfigs, ResubOpts } from '../accounts/types';
|
|
4
|
+
export declare class grpcSubscription {
|
|
5
|
+
private orderSubscriber;
|
|
6
|
+
private commitment;
|
|
7
|
+
private skipInitialLoad;
|
|
8
|
+
private resubOpts?;
|
|
9
|
+
private resyncIntervalMs?;
|
|
10
|
+
private subscriber?;
|
|
11
|
+
private resyncTimeoutId?;
|
|
12
|
+
private decoded?;
|
|
13
|
+
private grpcConfigs;
|
|
14
|
+
constructor({ grpcConfigs, orderSubscriber, commitment, skipInitialLoad, resubOpts, resyncIntervalMs, decoded, }: {
|
|
15
|
+
grpcConfigs: GrpcConfigs;
|
|
16
|
+
orderSubscriber: OrderSubscriber;
|
|
17
|
+
commitment: Commitment;
|
|
18
|
+
skipInitialLoad?: boolean;
|
|
19
|
+
resubOpts?: ResubOpts;
|
|
20
|
+
resyncIntervalMs?: number;
|
|
21
|
+
decoded?: boolean;
|
|
22
|
+
});
|
|
23
|
+
subscribe(): Promise<void>;
|
|
24
|
+
unsubscribe(): Promise<void>;
|
|
25
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.grpcSubscription = void 0;
|
|
4
|
+
const grpcProgramAccountSubscriber_1 = require("../accounts/grpcProgramAccountSubscriber");
|
|
5
|
+
const memcmp_1 = require("../memcmp");
|
|
6
|
+
class grpcSubscription {
|
|
7
|
+
constructor({ grpcConfigs, orderSubscriber, commitment, skipInitialLoad = false, resubOpts, resyncIntervalMs, decoded = true, }) {
|
|
8
|
+
this.orderSubscriber = orderSubscriber;
|
|
9
|
+
this.commitment = commitment;
|
|
10
|
+
this.skipInitialLoad = skipInitialLoad;
|
|
11
|
+
this.resubOpts = resubOpts;
|
|
12
|
+
this.resyncIntervalMs = resyncIntervalMs;
|
|
13
|
+
this.decoded = decoded;
|
|
14
|
+
this.grpcConfigs = grpcConfigs;
|
|
15
|
+
}
|
|
16
|
+
async subscribe() {
|
|
17
|
+
if (this.subscriber) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
this.subscriber = new grpcProgramAccountSubscriber_1.grpcProgramAccountSubscriber(this.grpcConfigs, 'OrderSubscriber', 'User', this.orderSubscriber.driftClient.program, this.orderSubscriber.decodeFn, {
|
|
21
|
+
filters: [(0, memcmp_1.getUserFilter)(), (0, memcmp_1.getNonIdleUserFilter)()],
|
|
22
|
+
commitment: this.commitment,
|
|
23
|
+
}, this.resubOpts);
|
|
24
|
+
await this.subscriber.subscribe((accountId, account, context, buffer) => {
|
|
25
|
+
var _a;
|
|
26
|
+
const userKey = accountId.toBase58();
|
|
27
|
+
if ((_a = this.decoded) !== null && _a !== void 0 ? _a : true) {
|
|
28
|
+
this.orderSubscriber.tryUpdateUserAccount(userKey, 'decoded', account, context.slot);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
this.orderSubscriber.tryUpdateUserAccount(userKey, 'buffer', buffer, context.slot);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
if (!this.skipInitialLoad) {
|
|
35
|
+
await this.orderSubscriber.fetch();
|
|
36
|
+
}
|
|
37
|
+
if (this.resyncIntervalMs) {
|
|
38
|
+
const recursiveResync = () => {
|
|
39
|
+
this.resyncTimeoutId = setTimeout(() => {
|
|
40
|
+
this.orderSubscriber
|
|
41
|
+
.fetch()
|
|
42
|
+
.catch((e) => {
|
|
43
|
+
console.error('Failed to resync in OrderSubscriber');
|
|
44
|
+
console.log(e);
|
|
45
|
+
})
|
|
46
|
+
.finally(() => {
|
|
47
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
48
|
+
if (!this.resyncTimeoutId)
|
|
49
|
+
return;
|
|
50
|
+
recursiveResync();
|
|
51
|
+
});
|
|
52
|
+
}, this.resyncIntervalMs);
|
|
53
|
+
};
|
|
54
|
+
recursiveResync();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
async unsubscribe() {
|
|
58
|
+
if (!this.subscriber)
|
|
59
|
+
return;
|
|
60
|
+
await this.subscriber.unsubscribe();
|
|
61
|
+
this.subscriber = undefined;
|
|
62
|
+
if (this.resyncTimeoutId !== undefined) {
|
|
63
|
+
clearTimeout(this.resyncTimeoutId);
|
|
64
|
+
this.resyncTimeoutId = undefined;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.grpcSubscription = grpcSubscription;
|
|
@@ -1,12 +1,21 @@
|
|
|
1
1
|
import { Commitment, PublicKey } from '@solana/web3.js';
|
|
2
2
|
import { Order, UserAccount } from '../types';
|
|
3
3
|
import { DriftClient } from '../driftClient';
|
|
4
|
+
import { GrpcConfigs } from '../accounts/types';
|
|
4
5
|
export type OrderSubscriberConfig = {
|
|
5
6
|
driftClient: DriftClient;
|
|
6
7
|
subscriptionConfig: {
|
|
7
8
|
type: 'polling';
|
|
8
9
|
frequency: number;
|
|
9
10
|
commitment?: Commitment;
|
|
11
|
+
} | {
|
|
12
|
+
type: 'grpc';
|
|
13
|
+
skipInitialLoad?: boolean;
|
|
14
|
+
resubTimeoutMs?: number;
|
|
15
|
+
logResubMessages?: boolean;
|
|
16
|
+
resyncIntervalMs?: number;
|
|
17
|
+
configs: GrpcConfigs;
|
|
18
|
+
commitment?: Commitment;
|
|
10
19
|
} | {
|
|
11
20
|
type: 'websocket';
|
|
12
21
|
skipInitialLoad?: boolean;
|
package/lib/user.js
CHANGED
|
@@ -15,6 +15,7 @@ const oracles_1 = require("./math/oracles");
|
|
|
15
15
|
const tiers_1 = require("./math/tiers");
|
|
16
16
|
const strictOraclePrice_1 = require("./oracles/strictOraclePrice");
|
|
17
17
|
const fuel_1 = require("./math/fuel");
|
|
18
|
+
const grpcUserAccountSubscriber_1 = require("./accounts/grpcUserAccountSubscriber");
|
|
18
19
|
class User {
|
|
19
20
|
get isSubscribed() {
|
|
20
21
|
return this._isSubscribed && this.accountSubscriber.isSubscribed;
|
|
@@ -23,7 +24,7 @@ class User {
|
|
|
23
24
|
this._isSubscribed = val;
|
|
24
25
|
}
|
|
25
26
|
constructor(config) {
|
|
26
|
-
var _a, _b, _c, _d, _e;
|
|
27
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
27
28
|
this._isSubscribed = false;
|
|
28
29
|
this.driftClient = config.driftClient;
|
|
29
30
|
this.userAccountPublicKey = config.userAccountPublicKey;
|
|
@@ -33,11 +34,17 @@ class User {
|
|
|
33
34
|
else if (((_b = config.accountSubscription) === null || _b === void 0 ? void 0 : _b.type) === 'custom') {
|
|
34
35
|
this.accountSubscriber = config.accountSubscription.userAccountSubscriber;
|
|
35
36
|
}
|
|
37
|
+
else if (((_c = config.accountSubscription) === null || _c === void 0 ? void 0 : _c.type) === 'grpc') {
|
|
38
|
+
this.accountSubscriber = new grpcUserAccountSubscriber_1.grpcUserAccountSubscriber(config.accountSubscription.configs, config.driftClient.program, config.userAccountPublicKey, {
|
|
39
|
+
resubTimeoutMs: (_d = config.accountSubscription) === null || _d === void 0 ? void 0 : _d.resubTimeoutMs,
|
|
40
|
+
logResubMessages: (_e = config.accountSubscription) === null || _e === void 0 ? void 0 : _e.logResubMessages,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
36
43
|
else {
|
|
37
44
|
this.accountSubscriber = new webSocketUserAccountSubscriber_1.WebSocketUserAccountSubscriber(config.driftClient.program, config.userAccountPublicKey, {
|
|
38
|
-
resubTimeoutMs: (
|
|
39
|
-
logResubMessages: (
|
|
40
|
-
}, (
|
|
45
|
+
resubTimeoutMs: (_f = config.accountSubscription) === null || _f === void 0 ? void 0 : _f.resubTimeoutMs,
|
|
46
|
+
logResubMessages: (_g = config.accountSubscription) === null || _g === void 0 ? void 0 : _g.logResubMessages,
|
|
47
|
+
}, (_h = config.accountSubscription) === null || _h === void 0 ? void 0 : _h.commitment);
|
|
41
48
|
}
|
|
42
49
|
this.eventEmitter = this.accountSubscriber.eventEmitter;
|
|
43
50
|
}
|
package/lib/userConfig.d.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
|
export type UserConfig = {
|
|
6
6
|
accountSubscription?: UserSubscriptionConfig;
|
|
7
7
|
driftClient: DriftClient;
|
|
@@ -18,4 +18,9 @@ export type UserSubscriptionConfig = {
|
|
|
18
18
|
} | {
|
|
19
19
|
type: 'custom';
|
|
20
20
|
userAccountSubscriber: UserAccountSubscriber;
|
|
21
|
+
} | {
|
|
22
|
+
type: 'grpc';
|
|
23
|
+
resubTimeoutMs?: number;
|
|
24
|
+
logResubMessages?: boolean;
|
|
25
|
+
configs: GrpcConfigs;
|
|
21
26
|
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { UserMap } from './userMap';
|
|
3
|
+
import { UserAccount } from '../types';
|
|
4
|
+
import { Commitment } from '@solana/web3.js';
|
|
5
|
+
import { GrpcConfigs, ResubOpts } from '../accounts/types';
|
|
6
|
+
export declare class grpcSubscription {
|
|
7
|
+
private configs;
|
|
8
|
+
private userMap;
|
|
9
|
+
private commitment;
|
|
10
|
+
private skipInitialLoad;
|
|
11
|
+
private resubOpts?;
|
|
12
|
+
private includeIdle?;
|
|
13
|
+
private decodeFn;
|
|
14
|
+
private subscriber;
|
|
15
|
+
constructor({ configs, userMap, commitment, skipInitialLoad, resubOpts, includeIdle, decodeFn, }: {
|
|
16
|
+
configs: GrpcConfigs;
|
|
17
|
+
userMap: UserMap;
|
|
18
|
+
commitment: Commitment;
|
|
19
|
+
skipInitialLoad?: boolean;
|
|
20
|
+
resubOpts?: ResubOpts;
|
|
21
|
+
includeIdle?: boolean;
|
|
22
|
+
decodeFn: (name: string, data: Buffer) => UserAccount;
|
|
23
|
+
});
|
|
24
|
+
subscribe(): Promise<void>;
|
|
25
|
+
unsubscribe(): Promise<void>;
|
|
26
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.grpcSubscription = void 0;
|
|
4
|
+
const memcmp_1 = require("../memcmp");
|
|
5
|
+
const grpcProgramAccountSubscriber_1 = require("../accounts/grpcProgramAccountSubscriber");
|
|
6
|
+
class grpcSubscription {
|
|
7
|
+
constructor({ configs, userMap, commitment, skipInitialLoad = false, resubOpts, includeIdle = false, decodeFn, }) {
|
|
8
|
+
this.userMap = userMap;
|
|
9
|
+
this.commitment = commitment;
|
|
10
|
+
this.skipInitialLoad = skipInitialLoad;
|
|
11
|
+
this.resubOpts = resubOpts;
|
|
12
|
+
this.includeIdle = includeIdle || false;
|
|
13
|
+
this.decodeFn = decodeFn;
|
|
14
|
+
this.configs = configs;
|
|
15
|
+
}
|
|
16
|
+
async subscribe() {
|
|
17
|
+
if (!this.subscriber) {
|
|
18
|
+
const filters = [(0, memcmp_1.getUserFilter)()];
|
|
19
|
+
if (!this.includeIdle) {
|
|
20
|
+
filters.push((0, memcmp_1.getNonIdleUserFilter)());
|
|
21
|
+
}
|
|
22
|
+
this.subscriber = new grpcProgramAccountSubscriber_1.grpcProgramAccountSubscriber(this.configs, 'UserMap', 'User', this.userMap.driftClient.program, this.decodeFn, {
|
|
23
|
+
filters,
|
|
24
|
+
commitment: this.commitment,
|
|
25
|
+
}, this.resubOpts);
|
|
26
|
+
}
|
|
27
|
+
await this.subscriber.subscribe((accountId, account, context) => {
|
|
28
|
+
const userKey = accountId.toBase58();
|
|
29
|
+
this.userMap.updateUserAccount(userKey, account, context.slot);
|
|
30
|
+
});
|
|
31
|
+
if (!this.skipInitialLoad) {
|
|
32
|
+
await this.userMap.sync();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async unsubscribe() {
|
|
36
|
+
if (!this.subscriber)
|
|
37
|
+
return;
|
|
38
|
+
await this.subscriber.unsubscribe();
|
|
39
|
+
this.subscriber = undefined;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.grpcSubscription = grpcSubscription;
|
package/lib/userMap/userMap.js
CHANGED
|
@@ -9,6 +9,7 @@ const memcmp_1 = require("../memcmp");
|
|
|
9
9
|
const WebsocketSubscription_1 = require("./WebsocketSubscription");
|
|
10
10
|
const PollingSubscription_1 = require("./PollingSubscription");
|
|
11
11
|
const user_1 = require("../decode/user");
|
|
12
|
+
const grpcSubscription_1 = require("./grpcSubscription");
|
|
12
13
|
const MAX_USER_ACCOUNT_SIZE_BYTES = 4376;
|
|
13
14
|
class UserMap {
|
|
14
15
|
/**
|
|
@@ -52,6 +53,19 @@ class UserMap {
|
|
|
52
53
|
skipInitialLoad: config.skipInitialLoad,
|
|
53
54
|
});
|
|
54
55
|
}
|
|
56
|
+
else if (config.subscriptionConfig.type === 'grpc') {
|
|
57
|
+
this.subscription = new grpcSubscription_1.grpcSubscription({
|
|
58
|
+
configs: config.subscriptionConfig.configs,
|
|
59
|
+
userMap: this,
|
|
60
|
+
commitment: this.commitment,
|
|
61
|
+
resubOpts: {
|
|
62
|
+
resubTimeoutMs: config.subscriptionConfig.resubTimeoutMs,
|
|
63
|
+
logResubMessages: config.subscriptionConfig.logResubMessages,
|
|
64
|
+
},
|
|
65
|
+
skipInitialLoad: config.skipInitialLoad,
|
|
66
|
+
decodeFn,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
55
69
|
else {
|
|
56
70
|
this.subscription = new WebsocketSubscription_1.WebsocketSubscription({
|
|
57
71
|
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
|
export type UserAccountFilterCriteria = {
|
|
4
5
|
hasOpenOrders: boolean;
|
|
5
6
|
};
|
|
@@ -17,6 +18,12 @@ export type UserMapConfig = {
|
|
|
17
18
|
type: 'polling';
|
|
18
19
|
frequency: number;
|
|
19
20
|
commitment?: Commitment;
|
|
21
|
+
} | {
|
|
22
|
+
type: 'grpc';
|
|
23
|
+
configs: GrpcConfigs;
|
|
24
|
+
resubTimeoutMs?: number;
|
|
25
|
+
logResubMessages?: boolean;
|
|
26
|
+
commitment?: Commitment;
|
|
20
27
|
} | {
|
|
21
28
|
type: 'websocket';
|
|
22
29
|
resubTimeoutMs?: number;
|
package/lib/userStatsConfig.d.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
|
export type UserStatsConfig = {
|
|
5
6
|
accountSubscription?: UserStatsSubscriptionConfig;
|
|
6
7
|
driftClient: DriftClient;
|
|
@@ -16,4 +17,9 @@ export type UserStatsSubscriptionConfig = {
|
|
|
16
17
|
accountLoader: BulkAccountLoader;
|
|
17
18
|
} | {
|
|
18
19
|
type: 'custom';
|
|
20
|
+
} | {
|
|
21
|
+
type: 'grpc';
|
|
22
|
+
resubTimeoutMs?: number;
|
|
23
|
+
logResubMessages?: boolean;
|
|
24
|
+
configs: GrpcConfigs;
|
|
19
25
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@drift-labs/sdk",
|
|
3
|
-
"version": "2.96.0-beta.
|
|
3
|
+
"version": "2.96.0-beta.1",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"types": "lib/index.d.ts",
|
|
6
6
|
"author": "crispheaney",
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
"@coral-xyz/anchor": "0.28.0",
|
|
38
38
|
"@coral-xyz/anchor-30": "npm:@coral-xyz/anchor@0.30.1",
|
|
39
39
|
"@ellipsis-labs/phoenix-sdk": "^1.4.2",
|
|
40
|
+
"@grpc/grpc-js": "^1.11.2",
|
|
40
41
|
"@openbook-dex/openbook-v2": "^0.2.10",
|
|
41
42
|
"@project-serum/serum": "^0.13.38",
|
|
42
43
|
"@pythnetwork/client": "2.5.3",
|
|
@@ -45,6 +46,7 @@
|
|
|
45
46
|
"@solana/spl-token": "0.3.7",
|
|
46
47
|
"@solana/web3.js": "1.92.3",
|
|
47
48
|
"@switchboard-xyz/on-demand": "1.2.32",
|
|
49
|
+
"@triton-one/yellowstone-grpc": "^0.6.0",
|
|
48
50
|
"anchor-bankrun": "^0.3.0",
|
|
49
51
|
"node-cache": "^5.1.2",
|
|
50
52
|
"rpc-websockets": "7.5.1",
|