Skip to content

Commit 7bea1a9

Browse files
authored
fix: oracle string map not initted grpc v2 (#1936)
* fix: oracle string map not initted grpc v2 * fix: prettier
1 parent da6fc9c commit 7bea1a9

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

sdk/scripts/client-test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ async function initializeGrpcDriftClientV2() {
104104
'Perp market data market index:',
105105
perpMarketData?.marketIndex
106106
);
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());
107113
perpMarketUpdateCount++;
108114
if (
109115
perpMarketUpdateCount >= 10 &&
@@ -127,6 +133,8 @@ async function initializeGrpcDriftClientV2() {
127133
'Spot market data market index:',
128134
spotMarketData?.marketIndex
129135
);
136+
const oracle = driftClient.getOracleDataForSpotMarket(data.marketIndex);
137+
console.log('Spot oracle price:', oracle.price.toString());
130138
spotMarketUpdateCount++;
131139
if (
132140
perpMarketUpdateCount >= 10 &&
@@ -175,6 +183,21 @@ async function initializeGrpcDriftClientV2() {
175183
await driftClient.subscribe();
176184
console.log('DriftClient initialized and listening for updates.');
177185

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+
178201
await updatePromise;
179202
console.log('Received required number of updates.');
180203
}

sdk/src/accounts/grpcDriftClientAccountSubscriberV2.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,58 @@ export class grpcDriftClientAccountSubscriberV2 extends WebSocketDriftClientAcco
149149
);
150150
}
151151

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+
152204
override async subscribeToPerpMarketAccounts(): Promise<boolean> {
153205
const perpMarketIndexToAccountPubkeys: Array<[number, PublicKey]> =
154206
await Promise.all(

sdk/src/accounts/grpcMultiAccountSubscriber.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ export class grpcMultiAccountSubscriber<T> {
101101
return this.dataMap.get(accountPubkey.toBase58());
102102
}
103103

104+
getAccountDataMap(): Map<string, DataAndSlot<T>> {
105+
return this.dataMap;
106+
}
107+
104108
async subscribe(
105109
accounts: PublicKey[],
106110
onChange: (

0 commit comments

Comments
 (0)