@drift-labs/sdk 2.142.0-beta.13 → 2.142.0-beta.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/VERSION +1 -1
- package/lib/browser/accounts/grpcDriftClientAccountSubscriberV2.d.ts +45 -5
- package/lib/browser/accounts/grpcDriftClientAccountSubscriberV2.js +187 -8
- package/lib/browser/accounts/grpcMultiAccountSubscriber.d.ts +1 -0
- package/lib/browser/accounts/grpcMultiAccountSubscriber.js +46 -0
- package/lib/node/accounts/grpcDriftClientAccountSubscriberV2.d.ts +45 -5
- package/lib/node/accounts/grpcDriftClientAccountSubscriberV2.d.ts.map +1 -1
- package/lib/node/accounts/grpcDriftClientAccountSubscriberV2.js +187 -8
- package/lib/node/accounts/grpcMultiAccountSubscriber.d.ts +1 -0
- package/lib/node/accounts/grpcMultiAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/grpcMultiAccountSubscriber.js +46 -0
- package/package.json +1 -1
- package/src/accounts/grpcDriftClientAccountSubscriberV2.ts +309 -22
- package/src/accounts/grpcMultiAccountSubscriber.ts +66 -1
|
@@ -1,19 +1,114 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.grpcDriftClientAccountSubscriberV2 = void 0;
|
|
4
|
-
const
|
|
4
|
+
const events_1 = require("events");
|
|
5
5
|
const web3_js_1 = require("@solana/web3.js");
|
|
6
6
|
const config_1 = require("../config");
|
|
7
7
|
const pda_1 = require("../addresses/pda");
|
|
8
|
+
const types_1 = require("./types");
|
|
8
9
|
const grpcAccountSubscriber_1 = require("./grpcAccountSubscriber");
|
|
9
10
|
const grpcMultiAccountSubscriber_1 = require("./grpcMultiAccountSubscriber");
|
|
10
11
|
const oracleId_1 = require("../oracles/oracleId");
|
|
11
|
-
|
|
12
|
+
const oracleClientCache_1 = require("../oracles/oracleClientCache");
|
|
13
|
+
const utils_1 = require("./utils");
|
|
14
|
+
class grpcDriftClientAccountSubscriberV2 {
|
|
12
15
|
constructor(grpcConfigs, program, perpMarketIndexes, spotMarketIndexes, oracleInfos, shouldFindAllMarketsAndOracles, delistedMarketSetting, resubOpts) {
|
|
13
|
-
super(program, perpMarketIndexes, spotMarketIndexes, oracleInfos, shouldFindAllMarketsAndOracles, delistedMarketSetting, resubOpts);
|
|
14
16
|
this.perpMarketIndexToAccountPubkeyMap = new Map();
|
|
15
17
|
this.spotMarketIndexToAccountPubkeyMap = new Map();
|
|
18
|
+
this.perpOracleMap = new Map();
|
|
19
|
+
this.perpOracleStringMap = new Map();
|
|
20
|
+
this.spotOracleMap = new Map();
|
|
21
|
+
this.spotOracleStringMap = new Map();
|
|
22
|
+
this.oracleClientCache = new oracleClientCache_1.OracleClientCache();
|
|
23
|
+
this.chunks = (array, size) => {
|
|
24
|
+
return new Array(Math.ceil(array.length / size))
|
|
25
|
+
.fill(null)
|
|
26
|
+
.map((_, index) => index * size)
|
|
27
|
+
.map((begin) => array.slice(begin, begin + size));
|
|
28
|
+
};
|
|
29
|
+
this.eventEmitter = new events_1.EventEmitter();
|
|
30
|
+
this.isSubscribed = false;
|
|
31
|
+
this.isSubscribing = false;
|
|
32
|
+
this.program = program;
|
|
33
|
+
this.perpMarketIndexes = perpMarketIndexes;
|
|
34
|
+
this.spotMarketIndexes = spotMarketIndexes;
|
|
35
|
+
this.shouldFindAllMarketsAndOracles = shouldFindAllMarketsAndOracles;
|
|
36
|
+
this.oracleInfos = oracleInfos;
|
|
37
|
+
this.initialPerpMarketAccountData = new Map();
|
|
38
|
+
this.initialSpotMarketAccountData = new Map();
|
|
39
|
+
this.initialOraclePriceData = new Map();
|
|
40
|
+
this.perpOracleMap = new Map();
|
|
41
|
+
this.perpOracleStringMap = new Map();
|
|
42
|
+
this.spotOracleMap = new Map();
|
|
43
|
+
this.spotOracleStringMap = new Map();
|
|
16
44
|
this.grpcConfigs = grpcConfigs;
|
|
45
|
+
this.resubOpts = resubOpts;
|
|
46
|
+
this.delistedMarketSetting = delistedMarketSetting;
|
|
47
|
+
}
|
|
48
|
+
async setInitialData() {
|
|
49
|
+
const connection = this.program.provider.connection;
|
|
50
|
+
if (!this.initialPerpMarketAccountData ||
|
|
51
|
+
this.initialPerpMarketAccountData.size === 0) {
|
|
52
|
+
const perpMarketPublicKeys = this.perpMarketIndexes.map((marketIndex) => (0, pda_1.getPerpMarketPublicKeySync)(this.program.programId, marketIndex));
|
|
53
|
+
const perpMarketPublicKeysChunks = this.chunks(perpMarketPublicKeys, 75);
|
|
54
|
+
const perpMarketAccountInfos = (await Promise.all(perpMarketPublicKeysChunks.map((perpMarketPublicKeysChunk) => connection.getMultipleAccountsInfo(perpMarketPublicKeysChunk)))).flat();
|
|
55
|
+
this.initialPerpMarketAccountData = new Map(perpMarketAccountInfos
|
|
56
|
+
.filter((accountInfo) => !!accountInfo)
|
|
57
|
+
.map((accountInfo) => {
|
|
58
|
+
const perpMarket = this.program.coder.accounts.decode('PerpMarket', accountInfo.data);
|
|
59
|
+
return [perpMarket.marketIndex, perpMarket];
|
|
60
|
+
}));
|
|
61
|
+
}
|
|
62
|
+
if (!this.initialSpotMarketAccountData ||
|
|
63
|
+
this.initialSpotMarketAccountData.size === 0) {
|
|
64
|
+
const spotMarketPublicKeys = this.spotMarketIndexes.map((marketIndex) => (0, pda_1.getSpotMarketPublicKeySync)(this.program.programId, marketIndex));
|
|
65
|
+
const spotMarketPublicKeysChunks = this.chunks(spotMarketPublicKeys, 75);
|
|
66
|
+
const spotMarketAccountInfos = (await Promise.all(spotMarketPublicKeysChunks.map((spotMarketPublicKeysChunk) => connection.getMultipleAccountsInfo(spotMarketPublicKeysChunk)))).flat();
|
|
67
|
+
this.initialSpotMarketAccountData = new Map(spotMarketAccountInfos
|
|
68
|
+
.filter((accountInfo) => !!accountInfo)
|
|
69
|
+
.map((accountInfo) => {
|
|
70
|
+
const spotMarket = this.program.coder.accounts.decode('SpotMarket', accountInfo.data);
|
|
71
|
+
return [spotMarket.marketIndex, spotMarket];
|
|
72
|
+
}));
|
|
73
|
+
}
|
|
74
|
+
const oracleAccountPubkeyChunks = this.chunks(this.oracleInfos.map((oracleInfo) => oracleInfo.publicKey), 75);
|
|
75
|
+
const oracleAccountInfos = (await Promise.all(oracleAccountPubkeyChunks.map((oracleAccountPublicKeysChunk) => connection.getMultipleAccountsInfo(oracleAccountPublicKeysChunk)))).flat();
|
|
76
|
+
this.initialOraclePriceData = new Map(this.oracleInfos.reduce((result, oracleInfo, i) => {
|
|
77
|
+
if (!oracleAccountInfos[i]) {
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
const oracleClient = this.oracleClientCache.get(oracleInfo.source, connection, this.program);
|
|
81
|
+
const oraclePriceData = oracleClient.getOraclePriceDataFromBuffer(oracleAccountInfos[i].data);
|
|
82
|
+
result.push([
|
|
83
|
+
(0, oracleId_1.getOracleId)(oracleInfo.publicKey, oracleInfo.source),
|
|
84
|
+
oraclePriceData,
|
|
85
|
+
]);
|
|
86
|
+
return result;
|
|
87
|
+
}, []));
|
|
88
|
+
}
|
|
89
|
+
async addPerpMarket(_marketIndex) {
|
|
90
|
+
if (!this.perpMarketIndexes.includes(_marketIndex)) {
|
|
91
|
+
this.perpMarketIndexes = this.perpMarketIndexes.concat(_marketIndex);
|
|
92
|
+
}
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
async addSpotMarket(_marketIndex) {
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
async addOracle(oracleInfo) {
|
|
99
|
+
if (oracleInfo.publicKey.equals(web3_js_1.PublicKey.default)) {
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
const exists = this.oracleInfos.some((o) => o.source === oracleInfo.source &&
|
|
103
|
+
o.publicKey.equals(oracleInfo.publicKey));
|
|
104
|
+
if (!exists) {
|
|
105
|
+
this.oracleInfos = this.oracleInfos.concat(oracleInfo);
|
|
106
|
+
}
|
|
107
|
+
if (this.oracleMultiSubscriber) {
|
|
108
|
+
await this.unsubscribeFromOracles();
|
|
109
|
+
await this.subscribeToOracles();
|
|
110
|
+
}
|
|
111
|
+
return true;
|
|
17
112
|
}
|
|
18
113
|
async subscribe() {
|
|
19
114
|
if (this.isSubscribed) {
|
|
@@ -61,6 +156,32 @@ class grpcDriftClientAccountSubscriberV2 extends webSocketDriftClientAccountSubs
|
|
|
61
156
|
this.removeInitialData();
|
|
62
157
|
return true;
|
|
63
158
|
}
|
|
159
|
+
async fetch() {
|
|
160
|
+
var _a, _b, _c, _d;
|
|
161
|
+
await ((_a = this.stateAccountSubscriber) === null || _a === void 0 ? void 0 : _a.fetch());
|
|
162
|
+
await ((_b = this.perpMarketsSubscriber) === null || _b === void 0 ? void 0 : _b.fetch());
|
|
163
|
+
await ((_c = this.spotMarketsSubscriber) === null || _c === void 0 ? void 0 : _c.fetch());
|
|
164
|
+
await ((_d = this.oracleMultiSubscriber) === null || _d === void 0 ? void 0 : _d.fetch());
|
|
165
|
+
}
|
|
166
|
+
assertIsSubscribed() {
|
|
167
|
+
if (!this.isSubscribed) {
|
|
168
|
+
throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
getStateAccountAndSlot() {
|
|
172
|
+
this.assertIsSubscribed();
|
|
173
|
+
return this.stateAccountSubscriber.dataAndSlot;
|
|
174
|
+
}
|
|
175
|
+
getMarketAccountsAndSlots() {
|
|
176
|
+
var _a, _b;
|
|
177
|
+
const map = (_a = this.perpMarketsSubscriber) === null || _a === void 0 ? void 0 : _a.getAccountDataMap();
|
|
178
|
+
return Array.from((_b = map === null || map === void 0 ? void 0 : map.values()) !== null && _b !== void 0 ? _b : []);
|
|
179
|
+
}
|
|
180
|
+
getSpotMarketAccountsAndSlots() {
|
|
181
|
+
var _a, _b;
|
|
182
|
+
const map = (_a = this.spotMarketsSubscriber) === null || _a === void 0 ? void 0 : _a.getAccountDataMap();
|
|
183
|
+
return Array.from((_b = map === null || map === void 0 ? void 0 : map.values()) !== null && _b !== void 0 ? _b : []);
|
|
184
|
+
}
|
|
64
185
|
getMarketAccountAndSlot(marketIndex) {
|
|
65
186
|
var _a;
|
|
66
187
|
return (_a = this.perpMarketsSubscriber) === null || _a === void 0 ? void 0 : _a.getAccountData(this.perpMarketIndexToAccountPubkeyMap.get(marketIndex));
|
|
@@ -69,8 +190,40 @@ class grpcDriftClientAccountSubscriberV2 extends webSocketDriftClientAccountSubs
|
|
|
69
190
|
var _a;
|
|
70
191
|
return (_a = this.spotMarketsSubscriber) === null || _a === void 0 ? void 0 : _a.getAccountData(this.spotMarketIndexToAccountPubkeyMap.get(marketIndex));
|
|
71
192
|
}
|
|
72
|
-
|
|
193
|
+
getOraclePriceDataAndSlot(oracleId) {
|
|
73
194
|
var _a;
|
|
195
|
+
this.assertIsSubscribed();
|
|
196
|
+
const { publicKey } = (0, oracleId_1.getPublicKeyAndSourceFromOracleId)(oracleId);
|
|
197
|
+
return (_a = this.oracleMultiSubscriber) === null || _a === void 0 ? void 0 : _a.getAccountData(publicKey.toBase58());
|
|
198
|
+
}
|
|
199
|
+
getOraclePriceDataAndSlotForPerpMarket(marketIndex) {
|
|
200
|
+
const perpMarketAccount = this.getMarketAccountAndSlot(marketIndex);
|
|
201
|
+
const oracle = this.perpOracleMap.get(marketIndex);
|
|
202
|
+
const oracleId = this.perpOracleStringMap.get(marketIndex);
|
|
203
|
+
if (!perpMarketAccount || !oracleId) {
|
|
204
|
+
return undefined;
|
|
205
|
+
}
|
|
206
|
+
if (!perpMarketAccount.data.amm.oracle.equals(oracle)) {
|
|
207
|
+
// If the oracle has changed, we need to update the oracle map in background
|
|
208
|
+
this.setPerpOracleMap();
|
|
209
|
+
}
|
|
210
|
+
return this.getOraclePriceDataAndSlot(oracleId);
|
|
211
|
+
}
|
|
212
|
+
getOraclePriceDataAndSlotForSpotMarket(marketIndex) {
|
|
213
|
+
const spotMarketAccount = this.getSpotMarketAccountAndSlot(marketIndex);
|
|
214
|
+
const oracle = this.spotOracleMap.get(marketIndex);
|
|
215
|
+
const oracleId = this.spotOracleStringMap.get(marketIndex);
|
|
216
|
+
if (!spotMarketAccount || !oracleId) {
|
|
217
|
+
return undefined;
|
|
218
|
+
}
|
|
219
|
+
if (!spotMarketAccount.data.oracle.equals(oracle)) {
|
|
220
|
+
// If the oracle has changed, we need to update the oracle map in background
|
|
221
|
+
this.setSpotOracleMap();
|
|
222
|
+
}
|
|
223
|
+
return this.getOraclePriceDataAndSlot(oracleId);
|
|
224
|
+
}
|
|
225
|
+
async setPerpOracleMap() {
|
|
226
|
+
var _a, _b;
|
|
74
227
|
const perpMarketsMap = (_a = this.perpMarketsSubscriber) === null || _a === void 0 ? void 0 : _a.getAccountDataMap();
|
|
75
228
|
const perpMarkets = Array.from(perpMarketsMap.values());
|
|
76
229
|
const addOraclePromises = [];
|
|
@@ -82,7 +235,7 @@ class grpcDriftClientAccountSubscriberV2 extends webSocketDriftClientAccountSubs
|
|
|
82
235
|
const perpMarketIndex = perpMarketAccount.marketIndex;
|
|
83
236
|
const oracle = perpMarketAccount.amm.oracle;
|
|
84
237
|
const oracleId = (0, oracleId_1.getOracleId)(oracle, perpMarket.data.amm.oracleSource);
|
|
85
|
-
if (!this.
|
|
238
|
+
if (!((_b = this.oracleMultiSubscriber) === null || _b === void 0 ? void 0 : _b.getAccountDataMap().has(oracleId))) {
|
|
86
239
|
addOraclePromises.push(this.addOracle({
|
|
87
240
|
publicKey: oracle,
|
|
88
241
|
source: perpMarket.data.amm.oracleSource,
|
|
@@ -94,7 +247,7 @@ class grpcDriftClientAccountSubscriberV2 extends webSocketDriftClientAccountSubs
|
|
|
94
247
|
await Promise.all(addOraclePromises);
|
|
95
248
|
}
|
|
96
249
|
async setSpotOracleMap() {
|
|
97
|
-
var _a;
|
|
250
|
+
var _a, _b;
|
|
98
251
|
const spotMarketsMap = (_a = this.spotMarketsSubscriber) === null || _a === void 0 ? void 0 : _a.getAccountDataMap();
|
|
99
252
|
const spotMarkets = Array.from(spotMarketsMap.values());
|
|
100
253
|
const addOraclePromises = [];
|
|
@@ -106,7 +259,7 @@ class grpcDriftClientAccountSubscriberV2 extends webSocketDriftClientAccountSubs
|
|
|
106
259
|
const spotMarketIndex = spotMarketAccount.marketIndex;
|
|
107
260
|
const oracle = spotMarketAccount.oracle;
|
|
108
261
|
const oracleId = (0, oracleId_1.getOracleId)(oracle, spotMarketAccount.oracleSource);
|
|
109
|
-
if (!this.
|
|
262
|
+
if (!((_b = this.oracleMultiSubscriber) === null || _b === void 0 ? void 0 : _b.getAccountDataMap().has(oracleId))) {
|
|
110
263
|
addOraclePromises.push(this.addOracle({
|
|
111
264
|
publicKey: oracle,
|
|
112
265
|
source: spotMarketAccount.oracleSource,
|
|
@@ -233,19 +386,45 @@ class grpcDriftClientAccountSubscriberV2 extends webSocketDriftClientAccountSubs
|
|
|
233
386
|
});
|
|
234
387
|
return true;
|
|
235
388
|
}
|
|
389
|
+
async handleDelistedMarkets() {
|
|
390
|
+
var _a, _b;
|
|
391
|
+
if (this.delistedMarketSetting === types_1.DelistedMarketSetting.Subscribe) {
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
const { perpMarketIndexes, oracles } = (0, utils_1.findDelistedPerpMarketsAndOracles)(Array.from(((_a = this.perpMarketsSubscriber) === null || _a === void 0 ? void 0 : _a.getAccountDataMap().values()) || []), Array.from(((_b = this.spotMarketsSubscriber) === null || _b === void 0 ? void 0 : _b.getAccountDataMap().values()) || []));
|
|
395
|
+
for (const perpMarketIndex of perpMarketIndexes) {
|
|
396
|
+
await this.perpMarketsSubscriber.removeAccounts([
|
|
397
|
+
new web3_js_1.PublicKey(this.perpMarketIndexToAccountPubkeyMap.get(perpMarketIndex) || ''),
|
|
398
|
+
]);
|
|
399
|
+
if (this.delistedMarketSetting === types_1.DelistedMarketSetting.Discard) {
|
|
400
|
+
this.perpMarketIndexToAccountPubkeyMap.delete(perpMarketIndex);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
for (const oracle of oracles) {
|
|
404
|
+
await this.oracleMultiSubscriber.removeAccounts([oracle.publicKey]);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
removeInitialData() {
|
|
408
|
+
this.initialPerpMarketAccountData = new Map();
|
|
409
|
+
this.initialSpotMarketAccountData = new Map();
|
|
410
|
+
this.initialOraclePriceData = new Map();
|
|
411
|
+
}
|
|
236
412
|
async unsubscribeFromOracles() {
|
|
237
413
|
if (this.oracleMultiSubscriber) {
|
|
238
414
|
await this.oracleMultiSubscriber.unsubscribe();
|
|
239
415
|
this.oracleMultiSubscriber = undefined;
|
|
240
416
|
return;
|
|
241
417
|
}
|
|
242
|
-
await super.unsubscribeFromOracles();
|
|
243
418
|
}
|
|
244
419
|
async unsubscribe() {
|
|
420
|
+
var _a, _b;
|
|
245
421
|
if (this.isSubscribed) {
|
|
246
422
|
return;
|
|
247
423
|
}
|
|
248
424
|
await this.stateAccountSubscriber.unsubscribe();
|
|
425
|
+
await this.unsubscribeFromOracles();
|
|
426
|
+
await ((_a = this.perpMarketsSubscriber) === null || _a === void 0 ? void 0 : _a.unsubscribe());
|
|
427
|
+
await ((_b = this.spotMarketsSubscriber) === null || _b === void 0 ? void 0 : _b.unsubscribe());
|
|
249
428
|
}
|
|
250
429
|
}
|
|
251
430
|
exports.grpcDriftClientAccountSubscriberV2 = grpcDriftClientAccountSubscriberV2;
|
|
@@ -25,6 +25,7 @@ export declare class grpcMultiAccountSubscriber<T> {
|
|
|
25
25
|
setAccountData(accountPubkey: string, data: T, slot?: number): void;
|
|
26
26
|
getAccountData(accountPubkey: string): DataAndSlot<T> | undefined;
|
|
27
27
|
getAccountDataMap(): Map<string, DataAndSlot<T>>;
|
|
28
|
+
fetch(): Promise<void>;
|
|
28
29
|
subscribe(accounts: PublicKey[], onChange: (accountId: PublicKey, data: T, context: Context, buffer: Buffer) => void): Promise<void>;
|
|
29
30
|
addAccounts(accounts: PublicKey[]): Promise<void>;
|
|
30
31
|
removeAccounts(accounts: PublicKey[]): Promise<void>;
|
|
@@ -1 +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,
|
|
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,EAAc,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAIjE,OAAO,EACN,MAAM,EAMN,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAyB9D,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,CAAC,OAAO,CAAqC;IAEpD,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;IAuBzC,cAAc,CAAC,aAAa,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI;IAInE,cAAc,CAAC,aAAa,EAAE,MAAM,GAAG,WAAW,CAAC,CAAC,CAAC,GAAG,SAAS;IAIjE,iBAAiB,IAAI,GAAG,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC;IAI1C,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAkDtB,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;IAgGV,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"}
|
|
@@ -31,6 +31,18 @@ const web3_js_1 = require("@solana/web3.js");
|
|
|
31
31
|
const Buffer = __importStar(require("buffer"));
|
|
32
32
|
const bs58_1 = __importDefault(require("bs58"));
|
|
33
33
|
const grpc_1 = require("../isomorphic/grpc");
|
|
34
|
+
function commitmentLevelToCommitment(commitmentLevel) {
|
|
35
|
+
switch (commitmentLevel) {
|
|
36
|
+
case grpc_1.CommitmentLevel.PROCESSED:
|
|
37
|
+
return 'processed';
|
|
38
|
+
case grpc_1.CommitmentLevel.CONFIRMED:
|
|
39
|
+
return 'confirmed';
|
|
40
|
+
case grpc_1.CommitmentLevel.FINALIZED:
|
|
41
|
+
return 'finalized';
|
|
42
|
+
default:
|
|
43
|
+
return 'confirmed';
|
|
44
|
+
}
|
|
45
|
+
}
|
|
34
46
|
class grpcMultiAccountSubscriber {
|
|
35
47
|
constructor(client, commitmentLevel, accountName, program, decodeBuffer, resubOpts, onUnsubscribe) {
|
|
36
48
|
this.isUnsubscribing = false;
|
|
@@ -65,6 +77,40 @@ class grpcMultiAccountSubscriber {
|
|
|
65
77
|
getAccountDataMap() {
|
|
66
78
|
return this.dataMap;
|
|
67
79
|
}
|
|
80
|
+
async fetch() {
|
|
81
|
+
var _a;
|
|
82
|
+
try {
|
|
83
|
+
// Chunk account IDs into groups of 100 (getMultipleAccounts limit)
|
|
84
|
+
const chunkSize = 100;
|
|
85
|
+
const chunks = [];
|
|
86
|
+
const accountIds = Array.from(this.subscribedAccounts.values());
|
|
87
|
+
for (let i = 0; i < accountIds.length; i += chunkSize) {
|
|
88
|
+
chunks.push(accountIds.slice(i, i + chunkSize));
|
|
89
|
+
}
|
|
90
|
+
// Process all chunks concurrently
|
|
91
|
+
await Promise.all(chunks.map(async (chunk) => {
|
|
92
|
+
const accountAddresses = chunk.map((accountId) => new web3_js_1.PublicKey(accountId));
|
|
93
|
+
const rpcResponseAndContext = await this.program.provider.connection.getMultipleAccountsInfoAndContext(accountAddresses, {
|
|
94
|
+
commitment: commitmentLevelToCommitment(this.commitmentLevel),
|
|
95
|
+
});
|
|
96
|
+
const rpcResponse = rpcResponseAndContext.value;
|
|
97
|
+
const currentSlot = rpcResponseAndContext.context.slot;
|
|
98
|
+
for (let i = 0; i < chunk.length; i++) {
|
|
99
|
+
const accountId = chunk[i];
|
|
100
|
+
const accountInfo = rpcResponse[i];
|
|
101
|
+
if (accountInfo) {
|
|
102
|
+
const perpMarket = this.program.coder.accounts.decode('PerpMarket', accountInfo.data);
|
|
103
|
+
this.setAccountData(accountId, perpMarket, currentSlot);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}));
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.logResubMessages) {
|
|
110
|
+
console.log(`[${this.accountName}] grpcMultiAccountSubscriber error fetching accounts:`, error);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
68
114
|
async subscribe(accounts, onChange) {
|
|
69
115
|
if (this.listenerId != null || this.isUnsubscribing) {
|
|
70
116
|
return;
|