Skip to content

Commit 068ed66

Browse files
authored
fix: oracle info wrong on grpc sub v2 (#1935)
1 parent 50f115d commit 068ed66

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

sdk/scripts/client-test.ts

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,21 @@ import { DriftClient } from '../src/driftClient';
22
import { grpcDriftClientAccountSubscriberV2 } from '../src/accounts/grpcDriftClientAccountSubscriberV2';
33
import { Connection, Keypair, PublicKey } from '@solana/web3.js';
44
import { DriftClientConfig } from '../src/driftClientConfig';
5-
import { decodeName, DRIFT_PROGRAM_ID, Wallet } from '../src';
5+
import {
6+
decodeName,
7+
DRIFT_PROGRAM_ID,
8+
PerpMarketAccount,
9+
Wallet,
10+
} from '../src';
611
import { CommitmentLevel } from '@triton-one/yellowstone-grpc';
712
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';
820

921
const GRPC_ENDPOINT = process.env.GRPC_ENDPOINT;
1022
const TOKEN = process.env.TOKEN;
@@ -13,6 +25,43 @@ async function initializeGrpcDriftClientV2() {
1325
const connection = new Connection('https://api.mainnet-beta.solana.com');
1426
const wallet = new Wallet(new Keypair());
1527
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+
1665
const config: DriftClientConfig = {
1766
connection,
1867
wallet,
@@ -31,9 +80,9 @@ async function initializeGrpcDriftClientV2() {
3180
},
3281
driftClientAccountSubscriber: grpcDriftClientAccountSubscriberV2,
3382
},
34-
perpMarketIndexes: [0, 1, 2], // Example market indexes
35-
spotMarketIndexes: [0, 1, 2], // Example market indexes
36-
oracleInfos: [], // Add oracle information if needed
83+
perpMarketIndexes: [0, 1, 2],
84+
spotMarketIndexes: [0, 1, 2],
85+
oracleInfos: [solOracleInfo, ethOracleInfo, btcOracleInfo],
3786
};
3887

3988
const driftClient = new DriftClient(config);

sdk/src/accounts/grpcDriftClientAccountSubscriberV2.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ import {
1717
import { grpcAccountSubscriber } from './grpcAccountSubscriber';
1818
import { grpcMultiAccountSubscriber } from './grpcMultiAccountSubscriber';
1919
import { PerpMarketAccount, SpotMarketAccount, StateAccount } from '../types';
20-
import { getOracleId } from '../oracles/oracleId';
20+
import {
21+
getOracleId,
22+
getPublicKeyAndSourceFromOracleId,
23+
} from '../oracles/oracleId';
2124

2225
export class grpcDriftClientAccountSubscriberV2 extends WebSocketDriftClientAccountSubscriber {
2326
private grpcConfigs: GrpcConfigs;
@@ -321,10 +324,8 @@ export class grpcDriftClientAccountSubscriberV2 extends WebSocketDriftClientAcco
321324
);
322325

323326
for (const data of this.initialOraclePriceData.entries()) {
324-
this.oracleMultiSubscriber.setAccountData(
325-
new PublicKey(data[0]),
326-
data[1]
327-
);
327+
const { publicKey } = getPublicKeyAndSourceFromOracleId(data[0]);
328+
this.oracleMultiSubscriber.setAccountData(publicKey, data[1]);
328329
}
329330

330331
await this.oracleMultiSubscriber.subscribe(

0 commit comments

Comments
 (0)