@@ -3,6 +3,11 @@ import { DriftEnv, PerpMarkets } from '@drift-labs/sdk';
33import { RedisClient } from '@drift/common/clients' ;
44import * as axios from 'axios' ;
55
6+ export type PythLazerPriceFeedArray = {
7+ channel ?: Channel ;
8+ priceFeedIds : number [ ] ;
9+ } ;
10+
611export class PythLazerSubscriber {
712 private pythLazerClient ?: PythLazerClient ;
813 feedIdChunkToPriceMessage : Map < string , string > = new Map ( ) ;
@@ -22,34 +27,35 @@ export class PythLazerSubscriber {
2227 constructor (
2328 private endpoints : string [ ] ,
2429 private token : string ,
25- private priceFeedIdsArrays : number [ ] [ ] ,
30+ private priceFeedArrays : PythLazerPriceFeedArray [ ] ,
2631 env : DriftEnv = 'devnet' ,
2732 private redisClient ?: RedisClient ,
2833 private httpEndpoints : string [ ] = [ ] ,
29- private resubTimeoutMs : number = 2000 ,
30- private subscribeChannel = 'fixed_rate@200ms'
34+ private resubTimeoutMs : number = 2000
3135 ) {
3236 const markets = PerpMarkets [ env ] . filter (
3337 ( market ) => market . pythLazerId !== undefined
3438 ) ;
3539
36- this . allSubscribedIds = this . priceFeedIdsArrays . flat ( ) ;
40+ this . allSubscribedIds = this . priceFeedArrays
41+ . map ( ( array ) => array . priceFeedIds )
42+ . flat ( ) ;
3743 if (
38- priceFeedIdsArrays [ 0 ] . length === 1 &&
44+ priceFeedArrays [ 0 ] . priceFeedIds . length === 1 &&
3945 this . allSubscribedIds . length > 3 &&
4046 this . httpEndpoints . length > 0
4147 ) {
4248 this . useHttpRequests = true ;
4349 }
4450
45- for ( const priceFeedIds of priceFeedIdsArrays ) {
51+ for ( const priceFeedIds of priceFeedArrays ) {
4652 const filteredMarkets = markets . filter ( ( market ) =>
47- priceFeedIds . includes ( market . pythLazerId ! )
53+ priceFeedIds . priceFeedIds . includes ( market . pythLazerId ! )
4854 ) ;
4955 for ( const market of filteredMarkets ) {
5056 this . marketIndextoPriceFeedIdChunk . set (
5157 market . marketIndex ,
52- priceFeedIds
58+ priceFeedIds . priceFeedIds
5359 ) ;
5460 this . marketIndextoPriceFeedId . set (
5561 market . marketIndex ,
@@ -70,9 +76,9 @@ export class PythLazerSubscriber {
7076 this . token
7177 ) ;
7278 let subscriptionId = 1 ;
73- for ( const priceFeedIds of this . priceFeedIdsArrays ) {
74- const feedIdsHash = this . hash ( priceFeedIds ) ;
75- this . feedIdHashToFeedIds . set ( feedIdsHash , priceFeedIds ) ;
79+ for ( const priceFeedIds of this . priceFeedArrays ) {
80+ const feedIdsHash = this . hash ( priceFeedIds . priceFeedIds ) ;
81+ this . feedIdHashToFeedIds . set ( feedIdsHash , priceFeedIds . priceFeedIds ) ;
7682 this . subscriptionIdsToFeedIdsHash . set ( subscriptionId , feedIdsHash ) ;
7783 this . pythLazerClient . addMessageListener ( ( message ) => {
7884 this . receivingData = true ;
@@ -108,11 +114,11 @@ export class PythLazerSubscriber {
108114 this . pythLazerClient . send ( {
109115 type : 'subscribe' ,
110116 subscriptionId,
111- priceFeedIds,
117+ priceFeedIds : priceFeedIds . priceFeedIds ,
112118 properties : [ 'price' , 'bestAskPrice' , 'bestBidPrice' , 'exponent' ] ,
113119 chains : [ 'solana' ] ,
114120 deliveryFormat : 'json' ,
115- channel : this . subscribeChannel as Channel ,
121+ channel : priceFeedIds . channel ?? ( 'fixed_rate@200ms' as Channel ) ,
116122 jsonBinaryEncoding : 'hex' ,
117123 } ) ;
118124 subscriptionId ++ ;
0 commit comments