@drift-labs/sdk 2.142.0-beta.0 → 2.142.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/bun.lock +25 -10
- package/lib/browser/accounts/grpcAccountSubscriber.d.ts +2 -1
- package/lib/browser/accounts/grpcAccountSubscriber.js +4 -2
- package/lib/browser/accounts/grpcDriftClientAccountSubscriber.d.ts +1 -1
- package/lib/browser/accounts/grpcDriftClientAccountSubscriber.js +3 -3
- package/lib/browser/accounts/grpcDriftClientAccountSubscriberV2.d.ts +17 -0
- package/lib/browser/accounts/grpcDriftClientAccountSubscriberV2.js +161 -0
- package/lib/browser/accounts/grpcMultiAccountSubscriber.d.ts +30 -0
- package/lib/browser/accounts/grpcMultiAccountSubscriber.js +271 -0
- package/lib/browser/driftClient.js +11 -10
- package/lib/browser/driftClientConfig.d.ts +3 -0
- package/lib/browser/index.d.ts +1 -0
- package/lib/browser/index.js +1 -0
- package/lib/node/accounts/grpcAccountSubscriber.d.ts +2 -1
- package/lib/node/accounts/grpcAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/grpcAccountSubscriber.js +4 -2
- package/lib/node/accounts/grpcDriftClientAccountSubscriber.d.ts +1 -1
- package/lib/node/accounts/grpcDriftClientAccountSubscriber.js +3 -3
- package/lib/node/accounts/grpcDriftClientAccountSubscriberV2.d.ts +18 -0
- package/lib/node/accounts/grpcDriftClientAccountSubscriberV2.d.ts.map +1 -0
- package/lib/node/accounts/grpcDriftClientAccountSubscriberV2.js +161 -0
- package/lib/node/accounts/grpcMultiAccountSubscriber.d.ts +31 -0
- package/lib/node/accounts/grpcMultiAccountSubscriber.d.ts.map +1 -0
- package/lib/node/accounts/grpcMultiAccountSubscriber.js +271 -0
- package/lib/node/driftClient.d.ts.map +1 -1
- package/lib/node/driftClient.js +11 -10
- package/lib/node/driftClientConfig.d.ts +3 -0
- package/lib/node/driftClientConfig.d.ts.map +1 -1
- package/lib/node/index.d.ts +1 -0
- package/lib/node/index.d.ts.map +1 -1
- package/lib/node/index.js +1 -0
- package/lib/node/isomorphic/grpc.d.ts +5 -3
- package/lib/node/isomorphic/grpc.js +1 -3
- package/lib/node/isomorphic/grpc.node.d.ts +5 -3
- package/lib/node/isomorphic/grpc.node.d.ts.map +1 -1
- package/lib/node/isomorphic/grpc.node.js +1 -3
- package/package.json +3 -3
- package/scripts/client-test.ts +87 -0
- package/src/accounts/grpcAccountSubscriber.ts +9 -6
- package/src/accounts/grpcDriftClientAccountSubscriber.ts +1 -1
- package/src/accounts/grpcDriftClientAccountSubscriberV2.ts +294 -0
- package/src/accounts/grpcMultiAccountSubscriber.ts +328 -0
- package/src/driftClient.ts +5 -2
- package/src/driftClientConfig.ts +13 -0
- package/src/index.ts +1 -0
- package/src/isomorphic/grpc.node.ts +11 -7
|
@@ -86,7 +86,7 @@ class DriftClient {
|
|
|
86
86
|
this._isSubscribed = val;
|
|
87
87
|
}
|
|
88
88
|
constructor(config) {
|
|
89
|
-
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, _20, _21, _22, _23;
|
|
89
|
+
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, _20, _21, _22, _23, _24, _25;
|
|
90
90
|
this.users = new Map();
|
|
91
91
|
this._isSubscribed = false;
|
|
92
92
|
this.perpMarketLastSlotCache = new Map();
|
|
@@ -208,17 +208,18 @@ class DriftClient {
|
|
|
208
208
|
this.accountSubscriber = new pollingDriftClientAccountSubscriber_1.PollingDriftClientAccountSubscriber(this.program, config.accountSubscription.accountLoader, (_6 = config.perpMarketIndexes) !== null && _6 !== void 0 ? _6 : [], (_7 = config.spotMarketIndexes) !== null && _7 !== void 0 ? _7 : [], (_8 = config.oracleInfos) !== null && _8 !== void 0 ? _8 : [], noMarketsAndOraclesSpecified, delistedMarketSetting);
|
|
209
209
|
}
|
|
210
210
|
else if (((_9 = config.accountSubscription) === null || _9 === void 0 ? void 0 : _9.type) === 'grpc') {
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
211
|
+
const accountSubscriberClass = (_11 = (_10 = config.accountSubscription) === null || _10 === void 0 ? void 0 : _10.driftClientAccountSubscriber) !== null && _11 !== void 0 ? _11 : grpcDriftClientAccountSubscriber_1.grpcDriftClientAccountSubscriber;
|
|
212
|
+
this.accountSubscriber = new accountSubscriberClass(config.accountSubscription.grpcConfigs, this.program, (_12 = config.perpMarketIndexes) !== null && _12 !== void 0 ? _12 : [], (_13 = config.spotMarketIndexes) !== null && _13 !== void 0 ? _13 : [], (_14 = config.oracleInfos) !== null && _14 !== void 0 ? _14 : [], noMarketsAndOraclesSpecified, delistedMarketSetting, {
|
|
213
|
+
resubTimeoutMs: (_15 = config.accountSubscription) === null || _15 === void 0 ? void 0 : _15.resubTimeoutMs,
|
|
214
|
+
logResubMessages: (_16 = config.accountSubscription) === null || _16 === void 0 ? void 0 : _16.logResubMessages,
|
|
214
215
|
});
|
|
215
216
|
}
|
|
216
217
|
else {
|
|
217
|
-
const accountSubscriberClass = (
|
|
218
|
-
this.accountSubscriber = new accountSubscriberClass(this.program, (
|
|
219
|
-
resubTimeoutMs: (
|
|
220
|
-
logResubMessages: (
|
|
221
|
-
}, (
|
|
218
|
+
const accountSubscriberClass = (_18 = (_17 = config.accountSubscription) === null || _17 === void 0 ? void 0 : _17.driftClientAccountSubscriber) !== null && _18 !== void 0 ? _18 : webSocketDriftClientAccountSubscriber_1.WebSocketDriftClientAccountSubscriber;
|
|
219
|
+
this.accountSubscriber = new accountSubscriberClass(this.program, (_19 = config.perpMarketIndexes) !== null && _19 !== void 0 ? _19 : [], (_20 = config.spotMarketIndexes) !== null && _20 !== void 0 ? _20 : [], (_21 = config.oracleInfos) !== null && _21 !== void 0 ? _21 : [], noMarketsAndOraclesSpecified, delistedMarketSetting, {
|
|
220
|
+
resubTimeoutMs: (_22 = config.accountSubscription) === null || _22 === void 0 ? void 0 : _22.resubTimeoutMs,
|
|
221
|
+
logResubMessages: (_23 = config.accountSubscription) === null || _23 === void 0 ? void 0 : _23.logResubMessages,
|
|
222
|
+
}, (_24 = config.accountSubscription) === null || _24 === void 0 ? void 0 : _24.commitment);
|
|
222
223
|
}
|
|
223
224
|
this.eventEmitter = this.accountSubscriber.eventEmitter;
|
|
224
225
|
this.metricsEventEmitter = new events_1.EventEmitter();
|
|
@@ -226,7 +227,7 @@ class DriftClient {
|
|
|
226
227
|
this.enableMetricsEvents = true;
|
|
227
228
|
}
|
|
228
229
|
this.txSender =
|
|
229
|
-
(
|
|
230
|
+
(_25 = config.txSender) !== null && _25 !== void 0 ? _25 : new retryTxSender_1.RetryTxSender({
|
|
230
231
|
connection: this.connection,
|
|
231
232
|
wallet: this.wallet,
|
|
232
233
|
opts: this.opts,
|
|
@@ -14,6 +14,8 @@ import { WebSocketAccountSubscriberV2 } from './accounts/webSocketAccountSubscri
|
|
|
14
14
|
import { WebSocketProgramAccountSubscriber } from './accounts/webSocketProgramAccountSubscriber';
|
|
15
15
|
import { WebSocketDriftClientAccountSubscriberV2 } from './accounts/webSocketDriftClientAccountSubscriberV2';
|
|
16
16
|
import { WebSocketDriftClientAccountSubscriber } from './accounts/webSocketDriftClientAccountSubscriber';
|
|
17
|
+
import { grpcDriftClientAccountSubscriberV2 } from './accounts/grpcDriftClientAccountSubscriberV2';
|
|
18
|
+
import { grpcDriftClientAccountSubscriber } from './accounts/grpcDriftClientAccountSubscriber';
|
|
17
19
|
export type DriftClientConfig = {
|
|
18
20
|
connection: Connection;
|
|
19
21
|
wallet: IWallet;
|
|
@@ -49,6 +51,7 @@ export type DriftClientSubscriptionConfig = {
|
|
|
49
51
|
grpcConfigs: GrpcConfigs;
|
|
50
52
|
resubTimeoutMs?: number;
|
|
51
53
|
logResubMessages?: boolean;
|
|
54
|
+
driftClientAccountSubscriber?: new (grpcConfigs: GrpcConfigs, program: Program, perpMarketIndexes: number[], spotMarketIndexes: number[], oracleInfos: OracleInfo[], shouldFindAllMarketsAndOracles: boolean, delistedMarketSetting: DelistedMarketSetting) => grpcDriftClientAccountSubscriberV2 | grpcDriftClientAccountSubscriber;
|
|
52
55
|
} | {
|
|
53
56
|
type: 'websocket';
|
|
54
57
|
resubTimeoutMs?: number;
|
package/lib/browser/index.d.ts
CHANGED
|
@@ -53,6 +53,7 @@ export * from './events/parse';
|
|
|
53
53
|
export * from './events/pollingLogProvider';
|
|
54
54
|
export * from './jupiter/jupiterClient';
|
|
55
55
|
export * from './math/auction';
|
|
56
|
+
export * from './math/builder';
|
|
56
57
|
export * from './math/spotMarket';
|
|
57
58
|
export * from './math/conversion';
|
|
58
59
|
export * from './math/exchangeStatus';
|
package/lib/browser/index.js
CHANGED
|
@@ -82,6 +82,7 @@ __exportStar(require("./events/parse"), exports);
|
|
|
82
82
|
__exportStar(require("./events/pollingLogProvider"), exports);
|
|
83
83
|
__exportStar(require("./jupiter/jupiterClient"), exports);
|
|
84
84
|
__exportStar(require("./math/auction"), exports);
|
|
85
|
+
__exportStar(require("./math/builder"), exports);
|
|
85
86
|
__exportStar(require("./math/spotMarket"), exports);
|
|
86
87
|
__exportStar(require("./math/conversion"), exports);
|
|
87
88
|
__exportStar(require("./math/exchangeStatus"), exports);
|
|
@@ -4,13 +4,14 @@ import { ResubOpts, GrpcConfigs } from './types';
|
|
|
4
4
|
import { Program } from '@coral-xyz/anchor';
|
|
5
5
|
import { PublicKey } from '@solana/web3.js';
|
|
6
6
|
import { WebSocketAccountSubscriber } from './webSocketAccountSubscriber';
|
|
7
|
+
import { Client } from '../isomorphic/grpc';
|
|
7
8
|
export declare class grpcAccountSubscriber<T> extends WebSocketAccountSubscriber<T> {
|
|
8
9
|
private client;
|
|
9
10
|
private stream;
|
|
10
11
|
private commitmentLevel;
|
|
11
12
|
listenerId?: number;
|
|
12
13
|
private constructor();
|
|
13
|
-
static create<U>(grpcConfigs: GrpcConfigs, accountName: string, program: Program, accountPublicKey: PublicKey, decodeBuffer?: (buffer: Buffer) => U, resubOpts?: ResubOpts): Promise<grpcAccountSubscriber<U>>;
|
|
14
|
+
static create<U>(grpcConfigs: GrpcConfigs, accountName: string, program: Program, accountPublicKey: PublicKey, decodeBuffer?: (buffer: Buffer) => U, resubOpts?: ResubOpts, clientProp?: Client): Promise<grpcAccountSubscriber<U>>;
|
|
14
15
|
subscribe(onChange: (data: T) => void): Promise<void>;
|
|
15
16
|
unsubscribe(onResub?: boolean): Promise<void>;
|
|
16
17
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"grpcAccountSubscriber.d.ts","sourceRoot":"","sources":["../../../src/accounts/grpcAccountSubscriber.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACjD,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAG5C,OAAO,EAAE,0BAA0B,EAAE,MAAM,8BAA8B,CAAC;
|
|
1
|
+
{"version":3,"file":"grpcAccountSubscriber.d.ts","sourceRoot":"","sources":["../../../src/accounts/grpcAccountSubscriber.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACjD,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAG5C,OAAO,EAAE,0BAA0B,EAAE,MAAM,8BAA8B,CAAC;AAC1E,OAAO,EACN,MAAM,EAMN,MAAM,oBAAoB,CAAC;AAE5B,qBAAa,qBAAqB,CAAC,CAAC,CAAE,SAAQ,0BAA0B,CAAC,CAAC,CAAC;IAC1E,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,MAAM,CAAwD;IACtE,OAAO,CAAC,eAAe,CAAkB;IAClC,UAAU,CAAC,EAAE,MAAM,CAAC;IAE3B,OAAO;WAca,MAAM,CAAC,CAAC,EAC3B,WAAW,EAAE,WAAW,EACxB,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE,OAAO,EAChB,gBAAgB,EAAE,SAAS,EAC3B,YAAY,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,CAAC,EACpC,SAAS,CAAC,EAAE,SAAS,EACrB,UAAU,CAAC,EAAE,MAAM,GACjB,OAAO,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;IAuBrB,SAAS,CAAC,QAAQ,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAkFrD,WAAW,CAAC,OAAO,UAAQ,GAAG,OAAO,CAAC,IAAI,CAAC;CAsC1D"}
|
|
@@ -34,9 +34,11 @@ class grpcAccountSubscriber extends webSocketAccountSubscriber_1.WebSocketAccoun
|
|
|
34
34
|
this.client = client;
|
|
35
35
|
this.commitmentLevel = commitmentLevel;
|
|
36
36
|
}
|
|
37
|
-
static async create(grpcConfigs, accountName, program, accountPublicKey, decodeBuffer, resubOpts) {
|
|
37
|
+
static async create(grpcConfigs, accountName, program, accountPublicKey, decodeBuffer, resubOpts, clientProp) {
|
|
38
38
|
var _a, _b;
|
|
39
|
-
const client =
|
|
39
|
+
const client = clientProp
|
|
40
|
+
? clientProp
|
|
41
|
+
: await (0, grpc_1.createClient)(grpcConfigs.endpoint, grpcConfigs.token, (_a = grpcConfigs.channelOptions) !== null && _a !== void 0 ? _a : {});
|
|
40
42
|
const commitmentLevel =
|
|
41
43
|
// @ts-ignore :: isomorphic exported enum fails typescript but will work at runtime
|
|
42
44
|
(_b = grpcConfigs.commitmentLevel) !== null && _b !== void 0 ? _b : grpc_1.CommitmentLevel.CONFIRMED;
|
|
@@ -2,7 +2,7 @@ import { WebSocketDriftClientAccountSubscriber } from './webSocketDriftClientAcc
|
|
|
2
2
|
import { OracleInfo } from '../oracles/types';
|
|
3
3
|
import { Program } from '@coral-xyz/anchor';
|
|
4
4
|
import { DelistedMarketSetting, GrpcConfigs, ResubOpts } from './types';
|
|
5
|
-
export declare class
|
|
5
|
+
export declare class grpcDriftClientAccountSubscriber extends WebSocketDriftClientAccountSubscriber {
|
|
6
6
|
private grpcConfigs;
|
|
7
7
|
constructor(grpcConfigs: GrpcConfigs, program: Program, perpMarketIndexes: number[], spotMarketIndexes: number[], oracleInfos: OracleInfo[], shouldFindAllMarketsAndOracles: boolean, delistedMarketSetting: DelistedMarketSetting, resubOpts?: ResubOpts);
|
|
8
8
|
subscribe(): Promise<boolean>;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.grpcDriftClientAccountSubscriber = void 0;
|
|
4
4
|
const webSocketDriftClientAccountSubscriber_1 = require("./webSocketDriftClientAccountSubscriber");
|
|
5
5
|
const config_1 = require("../config");
|
|
6
6
|
const pda_1 = require("../addresses/pda");
|
|
7
7
|
const grpcAccountSubscriber_1 = require("./grpcAccountSubscriber");
|
|
8
8
|
const oracleId_1 = require("../oracles/oracleId");
|
|
9
|
-
class
|
|
9
|
+
class grpcDriftClientAccountSubscriber extends webSocketDriftClientAccountSubscriber_1.WebSocketDriftClientAccountSubscriber {
|
|
10
10
|
constructor(grpcConfigs, program, perpMarketIndexes, spotMarketIndexes, oracleInfos, shouldFindAllMarketsAndOracles, delistedMarketSetting, resubOpts) {
|
|
11
11
|
super(program, perpMarketIndexes, spotMarketIndexes, oracleInfos, shouldFindAllMarketsAndOracles, delistedMarketSetting, resubOpts);
|
|
12
12
|
this.grpcConfigs = grpcConfigs;
|
|
@@ -96,4 +96,4 @@ class gprcDriftClientAccountSubscriber extends webSocketDriftClientAccountSubscr
|
|
|
96
96
|
return true;
|
|
97
97
|
}
|
|
98
98
|
}
|
|
99
|
-
exports.
|
|
99
|
+
exports.grpcDriftClientAccountSubscriber = grpcDriftClientAccountSubscriber;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { WebSocketDriftClientAccountSubscriber } from './webSocketDriftClientAccountSubscriber';
|
|
2
|
+
import { OracleInfo } from '../oracles/types';
|
|
3
|
+
import { Program } from '@coral-xyz/anchor';
|
|
4
|
+
import { DelistedMarketSetting, GrpcConfigs, ResubOpts } from './types';
|
|
5
|
+
export declare class grpcDriftClientAccountSubscriberV2 extends WebSocketDriftClientAccountSubscriber {
|
|
6
|
+
private grpcConfigs;
|
|
7
|
+
private perpMarketsSubscriber?;
|
|
8
|
+
private spotMarketsSubscriber?;
|
|
9
|
+
private oracleMultiSubscriber?;
|
|
10
|
+
constructor(grpcConfigs: GrpcConfigs, program: Program, perpMarketIndexes: number[], spotMarketIndexes: number[], oracleInfos: OracleInfo[], shouldFindAllMarketsAndOracles: boolean, delistedMarketSetting: DelistedMarketSetting, resubOpts?: ResubOpts);
|
|
11
|
+
subscribe(): Promise<boolean>;
|
|
12
|
+
subscribeToPerpMarketAccounts(): Promise<boolean>;
|
|
13
|
+
subscribeToSpotMarketAccounts(): Promise<boolean>;
|
|
14
|
+
subscribeToOracles(): Promise<boolean>;
|
|
15
|
+
unsubscribeFromOracles(): Promise<void>;
|
|
16
|
+
unsubscribe(): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=grpcDriftClientAccountSubscriberV2.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grpcDriftClientAccountSubscriberV2.d.ts","sourceRoot":"","sources":["../../../src/accounts/grpcDriftClientAccountSubscriberV2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qCAAqC,EAAE,MAAM,yCAAyC,CAAC;AAChG,OAAO,EAAE,UAAU,EAAmB,MAAM,kBAAkB,CAAC;AAC/D,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAQ5C,OAAO,EAAE,qBAAqB,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAMxE,qBAAa,kCAAmC,SAAQ,qCAAqC;IAC5F,OAAO,CAAC,WAAW,CAAc;IACjC,OAAO,CAAC,qBAAqB,CAAC,CAAgD;IAC9E,OAAO,CAAC,qBAAqB,CAAC,CAAgD;IAC9E,OAAO,CAAC,qBAAqB,CAAC,CAA8C;gBAG3E,WAAW,EAAE,WAAW,EACxB,OAAO,EAAE,OAAO,EAChB,iBAAiB,EAAE,MAAM,EAAE,EAC3B,iBAAiB,EAAE,MAAM,EAAE,EAC3B,WAAW,EAAE,UAAU,EAAE,EACzB,8BAA8B,EAAE,OAAO,EACvC,qBAAqB,EAAE,qBAAqB,EAC5C,SAAS,CAAC,EAAE,SAAS;IAcT,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;IAiF3B,6BAA6B,IAAI,OAAO,CAAC,OAAO,CAAC;IA0CjD,6BAA6B,IAAI,OAAO,CAAC,OAAO,CAAC;IA0CjD,kBAAkB,IAAI,OAAO,CAAC,OAAO,CAAC;IAoE/C,sBAAsB,IAAI,OAAO,CAAC,IAAI,CAAC;IAS9B,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;CAO3C"}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.grpcDriftClientAccountSubscriberV2 = void 0;
|
|
4
|
+
const webSocketDriftClientAccountSubscriber_1 = require("./webSocketDriftClientAccountSubscriber");
|
|
5
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
6
|
+
const config_1 = require("../config");
|
|
7
|
+
const pda_1 = require("../addresses/pda");
|
|
8
|
+
const grpcAccountSubscriber_1 = require("./grpcAccountSubscriber");
|
|
9
|
+
const grpcMultiAccountSubscriber_1 = require("./grpcMultiAccountSubscriber");
|
|
10
|
+
const oracleId_1 = require("../oracles/oracleId");
|
|
11
|
+
class grpcDriftClientAccountSubscriberV2 extends webSocketDriftClientAccountSubscriber_1.WebSocketDriftClientAccountSubscriber {
|
|
12
|
+
constructor(grpcConfigs, program, perpMarketIndexes, spotMarketIndexes, oracleInfos, shouldFindAllMarketsAndOracles, delistedMarketSetting, resubOpts) {
|
|
13
|
+
super(program, perpMarketIndexes, spotMarketIndexes, oracleInfos, shouldFindAllMarketsAndOracles, delistedMarketSetting, resubOpts);
|
|
14
|
+
this.grpcConfigs = grpcConfigs;
|
|
15
|
+
}
|
|
16
|
+
async subscribe() {
|
|
17
|
+
if (this.isSubscribed) {
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
if (this.isSubscribing) {
|
|
21
|
+
return await this.subscriptionPromise;
|
|
22
|
+
}
|
|
23
|
+
this.isSubscribing = true;
|
|
24
|
+
this.subscriptionPromise = new Promise((res) => {
|
|
25
|
+
this.subscriptionPromiseResolver = res;
|
|
26
|
+
});
|
|
27
|
+
if (this.shouldFindAllMarketsAndOracles) {
|
|
28
|
+
const { perpMarketIndexes, perpMarketAccounts, spotMarketIndexes, spotMarketAccounts, oracleInfos, } = await (0, config_1.findAllMarketAndOracles)(this.program);
|
|
29
|
+
this.perpMarketIndexes = perpMarketIndexes;
|
|
30
|
+
this.spotMarketIndexes = spotMarketIndexes;
|
|
31
|
+
this.oracleInfos = oracleInfos;
|
|
32
|
+
// front run and set the initial data here to save extra gma call in set initial data
|
|
33
|
+
this.initialPerpMarketAccountData = new Map(perpMarketAccounts.map((market) => [market.marketIndex, market]));
|
|
34
|
+
this.initialSpotMarketAccountData = new Map(spotMarketAccounts.map((market) => [market.marketIndex, market]));
|
|
35
|
+
}
|
|
36
|
+
const statePublicKey = await (0, pda_1.getDriftStateAccountPublicKey)(this.program.programId);
|
|
37
|
+
// create and activate main state account subscription
|
|
38
|
+
this.stateAccountSubscriber =
|
|
39
|
+
await grpcAccountSubscriber_1.grpcAccountSubscriber.create(this.grpcConfigs, 'state', this.program, statePublicKey, undefined, undefined);
|
|
40
|
+
await this.stateAccountSubscriber.subscribe((data) => {
|
|
41
|
+
this.eventEmitter.emit('stateAccountUpdate', data);
|
|
42
|
+
this.eventEmitter.emit('update');
|
|
43
|
+
});
|
|
44
|
+
// set initial data to avoid spamming getAccountInfo calls in webSocketAccountSubscriber
|
|
45
|
+
await this.setInitialData();
|
|
46
|
+
// subscribe to perp + spot markets (separate) and oracles
|
|
47
|
+
await Promise.all([
|
|
48
|
+
this.subscribeToPerpMarketAccounts(),
|
|
49
|
+
this.subscribeToSpotMarketAccounts(),
|
|
50
|
+
this.subscribeToOracles(),
|
|
51
|
+
]);
|
|
52
|
+
this.eventEmitter.emit('update');
|
|
53
|
+
await this.handleDelistedMarkets();
|
|
54
|
+
await Promise.all([this.setPerpOracleMap(), this.setSpotOracleMap()]);
|
|
55
|
+
this.subscriptionPromiseResolver(true);
|
|
56
|
+
this.isSubscribing = false;
|
|
57
|
+
this.isSubscribed = true;
|
|
58
|
+
// delete initial data
|
|
59
|
+
this.removeInitialData();
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
async subscribeToPerpMarketAccounts() {
|
|
63
|
+
const perpMarketPubkeys = await Promise.all(this.perpMarketIndexes.map((marketIndex) => (0, pda_1.getPerpMarketPublicKey)(this.program.programId, marketIndex)));
|
|
64
|
+
this.perpMarketsSubscriber =
|
|
65
|
+
await grpcMultiAccountSubscriber_1.grpcMultiAccountSubscriber.create(this.grpcConfigs, 'perpMarket', this.program, undefined, this.resubOpts, undefined, async () => {
|
|
66
|
+
var _a;
|
|
67
|
+
try {
|
|
68
|
+
if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.logResubMessages) {
|
|
69
|
+
console.log('[grpcDriftClientAccountSubscriberV2] perp markets subscriber unsubscribed; resubscribing');
|
|
70
|
+
}
|
|
71
|
+
await this.subscribeToPerpMarketAccounts();
|
|
72
|
+
}
|
|
73
|
+
catch (e) {
|
|
74
|
+
console.error('Perp markets resubscribe failed:', e);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
await this.perpMarketsSubscriber.subscribe(perpMarketPubkeys, (_accountId, data) => {
|
|
78
|
+
this.eventEmitter.emit('perpMarketAccountUpdate', data);
|
|
79
|
+
this.eventEmitter.emit('update');
|
|
80
|
+
});
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
async subscribeToSpotMarketAccounts() {
|
|
84
|
+
const spotMarketPubkeys = await Promise.all(this.spotMarketIndexes.map((marketIndex) => (0, pda_1.getSpotMarketPublicKey)(this.program.programId, marketIndex)));
|
|
85
|
+
this.spotMarketsSubscriber =
|
|
86
|
+
await grpcMultiAccountSubscriber_1.grpcMultiAccountSubscriber.create(this.grpcConfigs, 'spotMarket', this.program, undefined, this.resubOpts, undefined, async () => {
|
|
87
|
+
var _a;
|
|
88
|
+
try {
|
|
89
|
+
if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.logResubMessages) {
|
|
90
|
+
console.log('[grpcDriftClientAccountSubscriberV2] spot markets subscriber unsubscribed; resubscribing');
|
|
91
|
+
}
|
|
92
|
+
await this.subscribeToSpotMarketAccounts();
|
|
93
|
+
}
|
|
94
|
+
catch (e) {
|
|
95
|
+
console.error('Spot markets resubscribe failed:', e);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
await this.spotMarketsSubscriber.subscribe(spotMarketPubkeys, (_accountId, data) => {
|
|
99
|
+
this.eventEmitter.emit('spotMarketAccountUpdate', data);
|
|
100
|
+
this.eventEmitter.emit('update');
|
|
101
|
+
});
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
async subscribeToOracles() {
|
|
105
|
+
// Build list of unique oracle pubkeys and a lookup for sources
|
|
106
|
+
const uniqueOraclePubkeys = new Map();
|
|
107
|
+
for (const info of this.oracleInfos) {
|
|
108
|
+
const id = (0, oracleId_1.getOracleId)(info.publicKey, info.source);
|
|
109
|
+
if (!uniqueOraclePubkeys.has(id) &&
|
|
110
|
+
!info.publicKey.equals(web3_js_1.PublicKey.default)) {
|
|
111
|
+
uniqueOraclePubkeys.set(id, info);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const oraclePubkeys = Array.from(uniqueOraclePubkeys.values()).map((i) => i.publicKey);
|
|
115
|
+
const pubkeyToSource = new Map(Array.from(uniqueOraclePubkeys.values()).map((i) => [
|
|
116
|
+
i.publicKey.toBase58(),
|
|
117
|
+
i.source,
|
|
118
|
+
]));
|
|
119
|
+
this.oracleMultiSubscriber =
|
|
120
|
+
await grpcMultiAccountSubscriber_1.grpcMultiAccountSubscriber.create(this.grpcConfigs, 'oracle', this.program, (buffer, pubkey) => {
|
|
121
|
+
if (!pubkey) {
|
|
122
|
+
throw new Error('Oracle pubkey missing in decode');
|
|
123
|
+
}
|
|
124
|
+
const source = pubkeyToSource.get(pubkey);
|
|
125
|
+
const client = this.oracleClientCache.get(source, this.program.provider.connection, this.program);
|
|
126
|
+
return client.getOraclePriceDataFromBuffer(buffer);
|
|
127
|
+
}, this.resubOpts, undefined, async () => {
|
|
128
|
+
var _a;
|
|
129
|
+
try {
|
|
130
|
+
if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.logResubMessages) {
|
|
131
|
+
console.log('[grpcDriftClientAccountSubscriberV2] oracle subscriber unsubscribed; resubscribing');
|
|
132
|
+
}
|
|
133
|
+
await this.subscribeToOracles();
|
|
134
|
+
}
|
|
135
|
+
catch (e) {
|
|
136
|
+
console.error('Oracle resubscribe failed:', e);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
await this.oracleMultiSubscriber.subscribe(oraclePubkeys, (accountId, data) => {
|
|
140
|
+
const source = pubkeyToSource.get(accountId.toBase58());
|
|
141
|
+
this.eventEmitter.emit('oraclePriceUpdate', accountId, source, data);
|
|
142
|
+
this.eventEmitter.emit('update');
|
|
143
|
+
});
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
async unsubscribeFromOracles() {
|
|
147
|
+
if (this.oracleMultiSubscriber) {
|
|
148
|
+
await this.oracleMultiSubscriber.unsubscribe();
|
|
149
|
+
this.oracleMultiSubscriber = undefined;
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
await super.unsubscribeFromOracles();
|
|
153
|
+
}
|
|
154
|
+
async unsubscribe() {
|
|
155
|
+
if (this.isSubscribed) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
await this.stateAccountSubscriber.unsubscribe();
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
exports.grpcDriftClientAccountSubscriberV2 = grpcDriftClientAccountSubscriberV2;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { Program } from '@coral-xyz/anchor';
|
|
4
|
+
import { Context, PublicKey } from '@solana/web3.js';
|
|
5
|
+
import { Client } from '../isomorphic/grpc';
|
|
6
|
+
import { GrpcConfigs, ResubOpts } from './types';
|
|
7
|
+
export declare class grpcMultiAccountSubscriber<T> {
|
|
8
|
+
private client;
|
|
9
|
+
private stream;
|
|
10
|
+
private commitmentLevel;
|
|
11
|
+
private program;
|
|
12
|
+
private accountName;
|
|
13
|
+
private decodeBufferFn?;
|
|
14
|
+
private resubOpts?;
|
|
15
|
+
private onUnsubscribe?;
|
|
16
|
+
listenerId?: number;
|
|
17
|
+
isUnsubscribing: boolean;
|
|
18
|
+
private timeoutId?;
|
|
19
|
+
private receivingData;
|
|
20
|
+
private subscribedAccounts;
|
|
21
|
+
private onChangeMap;
|
|
22
|
+
private constructor();
|
|
23
|
+
static create<U>(grpcConfigs: GrpcConfigs, accountName: string, program: Program, decodeBuffer?: (buffer: Buffer, pubkey?: string) => U, resubOpts?: ResubOpts, clientProp?: Client, onUnsubscribe?: () => Promise<void>): Promise<grpcMultiAccountSubscriber<U>>;
|
|
24
|
+
subscribe(accounts: PublicKey[], onChange: (accountId: PublicKey, data: T, context: Context, buffer: Buffer) => void): Promise<void>;
|
|
25
|
+
addAccounts(accounts: PublicKey[]): Promise<void>;
|
|
26
|
+
removeAccounts(accounts: PublicKey[]): Promise<void>;
|
|
27
|
+
unsubscribe(): Promise<void>;
|
|
28
|
+
private setTimeout;
|
|
29
|
+
private capitalize;
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=grpcMultiAccountSubscriber.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grpcMultiAccountSubscriber.d.ts","sourceRoot":"","sources":["../../../src/accounts/grpcMultiAccountSubscriber.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAIrD,OAAO,EACN,MAAM,EAMN,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAUjD,qBAAa,0BAA0B,CAAC,CAAC;IACxC,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,MAAM,CAAwD;IACtE,OAAO,CAAC,eAAe,CAAkB;IACzC,OAAO,CAAC,OAAO,CAAU;IACzB,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,cAAc,CAAC,CAAyC;IAChE,OAAO,CAAC,SAAS,CAAC,CAAY;IAC9B,OAAO,CAAC,aAAa,CAAC,CAAsB;IAErC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,eAAe,UAAS;IAC/B,OAAO,CAAC,SAAS,CAAC,CAAgC;IAClD,OAAO,CAAC,aAAa,CAAS;IAE9B,OAAO,CAAC,kBAAkB,CAAqB;IAC/C,OAAO,CAAC,WAAW,CAGf;IAEJ,OAAO;WAkBa,MAAM,CAAC,CAAC,EAC3B,WAAW,EAAE,WAAW,EACxB,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE,OAAO,EAChB,YAAY,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,KAAK,CAAC,EACrD,SAAS,CAAC,EAAE,SAAS,EACrB,UAAU,CAAC,EAAE,MAAM,EACnB,aAAa,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GACjC,OAAO,CAAC,0BAA0B,CAAC,CAAC,CAAC,CAAC;IAuBnC,SAAS,CACd,QAAQ,EAAE,SAAS,EAAE,EACrB,QAAQ,EAAE,CACT,SAAS,EAAE,SAAS,EACpB,IAAI,EAAE,CAAC,EACP,OAAO,EAAE,OAAO,EAChB,MAAM,EAAE,MAAM,KACV,IAAI,GACP,OAAO,CAAC,IAAI,CAAC;IA+FV,WAAW,CAAC,QAAQ,EAAE,SAAS,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAiCjD,cAAc,CAAC,QAAQ,EAAE,SAAS,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAmCpD,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;IA4ClC,OAAO,CAAC,UAAU;IAelB,OAAO,CAAC,UAAU;CAIlB"}
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.grpcMultiAccountSubscriber = void 0;
|
|
30
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
31
|
+
const Buffer = __importStar(require("buffer"));
|
|
32
|
+
const bs58_1 = __importDefault(require("bs58"));
|
|
33
|
+
const grpc_1 = require("../isomorphic/grpc");
|
|
34
|
+
class grpcMultiAccountSubscriber {
|
|
35
|
+
constructor(client, commitmentLevel, accountName, program, decodeBuffer, resubOpts, onUnsubscribe) {
|
|
36
|
+
this.isUnsubscribing = false;
|
|
37
|
+
this.receivingData = false;
|
|
38
|
+
this.subscribedAccounts = new Set();
|
|
39
|
+
this.onChangeMap = new Map();
|
|
40
|
+
this.client = client;
|
|
41
|
+
this.commitmentLevel = commitmentLevel;
|
|
42
|
+
this.accountName = accountName;
|
|
43
|
+
this.program = program;
|
|
44
|
+
this.decodeBufferFn = decodeBuffer;
|
|
45
|
+
this.resubOpts = resubOpts;
|
|
46
|
+
this.onUnsubscribe = onUnsubscribe;
|
|
47
|
+
}
|
|
48
|
+
static async create(grpcConfigs, accountName, program, decodeBuffer, resubOpts, clientProp, onUnsubscribe) {
|
|
49
|
+
var _a, _b;
|
|
50
|
+
const client = clientProp
|
|
51
|
+
? clientProp
|
|
52
|
+
: await (0, grpc_1.createClient)(grpcConfigs.endpoint, grpcConfigs.token, (_a = grpcConfigs.channelOptions) !== null && _a !== void 0 ? _a : {});
|
|
53
|
+
const commitmentLevel =
|
|
54
|
+
// @ts-ignore :: isomorphic exported enum fails typescript but will work at runtime
|
|
55
|
+
(_b = grpcConfigs.commitmentLevel) !== null && _b !== void 0 ? _b : grpc_1.CommitmentLevel.CONFIRMED;
|
|
56
|
+
return new grpcMultiAccountSubscriber(client, commitmentLevel, accountName, program, decodeBuffer, resubOpts, onUnsubscribe);
|
|
57
|
+
}
|
|
58
|
+
async subscribe(accounts, onChange) {
|
|
59
|
+
if (this.listenerId != null || this.isUnsubscribing) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
// Track accounts and single onChange for all
|
|
63
|
+
for (const pk of accounts) {
|
|
64
|
+
const key = pk.toBase58();
|
|
65
|
+
this.subscribedAccounts.add(key);
|
|
66
|
+
this.onChangeMap.set(key, (data, ctx, buffer) => onChange(new web3_js_1.PublicKey(key), data, ctx, buffer));
|
|
67
|
+
}
|
|
68
|
+
this.stream =
|
|
69
|
+
(await this.client.subscribe());
|
|
70
|
+
const request = {
|
|
71
|
+
slots: {},
|
|
72
|
+
accounts: {
|
|
73
|
+
account: {
|
|
74
|
+
account: accounts.map((a) => a.toBase58()),
|
|
75
|
+
owner: [],
|
|
76
|
+
filters: [],
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
transactions: {},
|
|
80
|
+
blocks: {},
|
|
81
|
+
blocksMeta: {},
|
|
82
|
+
accountsDataSlice: [],
|
|
83
|
+
commitment: this.commitmentLevel,
|
|
84
|
+
entry: {},
|
|
85
|
+
transactionsStatus: {},
|
|
86
|
+
};
|
|
87
|
+
this.stream.on('data', (chunk) => {
|
|
88
|
+
var _a;
|
|
89
|
+
if (!chunk.account) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const slot = Number(chunk.account.slot);
|
|
93
|
+
const accountPubkeyBytes = chunk.account.account.pubkey;
|
|
94
|
+
const accountPubkey = bs58_1.default.encode(accountPubkeyBytes);
|
|
95
|
+
if (!accountPubkey || !this.subscribedAccounts.has(accountPubkey)) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
const accountInfo = {
|
|
99
|
+
owner: new web3_js_1.PublicKey(chunk.account.account.owner),
|
|
100
|
+
lamports: Number(chunk.account.account.lamports),
|
|
101
|
+
data: Buffer.Buffer.from(chunk.account.account.data),
|
|
102
|
+
executable: chunk.account.account.executable,
|
|
103
|
+
rentEpoch: Number(chunk.account.account.rentEpoch),
|
|
104
|
+
};
|
|
105
|
+
const context = { slot };
|
|
106
|
+
const buffer = accountInfo.data;
|
|
107
|
+
const data = this.decodeBufferFn
|
|
108
|
+
? this.decodeBufferFn(buffer, accountPubkey)
|
|
109
|
+
: this.program.account[this.accountName].coder.accounts.decode(this.capitalize(this.accountName), buffer);
|
|
110
|
+
const handler = this.onChangeMap.get(accountPubkey);
|
|
111
|
+
if (handler) {
|
|
112
|
+
if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs) {
|
|
113
|
+
this.receivingData = true;
|
|
114
|
+
clearTimeout(this.timeoutId);
|
|
115
|
+
handler(data, context, buffer);
|
|
116
|
+
this.setTimeout();
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
handler(data, context, buffer);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
return new Promise((resolve, reject) => {
|
|
124
|
+
this.stream.write(request, (err) => {
|
|
125
|
+
var _a;
|
|
126
|
+
if (err === null || err === undefined) {
|
|
127
|
+
this.listenerId = 1;
|
|
128
|
+
if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs) {
|
|
129
|
+
this.receivingData = true;
|
|
130
|
+
this.setTimeout();
|
|
131
|
+
}
|
|
132
|
+
resolve();
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
reject(err);
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
}).catch((reason) => {
|
|
139
|
+
console.error(reason);
|
|
140
|
+
throw reason;
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
async addAccounts(accounts) {
|
|
144
|
+
for (const pk of accounts) {
|
|
145
|
+
this.subscribedAccounts.add(pk.toBase58());
|
|
146
|
+
}
|
|
147
|
+
const request = {
|
|
148
|
+
slots: {},
|
|
149
|
+
accounts: {
|
|
150
|
+
account: {
|
|
151
|
+
account: Array.from(this.subscribedAccounts.values()),
|
|
152
|
+
owner: [],
|
|
153
|
+
filters: [],
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
transactions: {},
|
|
157
|
+
blocks: {},
|
|
158
|
+
blocksMeta: {},
|
|
159
|
+
accountsDataSlice: [],
|
|
160
|
+
commitment: this.commitmentLevel,
|
|
161
|
+
entry: {},
|
|
162
|
+
transactionsStatus: {},
|
|
163
|
+
};
|
|
164
|
+
await new Promise((resolve, reject) => {
|
|
165
|
+
this.stream.write(request, (err) => {
|
|
166
|
+
if (err === null || err === undefined) {
|
|
167
|
+
resolve();
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
reject(err);
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
async removeAccounts(accounts) {
|
|
176
|
+
for (const pk of accounts) {
|
|
177
|
+
const k = pk.toBase58();
|
|
178
|
+
this.subscribedAccounts.delete(k);
|
|
179
|
+
this.onChangeMap.delete(k);
|
|
180
|
+
}
|
|
181
|
+
const request = {
|
|
182
|
+
slots: {},
|
|
183
|
+
accounts: {
|
|
184
|
+
account: {
|
|
185
|
+
account: Array.from(this.subscribedAccounts.values()),
|
|
186
|
+
owner: [],
|
|
187
|
+
filters: [],
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
transactions: {},
|
|
191
|
+
blocks: {},
|
|
192
|
+
blocksMeta: {},
|
|
193
|
+
accountsDataSlice: [],
|
|
194
|
+
commitment: this.commitmentLevel,
|
|
195
|
+
entry: {},
|
|
196
|
+
transactionsStatus: {},
|
|
197
|
+
};
|
|
198
|
+
await new Promise((resolve, reject) => {
|
|
199
|
+
this.stream.write(request, (err) => {
|
|
200
|
+
if (err === null || err === undefined) {
|
|
201
|
+
resolve();
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
reject(err);
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
async unsubscribe() {
|
|
210
|
+
this.isUnsubscribing = true;
|
|
211
|
+
clearTimeout(this.timeoutId);
|
|
212
|
+
this.timeoutId = undefined;
|
|
213
|
+
if (this.listenerId != null) {
|
|
214
|
+
const promise = new Promise((resolve, reject) => {
|
|
215
|
+
const request = {
|
|
216
|
+
slots: {},
|
|
217
|
+
accounts: {},
|
|
218
|
+
transactions: {},
|
|
219
|
+
blocks: {},
|
|
220
|
+
blocksMeta: {},
|
|
221
|
+
accountsDataSlice: [],
|
|
222
|
+
entry: {},
|
|
223
|
+
transactionsStatus: {},
|
|
224
|
+
};
|
|
225
|
+
this.stream.write(request, (err) => {
|
|
226
|
+
if (err === null || err === undefined) {
|
|
227
|
+
this.listenerId = undefined;
|
|
228
|
+
this.isUnsubscribing = false;
|
|
229
|
+
resolve();
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
reject(err);
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
}).catch((reason) => {
|
|
236
|
+
console.error(reason);
|
|
237
|
+
throw reason;
|
|
238
|
+
});
|
|
239
|
+
return promise;
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
this.isUnsubscribing = false;
|
|
243
|
+
}
|
|
244
|
+
if (this.onUnsubscribe) {
|
|
245
|
+
try {
|
|
246
|
+
await this.onUnsubscribe();
|
|
247
|
+
}
|
|
248
|
+
catch (e) {
|
|
249
|
+
console.error(e);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
setTimeout() {
|
|
254
|
+
var _a;
|
|
255
|
+
this.timeoutId = setTimeout(async () => {
|
|
256
|
+
if (this.isUnsubscribing) {
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
if (this.receivingData) {
|
|
260
|
+
await this.unsubscribe();
|
|
261
|
+
this.receivingData = false;
|
|
262
|
+
}
|
|
263
|
+
}, (_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs);
|
|
264
|
+
}
|
|
265
|
+
capitalize(value) {
|
|
266
|
+
if (!value)
|
|
267
|
+
return value;
|
|
268
|
+
return value.charAt(0).toUpperCase() + value.slice(1);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
exports.grpcMultiAccountSubscriber = grpcMultiAccountSubscriber;
|