@drift-labs/sdk 2.96.0-beta.1 → 2.96.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/VERSION +1 -1
  2. package/lib/accounts/types.d.ts +0 -8
  3. package/lib/accounts/webSocketAccountSubscriber.d.ts +1 -1
  4. package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +3 -3
  5. package/lib/accounts/webSocketProgramAccountSubscriber.d.ts +1 -1
  6. package/lib/driftClient.js +14 -35
  7. package/lib/driftClientConfig.d.ts +0 -6
  8. package/lib/events/eventSubscriber.d.ts +7 -0
  9. package/lib/events/eventSubscriber.js +69 -32
  10. package/lib/events/eventsServerLogProvider.d.ts +21 -0
  11. package/lib/events/eventsServerLogProvider.js +121 -0
  12. package/lib/events/pollingLogProvider.js +1 -1
  13. package/lib/events/types.d.ts +12 -5
  14. package/lib/events/types.js +5 -1
  15. package/lib/events/webSocketLogProvider.js +2 -2
  16. package/lib/orderSubscriber/OrderSubscriber.d.ts +1 -2
  17. package/lib/orderSubscriber/OrderSubscriber.js +4 -19
  18. package/lib/orderSubscriber/types.d.ts +0 -9
  19. package/lib/user.js +4 -11
  20. package/lib/userConfig.d.ts +1 -6
  21. package/lib/userMap/userMap.js +0 -14
  22. package/lib/userMap/userMapConfig.d.ts +0 -7
  23. package/lib/userStatsConfig.d.ts +0 -6
  24. package/package.json +1 -3
  25. package/src/accounts/types.ts +0 -9
  26. package/src/accounts/webSocketAccountSubscriber.ts +1 -1
  27. package/src/accounts/webSocketDriftClientAccountSubscriber.ts +3 -3
  28. package/src/accounts/webSocketProgramAccountSubscriber.ts +1 -1
  29. package/src/driftClient.ts +0 -28
  30. package/src/driftClientConfig.ts +0 -7
  31. package/src/events/eventSubscriber.ts +125 -54
  32. package/src/events/eventsServerLogProvider.ts +152 -0
  33. package/src/events/pollingLogProvider.ts +1 -1
  34. package/src/events/types.ts +29 -6
  35. package/src/events/webSocketLogProvider.ts +4 -4
  36. package/src/orderSubscriber/OrderSubscriber.ts +1 -15
  37. package/src/orderSubscriber/types.ts +0 -10
  38. package/src/user.ts +0 -11
  39. package/src/userConfig.ts +1 -7
  40. package/src/userMap/userMap.ts +1 -17
  41. package/src/userMap/userMapConfig.ts +0 -8
  42. package/src/userStatsConfig.ts +0 -7
  43. package/lib/accounts/grpcAccountSubscriber.d.ts +0 -16
  44. package/lib/accounts/grpcAccountSubscriber.js +0 -155
  45. package/lib/accounts/grpcDriftClientAccountSubscriber.d.ts +0 -13
  46. package/lib/accounts/grpcDriftClientAccountSubscriber.js +0 -96
  47. package/lib/accounts/grpcInsuranceFundStakeAccountSubscriber.d.ts +0 -10
  48. package/lib/accounts/grpcInsuranceFundStakeAccountSubscriber.js +0 -30
  49. package/lib/accounts/grpcProgramAccountSubscriber.d.ts +0 -19
  50. package/lib/accounts/grpcProgramAccountSubscriber.js +0 -161
  51. package/lib/accounts/grpcUserAccountSubscriber.d.ts +0 -10
  52. package/lib/accounts/grpcUserAccountSubscriber.js +0 -28
  53. package/lib/accounts/grpcUserStatsAccountSubscriber.d.ts +0 -10
  54. package/lib/accounts/grpcUserStatsAccountSubscriber.js +0 -28
  55. package/lib/orderSubscriber/grpcSubscription.d.ts +0 -25
  56. package/lib/orderSubscriber/grpcSubscription.js +0 -68
  57. package/lib/userMap/grpcSubscription.d.ts +0 -26
  58. package/lib/userMap/grpcSubscription.js +0 -42
  59. package/src/accounts/grpcAccountSubscriber.ts +0 -158
  60. package/src/accounts/grpcDriftClientAccountSubscriber.ts +0 -196
  61. package/src/accounts/grpcInsuranceFundStakeAccountSubscriber.ts +0 -62
  62. package/src/accounts/grpcProgramAccountSubscriber.ts +0 -181
  63. package/src/accounts/grpcUserAccountSubscriber.ts +0 -48
  64. package/src/accounts/grpcUserStatsAccountSubscriber.ts +0 -51
  65. package/src/orderSubscriber/grpcSubscription.ts +0 -126
  66. package/src/userMap/grpcSubscription.ts +0 -83
@@ -1,96 +0,0 @@
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;
@@ -1,10 +0,0 @@
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
- }
@@ -1,30 +0,0 @@
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;
@@ -1,19 +0,0 @@
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
- }
@@ -1,161 +0,0 @@
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;
@@ -1,10 +0,0 @@
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
- }
@@ -1,28 +0,0 @@
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;
@@ -1,10 +0,0 @@
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
- }
@@ -1,28 +0,0 @@
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;
@@ -1,25 +0,0 @@
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
- }
@@ -1,68 +0,0 @@
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,26 +0,0 @@
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
- }
@@ -1,42 +0,0 @@
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;