@drift-labs/sdk 2.142.0-beta.4 → 2.142.0-beta.6
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 +2 -0
- package/lib/browser/accounts/grpcDriftClientAccountSubscriberV2.js +50 -1
- package/lib/browser/accounts/grpcMultiAccountSubscriber.d.ts +1 -0
- package/lib/browser/accounts/grpcMultiAccountSubscriber.js +3 -0
- package/lib/node/accounts/grpcDriftClientAccountSubscriberV2.d.ts +2 -0
- package/lib/node/accounts/grpcDriftClientAccountSubscriberV2.d.ts.map +1 -1
- package/lib/node/accounts/grpcDriftClientAccountSubscriberV2.js +50 -1
- 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 +3 -0
- package/package.json +1 -1
- package/scripts/client-test.ts +76 -4
- package/src/accounts/grpcDriftClientAccountSubscriberV2.ts +58 -5
- package/src/accounts/grpcMultiAccountSubscriber.ts +4 -0
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.142.0-beta.
|
|
1
|
+
2.142.0-beta.6
|
|
@@ -14,6 +14,8 @@ export declare class grpcDriftClientAccountSubscriberV2 extends WebSocketDriftCl
|
|
|
14
14
|
subscribe(): Promise<boolean>;
|
|
15
15
|
getMarketAccountAndSlot(marketIndex: number): DataAndSlot<PerpMarketAccount> | undefined;
|
|
16
16
|
getSpotMarketAccountAndSlot(marketIndex: number): DataAndSlot<SpotMarketAccount> | undefined;
|
|
17
|
+
setPerpOracleMap(): Promise<void>;
|
|
18
|
+
setSpotOracleMap(): Promise<void>;
|
|
17
19
|
subscribeToPerpMarketAccounts(): Promise<boolean>;
|
|
18
20
|
subscribeToSpotMarketAccounts(): Promise<boolean>;
|
|
19
21
|
subscribeToOracles(): Promise<boolean>;
|
|
@@ -69,6 +69,54 @@ class grpcDriftClientAccountSubscriberV2 extends webSocketDriftClientAccountSubs
|
|
|
69
69
|
var _a;
|
|
70
70
|
return (_a = this.spotMarketsSubscriber) === null || _a === void 0 ? void 0 : _a.getAccountData(this.spotMarketIndexToAccountPubkeyMap.get(marketIndex));
|
|
71
71
|
}
|
|
72
|
+
async setPerpOracleMap() {
|
|
73
|
+
var _a;
|
|
74
|
+
const perpMarketsMap = (_a = this.perpMarketsSubscriber) === null || _a === void 0 ? void 0 : _a.getAccountDataMap();
|
|
75
|
+
const perpMarkets = Array.from(perpMarketsMap.values());
|
|
76
|
+
const addOraclePromises = [];
|
|
77
|
+
for (const perpMarket of perpMarkets) {
|
|
78
|
+
if (!perpMarket || !perpMarket.data) {
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
const perpMarketAccount = perpMarket.data;
|
|
82
|
+
const perpMarketIndex = perpMarketAccount.marketIndex;
|
|
83
|
+
const oracle = perpMarketAccount.amm.oracle;
|
|
84
|
+
const oracleId = (0, oracleId_1.getOracleId)(oracle, perpMarket.data.amm.oracleSource);
|
|
85
|
+
if (!this.oracleSubscribers.has(oracleId)) {
|
|
86
|
+
addOraclePromises.push(this.addOracle({
|
|
87
|
+
publicKey: oracle,
|
|
88
|
+
source: perpMarket.data.amm.oracleSource,
|
|
89
|
+
}));
|
|
90
|
+
}
|
|
91
|
+
this.perpOracleMap.set(perpMarketIndex, oracle);
|
|
92
|
+
this.perpOracleStringMap.set(perpMarketIndex, oracleId);
|
|
93
|
+
}
|
|
94
|
+
await Promise.all(addOraclePromises);
|
|
95
|
+
}
|
|
96
|
+
async setSpotOracleMap() {
|
|
97
|
+
var _a;
|
|
98
|
+
const spotMarketsMap = (_a = this.spotMarketsSubscriber) === null || _a === void 0 ? void 0 : _a.getAccountDataMap();
|
|
99
|
+
const spotMarkets = Array.from(spotMarketsMap.values());
|
|
100
|
+
const addOraclePromises = [];
|
|
101
|
+
for (const spotMarket of spotMarkets) {
|
|
102
|
+
if (!spotMarket || !spotMarket.data) {
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
const spotMarketAccount = spotMarket.data;
|
|
106
|
+
const spotMarketIndex = spotMarketAccount.marketIndex;
|
|
107
|
+
const oracle = spotMarketAccount.oracle;
|
|
108
|
+
const oracleId = (0, oracleId_1.getOracleId)(oracle, spotMarketAccount.oracleSource);
|
|
109
|
+
if (!this.oracleSubscribers.has(oracleId)) {
|
|
110
|
+
addOraclePromises.push(this.addOracle({
|
|
111
|
+
publicKey: oracle,
|
|
112
|
+
source: spotMarketAccount.oracleSource,
|
|
113
|
+
}));
|
|
114
|
+
}
|
|
115
|
+
this.spotOracleMap.set(spotMarketIndex, oracle);
|
|
116
|
+
this.spotOracleStringMap.set(spotMarketIndex, oracleId);
|
|
117
|
+
}
|
|
118
|
+
await Promise.all(addOraclePromises);
|
|
119
|
+
}
|
|
72
120
|
async subscribeToPerpMarketAccounts() {
|
|
73
121
|
const perpMarketIndexToAccountPubkeys = await Promise.all(this.perpMarketIndexes.map(async (marketIndex) => [
|
|
74
122
|
marketIndex,
|
|
@@ -167,7 +215,8 @@ class grpcDriftClientAccountSubscriberV2 extends webSocketDriftClientAccountSubs
|
|
|
167
215
|
}
|
|
168
216
|
});
|
|
169
217
|
for (const data of this.initialOraclePriceData.entries()) {
|
|
170
|
-
|
|
218
|
+
const { publicKey } = (0, oracleId_1.getPublicKeyAndSourceFromOracleId)(data[0]);
|
|
219
|
+
this.oracleMultiSubscriber.setAccountData(publicKey, data[1]);
|
|
171
220
|
}
|
|
172
221
|
await this.oracleMultiSubscriber.subscribe(oraclePubkeys, (accountId, data) => {
|
|
173
222
|
const source = pubkeyToSource.get(accountId.toBase58());
|
|
@@ -24,6 +24,7 @@ export declare class grpcMultiAccountSubscriber<T> {
|
|
|
24
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
25
|
setAccountData(accountPubkey: PublicKey, data: T, slot?: number): void;
|
|
26
26
|
getAccountData(accountPubkey: PublicKey): DataAndSlot<T> | undefined;
|
|
27
|
+
getAccountDataMap(): Map<string, DataAndSlot<T>>;
|
|
27
28
|
subscribe(accounts: PublicKey[], onChange: (accountId: PublicKey, data: T, context: Context, buffer: Buffer) => void): Promise<void>;
|
|
28
29
|
addAccounts(accounts: PublicKey[]): Promise<void>;
|
|
29
30
|
removeAccounts(accounts: PublicKey[]): Promise<void>;
|
|
@@ -62,6 +62,9 @@ class grpcMultiAccountSubscriber {
|
|
|
62
62
|
getAccountData(accountPubkey) {
|
|
63
63
|
return this.dataMap.get(accountPubkey.toBase58());
|
|
64
64
|
}
|
|
65
|
+
getAccountDataMap() {
|
|
66
|
+
return this.dataMap;
|
|
67
|
+
}
|
|
65
68
|
async subscribe(accounts, onChange) {
|
|
66
69
|
if (this.listenerId != null || this.isUnsubscribing) {
|
|
67
70
|
return;
|
|
@@ -14,6 +14,8 @@ export declare class grpcDriftClientAccountSubscriberV2 extends WebSocketDriftCl
|
|
|
14
14
|
subscribe(): Promise<boolean>;
|
|
15
15
|
getMarketAccountAndSlot(marketIndex: number): DataAndSlot<PerpMarketAccount> | undefined;
|
|
16
16
|
getSpotMarketAccountAndSlot(marketIndex: number): DataAndSlot<SpotMarketAccount> | undefined;
|
|
17
|
+
setPerpOracleMap(): Promise<void>;
|
|
18
|
+
setSpotOracleMap(): Promise<void>;
|
|
17
19
|
subscribeToPerpMarketAccounts(): Promise<boolean>;
|
|
18
20
|
subscribeToSpotMarketAccounts(): Promise<boolean>;
|
|
19
21
|
subscribeToOracles(): Promise<boolean>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"grpcDriftClientAccountSubscriberV2.d.ts","sourceRoot":"","sources":["../../../src/accounts/grpcDriftClientAccountSubscriberV2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qCAAqC,EAAE,MAAM,yCAAyC,CAAC;AAChG,OAAO,EAAE,UAAU,EAAmB,MAAM,kBAAkB,CAAC;AAC/D,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAQ5C,OAAO,EACN,WAAW,EACX,qBAAqB,EACrB,WAAW,EACX,SAAS,EACT,MAAM,SAAS,CAAC;AAGjB,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAgB,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"grpcDriftClientAccountSubscriberV2.d.ts","sourceRoot":"","sources":["../../../src/accounts/grpcDriftClientAccountSubscriberV2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qCAAqC,EAAE,MAAM,yCAAyC,CAAC;AAChG,OAAO,EAAE,UAAU,EAAmB,MAAM,kBAAkB,CAAC;AAC/D,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAQ5C,OAAO,EACN,WAAW,EACX,qBAAqB,EACrB,WAAW,EACX,SAAS,EACT,MAAM,SAAS,CAAC;AAGjB,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAgB,MAAM,UAAU,CAAC;AAM9E,qBAAa,kCAAmC,SAAQ,qCAAqC;IAC5F,OAAO,CAAC,WAAW,CAAc;IACjC,OAAO,CAAC,qBAAqB,CAAC,CAAgD;IAC9E,OAAO,CAAC,qBAAqB,CAAC,CAAgD;IAC9E,OAAO,CAAC,qBAAqB,CAAC,CAA8C;IAC5E,OAAO,CAAC,iCAAiC,CAAgC;IACzE,OAAO,CAAC,iCAAiC,CAAgC;gBAGxE,WAAW,EAAE,WAAW,EACxB,OAAO,EAAE,OAAO,EAChB,iBAAiB,EAAE,MAAM,EAAE,EAC3B,iBAAiB,EAAE,MAAM,EAAE,EAC3B,WAAW,EAAE,UAAU,EAAE,EACzB,8BAA8B,EAAE,OAAO,EACvC,qBAAqB,EAAE,qBAAqB,EAC5C,SAAS,CAAC,EAAE,SAAS;IAcT,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;IAiFjC,uBAAuB,CAC/B,WAAW,EAAE,MAAM,GACjB,WAAW,CAAC,iBAAiB,CAAC,GAAG,SAAS;IAMpC,2BAA2B,CACnC,WAAW,EAAE,MAAM,GACjB,WAAW,CAAC,iBAAiB,CAAC,GAAG,SAAS;IAM9B,gBAAgB;IA0BhB,gBAAgB;IA0BhB,6BAA6B,IAAI,OAAO,CAAC,OAAO,CAAC;IA2DjD,6BAA6B,IAAI,OAAO,CAAC,OAAO,CAAC;IA2DjD,kBAAkB,IAAI,OAAO,CAAC,OAAO,CAAC;IAyE/C,sBAAsB,IAAI,OAAO,CAAC,IAAI,CAAC;IAS9B,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;CAO3C"}
|
|
@@ -69,6 +69,54 @@ class grpcDriftClientAccountSubscriberV2 extends webSocketDriftClientAccountSubs
|
|
|
69
69
|
var _a;
|
|
70
70
|
return (_a = this.spotMarketsSubscriber) === null || _a === void 0 ? void 0 : _a.getAccountData(this.spotMarketIndexToAccountPubkeyMap.get(marketIndex));
|
|
71
71
|
}
|
|
72
|
+
async setPerpOracleMap() {
|
|
73
|
+
var _a;
|
|
74
|
+
const perpMarketsMap = (_a = this.perpMarketsSubscriber) === null || _a === void 0 ? void 0 : _a.getAccountDataMap();
|
|
75
|
+
const perpMarkets = Array.from(perpMarketsMap.values());
|
|
76
|
+
const addOraclePromises = [];
|
|
77
|
+
for (const perpMarket of perpMarkets) {
|
|
78
|
+
if (!perpMarket || !perpMarket.data) {
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
const perpMarketAccount = perpMarket.data;
|
|
82
|
+
const perpMarketIndex = perpMarketAccount.marketIndex;
|
|
83
|
+
const oracle = perpMarketAccount.amm.oracle;
|
|
84
|
+
const oracleId = (0, oracleId_1.getOracleId)(oracle, perpMarket.data.amm.oracleSource);
|
|
85
|
+
if (!this.oracleSubscribers.has(oracleId)) {
|
|
86
|
+
addOraclePromises.push(this.addOracle({
|
|
87
|
+
publicKey: oracle,
|
|
88
|
+
source: perpMarket.data.amm.oracleSource,
|
|
89
|
+
}));
|
|
90
|
+
}
|
|
91
|
+
this.perpOracleMap.set(perpMarketIndex, oracle);
|
|
92
|
+
this.perpOracleStringMap.set(perpMarketIndex, oracleId);
|
|
93
|
+
}
|
|
94
|
+
await Promise.all(addOraclePromises);
|
|
95
|
+
}
|
|
96
|
+
async setSpotOracleMap() {
|
|
97
|
+
var _a;
|
|
98
|
+
const spotMarketsMap = (_a = this.spotMarketsSubscriber) === null || _a === void 0 ? void 0 : _a.getAccountDataMap();
|
|
99
|
+
const spotMarkets = Array.from(spotMarketsMap.values());
|
|
100
|
+
const addOraclePromises = [];
|
|
101
|
+
for (const spotMarket of spotMarkets) {
|
|
102
|
+
if (!spotMarket || !spotMarket.data) {
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
const spotMarketAccount = spotMarket.data;
|
|
106
|
+
const spotMarketIndex = spotMarketAccount.marketIndex;
|
|
107
|
+
const oracle = spotMarketAccount.oracle;
|
|
108
|
+
const oracleId = (0, oracleId_1.getOracleId)(oracle, spotMarketAccount.oracleSource);
|
|
109
|
+
if (!this.oracleSubscribers.has(oracleId)) {
|
|
110
|
+
addOraclePromises.push(this.addOracle({
|
|
111
|
+
publicKey: oracle,
|
|
112
|
+
source: spotMarketAccount.oracleSource,
|
|
113
|
+
}));
|
|
114
|
+
}
|
|
115
|
+
this.spotOracleMap.set(spotMarketIndex, oracle);
|
|
116
|
+
this.spotOracleStringMap.set(spotMarketIndex, oracleId);
|
|
117
|
+
}
|
|
118
|
+
await Promise.all(addOraclePromises);
|
|
119
|
+
}
|
|
72
120
|
async subscribeToPerpMarketAccounts() {
|
|
73
121
|
const perpMarketIndexToAccountPubkeys = await Promise.all(this.perpMarketIndexes.map(async (marketIndex) => [
|
|
74
122
|
marketIndex,
|
|
@@ -167,7 +215,8 @@ class grpcDriftClientAccountSubscriberV2 extends webSocketDriftClientAccountSubs
|
|
|
167
215
|
}
|
|
168
216
|
});
|
|
169
217
|
for (const data of this.initialOraclePriceData.entries()) {
|
|
170
|
-
|
|
218
|
+
const { publicKey } = (0, oracleId_1.getPublicKeyAndSourceFromOracleId)(data[0]);
|
|
219
|
+
this.oracleMultiSubscriber.setAccountData(publicKey, data[1]);
|
|
171
220
|
}
|
|
172
221
|
await this.oracleMultiSubscriber.subscribe(oraclePubkeys, (accountId, data) => {
|
|
173
222
|
const source = pubkeyToSource.get(accountId.toBase58());
|
|
@@ -24,6 +24,7 @@ export declare class grpcMultiAccountSubscriber<T> {
|
|
|
24
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
25
|
setAccountData(accountPubkey: PublicKey, data: T, slot?: number): void;
|
|
26
26
|
getAccountData(accountPubkey: PublicKey): DataAndSlot<T> | undefined;
|
|
27
|
+
getAccountDataMap(): Map<string, DataAndSlot<T>>;
|
|
27
28
|
subscribe(accounts: PublicKey[], onChange: (accountId: PublicKey, data: T, context: Context, buffer: Buffer) => void): Promise<void>;
|
|
28
29
|
addAccounts(accounts: PublicKey[]): Promise<void>;
|
|
29
30
|
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,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAIrD,OAAO,EACN,MAAM,EAMN,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,SAAS,EAA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AE,SAAS,GAAG,WAAW,CAAC,CAAC,CAAC,GAAG,SAAS;IAIpE,iBAAiB,IAAI,GAAG,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC;IAI1C,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;IA+FV,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"}
|
|
@@ -62,6 +62,9 @@ class grpcMultiAccountSubscriber {
|
|
|
62
62
|
getAccountData(accountPubkey) {
|
|
63
63
|
return this.dataMap.get(accountPubkey.toBase58());
|
|
64
64
|
}
|
|
65
|
+
getAccountDataMap() {
|
|
66
|
+
return this.dataMap;
|
|
67
|
+
}
|
|
65
68
|
async subscribe(accounts, onChange) {
|
|
66
69
|
if (this.listenerId != null || this.isUnsubscribing) {
|
|
67
70
|
return;
|
package/package.json
CHANGED
package/scripts/client-test.ts
CHANGED
|
@@ -2,9 +2,21 @@ import { DriftClient } from '../src/driftClient';
|
|
|
2
2
|
import { grpcDriftClientAccountSubscriberV2 } from '../src/accounts/grpcDriftClientAccountSubscriberV2';
|
|
3
3
|
import { Connection, Keypair, PublicKey } from '@solana/web3.js';
|
|
4
4
|
import { DriftClientConfig } from '../src/driftClientConfig';
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
decodeName,
|
|
7
|
+
DRIFT_PROGRAM_ID,
|
|
8
|
+
PerpMarketAccount,
|
|
9
|
+
Wallet,
|
|
10
|
+
} from '../src';
|
|
6
11
|
import { CommitmentLevel } from '@triton-one/yellowstone-grpc';
|
|
7
12
|
import dotenv from 'dotenv';
|
|
13
|
+
import {
|
|
14
|
+
AnchorProvider,
|
|
15
|
+
Idl,
|
|
16
|
+
Program,
|
|
17
|
+
ProgramAccount,
|
|
18
|
+
} from '@coral-xyz/anchor';
|
|
19
|
+
import driftIDL from '../src/idl/drift.json';
|
|
8
20
|
|
|
9
21
|
const GRPC_ENDPOINT = process.env.GRPC_ENDPOINT;
|
|
10
22
|
const TOKEN = process.env.TOKEN;
|
|
@@ -13,6 +25,43 @@ async function initializeGrpcDriftClientV2() {
|
|
|
13
25
|
const connection = new Connection('https://api.mainnet-beta.solana.com');
|
|
14
26
|
const wallet = new Wallet(new Keypair());
|
|
15
27
|
dotenv.config({ path: '../' });
|
|
28
|
+
|
|
29
|
+
const programId = new PublicKey(DRIFT_PROGRAM_ID);
|
|
30
|
+
const provider = new AnchorProvider(
|
|
31
|
+
connection,
|
|
32
|
+
// @ts-ignore
|
|
33
|
+
wallet,
|
|
34
|
+
{
|
|
35
|
+
commitment: 'confirmed',
|
|
36
|
+
}
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
const program = new Program(driftIDL as Idl, programId, provider);
|
|
40
|
+
|
|
41
|
+
const perpMarketProgramAccounts =
|
|
42
|
+
(await program.account.perpMarket.all()) as ProgramAccount<PerpMarketAccount>[];
|
|
43
|
+
const solPerpMarket = perpMarketProgramAccounts.find(
|
|
44
|
+
(account) => account.account.marketIndex === 0
|
|
45
|
+
);
|
|
46
|
+
const solOracleInfo = {
|
|
47
|
+
publicKey: solPerpMarket.account.amm.oracle,
|
|
48
|
+
source: solPerpMarket.account.amm.oracleSource,
|
|
49
|
+
};
|
|
50
|
+
const ethPerpMarket = perpMarketProgramAccounts.find(
|
|
51
|
+
(account) => account.account.marketIndex === 2
|
|
52
|
+
);
|
|
53
|
+
const ethOracleInfo = {
|
|
54
|
+
publicKey: ethPerpMarket.account.amm.oracle,
|
|
55
|
+
source: ethPerpMarket.account.amm.oracleSource,
|
|
56
|
+
};
|
|
57
|
+
const btcPerpMarket = perpMarketProgramAccounts.find(
|
|
58
|
+
(account) => account.account.marketIndex === 1
|
|
59
|
+
);
|
|
60
|
+
const btcOracleInfo = {
|
|
61
|
+
publicKey: btcPerpMarket.account.amm.oracle,
|
|
62
|
+
source: btcPerpMarket.account.amm.oracleSource,
|
|
63
|
+
};
|
|
64
|
+
|
|
16
65
|
const config: DriftClientConfig = {
|
|
17
66
|
connection,
|
|
18
67
|
wallet,
|
|
@@ -31,9 +80,9 @@ async function initializeGrpcDriftClientV2() {
|
|
|
31
80
|
},
|
|
32
81
|
driftClientAccountSubscriber: grpcDriftClientAccountSubscriberV2,
|
|
33
82
|
},
|
|
34
|
-
perpMarketIndexes: [0, 1, 2],
|
|
35
|
-
spotMarketIndexes: [0, 1, 2],
|
|
36
|
-
oracleInfos: [
|
|
83
|
+
perpMarketIndexes: [0, 1, 2],
|
|
84
|
+
spotMarketIndexes: [0, 1, 2],
|
|
85
|
+
oracleInfos: [solOracleInfo, ethOracleInfo, btcOracleInfo],
|
|
37
86
|
};
|
|
38
87
|
|
|
39
88
|
const driftClient = new DriftClient(config);
|
|
@@ -55,6 +104,12 @@ async function initializeGrpcDriftClientV2() {
|
|
|
55
104
|
'Perp market data market index:',
|
|
56
105
|
perpMarketData?.marketIndex
|
|
57
106
|
);
|
|
107
|
+
const oracle = driftClient.getOracleDataForPerpMarket(data.marketIndex);
|
|
108
|
+
const mmOracle = driftClient.getMMOracleDataForPerpMarket(
|
|
109
|
+
data.marketIndex
|
|
110
|
+
);
|
|
111
|
+
console.log('Perp oracle price:', oracle.price.toString());
|
|
112
|
+
console.log('Perp MM oracle price:', mmOracle.price.toString());
|
|
58
113
|
perpMarketUpdateCount++;
|
|
59
114
|
if (
|
|
60
115
|
perpMarketUpdateCount >= 10 &&
|
|
@@ -78,6 +133,8 @@ async function initializeGrpcDriftClientV2() {
|
|
|
78
133
|
'Spot market data market index:',
|
|
79
134
|
spotMarketData?.marketIndex
|
|
80
135
|
);
|
|
136
|
+
const oracle = driftClient.getOracleDataForSpotMarket(data.marketIndex);
|
|
137
|
+
console.log('Spot oracle price:', oracle.price.toString());
|
|
81
138
|
spotMarketUpdateCount++;
|
|
82
139
|
if (
|
|
83
140
|
perpMarketUpdateCount >= 10 &&
|
|
@@ -126,6 +183,21 @@ async function initializeGrpcDriftClientV2() {
|
|
|
126
183
|
await driftClient.subscribe();
|
|
127
184
|
console.log('DriftClient initialized and listening for updates.');
|
|
128
185
|
|
|
186
|
+
for (const marketIndex of config.perpMarketIndexes) {
|
|
187
|
+
const oracle = driftClient.getOracleDataForPerpMarket(marketIndex);
|
|
188
|
+
const mmOracle = driftClient.getMMOracleDataForPerpMarket(marketIndex);
|
|
189
|
+
console.log('Initial perp oracle price:', oracle.price.toString());
|
|
190
|
+
console.log('Initial perp MM oracle price:', mmOracle.price.toString());
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
for (const marketIndex of config.spotMarketIndexes) {
|
|
194
|
+
const oracle = driftClient.getOracleDataForSpotMarket(marketIndex);
|
|
195
|
+
console.log('Initial spot oracle price:', oracle.price.toString());
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const stateAccount = driftClient.getStateAccount();
|
|
199
|
+
console.log('Initial state account:', stateAccount.toString());
|
|
200
|
+
|
|
129
201
|
await updatePromise;
|
|
130
202
|
console.log('Received required number of updates.');
|
|
131
203
|
}
|
|
@@ -17,7 +17,10 @@ import {
|
|
|
17
17
|
import { grpcAccountSubscriber } from './grpcAccountSubscriber';
|
|
18
18
|
import { grpcMultiAccountSubscriber } from './grpcMultiAccountSubscriber';
|
|
19
19
|
import { PerpMarketAccount, SpotMarketAccount, StateAccount } from '../types';
|
|
20
|
-
import {
|
|
20
|
+
import {
|
|
21
|
+
getOracleId,
|
|
22
|
+
getPublicKeyAndSourceFromOracleId,
|
|
23
|
+
} from '../oracles/oracleId';
|
|
21
24
|
|
|
22
25
|
export class grpcDriftClientAccountSubscriberV2 extends WebSocketDriftClientAccountSubscriber {
|
|
23
26
|
private grpcConfigs: GrpcConfigs;
|
|
@@ -146,6 +149,58 @@ export class grpcDriftClientAccountSubscriberV2 extends WebSocketDriftClientAcco
|
|
|
146
149
|
);
|
|
147
150
|
}
|
|
148
151
|
|
|
152
|
+
override async setPerpOracleMap() {
|
|
153
|
+
const perpMarketsMap = this.perpMarketsSubscriber?.getAccountDataMap();
|
|
154
|
+
const perpMarkets = Array.from(perpMarketsMap.values());
|
|
155
|
+
const addOraclePromises = [];
|
|
156
|
+
for (const perpMarket of perpMarkets) {
|
|
157
|
+
if (!perpMarket || !perpMarket.data) {
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
const perpMarketAccount = perpMarket.data;
|
|
161
|
+
const perpMarketIndex = perpMarketAccount.marketIndex;
|
|
162
|
+
const oracle = perpMarketAccount.amm.oracle;
|
|
163
|
+
const oracleId = getOracleId(oracle, perpMarket.data.amm.oracleSource);
|
|
164
|
+
if (!this.oracleSubscribers.has(oracleId)) {
|
|
165
|
+
addOraclePromises.push(
|
|
166
|
+
this.addOracle({
|
|
167
|
+
publicKey: oracle,
|
|
168
|
+
source: perpMarket.data.amm.oracleSource,
|
|
169
|
+
})
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
this.perpOracleMap.set(perpMarketIndex, oracle);
|
|
173
|
+
this.perpOracleStringMap.set(perpMarketIndex, oracleId);
|
|
174
|
+
}
|
|
175
|
+
await Promise.all(addOraclePromises);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
override async setSpotOracleMap() {
|
|
179
|
+
const spotMarketsMap = this.spotMarketsSubscriber?.getAccountDataMap();
|
|
180
|
+
const spotMarkets = Array.from(spotMarketsMap.values());
|
|
181
|
+
const addOraclePromises = [];
|
|
182
|
+
for (const spotMarket of spotMarkets) {
|
|
183
|
+
if (!spotMarket || !spotMarket.data) {
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
const spotMarketAccount = spotMarket.data;
|
|
187
|
+
const spotMarketIndex = spotMarketAccount.marketIndex;
|
|
188
|
+
const oracle = spotMarketAccount.oracle;
|
|
189
|
+
const oracleId = getOracleId(oracle, spotMarketAccount.oracleSource);
|
|
190
|
+
if (!this.oracleSubscribers.has(oracleId)) {
|
|
191
|
+
addOraclePromises.push(
|
|
192
|
+
this.addOracle({
|
|
193
|
+
publicKey: oracle,
|
|
194
|
+
source: spotMarketAccount.oracleSource,
|
|
195
|
+
})
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
this.spotOracleMap.set(spotMarketIndex, oracle);
|
|
199
|
+
this.spotOracleStringMap.set(spotMarketIndex, oracleId);
|
|
200
|
+
}
|
|
201
|
+
await Promise.all(addOraclePromises);
|
|
202
|
+
}
|
|
203
|
+
|
|
149
204
|
override async subscribeToPerpMarketAccounts(): Promise<boolean> {
|
|
150
205
|
const perpMarketIndexToAccountPubkeys: Array<[number, PublicKey]> =
|
|
151
206
|
await Promise.all(
|
|
@@ -321,10 +376,8 @@ export class grpcDriftClientAccountSubscriberV2 extends WebSocketDriftClientAcco
|
|
|
321
376
|
);
|
|
322
377
|
|
|
323
378
|
for (const data of this.initialOraclePriceData.entries()) {
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
data[1]
|
|
327
|
-
);
|
|
379
|
+
const { publicKey } = getPublicKeyAndSourceFromOracleId(data[0]);
|
|
380
|
+
this.oracleMultiSubscriber.setAccountData(publicKey, data[1]);
|
|
328
381
|
}
|
|
329
382
|
|
|
330
383
|
await this.oracleMultiSubscriber.subscribe(
|
|
@@ -101,6 +101,10 @@ export class grpcMultiAccountSubscriber<T> {
|
|
|
101
101
|
return this.dataMap.get(accountPubkey.toBase58());
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
+
getAccountDataMap(): Map<string, DataAndSlot<T>> {
|
|
105
|
+
return this.dataMap;
|
|
106
|
+
}
|
|
107
|
+
|
|
104
108
|
async subscribe(
|
|
105
109
|
accounts: PublicKey[],
|
|
106
110
|
onChange: (
|