@drift-labs/sdk 2.96.0-beta.2 → 2.96.0-beta.21

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 (110) hide show
  1. package/README.md +2 -0
  2. package/VERSION +1 -1
  3. package/bun.lockb +0 -0
  4. package/lib/accounts/pollingDriftClientAccountSubscriber.d.ts +5 -3
  5. package/lib/accounts/pollingDriftClientAccountSubscriber.js +24 -1
  6. package/lib/accounts/types.d.ts +5 -8
  7. package/lib/accounts/types.js +7 -1
  8. package/lib/accounts/utils.d.ts +7 -0
  9. package/lib/accounts/utils.js +33 -1
  10. package/lib/accounts/webSocketAccountSubscriber.d.ts +1 -1
  11. package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +8 -7
  12. package/lib/accounts/webSocketDriftClientAccountSubscriber.js +24 -1
  13. package/lib/accounts/webSocketProgramAccountSubscriber.d.ts +1 -1
  14. package/lib/config.d.ts +5 -1
  15. package/lib/config.js +9 -1
  16. package/lib/constants/perpMarkets.js +21 -0
  17. package/lib/constants/spotMarkets.js +12 -1
  18. package/lib/driftClient.d.ts +44 -9
  19. package/lib/driftClient.js +181 -61
  20. package/lib/driftClientConfig.d.ts +2 -6
  21. package/lib/events/eventSubscriber.js +9 -8
  22. package/lib/events/types.js +1 -5
  23. package/lib/idl/drift.json +169 -1
  24. package/lib/index.d.ts +1 -0
  25. package/lib/index.js +1 -0
  26. package/lib/math/margin.d.ts +16 -1
  27. package/lib/math/margin.js +67 -1
  28. package/lib/orderParams.js +8 -8
  29. package/lib/orderSubscriber/OrderSubscriber.d.ts +1 -2
  30. package/lib/orderSubscriber/OrderSubscriber.js +4 -19
  31. package/lib/orderSubscriber/types.d.ts +0 -9
  32. package/lib/tokenFaucet.js +2 -1
  33. package/lib/tx/baseTxSender.js +2 -2
  34. package/lib/tx/fastSingleTxSender.js +2 -2
  35. package/lib/tx/forwardOnlyTxSender.js +2 -2
  36. package/lib/tx/retryTxSender.js +2 -2
  37. package/lib/tx/txHandler.js +10 -7
  38. package/lib/tx/whileValidTxSender.d.ts +2 -4
  39. package/lib/tx/whileValidTxSender.js +16 -17
  40. package/lib/types.d.ts +21 -1
  41. package/lib/types.js +6 -1
  42. package/lib/user.d.ts +4 -1
  43. package/lib/user.js +13 -13
  44. package/lib/userConfig.d.ts +1 -6
  45. package/lib/userMap/userMap.js +0 -14
  46. package/lib/userMap/userMapConfig.d.ts +0 -7
  47. package/lib/userStatsConfig.d.ts +0 -6
  48. package/lib/util/TransactionConfirmationManager.d.ts +14 -0
  49. package/lib/util/TransactionConfirmationManager.js +96 -0
  50. package/package.json +4 -5
  51. package/src/accounts/pollingDriftClientAccountSubscriber.ts +41 -5
  52. package/src/accounts/types.ts +6 -9
  53. package/src/accounts/utils.ts +42 -0
  54. package/src/accounts/webSocketAccountSubscriber.ts +1 -1
  55. package/src/accounts/webSocketDriftClientAccountSubscriber.ts +43 -8
  56. package/src/accounts/webSocketProgramAccountSubscriber.ts +1 -1
  57. package/src/config.ts +15 -1
  58. package/src/constants/perpMarkets.ts +22 -0
  59. package/src/constants/spotMarkets.ts +14 -1
  60. package/src/driftClient.ts +423 -91
  61. package/src/driftClientConfig.ts +2 -7
  62. package/src/events/eventSubscriber.ts +18 -11
  63. package/src/events/types.ts +1 -5
  64. package/src/idl/drift.json +169 -1
  65. package/src/index.ts +1 -0
  66. package/src/math/margin.ts +137 -1
  67. package/src/orderParams.ts +20 -12
  68. package/src/orderSubscriber/OrderSubscriber.ts +1 -15
  69. package/src/orderSubscriber/types.ts +0 -10
  70. package/src/tokenFaucet.ts +2 -2
  71. package/src/tx/baseTxSender.ts +2 -2
  72. package/src/tx/fastSingleTxSender.ts +2 -2
  73. package/src/tx/forwardOnlyTxSender.ts +2 -2
  74. package/src/tx/retryTxSender.ts +2 -2
  75. package/src/tx/txHandler.ts +8 -2
  76. package/src/tx/whileValidTxSender.ts +23 -26
  77. package/src/types.ts +30 -1
  78. package/src/user.ts +35 -13
  79. package/src/userConfig.ts +1 -7
  80. package/src/userMap/userMap.ts +1 -17
  81. package/src/userMap/userMapConfig.ts +0 -8
  82. package/src/userStatsConfig.ts +0 -7
  83. package/src/util/TransactionConfirmationManager.ts +155 -0
  84. package/tests/ci/idl.ts +12 -3
  85. package/tests/ci/verifyConstants.ts +13 -0
  86. package/tests/tx/TransactionConfirmationManager.test.ts +286 -0
  87. package/lib/accounts/grpcAccountSubscriber.d.ts +0 -16
  88. package/lib/accounts/grpcAccountSubscriber.js +0 -155
  89. package/lib/accounts/grpcDriftClientAccountSubscriber.d.ts +0 -13
  90. package/lib/accounts/grpcDriftClientAccountSubscriber.js +0 -96
  91. package/lib/accounts/grpcInsuranceFundStakeAccountSubscriber.d.ts +0 -10
  92. package/lib/accounts/grpcInsuranceFundStakeAccountSubscriber.js +0 -30
  93. package/lib/accounts/grpcProgramAccountSubscriber.d.ts +0 -19
  94. package/lib/accounts/grpcProgramAccountSubscriber.js +0 -161
  95. package/lib/accounts/grpcUserAccountSubscriber.d.ts +0 -10
  96. package/lib/accounts/grpcUserAccountSubscriber.js +0 -28
  97. package/lib/accounts/grpcUserStatsAccountSubscriber.d.ts +0 -10
  98. package/lib/accounts/grpcUserStatsAccountSubscriber.js +0 -28
  99. package/lib/orderSubscriber/grpcSubscription.d.ts +0 -25
  100. package/lib/orderSubscriber/grpcSubscription.js +0 -68
  101. package/lib/userMap/grpcSubscription.d.ts +0 -26
  102. package/lib/userMap/grpcSubscription.js +0 -42
  103. package/src/accounts/grpcAccountSubscriber.ts +0 -158
  104. package/src/accounts/grpcDriftClientAccountSubscriber.ts +0 -196
  105. package/src/accounts/grpcInsuranceFundStakeAccountSubscriber.ts +0 -62
  106. package/src/accounts/grpcProgramAccountSubscriber.ts +0 -181
  107. package/src/accounts/grpcUserAccountSubscriber.ts +0 -48
  108. package/src/accounts/grpcUserStatsAccountSubscriber.ts +0 -51
  109. package/src/orderSubscriber/grpcSubscription.ts +0 -126
  110. package/src/userMap/grpcSubscription.ts +0 -83
@@ -0,0 +1,286 @@
1
+ import { expect } from 'chai';
2
+ import sinon from 'sinon';
3
+ import { Connection, SignatureStatus } from '@solana/web3.js';
4
+ import { TransactionConfirmationManager } from '../../src/util/TransactionConfirmationManager';
5
+ import assert from 'assert';
6
+
7
+ describe('TransactionConfirmationManager', () => {
8
+ let manager: TransactionConfirmationManager;
9
+ let mockConnection: sinon.SinonStubbedInstance<Connection>;
10
+
11
+ beforeEach(() => {
12
+ mockConnection = sinon.createStubInstance(Connection);
13
+ manager = new TransactionConfirmationManager(
14
+ mockConnection as unknown as Connection
15
+ );
16
+ });
17
+
18
+ afterEach(() => {
19
+ sinon.restore();
20
+ });
21
+
22
+ it('should throw error for invalid poll interval', async () => {
23
+ try {
24
+ await manager.confirmTransaction('fakeTxSig', 'confirmed', 30000, 300);
25
+ assert.fail('Expected an error to be thrown');
26
+ } catch (error) {
27
+ assert(error instanceof Error);
28
+ assert.strictEqual(
29
+ error.message,
30
+ 'Transaction confirmation polling interval must be at least 400ms and a multiple of 100ms'
31
+ );
32
+ }
33
+ });
34
+
35
+ it('should resolve when transaction is confirmed', async () => {
36
+ const fakeTxSig = 'fakeTxSig';
37
+ const fakeStatus: SignatureStatus = {
38
+ slot: 100,
39
+ confirmations: 1,
40
+ err: null,
41
+ confirmationStatus: 'confirmed',
42
+ };
43
+
44
+ mockConnection.getSignatureStatuses.resolves({
45
+ context: { slot: 100 },
46
+ value: [fakeStatus],
47
+ });
48
+
49
+ const result = await manager.confirmTransaction(
50
+ fakeTxSig,
51
+ 'confirmed',
52
+ 30000,
53
+ 400
54
+ );
55
+
56
+ expect(result).to.deep.equal(fakeStatus);
57
+ expect(
58
+ mockConnection.getSignatureStatuses.calledWith([fakeTxSig], {
59
+ searchTransactionHistory: false,
60
+ })
61
+ ).to.be.true;
62
+ });
63
+
64
+ it('should reject when transaction fails', async () => {
65
+ const fakeTxSig = 'fakeTxSig';
66
+ const fakeStatus: SignatureStatus = {
67
+ slot: 100,
68
+ confirmations: 1,
69
+ err: { InstructionError: [0, 'Custom'] },
70
+ confirmationStatus: 'confirmed',
71
+ };
72
+
73
+ mockConnection.getSignatureStatuses.resolves({
74
+ context: { slot: 100 },
75
+ value: [fakeStatus],
76
+ });
77
+
78
+ try {
79
+ await manager.confirmTransaction(fakeTxSig, 'confirmed', 30000, 400);
80
+ assert.fail('Expected an error to be thrown');
81
+ } catch (error) {
82
+ assert(error instanceof Error);
83
+ assert.strictEqual(
84
+ error.message,
85
+ 'Transaction failed: {"InstructionError":[0,"Custom"]}'
86
+ );
87
+ }
88
+ });
89
+
90
+ it('should reject on timeout', async () => {
91
+ const clock = sinon.useFakeTimers();
92
+
93
+ const fakeTxSig = 'fakeTxSig';
94
+ mockConnection.getSignatureStatuses.resolves({
95
+ context: { slot: 100 },
96
+ value: [null],
97
+ });
98
+
99
+ const promise = manager.confirmTransaction(
100
+ fakeTxSig,
101
+ 'confirmed',
102
+ 5000,
103
+ 1000
104
+ );
105
+
106
+ clock.tick(6000);
107
+
108
+ try {
109
+ await promise;
110
+ assert.fail('Expected an error to be thrown');
111
+ } catch (error) {
112
+ assert(error instanceof Error);
113
+ assert.strictEqual(
114
+ error.message,
115
+ 'Transaction confirmation timeout after 5000ms'
116
+ );
117
+ }
118
+
119
+ clock.restore();
120
+ });
121
+
122
+ it('should check multiple transactions together', async () => {
123
+ const fakeTxSig1 = 'fakeTxSig1';
124
+ const fakeTxSig2 = 'fakeTxSig2';
125
+ const fakeStatus1: SignatureStatus = {
126
+ slot: 100,
127
+ confirmations: 1,
128
+ err: null,
129
+ confirmationStatus: 'confirmed',
130
+ };
131
+ const fakeStatus2: SignatureStatus = {
132
+ slot: 100,
133
+ confirmations: 1,
134
+ err: null,
135
+ confirmationStatus: 'confirmed',
136
+ };
137
+
138
+ mockConnection.getSignatureStatuses.resolves({
139
+ context: { slot: 100 },
140
+ value: [fakeStatus1, fakeStatus2],
141
+ });
142
+
143
+ const promise1 = manager.confirmTransaction(
144
+ fakeTxSig1,
145
+ 'confirmed',
146
+ 30000,
147
+ 400
148
+ );
149
+ const promise2 = manager.confirmTransaction(
150
+ fakeTxSig2,
151
+ 'confirmed',
152
+ 30000,
153
+ 400
154
+ );
155
+
156
+ const clock = sinon.useFakeTimers();
157
+ clock.tick(400);
158
+ clock.restore();
159
+
160
+ const [result1, result2] = await Promise.all([promise1, promise2]);
161
+
162
+ expect(result1).to.deep.equal(fakeStatus1);
163
+ expect(result2).to.deep.equal(fakeStatus2);
164
+ expect(
165
+ mockConnection.getSignatureStatuses.calledWith([fakeTxSig1, fakeTxSig2], {
166
+ searchTransactionHistory: false,
167
+ })
168
+ ).to.be.true;
169
+ });
170
+
171
+ it('should have overlapping request for transactions with 400ms and 1200ms intervals on the third 400ms interval', async function () {
172
+ this.timeout(5000); // Increase timeout for this test to 5 seconds
173
+
174
+ const fakeTxSig1 = 'fakeTxSig1'; // 400ms interval
175
+ const fakeTxSig2 = 'fakeTxSig2'; // 1200ms interval
176
+ const fakeStatus: SignatureStatus = {
177
+ slot: 100,
178
+ confirmations: 1,
179
+ err: null,
180
+ confirmationStatus: 'confirmed',
181
+ };
182
+
183
+ let callCount = 0;
184
+ const callTimes: number[] = [];
185
+ const callSignatures: string[][] = [];
186
+
187
+ mockConnection.getSignatureStatuses = async (signatures) => {
188
+ callCount++;
189
+ const currentTime = Date.now();
190
+ callTimes.push(currentTime);
191
+ callSignatures.push([...signatures]);
192
+
193
+ if (callCount < 3) {
194
+ return {
195
+ context: { slot: 100 },
196
+ value: signatures.map(() => null),
197
+ };
198
+ } else {
199
+ return {
200
+ context: { slot: 100 },
201
+ value: signatures.map(() => fakeStatus),
202
+ };
203
+ }
204
+ };
205
+
206
+ const startTime = Date.now();
207
+
208
+ // Start both confirmation processes
209
+ const promise1 = manager.confirmTransaction(
210
+ fakeTxSig1,
211
+ 'confirmed',
212
+ 5000,
213
+ 400
214
+ );
215
+ const promise2 = manager.confirmTransaction(
216
+ fakeTxSig2,
217
+ 'confirmed',
218
+ 5000,
219
+ 1200
220
+ );
221
+
222
+ // Wait for 1250ms to ensure we've hit the third 400ms interval and first 1200ms interval
223
+ await new Promise((resolve) => setTimeout(resolve, 1250));
224
+
225
+ // Resolve both promises
226
+ await Promise.all([promise1, promise2]);
227
+
228
+ // Check the call times and signatures
229
+ assert.strictEqual(callTimes.length, 3, 'Should have exactly 3 calls');
230
+
231
+ // Check if the third call is close to 1200ms and includes both signatures
232
+ const overlapCall = 2; // The third call should be the overlapping one
233
+ const overlapTime = callTimes[overlapCall] - startTime;
234
+
235
+ assert(
236
+ Math.abs(overlapTime - 1200) < 100,
237
+ `Overlapping call should be around 1200ms, but was at ${overlapTime}ms`
238
+ );
239
+
240
+ // Verify the call pattern
241
+ assert(
242
+ callSignatures[0].includes(fakeTxSig1) &&
243
+ !callSignatures[0].includes(fakeTxSig2),
244
+ 'First call should only include 400ms interval transaction'
245
+ );
246
+ assert(
247
+ callSignatures[1].includes(fakeTxSig1) &&
248
+ !callSignatures[1].includes(fakeTxSig2),
249
+ 'Second call should only include 400ms interval transaction'
250
+ );
251
+ assert(
252
+ callSignatures[2].includes(fakeTxSig1) &&
253
+ callSignatures[2].includes(fakeTxSig2),
254
+ 'Third call should include both transactions'
255
+ );
256
+
257
+ // Wait for 1000ms to check that we haven't made any more calls now that all transactions are confirmed
258
+ await new Promise((resolve) => setTimeout(resolve, 1000));
259
+
260
+ // Verify that no more calls were made
261
+ assert.strictEqual(
262
+ callTimes.length,
263
+ 3,
264
+ 'Should not have made any more calls after all transactions are confirmed'
265
+ );
266
+
267
+ // Verify that only the third call returns non-null results
268
+ callCount = 0;
269
+ const results = await Promise.all(
270
+ callSignatures.map((sigs) => mockConnection.getSignatureStatuses!(sigs))
271
+ );
272
+
273
+ assert(
274
+ results[0].value.every((v) => v === null),
275
+ 'First call should return null results'
276
+ );
277
+ assert(
278
+ results[1].value.every((v) => v === null),
279
+ 'Second call should return null results'
280
+ );
281
+ assert(
282
+ results[2].value.every((v) => v !== null),
283
+ 'Third call should return non-null results'
284
+ );
285
+ });
286
+ });
@@ -1,16 +0,0 @@
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
- }
@@ -1,155 +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.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;
@@ -1,13 +0,0 @@
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
- }
@@ -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
- }