@drift-labs/sdk 2.142.0-beta.1 → 2.142.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.
- package/VERSION +1 -1
- package/bun.lock +25 -10
- package/lib/browser/accounts/grpcAccountSubscriber.d.ts +2 -1
- package/lib/browser/accounts/grpcAccountSubscriber.js +4 -2
- package/lib/browser/accounts/grpcDriftClientAccountSubscriber.d.ts +1 -1
- package/lib/browser/accounts/grpcDriftClientAccountSubscriber.js +3 -3
- package/lib/browser/accounts/grpcDriftClientAccountSubscriberV2.d.ts +22 -0
- package/lib/browser/accounts/grpcDriftClientAccountSubscriberV2.js +194 -0
- package/lib/browser/accounts/grpcMultiAccountSubscriber.d.ts +33 -0
- package/lib/browser/accounts/grpcMultiAccountSubscriber.js +278 -0
- package/lib/browser/driftClient.js +11 -10
- package/lib/browser/driftClientConfig.d.ts +3 -0
- package/lib/node/accounts/grpcAccountSubscriber.d.ts +2 -1
- package/lib/node/accounts/grpcAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/grpcAccountSubscriber.js +4 -2
- package/lib/node/accounts/grpcDriftClientAccountSubscriber.d.ts +1 -1
- package/lib/node/accounts/grpcDriftClientAccountSubscriber.js +3 -3
- package/lib/node/accounts/grpcDriftClientAccountSubscriberV2.d.ts +23 -0
- package/lib/node/accounts/grpcDriftClientAccountSubscriberV2.d.ts.map +1 -0
- package/lib/node/accounts/grpcDriftClientAccountSubscriberV2.js +194 -0
- package/lib/node/accounts/grpcMultiAccountSubscriber.d.ts +34 -0
- package/lib/node/accounts/grpcMultiAccountSubscriber.d.ts.map +1 -0
- package/lib/node/accounts/grpcMultiAccountSubscriber.js +278 -0
- package/lib/node/driftClient.d.ts.map +1 -1
- package/lib/node/driftClient.js +11 -10
- package/lib/node/driftClientConfig.d.ts +3 -0
- package/lib/node/driftClientConfig.d.ts.map +1 -1
- package/lib/node/isomorphic/grpc.d.ts +5 -3
- package/lib/node/isomorphic/grpc.js +1 -3
- package/lib/node/isomorphic/grpc.node.d.ts +5 -3
- package/lib/node/isomorphic/grpc.node.d.ts.map +1 -1
- package/lib/node/isomorphic/grpc.node.js +1 -3
- package/package.json +3 -3
- package/scripts/client-test.ts +133 -0
- package/src/accounts/grpcAccountSubscriber.ts +9 -6
- package/src/accounts/grpcDriftClientAccountSubscriber.ts +1 -1
- package/src/accounts/grpcDriftClientAccountSubscriberV2.ts +358 -0
- package/src/accounts/grpcMultiAccountSubscriber.ts +338 -0
- package/src/driftClient.ts +5 -2
- package/src/driftClientConfig.ts +13 -0
- package/src/isomorphic/grpc.node.ts +11 -7
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.142.0-beta.
|
|
1
|
+
2.142.0-beta.3
|
package/bun.lock
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
"@coral-xyz/anchor": "0.29.0",
|
|
8
8
|
"@coral-xyz/anchor-30": "npm:@coral-xyz/anchor@0.30.1",
|
|
9
9
|
"@ellipsis-labs/phoenix-sdk": "1.4.5",
|
|
10
|
-
"@grpc/grpc-js": "1.
|
|
10
|
+
"@grpc/grpc-js": "1.14.0",
|
|
11
11
|
"@openbook-dex/openbook-v2": "0.2.10",
|
|
12
12
|
"@project-serum/serum": "0.13.65",
|
|
13
13
|
"@pythnetwork/client": "2.5.3",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"@solana/web3.js": "1.98.0",
|
|
18
18
|
"@switchboard-xyz/common": "3.0.14",
|
|
19
19
|
"@switchboard-xyz/on-demand": "2.4.1",
|
|
20
|
-
"@triton-one/yellowstone-grpc": "1.
|
|
20
|
+
"@triton-one/yellowstone-grpc": "1.4.1",
|
|
21
21
|
"anchor-bankrun": "0.3.0",
|
|
22
22
|
"gill": "^0.10.2",
|
|
23
23
|
"helius-laserstream": "0.1.8",
|
|
@@ -59,15 +59,24 @@
|
|
|
59
59
|
},
|
|
60
60
|
},
|
|
61
61
|
"overrides": {
|
|
62
|
-
"debug": "<4.4.2",
|
|
63
|
-
"supports-color": "7.2.0",
|
|
64
62
|
"ansi-regex": "5.0.1",
|
|
65
|
-
"color-convert": "<3.1.1",
|
|
66
63
|
"ansi-styles": "4.3.0",
|
|
67
|
-
"
|
|
64
|
+
"backslash": "<0.2.1",
|
|
68
65
|
"chalk": "4.1.2",
|
|
69
|
-
"
|
|
66
|
+
"chalk-template": "<1.1.1",
|
|
67
|
+
"color-convert": "<3.1.1",
|
|
70
68
|
"color-name": "<2.0.1",
|
|
69
|
+
"color-string": "<2.1.1",
|
|
70
|
+
"debug": "<4.4.2",
|
|
71
|
+
"error-ex": "<1.3.3",
|
|
72
|
+
"has-ansi": "<6.0.1",
|
|
73
|
+
"is-arrayish": "<0.3.3",
|
|
74
|
+
"simple-swizzle": "<0.2.3",
|
|
75
|
+
"slice-ansi": "3.0.0",
|
|
76
|
+
"strip-ansi": "6.0.1",
|
|
77
|
+
"supports-color": "7.2.0",
|
|
78
|
+
"supports-hyperlinks": "<4.1.1",
|
|
79
|
+
"wrap-ansi": "7.0.0",
|
|
71
80
|
},
|
|
72
81
|
"packages": {
|
|
73
82
|
"@babel/code-frame": ["@babel/code-frame@7.26.2", "", { "dependencies": { "@babel/helper-validator-identifier": "^7.25.9", "js-tokens": "^4.0.0", "picocolors": "^1.0.0" } }, "sha512-RJlIHRueQgwWitWgF8OdFYGZX328Ax5BCemNGlqHfplnRT9ESi8JkFlvaVYbS+UubVY6dpv87Fs2u5M29iNFVQ=="],
|
|
@@ -104,9 +113,9 @@
|
|
|
104
113
|
|
|
105
114
|
"@eslint/js": ["@eslint/js@8.57.0", "", {}, "sha512-Ys+3g2TaW7gADOJzPt83SJtCDhMjndcDMFVQ/Tj9iA1BfJzFKD9mAUXT3OenpuPHbI6P/myECxRJrofUsDx/5g=="],
|
|
106
115
|
|
|
107
|
-
"@grpc/grpc-js": ["@grpc/grpc-js@1.
|
|
116
|
+
"@grpc/grpc-js": ["@grpc/grpc-js@1.14.0", "", { "dependencies": { "@grpc/proto-loader": "^0.8.0", "@js-sdsl/ordered-map": "^4.4.2" } }, "sha512-N8Jx6PaYzcTRNzirReJCtADVoq4z7+1KQ4E70jTg/koQiMoUSN1kbNjPOqpPbhMFhfU1/l7ixspPl8dNY+FoUg=="],
|
|
108
117
|
|
|
109
|
-
"@grpc/proto-loader": ["@grpc/proto-loader@0.
|
|
118
|
+
"@grpc/proto-loader": ["@grpc/proto-loader@0.8.0", "", { "dependencies": { "lodash.camelcase": "^4.3.0", "long": "^5.0.0", "protobufjs": "^7.5.3", "yargs": "^17.7.2" }, "bin": { "proto-loader-gen-types": "build/bin/proto-loader-gen-types.js" } }, "sha512-rc1hOQtjIWGxcxpb9aHAfLpIctjEnsDehj0DAiVfBlmT84uvR0uUtN2hEi/ecvWVjXUGf5qPF4qEgiLOx1YIMQ=="],
|
|
110
119
|
|
|
111
120
|
"@humanwhocodes/config-array": ["@humanwhocodes/config-array@0.11.14", "", { "dependencies": { "@humanwhocodes/object-schema": "^2.0.2", "debug": "^4.3.1", "minimatch": "^3.0.5" } }, "sha512-3T8LkOmg45BV5FICb15QQMsyUSWrQ8AygVfC7ZG32zOalnqrilm018ZVCw0eapXux8FtA33q8PSRSstjee3jSg=="],
|
|
112
121
|
|
|
@@ -294,7 +303,7 @@
|
|
|
294
303
|
|
|
295
304
|
"@switchboard-xyz/on-demand": ["@switchboard-xyz/on-demand@2.4.1", "", { "dependencies": { "@coral-xyz/anchor-30": "npm:@coral-xyz/anchor@0.30.1", "@isaacs/ttlcache": "^1.4.1", "@switchboard-xyz/common": ">=3.0.0", "axios": "^1.8.3", "bs58": "^6.0.0", "buffer": "^6.0.3", "js-yaml": "^4.1.0" } }, "sha512-eSlBp+c8lxpcSgh0/2xK8OaLHPziTSZlcs8V96gZGdiCJz1KgWJRNE1qnIJDOwaGdFecZdwcmajfQRtLRLED3w=="],
|
|
296
305
|
|
|
297
|
-
"@triton-one/yellowstone-grpc": ["@triton-one/yellowstone-grpc@1.
|
|
306
|
+
"@triton-one/yellowstone-grpc": ["@triton-one/yellowstone-grpc@1.4.1", "", { "dependencies": { "@grpc/grpc-js": "^1.8.0" } }, "sha512-ZN49vooxFbOqWttll8u7AOsIVnX+srqX9ddhZ9ttE+OcehUo8c2p2suK8Gr2puab49cgsV0VGjiTn9Gua/ntIw=="],
|
|
298
307
|
|
|
299
308
|
"@ts-graphviz/adapter": ["@ts-graphviz/adapter@2.0.6", "", { "dependencies": { "@ts-graphviz/common": "^2.1.5" } }, "sha512-kJ10lIMSWMJkLkkCG5gt927SnGZcBuG0s0HHswGzcHTgvtUe7yk5/3zTEr0bafzsodsOq5Gi6FhQeV775nC35Q=="],
|
|
300
309
|
|
|
@@ -1176,6 +1185,8 @@
|
|
|
1176
1185
|
|
|
1177
1186
|
"@ellipsis-labs/phoenix-sdk/bs58": ["bs58@5.0.0", "", { "dependencies": { "base-x": "^4.0.0" } }, "sha512-r+ihvQJvahgYT50JD05dyJNKlmmSlMoOGwn1lCcEzanPglg7TxYjioQUYehQ9mAR/+hOSd2jRc/Z2y5UxBymvQ=="],
|
|
1178
1187
|
|
|
1188
|
+
"@grpc/proto-loader/protobufjs": ["protobufjs@7.5.4", "", { "dependencies": { "@protobufjs/aspromise": "^1.1.2", "@protobufjs/base64": "^1.1.2", "@protobufjs/codegen": "^2.0.4", "@protobufjs/eventemitter": "^1.1.0", "@protobufjs/fetch": "^1.1.0", "@protobufjs/float": "^1.0.2", "@protobufjs/inquire": "^1.1.0", "@protobufjs/path": "^1.1.2", "@protobufjs/pool": "^1.1.0", "@protobufjs/utf8": "^1.1.0", "@types/node": ">=13.7.0", "long": "^5.0.0" } }, "sha512-CvexbZtbov6jW2eXAvLukXjXUW1TzFaivC46BpWc/3BpcCysb5Vffu+B3XHMm8lVEuy2Mm4XGex8hBSg1yapPg=="],
|
|
1189
|
+
|
|
1179
1190
|
"@metaplex-foundation/beet-solana/bs58": ["bs58@5.0.0", "", { "dependencies": { "base-x": "^4.0.0" } }, "sha512-r+ihvQJvahgYT50JD05dyJNKlmmSlMoOGwn1lCcEzanPglg7TxYjioQUYehQ9mAR/+hOSd2jRc/Z2y5UxBymvQ=="],
|
|
1180
1191
|
|
|
1181
1192
|
"@metaplex-foundation/solita/@metaplex-foundation/beet": ["@metaplex-foundation/beet@0.4.0", "", { "dependencies": { "ansicolors": "^0.3.2", "bn.js": "^5.2.0", "debug": "^4.3.3" } }, "sha512-2OAKJnLatCc3mBXNL0QmWVQKAWK2C7XDfepgL0p/9+8oSx4bmRAFHFqptl1A/C0U5O3dxGwKfmKluW161OVGcA=="],
|
|
@@ -1248,6 +1259,8 @@
|
|
|
1248
1259
|
|
|
1249
1260
|
"jayson/ws": ["ws@7.5.10", "", { "peerDependencies": { "bufferutil": "^4.0.1", "utf-8-validate": "^5.0.2" }, "optionalPeers": ["bufferutil", "utf-8-validate"] }, "sha512-+dbF1tHwZpXcbOJdVOkzLDxZP1ailvSxM6ZweXTegylPny803bFhA+vqBYw4s31NSAk4S2Qz+AKXK9a4wkdjcQ=="],
|
|
1250
1261
|
|
|
1262
|
+
"jito-ts/@grpc/grpc-js": ["@grpc/grpc-js@1.12.6", "", { "dependencies": { "@grpc/proto-loader": "^0.7.13", "@js-sdsl/ordered-map": "^4.4.2" } }, "sha512-JXUj6PI0oqqzTGvKtzOkxtpsyPRNsrmhh41TtIz/zEB6J+AUiZZ0dxWzcMwO9Ns5rmSPuMdghlTbUuqIM48d3Q=="],
|
|
1263
|
+
|
|
1251
1264
|
"jito-ts/@solana/web3.js": ["@solana/web3.js@1.77.4", "", { "dependencies": { "@babel/runtime": "^7.12.5", "@noble/curves": "^1.0.0", "@noble/hashes": "^1.3.0", "@solana/buffer-layout": "^4.0.0", "agentkeepalive": "^4.2.1", "bigint-buffer": "^1.1.5", "bn.js": "^5.0.0", "borsh": "^0.7.0", "bs58": "^4.0.1", "buffer": "6.0.3", "fast-stable-stringify": "^1.0.0", "jayson": "^4.1.0", "node-fetch": "^2.6.7", "rpc-websockets": "^7.5.1", "superstruct": "^0.14.2" } }, "sha512-XdN0Lh4jdY7J8FYMyucxCwzn6Ga2Sr1DHDWRbqVzk7ZPmmpSPOVWHzO67X1cVT+jNi1D6gZi2tgjHgDPuj6e9Q=="],
|
|
1252
1265
|
|
|
1253
1266
|
"jito-ts/dotenv": ["dotenv@16.4.7", "", {}, "sha512-47qPchRCykZC03FhkYAhrvwU4xDBFIj1QPqaarj6mdM/hgUzfPHcpkHJOn3mJAufFeeAxAzeGsr5X0M4k6fLZQ=="],
|
|
@@ -1332,6 +1345,8 @@
|
|
|
1332
1345
|
|
|
1333
1346
|
"glob/minimatch/brace-expansion": ["brace-expansion@2.0.1", "", { "dependencies": { "balanced-match": "^1.0.0" } }, "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA=="],
|
|
1334
1347
|
|
|
1348
|
+
"jito-ts/@grpc/grpc-js/@grpc/proto-loader": ["@grpc/proto-loader@0.7.13", "", { "dependencies": { "lodash.camelcase": "^4.3.0", "long": "^5.0.0", "protobufjs": "^7.2.5", "yargs": "^17.7.2" }, "bin": { "proto-loader-gen-types": "build/bin/proto-loader-gen-types.js" } }, "sha512-AiXO/bfe9bmxBjxxtYxFAXGZvMaN5s8kO+jBHAJCON8rJoB5YS/D6X7ZNc6XQkuHNmyl4CYaMI1fJ/Gn27RGGw=="],
|
|
1349
|
+
|
|
1335
1350
|
"jito-ts/@solana/web3.js/superstruct": ["superstruct@0.14.2", "", {}, "sha512-nPewA6m9mR3d6k7WkZ8N8zpTWfenFH3q9pA2PkuiZxINr9DKB2+40wEQf0ixn8VaGuJ78AB6iWOtStI+/4FKZQ=="],
|
|
1336
1351
|
|
|
1337
1352
|
"mocha/minimatch/brace-expansion": ["brace-expansion@2.0.1", "", { "dependencies": { "balanced-match": "^1.0.0" } }, "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA=="],
|
|
@@ -4,13 +4,14 @@ import { ResubOpts, GrpcConfigs } from './types';
|
|
|
4
4
|
import { Program } from '@coral-xyz/anchor';
|
|
5
5
|
import { PublicKey } from '@solana/web3.js';
|
|
6
6
|
import { WebSocketAccountSubscriber } from './webSocketAccountSubscriber';
|
|
7
|
+
import { Client } from '../isomorphic/grpc';
|
|
7
8
|
export declare class grpcAccountSubscriber<T> extends WebSocketAccountSubscriber<T> {
|
|
8
9
|
private client;
|
|
9
10
|
private stream;
|
|
10
11
|
private commitmentLevel;
|
|
11
12
|
listenerId?: number;
|
|
12
13
|
private constructor();
|
|
13
|
-
static create<U>(grpcConfigs: GrpcConfigs, accountName: string, program: Program, accountPublicKey: PublicKey, decodeBuffer?: (buffer: Buffer) => U, resubOpts?: ResubOpts): Promise<grpcAccountSubscriber<U>>;
|
|
14
|
+
static create<U>(grpcConfigs: GrpcConfigs, accountName: string, program: Program, accountPublicKey: PublicKey, decodeBuffer?: (buffer: Buffer) => U, resubOpts?: ResubOpts, clientProp?: Client): Promise<grpcAccountSubscriber<U>>;
|
|
14
15
|
subscribe(onChange: (data: T) => void): Promise<void>;
|
|
15
16
|
unsubscribe(onResub?: boolean): Promise<void>;
|
|
16
17
|
}
|
|
@@ -34,9 +34,11 @@ class grpcAccountSubscriber extends webSocketAccountSubscriber_1.WebSocketAccoun
|
|
|
34
34
|
this.client = client;
|
|
35
35
|
this.commitmentLevel = commitmentLevel;
|
|
36
36
|
}
|
|
37
|
-
static async create(grpcConfigs, accountName, program, accountPublicKey, decodeBuffer, resubOpts) {
|
|
37
|
+
static async create(grpcConfigs, accountName, program, accountPublicKey, decodeBuffer, resubOpts, clientProp) {
|
|
38
38
|
var _a, _b;
|
|
39
|
-
const client =
|
|
39
|
+
const client = clientProp
|
|
40
|
+
? clientProp
|
|
41
|
+
: await (0, grpc_1.createClient)(grpcConfigs.endpoint, grpcConfigs.token, (_a = grpcConfigs.channelOptions) !== null && _a !== void 0 ? _a : {});
|
|
40
42
|
const commitmentLevel =
|
|
41
43
|
// @ts-ignore :: isomorphic exported enum fails typescript but will work at runtime
|
|
42
44
|
(_b = grpcConfigs.commitmentLevel) !== null && _b !== void 0 ? _b : grpc_1.CommitmentLevel.CONFIRMED;
|
|
@@ -2,7 +2,7 @@ import { WebSocketDriftClientAccountSubscriber } from './webSocketDriftClientAcc
|
|
|
2
2
|
import { OracleInfo } from '../oracles/types';
|
|
3
3
|
import { Program } from '@coral-xyz/anchor';
|
|
4
4
|
import { DelistedMarketSetting, GrpcConfigs, ResubOpts } from './types';
|
|
5
|
-
export declare class
|
|
5
|
+
export declare class grpcDriftClientAccountSubscriber extends WebSocketDriftClientAccountSubscriber {
|
|
6
6
|
private grpcConfigs;
|
|
7
7
|
constructor(grpcConfigs: GrpcConfigs, program: Program, perpMarketIndexes: number[], spotMarketIndexes: number[], oracleInfos: OracleInfo[], shouldFindAllMarketsAndOracles: boolean, delistedMarketSetting: DelistedMarketSetting, resubOpts?: ResubOpts);
|
|
8
8
|
subscribe(): Promise<boolean>;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.grpcDriftClientAccountSubscriber = void 0;
|
|
4
4
|
const webSocketDriftClientAccountSubscriber_1 = require("./webSocketDriftClientAccountSubscriber");
|
|
5
5
|
const config_1 = require("../config");
|
|
6
6
|
const pda_1 = require("../addresses/pda");
|
|
7
7
|
const grpcAccountSubscriber_1 = require("./grpcAccountSubscriber");
|
|
8
8
|
const oracleId_1 = require("../oracles/oracleId");
|
|
9
|
-
class
|
|
9
|
+
class grpcDriftClientAccountSubscriber extends webSocketDriftClientAccountSubscriber_1.WebSocketDriftClientAccountSubscriber {
|
|
10
10
|
constructor(grpcConfigs, program, perpMarketIndexes, spotMarketIndexes, oracleInfos, shouldFindAllMarketsAndOracles, delistedMarketSetting, resubOpts) {
|
|
11
11
|
super(program, perpMarketIndexes, spotMarketIndexes, oracleInfos, shouldFindAllMarketsAndOracles, delistedMarketSetting, resubOpts);
|
|
12
12
|
this.grpcConfigs = grpcConfigs;
|
|
@@ -96,4 +96,4 @@ class gprcDriftClientAccountSubscriber extends webSocketDriftClientAccountSubscr
|
|
|
96
96
|
return true;
|
|
97
97
|
}
|
|
98
98
|
}
|
|
99
|
-
exports.
|
|
99
|
+
exports.grpcDriftClientAccountSubscriber = grpcDriftClientAccountSubscriber;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { WebSocketDriftClientAccountSubscriber } from './webSocketDriftClientAccountSubscriber';
|
|
2
|
+
import { OracleInfo } from '../oracles/types';
|
|
3
|
+
import { Program } from '@coral-xyz/anchor';
|
|
4
|
+
import { DataAndSlot, DelistedMarketSetting, GrpcConfigs, ResubOpts } from './types';
|
|
5
|
+
import { PerpMarketAccount, SpotMarketAccount } from '../types';
|
|
6
|
+
export declare class grpcDriftClientAccountSubscriberV2 extends WebSocketDriftClientAccountSubscriber {
|
|
7
|
+
private grpcConfigs;
|
|
8
|
+
private perpMarketsSubscriber?;
|
|
9
|
+
private spotMarketsSubscriber?;
|
|
10
|
+
private oracleMultiSubscriber?;
|
|
11
|
+
private perpMarketIndexToAccountPubkeyMap;
|
|
12
|
+
private spotMarketIndexToAccountPubkeyMap;
|
|
13
|
+
constructor(grpcConfigs: GrpcConfigs, program: Program, perpMarketIndexes: number[], spotMarketIndexes: number[], oracleInfos: OracleInfo[], shouldFindAllMarketsAndOracles: boolean, delistedMarketSetting: DelistedMarketSetting, resubOpts?: ResubOpts);
|
|
14
|
+
subscribe(): Promise<boolean>;
|
|
15
|
+
getMarketAccountAndSlot(marketIndex: number): DataAndSlot<PerpMarketAccount> | undefined;
|
|
16
|
+
getSpotMarketAccountAndSlot(marketIndex: number): DataAndSlot<SpotMarketAccount> | undefined;
|
|
17
|
+
subscribeToPerpMarketAccounts(): Promise<boolean>;
|
|
18
|
+
subscribeToSpotMarketAccounts(): Promise<boolean>;
|
|
19
|
+
subscribeToOracles(): Promise<boolean>;
|
|
20
|
+
unsubscribeFromOracles(): Promise<void>;
|
|
21
|
+
unsubscribe(): Promise<void>;
|
|
22
|
+
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.grpcDriftClientAccountSubscriberV2 = void 0;
|
|
4
|
+
const webSocketDriftClientAccountSubscriber_1 = require("./webSocketDriftClientAccountSubscriber");
|
|
5
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
6
|
+
const config_1 = require("../config");
|
|
7
|
+
const pda_1 = require("../addresses/pda");
|
|
8
|
+
const grpcAccountSubscriber_1 = require("./grpcAccountSubscriber");
|
|
9
|
+
const grpcMultiAccountSubscriber_1 = require("./grpcMultiAccountSubscriber");
|
|
10
|
+
const oracleId_1 = require("../oracles/oracleId");
|
|
11
|
+
class grpcDriftClientAccountSubscriberV2 extends webSocketDriftClientAccountSubscriber_1.WebSocketDriftClientAccountSubscriber {
|
|
12
|
+
constructor(grpcConfigs, program, perpMarketIndexes, spotMarketIndexes, oracleInfos, shouldFindAllMarketsAndOracles, delistedMarketSetting, resubOpts) {
|
|
13
|
+
super(program, perpMarketIndexes, spotMarketIndexes, oracleInfos, shouldFindAllMarketsAndOracles, delistedMarketSetting, resubOpts);
|
|
14
|
+
this.perpMarketIndexToAccountPubkeyMap = new Map();
|
|
15
|
+
this.spotMarketIndexToAccountPubkeyMap = new Map();
|
|
16
|
+
this.grpcConfigs = grpcConfigs;
|
|
17
|
+
}
|
|
18
|
+
async subscribe() {
|
|
19
|
+
if (this.isSubscribed) {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
if (this.isSubscribing) {
|
|
23
|
+
return await this.subscriptionPromise;
|
|
24
|
+
}
|
|
25
|
+
this.isSubscribing = true;
|
|
26
|
+
this.subscriptionPromise = new Promise((res) => {
|
|
27
|
+
this.subscriptionPromiseResolver = res;
|
|
28
|
+
});
|
|
29
|
+
if (this.shouldFindAllMarketsAndOracles) {
|
|
30
|
+
const { perpMarketIndexes, perpMarketAccounts, spotMarketIndexes, spotMarketAccounts, oracleInfos, } = await (0, config_1.findAllMarketAndOracles)(this.program);
|
|
31
|
+
this.perpMarketIndexes = perpMarketIndexes;
|
|
32
|
+
this.spotMarketIndexes = spotMarketIndexes;
|
|
33
|
+
this.oracleInfos = oracleInfos;
|
|
34
|
+
// front run and set the initial data here to save extra gma call in set initial data
|
|
35
|
+
this.initialPerpMarketAccountData = new Map(perpMarketAccounts.map((market) => [market.marketIndex, market]));
|
|
36
|
+
this.initialSpotMarketAccountData = new Map(spotMarketAccounts.map((market) => [market.marketIndex, market]));
|
|
37
|
+
}
|
|
38
|
+
const statePublicKey = await (0, pda_1.getDriftStateAccountPublicKey)(this.program.programId);
|
|
39
|
+
// create and activate main state account subscription
|
|
40
|
+
this.stateAccountSubscriber =
|
|
41
|
+
await grpcAccountSubscriber_1.grpcAccountSubscriber.create(this.grpcConfigs, 'state', this.program, statePublicKey, undefined, undefined);
|
|
42
|
+
await this.stateAccountSubscriber.subscribe((data) => {
|
|
43
|
+
this.eventEmitter.emit('stateAccountUpdate', data);
|
|
44
|
+
this.eventEmitter.emit('update');
|
|
45
|
+
});
|
|
46
|
+
// set initial data to avoid spamming getAccountInfo calls in webSocketAccountSubscriber
|
|
47
|
+
await this.setInitialData();
|
|
48
|
+
// subscribe to perp + spot markets (separate) and oracles
|
|
49
|
+
await Promise.all([
|
|
50
|
+
this.subscribeToPerpMarketAccounts(),
|
|
51
|
+
this.subscribeToSpotMarketAccounts(),
|
|
52
|
+
this.subscribeToOracles(),
|
|
53
|
+
]);
|
|
54
|
+
this.eventEmitter.emit('update');
|
|
55
|
+
await this.handleDelistedMarkets();
|
|
56
|
+
await Promise.all([this.setPerpOracleMap(), this.setSpotOracleMap()]);
|
|
57
|
+
this.subscriptionPromiseResolver(true);
|
|
58
|
+
this.isSubscribing = false;
|
|
59
|
+
this.isSubscribed = true;
|
|
60
|
+
// delete initial data
|
|
61
|
+
this.removeInitialData();
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
getMarketAccountAndSlot(marketIndex) {
|
|
65
|
+
var _a;
|
|
66
|
+
return (_a = this.perpMarketsSubscriber) === null || _a === void 0 ? void 0 : _a.getAccountData(this.perpMarketIndexToAccountPubkeyMap.get(marketIndex));
|
|
67
|
+
}
|
|
68
|
+
getSpotMarketAccountAndSlot(marketIndex) {
|
|
69
|
+
var _a;
|
|
70
|
+
return (_a = this.spotMarketsSubscriber) === null || _a === void 0 ? void 0 : _a.getAccountData(this.spotMarketIndexToAccountPubkeyMap.get(marketIndex));
|
|
71
|
+
}
|
|
72
|
+
async subscribeToPerpMarketAccounts() {
|
|
73
|
+
const perpMarketIndexToAccountPubkeys = await Promise.all(this.perpMarketIndexes.map(async (marketIndex) => [
|
|
74
|
+
marketIndex,
|
|
75
|
+
await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, marketIndex),
|
|
76
|
+
]));
|
|
77
|
+
for (const [marketIndex, accountPubkey,] of perpMarketIndexToAccountPubkeys) {
|
|
78
|
+
this.perpMarketIndexToAccountPubkeyMap.set(marketIndex, accountPubkey);
|
|
79
|
+
}
|
|
80
|
+
const perpMarketPubkeys = perpMarketIndexToAccountPubkeys.map(([_, accountPubkey]) => accountPubkey);
|
|
81
|
+
this.perpMarketsSubscriber =
|
|
82
|
+
await grpcMultiAccountSubscriber_1.grpcMultiAccountSubscriber.create(this.grpcConfigs, 'perpMarket', this.program, undefined, this.resubOpts, undefined, async () => {
|
|
83
|
+
var _a;
|
|
84
|
+
try {
|
|
85
|
+
if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.logResubMessages) {
|
|
86
|
+
console.log('[grpcDriftClientAccountSubscriberV2] perp markets subscriber unsubscribed; resubscribing');
|
|
87
|
+
}
|
|
88
|
+
await this.subscribeToPerpMarketAccounts();
|
|
89
|
+
}
|
|
90
|
+
catch (e) {
|
|
91
|
+
console.error('Perp markets resubscribe failed:', e);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
for (const data of this.initialPerpMarketAccountData.values()) {
|
|
95
|
+
this.perpMarketsSubscriber.setAccountData(data.pubkey, data);
|
|
96
|
+
}
|
|
97
|
+
await this.perpMarketsSubscriber.subscribe(perpMarketPubkeys, (_accountId, data) => {
|
|
98
|
+
this.eventEmitter.emit('perpMarketAccountUpdate', data);
|
|
99
|
+
this.eventEmitter.emit('update');
|
|
100
|
+
});
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
async subscribeToSpotMarketAccounts() {
|
|
104
|
+
const spotMarketIndexToAccountPubkeys = await Promise.all(this.spotMarketIndexes.map(async (marketIndex) => [
|
|
105
|
+
marketIndex,
|
|
106
|
+
await (0, pda_1.getSpotMarketPublicKey)(this.program.programId, marketIndex),
|
|
107
|
+
]));
|
|
108
|
+
for (const [marketIndex, accountPubkey,] of spotMarketIndexToAccountPubkeys) {
|
|
109
|
+
this.spotMarketIndexToAccountPubkeyMap.set(marketIndex, accountPubkey);
|
|
110
|
+
}
|
|
111
|
+
const spotMarketPubkeys = spotMarketIndexToAccountPubkeys.map(([_, accountPubkey]) => accountPubkey);
|
|
112
|
+
this.spotMarketsSubscriber =
|
|
113
|
+
await grpcMultiAccountSubscriber_1.grpcMultiAccountSubscriber.create(this.grpcConfigs, 'spotMarket', this.program, undefined, this.resubOpts, undefined, async () => {
|
|
114
|
+
var _a;
|
|
115
|
+
try {
|
|
116
|
+
if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.logResubMessages) {
|
|
117
|
+
console.log('[grpcDriftClientAccountSubscriberV2] spot markets subscriber unsubscribed; resubscribing');
|
|
118
|
+
}
|
|
119
|
+
await this.subscribeToSpotMarketAccounts();
|
|
120
|
+
}
|
|
121
|
+
catch (e) {
|
|
122
|
+
console.error('Spot markets resubscribe failed:', e);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
for (const data of this.initialSpotMarketAccountData.values()) {
|
|
126
|
+
this.spotMarketsSubscriber.setAccountData(data.pubkey, data);
|
|
127
|
+
}
|
|
128
|
+
await this.spotMarketsSubscriber.subscribe(spotMarketPubkeys, (_accountId, data) => {
|
|
129
|
+
this.eventEmitter.emit('spotMarketAccountUpdate', data);
|
|
130
|
+
this.eventEmitter.emit('update');
|
|
131
|
+
});
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
async subscribeToOracles() {
|
|
135
|
+
// Build list of unique oracle pubkeys and a lookup for sources
|
|
136
|
+
const uniqueOraclePubkeys = new Map();
|
|
137
|
+
for (const info of this.oracleInfos) {
|
|
138
|
+
const id = (0, oracleId_1.getOracleId)(info.publicKey, info.source);
|
|
139
|
+
if (!uniqueOraclePubkeys.has(id) &&
|
|
140
|
+
!info.publicKey.equals(web3_js_1.PublicKey.default)) {
|
|
141
|
+
uniqueOraclePubkeys.set(id, info);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const oraclePubkeys = Array.from(uniqueOraclePubkeys.values()).map((i) => i.publicKey);
|
|
145
|
+
const pubkeyToSource = new Map(Array.from(uniqueOraclePubkeys.values()).map((i) => [
|
|
146
|
+
i.publicKey.toBase58(),
|
|
147
|
+
i.source,
|
|
148
|
+
]));
|
|
149
|
+
this.oracleMultiSubscriber =
|
|
150
|
+
await grpcMultiAccountSubscriber_1.grpcMultiAccountSubscriber.create(this.grpcConfigs, 'oracle', this.program, (buffer, pubkey) => {
|
|
151
|
+
if (!pubkey) {
|
|
152
|
+
throw new Error('Oracle pubkey missing in decode');
|
|
153
|
+
}
|
|
154
|
+
const source = pubkeyToSource.get(pubkey);
|
|
155
|
+
const client = this.oracleClientCache.get(source, this.program.provider.connection, this.program);
|
|
156
|
+
return client.getOraclePriceDataFromBuffer(buffer);
|
|
157
|
+
}, this.resubOpts, undefined, async () => {
|
|
158
|
+
var _a;
|
|
159
|
+
try {
|
|
160
|
+
if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.logResubMessages) {
|
|
161
|
+
console.log('[grpcDriftClientAccountSubscriberV2] oracle subscriber unsubscribed; resubscribing');
|
|
162
|
+
}
|
|
163
|
+
await this.subscribeToOracles();
|
|
164
|
+
}
|
|
165
|
+
catch (e) {
|
|
166
|
+
console.error('Oracle resubscribe failed:', e);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
for (const data of this.initialOraclePriceData.entries()) {
|
|
170
|
+
this.oracleMultiSubscriber.setAccountData(new web3_js_1.PublicKey(data[0]), data[1]);
|
|
171
|
+
}
|
|
172
|
+
await this.oracleMultiSubscriber.subscribe(oraclePubkeys, (accountId, data) => {
|
|
173
|
+
const source = pubkeyToSource.get(accountId.toBase58());
|
|
174
|
+
this.eventEmitter.emit('oraclePriceUpdate', accountId, source, data);
|
|
175
|
+
this.eventEmitter.emit('update');
|
|
176
|
+
});
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
async unsubscribeFromOracles() {
|
|
180
|
+
if (this.oracleMultiSubscriber) {
|
|
181
|
+
await this.oracleMultiSubscriber.unsubscribe();
|
|
182
|
+
this.oracleMultiSubscriber = undefined;
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
await super.unsubscribeFromOracles();
|
|
186
|
+
}
|
|
187
|
+
async unsubscribe() {
|
|
188
|
+
if (this.isSubscribed) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
await this.stateAccountSubscriber.unsubscribe();
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
exports.grpcDriftClientAccountSubscriberV2 = grpcDriftClientAccountSubscriberV2;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { Program } from '@coral-xyz/anchor';
|
|
4
|
+
import { Context, PublicKey } from '@solana/web3.js';
|
|
5
|
+
import { Client } from '../isomorphic/grpc';
|
|
6
|
+
import { DataAndSlot, GrpcConfigs, ResubOpts } from './types';
|
|
7
|
+
export declare class grpcMultiAccountSubscriber<T> {
|
|
8
|
+
private client;
|
|
9
|
+
private stream;
|
|
10
|
+
private commitmentLevel;
|
|
11
|
+
private program;
|
|
12
|
+
private accountName;
|
|
13
|
+
private decodeBufferFn?;
|
|
14
|
+
private resubOpts?;
|
|
15
|
+
private onUnsubscribe?;
|
|
16
|
+
listenerId?: number;
|
|
17
|
+
isUnsubscribing: boolean;
|
|
18
|
+
private timeoutId?;
|
|
19
|
+
private receivingData;
|
|
20
|
+
private subscribedAccounts;
|
|
21
|
+
private onChangeMap;
|
|
22
|
+
private dataMap;
|
|
23
|
+
private constructor();
|
|
24
|
+
static create<U>(grpcConfigs: GrpcConfigs, accountName: string, program: Program, decodeBuffer?: (buffer: Buffer, pubkey?: string) => U, resubOpts?: ResubOpts, clientProp?: Client, onUnsubscribe?: () => Promise<void>): Promise<grpcMultiAccountSubscriber<U>>;
|
|
25
|
+
setAccountData(accountPubkey: PublicKey, data: T, slot?: number): void;
|
|
26
|
+
getAccountData(accountPubkey: PublicKey): DataAndSlot<T> | undefined;
|
|
27
|
+
subscribe(accounts: PublicKey[], onChange: (accountId: PublicKey, data: T, context: Context, buffer: Buffer) => void): Promise<void>;
|
|
28
|
+
addAccounts(accounts: PublicKey[]): Promise<void>;
|
|
29
|
+
removeAccounts(accounts: PublicKey[]): Promise<void>;
|
|
30
|
+
unsubscribe(): Promise<void>;
|
|
31
|
+
private setTimeout;
|
|
32
|
+
private capitalize;
|
|
33
|
+
}
|