11import { WebSocketDriftClientAccountSubscriber } from './webSocketDriftClientAccountSubscriber' ;
22import { OracleInfo , OraclePriceData } from '../oracles/types' ;
33import { Program } from '@coral-xyz/anchor' ;
4- import { PublicKey } from '@solana/web3.js' ;
54import { findAllMarketAndOracles } from '../config' ;
65import {
76 getDriftStateAccountPublicKey ,
@@ -10,15 +9,11 @@ import {
109} from '../addresses/pda' ;
1110import { DelistedMarketSetting , GrpcConfigs , ResubOpts } from './types' ;
1211import { grpcAccountSubscriber } from './grpcAccountSubscriber' ;
13- import { grpcMultiAccountSubscriber } from './grpcMultiAccountSubscriber' ;
1412import { PerpMarketAccount , SpotMarketAccount , StateAccount } from '../types' ;
1513import { getOracleId } from '../oracles/oracleId' ;
1614
17- export class gprcDriftClientAccountSubscriber extends WebSocketDriftClientAccountSubscriber {
15+ export class grpcDriftClientAccountSubscriber extends WebSocketDriftClientAccountSubscriber {
1816 private grpcConfigs : GrpcConfigs ;
19- private perpMarketsSubscriber ?: grpcMultiAccountSubscriber < PerpMarketAccount > ;
20- private spotMarketsSubscriber ?: grpcMultiAccountSubscriber < SpotMarketAccount > ;
21- private oracleMultiSubscriber ?: grpcMultiAccountSubscriber < OraclePriceData > ;
2217
2318 constructor (
2419 grpcConfigs : GrpcConfigs ,
@@ -99,10 +94,12 @@ export class gprcDriftClientAccountSubscriber extends WebSocketDriftClientAccoun
9994 // set initial data to avoid spamming getAccountInfo calls in webSocketAccountSubscriber
10095 await this . setInitialData ( ) ;
10196
102- // subscribe to perp + spot markets (separate) and oracles
10397 await Promise . all ( [
98+ // subscribe to market accounts
10499 this . subscribeToPerpMarketAccounts ( ) ,
100+ // subscribe to spot market accounts
105101 this . subscribeToSpotMarketAccounts ( ) ,
102+ // subscribe to oracles
106103 this . subscribeToOracles ( ) ,
107104 ] ) ;
108105
@@ -123,128 +120,6 @@ export class gprcDriftClientAccountSubscriber extends WebSocketDriftClientAccoun
123120 return true ;
124121 }
125122
126- override async subscribeToPerpMarketAccounts ( ) : Promise < boolean > {
127- const perpMarketPubkeys = await Promise . all (
128- this . perpMarketIndexes . map ( ( marketIndex ) =>
129- getPerpMarketPublicKey ( this . program . programId , marketIndex )
130- )
131- ) ;
132-
133- this . perpMarketsSubscriber =
134- await grpcMultiAccountSubscriber . create < PerpMarketAccount > (
135- this . grpcConfigs ,
136- 'PerpMarket' ,
137- this . program ,
138- undefined ,
139- this . resubOpts
140- ) ;
141- await this . perpMarketsSubscriber . subscribe (
142- perpMarketPubkeys ,
143- ( _accountId , data ) => {
144- this . eventEmitter . emit (
145- 'perpMarketAccountUpdate' ,
146- data as PerpMarketAccount
147- ) ;
148- this . eventEmitter . emit ( 'update' ) ;
149- }
150- ) ;
151-
152- return true ;
153- }
154-
155- override async subscribeToSpotMarketAccounts ( ) : Promise < boolean > {
156- const spotMarketPubkeys = await Promise . all (
157- this . spotMarketIndexes . map ( ( marketIndex ) =>
158- getSpotMarketPublicKey ( this . program . programId , marketIndex )
159- )
160- ) ;
161-
162- this . spotMarketsSubscriber =
163- await grpcMultiAccountSubscriber . create < SpotMarketAccount > (
164- this . grpcConfigs ,
165- 'SpotMarket' ,
166- this . program ,
167- undefined ,
168- this . resubOpts
169- ) ;
170- await this . spotMarketsSubscriber . subscribe (
171- spotMarketPubkeys ,
172- ( _accountId , data ) => {
173- this . eventEmitter . emit (
174- 'spotMarketAccountUpdate' ,
175- data as SpotMarketAccount
176- ) ;
177- this . eventEmitter . emit ( 'update' ) ;
178- }
179- ) ;
180-
181- return true ;
182- }
183-
184- override async subscribeToOracles ( ) : Promise < boolean > {
185- // Build list of unique oracle pubkeys and a lookup for sources
186- const uniqueOraclePubkeys = new Map < string , OracleInfo > ( ) ;
187- for ( const info of this . oracleInfos ) {
188- const id = getOracleId ( info . publicKey , info . source ) ;
189- if (
190- ! uniqueOraclePubkeys . has ( id ) &&
191- ! info . publicKey . equals ( ( PublicKey as any ) . default )
192- ) {
193- uniqueOraclePubkeys . set ( id , info ) ;
194- }
195- }
196-
197- const oraclePubkeys = Array . from ( uniqueOraclePubkeys . values ( ) ) . map (
198- ( i ) => i . publicKey
199- ) ;
200- const pubkeyToSource = new Map < string , OracleInfo [ 'source' ] > (
201- Array . from ( uniqueOraclePubkeys . values ( ) ) . map ( ( i ) => [
202- i . publicKey . toBase58 ( ) ,
203- i . source ,
204- ] )
205- ) ;
206-
207- this . oracleMultiSubscriber =
208- await grpcMultiAccountSubscriber . create < OraclePriceData > (
209- this . grpcConfigs ,
210- 'oracle' ,
211- this . program ,
212- ( buffer : Buffer , pubkey ?: string ) => {
213- if ( ! pubkey ) {
214- throw new Error ( 'Oracle pubkey missing in decode' ) ;
215- }
216- const source = pubkeyToSource . get ( pubkey ) ;
217- const client = this . oracleClientCache . get (
218- source ,
219- this . program . provider . connection ,
220- this . program
221- ) ;
222- return client . getOraclePriceDataFromBuffer ( buffer ) ;
223- } ,
224- this . resubOpts
225- ) ;
226-
227- await this . oracleMultiSubscriber . subscribe (
228- oraclePubkeys ,
229- ( accountId , data ) => {
230- const source = pubkeyToSource . get ( accountId . toBase58 ( ) ) ;
231- this . eventEmitter . emit ( 'oraclePriceUpdate' , accountId , source , data ) ;
232- this . eventEmitter . emit ( 'update' ) ;
233- }
234- ) ;
235-
236- return true ;
237- }
238-
239- async unsubscribeFromOracles ( ) : Promise < void > {
240- if ( this . oracleMultiSubscriber ) {
241- await this . oracleMultiSubscriber . unsubscribe ( ) ;
242- this . oracleMultiSubscriber = undefined ;
243- return ;
244- }
245- await super . unsubscribeFromOracles ( ) ;
246- }
247-
248123 override async subscribeToSpotMarketAccount (
249124 marketIndex : number
250125 ) : Promise < boolean > {
@@ -329,4 +204,4 @@ export class gprcDriftClientAccountSubscriber extends WebSocketDriftClientAccoun
329204 this . oracleSubscribers . set ( oracleId , accountSubscriber ) ;
330205 return true ;
331206 }
332- }
207+ }
0 commit comments