@@ -8,7 +8,12 @@ import {
88 getPerpMarketPublicKey ,
99 getSpotMarketPublicKey ,
1010} from '../addresses/pda' ;
11- import { DelistedMarketSetting , GrpcConfigs , ResubOpts } from './types' ;
11+ import {
12+ DataAndSlot ,
13+ DelistedMarketSetting ,
14+ GrpcConfigs ,
15+ ResubOpts ,
16+ } from './types' ;
1217import { grpcAccountSubscriber } from './grpcAccountSubscriber' ;
1318import { grpcMultiAccountSubscriber } from './grpcMultiAccountSubscriber' ;
1419import { PerpMarketAccount , SpotMarketAccount , StateAccount } from '../types' ;
@@ -19,6 +24,8 @@ export class grpcDriftClientAccountSubscriberV2 extends WebSocketDriftClientAcco
1924 private perpMarketsSubscriber ?: grpcMultiAccountSubscriber < PerpMarketAccount > ;
2025 private spotMarketsSubscriber ?: grpcMultiAccountSubscriber < SpotMarketAccount > ;
2126 private oracleMultiSubscriber ?: grpcMultiAccountSubscriber < OraclePriceData > ;
27+ private perpMarketIndexToAccountPubkeyMap = new Map < number , PublicKey > ( ) ;
28+ private spotMarketIndexToAccountPubkeyMap = new Map < number , PublicKey > ( ) ;
2229
2330 constructor (
2431 grpcConfigs : GrpcConfigs ,
@@ -123,11 +130,39 @@ export class grpcDriftClientAccountSubscriberV2 extends WebSocketDriftClientAcco
123130 return true ;
124131 }
125132
133+ override getMarketAccountAndSlot (
134+ marketIndex : number
135+ ) : DataAndSlot < PerpMarketAccount > | undefined {
136+ return this . perpMarketsSubscriber ?. getAccountData (
137+ this . perpMarketIndexToAccountPubkeyMap . get ( marketIndex )
138+ ) ;
139+ }
140+
141+ override getSpotMarketAccountAndSlot (
142+ marketIndex : number
143+ ) : DataAndSlot < SpotMarketAccount > | undefined {
144+ return this . spotMarketsSubscriber ?. getAccountData (
145+ this . spotMarketIndexToAccountPubkeyMap . get ( marketIndex )
146+ ) ;
147+ }
148+
126149 override async subscribeToPerpMarketAccounts ( ) : Promise < boolean > {
127- const perpMarketPubkeys = await Promise . all (
128- this . perpMarketIndexes . map ( ( marketIndex ) =>
129- getPerpMarketPublicKey ( this . program . programId , marketIndex )
130- )
150+ const perpMarketIndexToAccountPubkeys : Array < [ number , PublicKey ] > =
151+ await Promise . all (
152+ this . perpMarketIndexes . map ( async ( marketIndex ) => [
153+ marketIndex ,
154+ await getPerpMarketPublicKey ( this . program . programId , marketIndex ) ,
155+ ] )
156+ ) ;
157+ for ( const [
158+ marketIndex ,
159+ accountPubkey ,
160+ ] of perpMarketIndexToAccountPubkeys ) {
161+ this . perpMarketIndexToAccountPubkeyMap . set ( marketIndex , accountPubkey ) ;
162+ }
163+
164+ const perpMarketPubkeys = perpMarketIndexToAccountPubkeys . map (
165+ ( [ _ , accountPubkey ] ) => accountPubkey
131166 ) ;
132167
133168 this . perpMarketsSubscriber =
@@ -151,6 +186,11 @@ export class grpcDriftClientAccountSubscriberV2 extends WebSocketDriftClientAcco
151186 }
152187 }
153188 ) ;
189+
190+ for ( const data of this . initialPerpMarketAccountData . values ( ) ) {
191+ this . perpMarketsSubscriber . setAccountData ( data . pubkey , data ) ;
192+ }
193+
154194 await this . perpMarketsSubscriber . subscribe (
155195 perpMarketPubkeys ,
156196 ( _accountId , data ) => {
@@ -166,10 +206,22 @@ export class grpcDriftClientAccountSubscriberV2 extends WebSocketDriftClientAcco
166206 }
167207
168208 override async subscribeToSpotMarketAccounts ( ) : Promise < boolean > {
169- const spotMarketPubkeys = await Promise . all (
170- this . spotMarketIndexes . map ( ( marketIndex ) =>
171- getSpotMarketPublicKey ( this . program . programId , marketIndex )
172- )
209+ const spotMarketIndexToAccountPubkeys : Array < [ number , PublicKey ] > =
210+ await Promise . all (
211+ this . spotMarketIndexes . map ( async ( marketIndex ) => [
212+ marketIndex ,
213+ await getSpotMarketPublicKey ( this . program . programId , marketIndex ) ,
214+ ] )
215+ ) ;
216+ for ( const [
217+ marketIndex ,
218+ accountPubkey ,
219+ ] of spotMarketIndexToAccountPubkeys ) {
220+ this . spotMarketIndexToAccountPubkeyMap . set ( marketIndex , accountPubkey ) ;
221+ }
222+
223+ const spotMarketPubkeys = spotMarketIndexToAccountPubkeys . map (
224+ ( [ _ , accountPubkey ] ) => accountPubkey
173225 ) ;
174226
175227 this . spotMarketsSubscriber =
@@ -193,6 +245,11 @@ export class grpcDriftClientAccountSubscriberV2 extends WebSocketDriftClientAcco
193245 }
194246 }
195247 ) ;
248+
249+ for ( const data of this . initialSpotMarketAccountData . values ( ) ) {
250+ this . spotMarketsSubscriber . setAccountData ( data . pubkey , data ) ;
251+ }
252+
196253 await this . spotMarketsSubscriber . subscribe (
197254 spotMarketPubkeys ,
198255 ( _accountId , data ) => {
@@ -263,6 +320,13 @@ export class grpcDriftClientAccountSubscriberV2 extends WebSocketDriftClientAcco
263320 }
264321 ) ;
265322
323+ for ( const data of this . initialOraclePriceData . entries ( ) ) {
324+ this . oracleMultiSubscriber . setAccountData (
325+ new PublicKey ( data [ 0 ] ) ,
326+ data [ 1 ]
327+ ) ;
328+ }
329+
266330 await this . oracleMultiSubscriber . subscribe (
267331 oraclePubkeys ,
268332 ( accountId , data ) => {
0 commit comments