@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.
Files changed (53) hide show
  1. package/VERSION +1 -1
  2. package/lib/accounts/grpcAccountSubscriber.d.ts +16 -0
  3. package/lib/accounts/grpcAccountSubscriber.js +155 -0
  4. package/lib/accounts/grpcDriftClientAccountSubscriber.d.ts +13 -0
  5. package/lib/accounts/grpcDriftClientAccountSubscriber.js +96 -0
  6. package/lib/accounts/grpcInsuranceFundStakeAccountSubscriber.d.ts +10 -0
  7. package/lib/accounts/grpcInsuranceFundStakeAccountSubscriber.js +30 -0
  8. package/lib/accounts/grpcProgramAccountSubscriber.d.ts +19 -0
  9. package/lib/accounts/grpcProgramAccountSubscriber.js +161 -0
  10. package/lib/accounts/grpcUserAccountSubscriber.d.ts +10 -0
  11. package/lib/accounts/grpcUserAccountSubscriber.js +28 -0
  12. package/lib/accounts/grpcUserStatsAccountSubscriber.d.ts +10 -0
  13. package/lib/accounts/grpcUserStatsAccountSubscriber.js +28 -0
  14. package/lib/accounts/types.d.ts +8 -0
  15. package/lib/accounts/webSocketAccountSubscriber.d.ts +1 -1
  16. package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +3 -3
  17. package/lib/accounts/webSocketProgramAccountSubscriber.d.ts +1 -1
  18. package/lib/driftClient.js +35 -14
  19. package/lib/driftClientConfig.d.ts +6 -0
  20. package/lib/orderSubscriber/OrderSubscriber.d.ts +2 -1
  21. package/lib/orderSubscriber/OrderSubscriber.js +19 -4
  22. package/lib/orderSubscriber/grpcSubscription.d.ts +25 -0
  23. package/lib/orderSubscriber/grpcSubscription.js +68 -0
  24. package/lib/orderSubscriber/types.d.ts +9 -0
  25. package/lib/user.js +11 -4
  26. package/lib/userConfig.d.ts +6 -1
  27. package/lib/userMap/grpcSubscription.d.ts +26 -0
  28. package/lib/userMap/grpcSubscription.js +42 -0
  29. package/lib/userMap/userMap.js +14 -0
  30. package/lib/userMap/userMapConfig.d.ts +7 -0
  31. package/lib/userStatsConfig.d.ts +6 -0
  32. package/package.json +3 -1
  33. package/src/accounts/grpcAccountSubscriber.ts +158 -0
  34. package/src/accounts/grpcDriftClientAccountSubscriber.ts +196 -0
  35. package/src/accounts/grpcInsuranceFundStakeAccountSubscriber.ts +62 -0
  36. package/src/accounts/grpcProgramAccountSubscriber.ts +181 -0
  37. package/src/accounts/grpcUserAccountSubscriber.ts +48 -0
  38. package/src/accounts/grpcUserStatsAccountSubscriber.ts +51 -0
  39. package/src/accounts/types.ts +9 -0
  40. package/src/accounts/webSocketAccountSubscriber.ts +1 -1
  41. package/src/accounts/webSocketDriftClientAccountSubscriber.ts +3 -3
  42. package/src/accounts/webSocketProgramAccountSubscriber.ts +1 -1
  43. package/src/driftClient.ts +28 -0
  44. package/src/driftClientConfig.ts +7 -0
  45. package/src/orderSubscriber/OrderSubscriber.ts +15 -1
  46. package/src/orderSubscriber/grpcSubscription.ts +126 -0
  47. package/src/orderSubscriber/types.ts +10 -0
  48. package/src/user.ts +11 -0
  49. package/src/userConfig.ts +7 -1
  50. package/src/userMap/grpcSubscription.ts +83 -0
  51. package/src/userMap/userMap.ts +17 -1
  52. package/src/userMap/userMapConfig.ts +8 -0
  53. package/src/userStatsConfig.ts +7 -0
package/VERSION CHANGED
@@ -1 +1 @@
1
- 2.96.0-beta.0
1
+ 2.96.0-beta.1
@@ -0,0 +1,16 @@
1
+ /// <reference types="node" />
2
+ import { ResubOpts, GrpcConfigs } from './types';
3
+ import { Program } from '@coral-xyz/anchor';
4
+ import { PublicKey } from '@solana/web3.js';
5
+ import { ClientDuplexStream } from '@grpc/grpc-js';
6
+ import Client, { CommitmentLevel, SubscribeRequest, SubscribeUpdate } from '@triton-one/yellowstone-grpc';
7
+ import { WebSocketAccountSubscriber } from './webSocketAccountSubscriber';
8
+ export declare class grpcAccountSubscriber<T> extends WebSocketAccountSubscriber<T> {
9
+ client: Client;
10
+ stream: ClientDuplexStream<SubscribeRequest, SubscribeUpdate>;
11
+ commitmentLevel: CommitmentLevel;
12
+ listenerId?: number;
13
+ constructor(grpcConfigs: GrpcConfigs, accountName: string, program: Program, accountPublicKey: PublicKey, decodeBuffer?: (buffer: Buffer) => T, resubOpts?: ResubOpts);
14
+ subscribe(onChange: (data: T) => void): Promise<void>;
15
+ unsubscribe(onResub?: boolean): Promise<void>;
16
+ }
@@ -0,0 +1,155 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.grpcAccountSubscriber = void 0;
27
+ const web3_js_1 = require("@solana/web3.js");
28
+ const Buffer = __importStar(require("buffer"));
29
+ const yellowstone_grpc_1 = __importStar(require("@triton-one/yellowstone-grpc"));
30
+ const webSocketAccountSubscriber_1 = require("./webSocketAccountSubscriber");
31
+ class grpcAccountSubscriber extends webSocketAccountSubscriber_1.WebSocketAccountSubscriber {
32
+ constructor(grpcConfigs, accountName, program, accountPublicKey, decodeBuffer, resubOpts) {
33
+ var _a, _b;
34
+ super(accountName, program, accountPublicKey, decodeBuffer, resubOpts);
35
+ this.client = new yellowstone_grpc_1.default(grpcConfigs.endpoint, grpcConfigs.token, (_a = grpcConfigs.channelOptions) !== null && _a !== void 0 ? _a : {});
36
+ this.commitmentLevel =
37
+ (_b = grpcConfigs.commitmentLevel) !== null && _b !== void 0 ? _b : yellowstone_grpc_1.CommitmentLevel.CONFIRMED;
38
+ }
39
+ async subscribe(onChange) {
40
+ if (this.listenerId != null || this.isUnsubscribing) {
41
+ return;
42
+ }
43
+ this.onChange = onChange;
44
+ if (!this.dataAndSlot) {
45
+ await this.fetch();
46
+ }
47
+ // Subscribe with grpc
48
+ this.stream = await this.client.subscribe();
49
+ const request = {
50
+ slots: {
51
+ slots: {},
52
+ },
53
+ accounts: {
54
+ account: {
55
+ account: [this.accountPublicKey.toString()],
56
+ owner: [],
57
+ filters: [],
58
+ },
59
+ },
60
+ transactions: {},
61
+ blocks: {},
62
+ blocksMeta: {},
63
+ accountsDataSlice: [],
64
+ commitment: this.commitmentLevel,
65
+ entry: {},
66
+ transactionsStatus: {},
67
+ };
68
+ this.stream.on('data', (chunk) => {
69
+ var _a;
70
+ if (!chunk.account) {
71
+ return;
72
+ }
73
+ const slot = Number(chunk.account.slot);
74
+ const accountInfo = {
75
+ owner: new web3_js_1.PublicKey(chunk.account.account.owner),
76
+ lamports: Number(chunk.account.account.lamports),
77
+ data: Buffer.Buffer.from(chunk.account.account.data),
78
+ executable: chunk.account.account.executable,
79
+ rentEpoch: Number(chunk.account.account.rentEpoch),
80
+ };
81
+ if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs) {
82
+ this.receivingData = true;
83
+ clearTimeout(this.timeoutId);
84
+ this.handleRpcResponse({
85
+ slot,
86
+ }, accountInfo);
87
+ this.setTimeout();
88
+ }
89
+ else {
90
+ this.handleRpcResponse({
91
+ slot,
92
+ }, accountInfo);
93
+ }
94
+ });
95
+ return new Promise((resolve, reject) => {
96
+ this.stream.write(request, (err) => {
97
+ var _a;
98
+ if (err === null || err === undefined) {
99
+ this.listenerId = 1;
100
+ if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs) {
101
+ this.receivingData = true;
102
+ this.setTimeout();
103
+ }
104
+ resolve();
105
+ }
106
+ else {
107
+ reject(err);
108
+ }
109
+ });
110
+ }).catch((reason) => {
111
+ console.error(reason);
112
+ throw reason;
113
+ });
114
+ }
115
+ async unsubscribe(onResub = false) {
116
+ if (!onResub && this.resubOpts) {
117
+ this.resubOpts.resubTimeoutMs = undefined;
118
+ }
119
+ this.isUnsubscribing = true;
120
+ clearTimeout(this.timeoutId);
121
+ this.timeoutId = undefined;
122
+ if (this.listenerId != null) {
123
+ const promise = new Promise((resolve, reject) => {
124
+ const request = {
125
+ slots: {},
126
+ accounts: {},
127
+ transactions: {},
128
+ blocks: {},
129
+ blocksMeta: {},
130
+ accountsDataSlice: [],
131
+ entry: {},
132
+ transactionsStatus: {},
133
+ };
134
+ this.stream.write(request, (err) => {
135
+ if (err === null || err === undefined) {
136
+ this.listenerId = undefined;
137
+ this.isUnsubscribing = false;
138
+ resolve();
139
+ }
140
+ else {
141
+ reject(err);
142
+ }
143
+ });
144
+ }).catch((reason) => {
145
+ console.error(reason);
146
+ throw reason;
147
+ });
148
+ return promise;
149
+ }
150
+ else {
151
+ this.isUnsubscribing = false;
152
+ }
153
+ }
154
+ }
155
+ exports.grpcAccountSubscriber = grpcAccountSubscriber;
@@ -0,0 +1,13 @@
1
+ import { WebSocketDriftClientAccountSubscriber } from './webSocketDriftClientAccountSubscriber';
2
+ import { OracleInfo } from '../oracles/types';
3
+ import { Program } from '@coral-xyz/anchor';
4
+ import { GrpcConfigs, ResubOpts } from './types';
5
+ import { Commitment } from '@solana/web3.js';
6
+ export declare class gprcDriftClientAccountSubscriber extends WebSocketDriftClientAccountSubscriber {
7
+ private grpcConfigs;
8
+ constructor(grpcConfigs: GrpcConfigs, program: Program, perpMarketIndexes: number[], spotMarketIndexes: number[], oracleInfos: OracleInfo[], shouldFindAllMarketsAndOracles: boolean, resubOpts?: ResubOpts, commitment?: Commitment);
9
+ subscribe(): Promise<boolean>;
10
+ subscribeToSpotMarketAccount(marketIndex: number): Promise<boolean>;
11
+ subscribeToPerpMarketAccount(marketIndex: number): Promise<boolean>;
12
+ subscribeToOracle(oracleInfo: OracleInfo): Promise<boolean>;
13
+ }
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.gprcDriftClientAccountSubscriber = void 0;
4
+ const webSocketDriftClientAccountSubscriber_1 = require("./webSocketDriftClientAccountSubscriber");
5
+ const config_1 = require("../config");
6
+ const pda_1 = require("../addresses/pda");
7
+ const grpcAccountSubscriber_1 = require("./grpcAccountSubscriber");
8
+ class gprcDriftClientAccountSubscriber extends webSocketDriftClientAccountSubscriber_1.WebSocketDriftClientAccountSubscriber {
9
+ constructor(grpcConfigs, program, perpMarketIndexes, spotMarketIndexes, oracleInfos, shouldFindAllMarketsAndOracles, resubOpts, commitment) {
10
+ super(program, perpMarketIndexes, spotMarketIndexes, oracleInfos, shouldFindAllMarketsAndOracles, resubOpts, commitment);
11
+ this.grpcConfigs = grpcConfigs;
12
+ }
13
+ async subscribe() {
14
+ if (this.isSubscribed) {
15
+ return true;
16
+ }
17
+ if (this.isSubscribing) {
18
+ return await this.subscriptionPromise;
19
+ }
20
+ this.isSubscribing = true;
21
+ this.subscriptionPromise = new Promise((res) => {
22
+ this.subscriptionPromiseResolver = res;
23
+ });
24
+ if (this.shouldFindAllMarketsAndOracles) {
25
+ const { perpMarketIndexes, perpMarketAccounts, spotMarketIndexes, spotMarketAccounts, oracleInfos, } = await (0, config_1.findAllMarketAndOracles)(this.program);
26
+ this.perpMarketIndexes = perpMarketIndexes;
27
+ this.spotMarketIndexes = spotMarketIndexes;
28
+ this.oracleInfos = oracleInfos;
29
+ // front run and set the initial data here to save extra gma call in set initial data
30
+ this.initialPerpMarketAccountData = new Map(perpMarketAccounts.map((market) => [market.marketIndex, market]));
31
+ this.initialSpotMarketAccountData = new Map(spotMarketAccounts.map((market) => [market.marketIndex, market]));
32
+ }
33
+ const statePublicKey = await (0, pda_1.getDriftStateAccountPublicKey)(this.program.programId);
34
+ // create and activate main state account subscription
35
+ this.stateAccountSubscriber = new grpcAccountSubscriber_1.grpcAccountSubscriber(this.grpcConfigs, 'state', this.program, statePublicKey, undefined, undefined);
36
+ await this.stateAccountSubscriber.subscribe((data) => {
37
+ this.eventEmitter.emit('stateAccountUpdate', data);
38
+ this.eventEmitter.emit('update');
39
+ });
40
+ // set initial data to avoid spamming getAccountInfo calls in webSocketAccountSubscriber
41
+ await this.setInitialData();
42
+ await Promise.all([
43
+ // subscribe to market accounts
44
+ this.subscribeToPerpMarketAccounts(),
45
+ // subscribe to spot market accounts
46
+ this.subscribeToSpotMarketAccounts(),
47
+ // subscribe to oracles
48
+ this.subscribeToOracles(),
49
+ ]);
50
+ this.eventEmitter.emit('update');
51
+ await Promise.all([this.setPerpOracleMap(), this.setSpotOracleMap()]);
52
+ this.subscriptionPromiseResolver(true);
53
+ this.isSubscribing = false;
54
+ this.isSubscribed = true;
55
+ // delete initial data
56
+ this.removeInitialData();
57
+ return true;
58
+ }
59
+ async subscribeToSpotMarketAccount(marketIndex) {
60
+ const marketPublicKey = await (0, pda_1.getSpotMarketPublicKey)(this.program.programId, marketIndex);
61
+ const accountSubscriber = new grpcAccountSubscriber_1.grpcAccountSubscriber(this.grpcConfigs, 'spotMarket', this.program, marketPublicKey, undefined, this.resubOpts);
62
+ accountSubscriber.setData(this.initialSpotMarketAccountData.get(marketIndex));
63
+ await accountSubscriber.subscribe((data) => {
64
+ this.eventEmitter.emit('spotMarketAccountUpdate', data);
65
+ this.eventEmitter.emit('update');
66
+ });
67
+ this.spotMarketAccountSubscribers.set(marketIndex, accountSubscriber);
68
+ return true;
69
+ }
70
+ async subscribeToPerpMarketAccount(marketIndex) {
71
+ const perpMarketPublicKey = await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, marketIndex);
72
+ const accountSubscriber = new grpcAccountSubscriber_1.grpcAccountSubscriber(this.grpcConfigs, 'perpMarket', this.program, perpMarketPublicKey, undefined, this.resubOpts);
73
+ accountSubscriber.setData(this.initialPerpMarketAccountData.get(marketIndex));
74
+ await accountSubscriber.subscribe((data) => {
75
+ this.eventEmitter.emit('perpMarketAccountUpdate', data);
76
+ this.eventEmitter.emit('update');
77
+ });
78
+ this.perpMarketAccountSubscribers.set(marketIndex, accountSubscriber);
79
+ return true;
80
+ }
81
+ async subscribeToOracle(oracleInfo) {
82
+ const oracleString = oracleInfo.publicKey.toString();
83
+ const client = this.oracleClientCache.get(oracleInfo.source, this.program.provider.connection, this.program);
84
+ const accountSubscriber = new grpcAccountSubscriber_1.grpcAccountSubscriber(this.grpcConfigs, 'oracle', this.program, oracleInfo.publicKey, (buffer) => {
85
+ return client.getOraclePriceDataFromBuffer(buffer);
86
+ }, this.resubOpts);
87
+ accountSubscriber.setData(this.initialOraclePriceData.get(oracleString));
88
+ await accountSubscriber.subscribe((data) => {
89
+ this.eventEmitter.emit('oraclePriceUpdate', oracleInfo.publicKey, data);
90
+ this.eventEmitter.emit('update');
91
+ });
92
+ this.oracleSubscribers.set(oracleString, accountSubscriber);
93
+ return true;
94
+ }
95
+ }
96
+ exports.gprcDriftClientAccountSubscriber = gprcDriftClientAccountSubscriber;
@@ -0,0 +1,10 @@
1
+ import { GrpcConfigs } from './types';
2
+ import { Program } from '@coral-xyz/anchor';
3
+ import { Commitment, PublicKey } from '@solana/web3.js';
4
+ import { InsuranceFundStake } from '../types';
5
+ import { WebSocketInsuranceFundStakeAccountSubscriber } from './webSocketInsuranceFundStakeAccountSubscriber';
6
+ export declare class grpcInsuranceFundStakeAccountSubscriber extends WebSocketInsuranceFundStakeAccountSubscriber {
7
+ private grpcConfigs;
8
+ constructor(grpcConfigs: GrpcConfigs, program: Program, insuranceFundStakeAccountPublicKey: PublicKey, resubTimeoutMs?: number, commitment?: Commitment);
9
+ subscribe(insuranceFundStakeAccount?: InsuranceFundStake): Promise<boolean>;
10
+ }
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.grpcInsuranceFundStakeAccountSubscriber = void 0;
4
+ const webSocketInsuranceFundStakeAccountSubscriber_1 = require("./webSocketInsuranceFundStakeAccountSubscriber");
5
+ const grpcAccountSubscriber_1 = require("./grpcAccountSubscriber");
6
+ class grpcInsuranceFundStakeAccountSubscriber extends webSocketInsuranceFundStakeAccountSubscriber_1.WebSocketInsuranceFundStakeAccountSubscriber {
7
+ constructor(grpcConfigs, program, insuranceFundStakeAccountPublicKey, resubTimeoutMs, commitment) {
8
+ super(program, insuranceFundStakeAccountPublicKey, resubTimeoutMs, commitment);
9
+ this.grpcConfigs = grpcConfigs;
10
+ }
11
+ async subscribe(insuranceFundStakeAccount) {
12
+ if (this.isSubscribed) {
13
+ return true;
14
+ }
15
+ this.insuranceFundStakeDataAccountSubscriber = new grpcAccountSubscriber_1.grpcAccountSubscriber(this.grpcConfigs, 'insuranceFundStake', this.program, this.insuranceFundStakeAccountPublicKey, undefined, {
16
+ resubTimeoutMs: this.resubTimeoutMs,
17
+ });
18
+ if (insuranceFundStakeAccount) {
19
+ this.insuranceFundStakeDataAccountSubscriber.setData(insuranceFundStakeAccount);
20
+ }
21
+ await this.insuranceFundStakeDataAccountSubscriber.subscribe((data) => {
22
+ this.eventEmitter.emit('insuranceFundStakeAccountUpdate', data);
23
+ this.eventEmitter.emit('update');
24
+ });
25
+ this.eventEmitter.emit('update');
26
+ this.isSubscribed = true;
27
+ return true;
28
+ }
29
+ }
30
+ exports.grpcInsuranceFundStakeAccountSubscriber = grpcInsuranceFundStakeAccountSubscriber;
@@ -0,0 +1,19 @@
1
+ /// <reference types="node" />
2
+ import { ResubOpts, GrpcConfigs } from './types';
3
+ import { Program } from '@coral-xyz/anchor';
4
+ import { Commitment, Context, MemcmpFilter, PublicKey } from '@solana/web3.js';
5
+ import { WebSocketProgramAccountSubscriber } from './webSocketProgramAccountSubscriber';
6
+ import { ClientDuplexStream } from '@grpc/grpc-js';
7
+ import Client, { SubscribeRequest, SubscribeUpdate, CommitmentLevel } from '@triton-one/yellowstone-grpc';
8
+ export declare class grpcProgramAccountSubscriber<T> extends WebSocketProgramAccountSubscriber<T> {
9
+ client: Client;
10
+ stream: ClientDuplexStream<SubscribeRequest, SubscribeUpdate>;
11
+ commitmentLevel: CommitmentLevel;
12
+ listenerId?: number;
13
+ constructor(grpcConfigs: GrpcConfigs, subscriptionName: string, accountDiscriminator: string, program: Program, decodeBufferFn: (accountName: string, ix: Buffer) => T, options?: {
14
+ filters: MemcmpFilter[];
15
+ commitment?: Commitment;
16
+ }, resubOpts?: ResubOpts);
17
+ subscribe(onChange: (accountId: PublicKey, data: T, context: Context, buffer: Buffer) => void): Promise<void>;
18
+ unsubscribe(onResub?: boolean): Promise<void>;
19
+ }
@@ -0,0 +1,161 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.grpcProgramAccountSubscriber = void 0;
27
+ const web3_js_1 = require("@solana/web3.js");
28
+ const Buffer = __importStar(require("buffer"));
29
+ const webSocketProgramAccountSubscriber_1 = require("./webSocketProgramAccountSubscriber");
30
+ const yellowstone_grpc_1 = __importStar(require("@triton-one/yellowstone-grpc"));
31
+ class grpcProgramAccountSubscriber extends webSocketProgramAccountSubscriber_1.WebSocketProgramAccountSubscriber {
32
+ constructor(grpcConfigs, subscriptionName, accountDiscriminator, program, decodeBufferFn, options = {
33
+ filters: [],
34
+ }, resubOpts) {
35
+ var _a, _b;
36
+ super(subscriptionName, accountDiscriminator, program, decodeBufferFn, options, resubOpts);
37
+ this.client = new yellowstone_grpc_1.default(grpcConfigs.endpoint, grpcConfigs.token, (_a = grpcConfigs.channelOptions) !== null && _a !== void 0 ? _a : {});
38
+ this.commitmentLevel =
39
+ (_b = grpcConfigs.commitmentLevel) !== null && _b !== void 0 ? _b : yellowstone_grpc_1.CommitmentLevel.CONFIRMED;
40
+ }
41
+ async subscribe(onChange) {
42
+ if (this.listenerId != null || this.isUnsubscribing) {
43
+ return;
44
+ }
45
+ this.onChange = onChange;
46
+ // Subscribe with grpc
47
+ this.stream = await this.client.subscribe();
48
+ const request = {
49
+ slots: {
50
+ slots: {},
51
+ },
52
+ accounts: {
53
+ drift: {
54
+ account: [],
55
+ owner: [this.program.programId.toBase58()],
56
+ // @ts-ignore
57
+ filters: this.options.filters,
58
+ },
59
+ },
60
+ transactions: {},
61
+ blocks: {},
62
+ blocksMeta: {},
63
+ accountsDataSlice: [],
64
+ commitment: this.commitmentLevel,
65
+ entry: {},
66
+ transactionsStatus: {},
67
+ };
68
+ this.stream.on('data', (chunk) => {
69
+ var _a;
70
+ if (!chunk.account) {
71
+ return;
72
+ }
73
+ const slot = Number(chunk.account.slot);
74
+ const accountInfo = {
75
+ owner: new web3_js_1.PublicKey(chunk.account.account.owner),
76
+ lamports: Number(chunk.account.account.lamports),
77
+ data: Buffer.Buffer.from(chunk.account.account.data),
78
+ executable: chunk.account.account.executable,
79
+ rentEpoch: Number(chunk.account.account.rentEpoch),
80
+ };
81
+ if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs) {
82
+ this.receivingData = true;
83
+ clearTimeout(this.timeoutId);
84
+ this.handleRpcResponse({
85
+ slot,
86
+ }, {
87
+ accountId: new web3_js_1.PublicKey(chunk.account.account.pubkey),
88
+ accountInfo,
89
+ });
90
+ this.setTimeout();
91
+ }
92
+ else {
93
+ this.handleRpcResponse({
94
+ slot,
95
+ }, {
96
+ accountId: new web3_js_1.PublicKey(chunk.account.account.pubkey),
97
+ accountInfo,
98
+ });
99
+ }
100
+ });
101
+ return new Promise((resolve, reject) => {
102
+ this.stream.write(request, (err) => {
103
+ var _a;
104
+ if (err === null || err === undefined) {
105
+ this.listenerId = 1;
106
+ if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs) {
107
+ this.receivingData = true;
108
+ this.setTimeout();
109
+ }
110
+ resolve();
111
+ }
112
+ else {
113
+ reject(err);
114
+ }
115
+ });
116
+ }).catch((reason) => {
117
+ console.error(reason);
118
+ throw reason;
119
+ });
120
+ }
121
+ async unsubscribe(onResub = false) {
122
+ if (!onResub && this.resubOpts) {
123
+ this.resubOpts.resubTimeoutMs = undefined;
124
+ }
125
+ this.isUnsubscribing = true;
126
+ clearTimeout(this.timeoutId);
127
+ this.timeoutId = undefined;
128
+ if (this.listenerId != null) {
129
+ const promise = new Promise((resolve, reject) => {
130
+ const request = {
131
+ slots: {},
132
+ accounts: {},
133
+ transactions: {},
134
+ blocks: {},
135
+ blocksMeta: {},
136
+ accountsDataSlice: [],
137
+ entry: {},
138
+ transactionsStatus: {},
139
+ };
140
+ this.stream.write(request, (err) => {
141
+ if (err === null || err === undefined) {
142
+ this.listenerId = undefined;
143
+ this.isUnsubscribing = false;
144
+ resolve();
145
+ }
146
+ else {
147
+ reject(err);
148
+ }
149
+ });
150
+ }).catch((reason) => {
151
+ console.error(reason);
152
+ throw reason;
153
+ });
154
+ return promise;
155
+ }
156
+ else {
157
+ this.isUnsubscribing = false;
158
+ }
159
+ }
160
+ }
161
+ exports.grpcProgramAccountSubscriber = grpcProgramAccountSubscriber;
@@ -0,0 +1,10 @@
1
+ import { ResubOpts, GrpcConfigs } from './types';
2
+ import { Program } from '@coral-xyz/anchor';
3
+ import { PublicKey } from '@solana/web3.js';
4
+ import { UserAccount } from '../types';
5
+ import { WebSocketUserAccountSubscriber } from './webSocketUserAccountSubscriber';
6
+ export declare class grpcUserAccountSubscriber extends WebSocketUserAccountSubscriber {
7
+ private grpcConfigs;
8
+ constructor(grpcConfigs: GrpcConfigs, program: Program, userAccountPublicKey: PublicKey, resubOpts?: ResubOpts);
9
+ subscribe(userAccount?: UserAccount): Promise<boolean>;
10
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.grpcUserAccountSubscriber = void 0;
4
+ const webSocketUserAccountSubscriber_1 = require("./webSocketUserAccountSubscriber");
5
+ const grpcAccountSubscriber_1 = require("./grpcAccountSubscriber");
6
+ class grpcUserAccountSubscriber extends webSocketUserAccountSubscriber_1.WebSocketUserAccountSubscriber {
7
+ constructor(grpcConfigs, program, userAccountPublicKey, resubOpts) {
8
+ super(program, userAccountPublicKey, resubOpts);
9
+ this.grpcConfigs = grpcConfigs;
10
+ }
11
+ async subscribe(userAccount) {
12
+ if (this.isSubscribed) {
13
+ return true;
14
+ }
15
+ this.userDataAccountSubscriber = new grpcAccountSubscriber_1.grpcAccountSubscriber(this.grpcConfigs, 'user', this.program, this.userAccountPublicKey, undefined, this.resubOpts);
16
+ if (userAccount) {
17
+ this.userDataAccountSubscriber.setData(userAccount);
18
+ }
19
+ await this.userDataAccountSubscriber.subscribe((data) => {
20
+ this.eventEmitter.emit('userAccountUpdate', data);
21
+ this.eventEmitter.emit('update');
22
+ });
23
+ this.eventEmitter.emit('update');
24
+ this.isSubscribed = true;
25
+ return true;
26
+ }
27
+ }
28
+ exports.grpcUserAccountSubscriber = grpcUserAccountSubscriber;
@@ -0,0 +1,10 @@
1
+ import { ResubOpts, GrpcConfigs } from './types';
2
+ import { Program } from '@coral-xyz/anchor';
3
+ import { Commitment, PublicKey } from '@solana/web3.js';
4
+ import { UserStatsAccount } from '../types';
5
+ import { WebSocketUserStatsAccountSubscriber } from './webSocketUserStatsAccountSubsriber';
6
+ export declare class grpcUserStatsAccountSubscriber extends WebSocketUserStatsAccountSubscriber {
7
+ private grpcConfigs;
8
+ constructor(grpcConfigs: GrpcConfigs, program: Program, userStatsAccountPublicKey: PublicKey, resubOpts?: ResubOpts, commitment?: Commitment);
9
+ subscribe(userStatsAccount?: UserStatsAccount): Promise<boolean>;
10
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.grpcUserStatsAccountSubscriber = void 0;
4
+ const webSocketUserStatsAccountSubsriber_1 = require("./webSocketUserStatsAccountSubsriber");
5
+ const grpcAccountSubscriber_1 = require("./grpcAccountSubscriber");
6
+ class grpcUserStatsAccountSubscriber extends webSocketUserStatsAccountSubsriber_1.WebSocketUserStatsAccountSubscriber {
7
+ constructor(grpcConfigs, program, userStatsAccountPublicKey, resubOpts, commitment) {
8
+ super(program, userStatsAccountPublicKey, resubOpts, commitment);
9
+ this.grpcConfigs = grpcConfigs;
10
+ }
11
+ async subscribe(userStatsAccount) {
12
+ if (this.isSubscribed) {
13
+ return true;
14
+ }
15
+ this.userStatsAccountSubscriber = new grpcAccountSubscriber_1.grpcAccountSubscriber(this.grpcConfigs, 'userStats', this.program, this.userStatsAccountPublicKey, undefined, this.resubOpts);
16
+ if (userStatsAccount) {
17
+ this.userStatsAccountSubscriber.setData(userStatsAccount);
18
+ }
19
+ await this.userStatsAccountSubscriber.subscribe((data) => {
20
+ this.eventEmitter.emit('userStatsAccountUpdate', data);
21
+ this.eventEmitter.emit('update');
22
+ });
23
+ this.eventEmitter.emit('update');
24
+ this.isSubscribed = true;
25
+ return true;
26
+ }
27
+ }
28
+ exports.grpcUserStatsAccountSubscriber = grpcUserStatsAccountSubscriber;
@@ -6,6 +6,8 @@ import { EventEmitter } from 'events';
6
6
  import { Context, PublicKey } from '@solana/web3.js';
7
7
  import { Account } from '@solana/spl-token';
8
8
  import { OracleInfo, OraclePriceData } from '..';
9
+ import { CommitmentLevel } from '@triton-one/yellowstone-grpc';
10
+ import { ChannelOptions } from '@grpc/grpc-js';
9
11
  export interface AccountSubscriber<T> {
10
12
  dataAndSlot?: DataAndSlot<T>;
11
13
  subscribe(onChange: (data: T) => void): Promise<void>;
@@ -140,3 +142,9 @@ export interface UserStatsAccountSubscriber {
140
142
  unsubscribe(): Promise<void>;
141
143
  getUserStatsAccountAndSlot(): DataAndSlot<UserStatsAccount>;
142
144
  }
145
+ export type GrpcConfigs = {
146
+ endpoint: string;
147
+ token: string;
148
+ commitmentLevel?: CommitmentLevel;
149
+ channelOptions?: ChannelOptions;
150
+ };
@@ -20,7 +20,7 @@ export declare class WebSocketAccountSubscriber<T> implements AccountSubscriber<
20
20
  constructor(accountName: string, program: Program, accountPublicKey: PublicKey, decodeBuffer?: (buffer: Buffer) => T, resubOpts?: ResubOpts, commitment?: Commitment);
21
21
  subscribe(onChange: (data: T) => void): Promise<void>;
22
22
  setData(data: T, slot?: number): void;
23
- private setTimeout;
23
+ protected setTimeout(): void;
24
24
  fetch(): Promise<void>;
25
25
  handleRpcResponse(context: Context, accountInfo?: AccountInfo<Buffer>): void;
26
26
  decodeBuffer(buffer: Buffer): T;
@@ -30,9 +30,9 @@ export declare class WebSocketDriftClientAccountSubscriber implements DriftClien
30
30
  initialPerpMarketAccountData: Map<number, PerpMarketAccount>;
31
31
  initialSpotMarketAccountData: Map<number, SpotMarketAccount>;
32
32
  initialOraclePriceData: Map<string, OraclePriceData>;
33
- private isSubscribing;
34
- private subscriptionPromise;
35
- private subscriptionPromiseResolver;
33
+ protected isSubscribing: boolean;
34
+ protected subscriptionPromise: Promise<boolean>;
35
+ protected subscriptionPromiseResolver: (val: boolean) => void;
36
36
  constructor(program: Program, perpMarketIndexes: number[], spotMarketIndexes: number[], oracleInfos: OracleInfo[], shouldFindAllMarketsAndOracles: boolean, resubOpts?: ResubOpts, commitment?: Commitment);
37
37
  subscribe(): Promise<boolean>;
38
38
  setInitialData(): Promise<void>;
@@ -27,7 +27,7 @@ export declare class WebSocketProgramAccountSubscriber<T> implements ProgramAcco
27
27
  commitment?: Commitment;
28
28
  }, resubOpts?: ResubOpts);
29
29
  subscribe(onChange: (accountId: PublicKey, data: T, context: Context, buffer: Buffer) => void): Promise<void>;
30
- private setTimeout;
30
+ protected setTimeout(): void;
31
31
  handleRpcResponse(context: Context, keyedAccountInfo: KeyedAccountInfo): void;
32
32
  unsubscribe(onResub?: boolean): Promise<void>;
33
33
  }