@@ -9,11 +9,15 @@ import {
99} from '../addresses/pda' ;
1010import { DelistedMarketSetting , GrpcConfigs , ResubOpts } from './types' ;
1111import { grpcAccountSubscriber } from './grpcAccountSubscriber' ;
12+ import { grpcMultiAccountSubscriber } from './grpcMultiAccountSubscriber' ;
1213import { PerpMarketAccount , SpotMarketAccount , StateAccount } from '../types' ;
1314import { getOracleId } from '../oracles/oracleId' ;
15+ import { Client , createClient } from '../isomorphic/grpc' ;
1416
1517export class gprcDriftClientAccountSubscriber extends WebSocketDriftClientAccountSubscriber {
1618 private grpcConfigs : GrpcConfigs ;
19+ private perpMarketsSubscriber ?: grpcMultiAccountSubscriber < PerpMarketAccount > ;
20+ private spotMarketsSubscriber ?: grpcMultiAccountSubscriber < SpotMarketAccount > ;
1721
1822 constructor (
1923 grpcConfigs : GrpcConfigs ,
@@ -94,12 +98,9 @@ export class gprcDriftClientAccountSubscriber extends WebSocketDriftClientAccoun
9498 // set initial data to avoid spamming getAccountInfo calls in webSocketAccountSubscriber
9599 await this . setInitialData ( ) ;
96100
101+ // subscribe to perp + spot markets using two gRPC streams and subscribe to oracles
97102 await Promise . all ( [
98- // subscribe to market accounts
99- this . subscribeToPerpMarketAccounts ( ) ,
100- // subscribe to spot market accounts
101- this . subscribeToSpotMarketAccounts ( ) ,
102- // subscribe to oracles
103+ this . subscribeToPerpAndSpotMarkets ( ) ,
103104 this . subscribeToOracles ( ) ,
104105 ] ) ;
105106
@@ -120,8 +121,64 @@ export class gprcDriftClientAccountSubscriber extends WebSocketDriftClientAccoun
120121 return true ;
121122 }
122123
124+ private async subscribeToPerpAndSpotMarkets ( ) : Promise < boolean > {
125+ const [ perpMarketPubkeys , spotMarketPubkeys ] = await Promise . all ( [
126+ Promise . all (
127+ this . perpMarketIndexes . map ( ( marketIndex ) =>
128+ getPerpMarketPublicKey ( this . program . programId , marketIndex )
129+ )
130+ ) ,
131+ Promise . all (
132+ this . spotMarketIndexes . map ( ( marketIndex ) =>
133+ getSpotMarketPublicKey ( this . program . programId , marketIndex )
134+ )
135+ ) ,
136+ ] ) ;
137+
138+ this . perpMarketsSubscriber =
139+ await grpcMultiAccountSubscriber . create < PerpMarketAccount > (
140+ this . grpcConfigs ,
141+ 'PerpMarket' ,
142+ this . program ,
143+ undefined ,
144+ this . resubOpts
145+ ) ;
146+ await this . perpMarketsSubscriber . subscribe (
147+ perpMarketPubkeys ,
148+ ( _accountId , data ) => {
149+ this . eventEmitter . emit (
150+ 'perpMarketAccountUpdate' ,
151+ data as PerpMarketAccount
152+ ) ;
153+ this . eventEmitter . emit ( 'update' ) ;
154+ }
155+ ) ;
156+
157+ this . spotMarketsSubscriber =
158+ await grpcMultiAccountSubscriber . create < SpotMarketAccount > (
159+ this . grpcConfigs ,
160+ 'SpotMarket' ,
161+ this . program ,
162+ undefined ,
163+ this . resubOpts
164+ ) ;
165+ await this . spotMarketsSubscriber . subscribe (
166+ spotMarketPubkeys ,
167+ ( _accountId , data ) => {
168+ this . eventEmitter . emit (
169+ 'spotMarketAccountUpdate' ,
170+ data as SpotMarketAccount
171+ ) ;
172+ this . eventEmitter . emit ( 'update' ) ;
173+ }
174+ ) ;
175+
176+ return true ;
177+ }
178+
123179 override async subscribeToSpotMarketAccount (
124- marketIndex : number
180+ marketIndex : number ,
181+ spotMarketClient ?: Client
125182 ) : Promise < boolean > {
126183 const marketPublicKey = await getSpotMarketPublicKey (
127184 this . program . programId ,
@@ -147,7 +204,10 @@ export class gprcDriftClientAccountSubscriber extends WebSocketDriftClientAccoun
147204 return true ;
148205 }
149206
150- async subscribeToPerpMarketAccount ( marketIndex : number ) : Promise < boolean > {
207+ async subscribeToPerpMarketAccount (
208+ marketIndex : number ,
209+ perpMarketClient ?: Client
210+ ) : Promise < boolean > {
151211 const perpMarketPublicKey = await getPerpMarketPublicKey (
152212 this . program . programId ,
153213 marketIndex
0 commit comments