@@ -19,6 +19,7 @@ import {
19
19
OrderSubscriber ,
20
20
MarketType ,
21
21
} from '@drift-labs/sdk' ;
22
+ import { RedisClient , RedisClientPrefix } from '@drift/common' ;
22
23
23
24
import { logger , setLogLevel } from './utils/logger' ;
24
25
@@ -42,43 +43,19 @@ import {
42
43
normalizeBatchQueryParams ,
43
44
sleep ,
44
45
validateDlobQuery ,
46
+ getAccountFromId ,
45
47
} from './utils/utils' ;
46
48
import FEATURE_FLAGS from './utils/featureFlags' ;
47
49
import { getDLOBProviderFromOrderSubscriber } from './dlobProvider' ;
48
- import { RedisClient } from './utils/redisClient' ;
49
50
50
51
require ( 'dotenv' ) . config ( ) ;
51
52
52
53
// Reading in Redis env vars
53
- const REDIS_HOSTS = process . env . REDIS_HOSTS ?. replace ( / ^ \[ | \] $ / g, '' )
54
+ const REDIS_CLIENTS = process . env . REDIS_CLIENTS ?. replace ( / ^ \[ | \] $ / g, '' )
54
55
. split ( ',' )
55
- . map ( ( host ) => host . trim ( ) ) || [ 'localhost' ] ;
56
- const REDIS_PORTS = process . env . REDIS_PORTS ?. replace ( / ^ \[ | \] $ / g, '' )
57
- . split ( ',' )
58
- . map ( ( port ) => parseInt ( port . trim ( ) , 10 ) ) || [ 6379 ] ;
59
- const REDIS_PASSWORDS_ENV = process . env . REDIS_PASSWORDS || "['']" ;
60
-
61
- let REDIS_PASSWORDS ;
62
-
63
- if ( REDIS_PASSWORDS_ENV . trim ( ) === "['']" ) {
64
- REDIS_PASSWORDS = [ undefined ] ;
65
- } else {
66
- REDIS_PASSWORDS = REDIS_PASSWORDS_ENV . replace ( / ^ \[ | \] $ / g, '' )
67
- . split ( / \s * , \s * / )
68
- . map ( ( pwd ) => pwd . replace ( / ( ^ ' | ' $ ) / g, '' ) . trim ( ) )
69
- . map ( ( pwd ) => ( pwd === '' ? undefined : pwd ) ) ;
70
- }
56
+ . map ( ( clients ) => clients . trim ( ) ) || [ 'DLOB' ] ;
71
57
72
- console . log ( 'Redis Hosts:' , REDIS_HOSTS ) ;
73
- console . log ( 'Redis Ports:' , REDIS_PORTS ) ;
74
- console . log ( 'Redis Passwords:' , REDIS_PASSWORDS ) ;
75
-
76
- if (
77
- REDIS_PORTS . length !== REDIS_PASSWORDS . length ||
78
- REDIS_PORTS . length !== REDIS_HOSTS . length
79
- ) {
80
- throw 'REDIS_HOSTS and REDIS_PASSWORDS and REDIS_PORTS must be the same length' ;
81
- }
58
+ console . log ( 'Redis Clients:' , REDIS_CLIENTS ) ;
82
59
83
60
const driftEnv = ( process . env . ENV || 'devnet' ) as DriftEnv ;
84
61
const commitHash = process . env . COMMIT ;
@@ -316,16 +293,11 @@ const main = async (): Promise<void> => {
316
293
> = new Map ( ) ;
317
294
if ( useRedis ) {
318
295
logger . info ( 'Connecting to redis' ) ;
319
- for ( let i = 0 ; i < REDIS_HOSTS . length ; i ++ ) {
320
- redisClients . push (
321
- new RedisClient (
322
- REDIS_HOSTS [ i ] ,
323
- REDIS_PORTS [ i ] . toString ( ) ,
324
- REDIS_PASSWORDS [ i ] || undefined
325
- )
326
- ) ;
327
- await redisClients [ i ] . connect ( ) ;
296
+ for ( let i = 0 ; i < REDIS_CLIENTS . length ; i ++ ) {
297
+ const prefix = RedisClientPrefix [ REDIS_CLIENTS [ i ] ] ;
298
+ redisClients . push ( new RedisClient ( { prefix } ) ) ;
328
299
}
300
+
329
301
for ( let i = 0 ; i < sdkConfig . SPOT_MARKETS . length ; i ++ ) {
330
302
spotMarketRedisMap . set ( sdkConfig . SPOT_MARKETS [ i ] . marketIndex , {
331
303
client : redisClients [ 0 ] ,
@@ -344,6 +316,8 @@ const main = async (): Promise<void> => {
344
316
}
345
317
}
346
318
319
+ const userMapClient = new RedisClient ( { prefix : RedisClientPrefix . USER_MAP } ) ;
320
+
347
321
function canRotate ( marketType : MarketType , marketIndex : number ) {
348
322
if ( isVariant ( marketType , 'spot' ) ) {
349
323
const state = spotMarketRedisMap . get ( marketIndex ) ;
@@ -427,15 +401,21 @@ const main = async (): Promise<void> => {
427
401
try {
428
402
const { marketIndex, marketType } = req . query ;
429
403
430
- const fees = await redisClients [
431
- parseInt ( process . env . HELIUS_REDIS_HOST_INDEX ) ?? 0
432
- ] . client . get ( `priorityFees_${ marketType } _${ marketIndex } ` ) ;
404
+ const fees = await redisClients
405
+ . find (
406
+ ( client ) =>
407
+ client . forceGetClient ( ) . options . keyPrefix ===
408
+ RedisClientPrefix . DLOB_HELIUS
409
+ )
410
+ . getRaw ( `priorityFees_${ marketType } _${ marketIndex } ` ) ;
411
+
433
412
if ( fees ) {
434
413
res . status ( 200 ) . json ( {
435
414
...JSON . parse ( fees ) ,
436
415
marketType,
437
416
marketIndex,
438
417
} ) ;
418
+
439
419
return ;
440
420
} else {
441
421
res . writeHead ( 404 ) ;
@@ -466,11 +446,16 @@ const main = async (): Promise<void> => {
466
446
467
447
const fees = await Promise . all (
468
448
normedParams . map ( async ( normedParam ) => {
469
- const fees = await redisClients [
470
- parseInt ( process . env . HELIUS_REDIS_HOST_INDEX ) ?? 0
471
- ] . client . get (
472
- `priorityFees_${ normedParam [ 'marketType' ] } _${ normedParam [ 'marketIndex' ] } `
473
- ) ;
449
+ const fees = await redisClients
450
+ . find (
451
+ ( client ) =>
452
+ client . forceGetClient ( ) . options . keyPrefix ===
453
+ RedisClientPrefix . DLOB_HELIUS
454
+ )
455
+ . getRaw (
456
+ `priorityFees_${ normedParam [ 'marketType' ] } _${ normedParam [ 'marketIndex' ] } `
457
+ ) ;
458
+
474
459
return {
475
460
...JSON . parse ( fees ) ,
476
461
marketType : normedParam [ 'marketType' ] ,
@@ -499,6 +484,7 @@ const main = async (): Promise<void> => {
499
484
marketType,
500
485
side, // bid or ask
501
486
limit,
487
+ includeAccounts,
502
488
} = req . query ;
503
489
504
490
const { normedMarketType, normedMarketIndex, error } = validateDlobQuery (
@@ -533,12 +519,17 @@ const main = async (): Promise<void> => {
533
519
normedLimit = 4 ;
534
520
}
535
521
522
+ let accountFlag = false ;
523
+ if ( includeAccounts ) {
524
+ accountFlag = includeAccounts === 'true' ;
525
+ }
526
+
536
527
let topMakers : string [ ] ;
537
528
if ( useRedis ) {
538
529
const redisClient = isVariant ( normedMarketType , 'perp' )
539
530
? perpMarketRedisMap . get ( normedMarketIndex ) . client
540
531
: spotMarketRedisMap . get ( normedMarketIndex ) . client ;
541
- const redisResponse = await redisClient . client . get (
532
+ const redisResponse = await redisClient . getRaw (
542
533
`last_update_orderbook_best_makers_${ getVariant (
543
534
normedMarketType
544
535
) } _${ marketIndex } `
@@ -565,6 +556,13 @@ const main = async (): Promise<void> => {
565
556
path : req . baseUrl + req . path ,
566
557
} ) ;
567
558
res . writeHead ( 200 ) ;
559
+
560
+ if ( accountFlag ) {
561
+ const topAccounts = await getAccountFromId ( userMapClient , topMakers ) ;
562
+ res . end ( JSON . stringify ( topAccounts ) ) ;
563
+ return ;
564
+ }
565
+
568
566
res . end ( JSON . stringify ( topMakers ) ) ;
569
567
return ;
570
568
}
@@ -619,6 +617,13 @@ const main = async (): Promise<void> => {
619
617
path : req . baseUrl + req . path ,
620
618
} ) ;
621
619
res . writeHead ( 200 ) ;
620
+
621
+ if ( accountFlag ) {
622
+ const topAccounts = await getAccountFromId ( userMapClient , topMakers ) ;
623
+ res . end ( JSON . stringify ( topAccounts ) ) ;
624
+ return ;
625
+ }
626
+
622
627
res . end ( JSON . stringify ( topMakers ) ) ;
623
628
} catch ( err ) {
624
629
next ( err ) ;
@@ -661,15 +666,15 @@ const main = async (): Promise<void> => {
661
666
let redisL2 : string ;
662
667
const redisClient = perpMarketRedisMap . get ( normedMarketIndex ) . client ;
663
668
if ( parseInt ( adjustedDepth as string ) === 5 ) {
664
- redisL2 = await redisClient . client . get (
669
+ redisL2 = await redisClient . getRaw (
665
670
`last_update_orderbook_perp_${ normedMarketIndex } _depth_5`
666
671
) ;
667
672
} else if ( parseInt ( adjustedDepth as string ) === 20 ) {
668
- redisL2 = await redisClient . client . get (
673
+ redisL2 = await redisClient . getRaw (
669
674
`last_update_orderbook_perp_${ normedMarketIndex } _depth_20`
670
675
) ;
671
676
} else if ( parseInt ( adjustedDepth as string ) === 100 ) {
672
- redisL2 = await redisClient . client . get (
677
+ redisL2 = await redisClient . getRaw (
673
678
`last_update_orderbook_perp_${ normedMarketIndex } _depth_100`
674
679
) ;
675
680
}
@@ -694,15 +699,15 @@ const main = async (): Promise<void> => {
694
699
let redisL2 : string ;
695
700
const redisClient = spotMarketRedisMap . get ( normedMarketIndex ) . client ;
696
701
if ( parseInt ( adjustedDepth as string ) === 5 ) {
697
- redisL2 = await redisClient . client . get (
702
+ redisL2 = await redisClient . getRaw (
698
703
`last_update_orderbook_spot_${ normedMarketIndex } _depth_5`
699
704
) ;
700
705
} else if ( parseInt ( adjustedDepth as string ) === 20 ) {
701
- redisL2 = await redisClient . client . get (
706
+ redisL2 = await redisClient . getRaw (
702
707
`last_update_orderbook_spot_${ normedMarketIndex } _depth_20`
703
708
) ;
704
709
} else if ( parseInt ( adjustedDepth as string ) === 100 ) {
705
- redisL2 = await redisClient . client . get (
710
+ redisL2 = await redisClient . getRaw (
706
711
`last_update_orderbook_spot_${ normedMarketIndex } _depth_100`
707
712
) ;
708
713
}
@@ -834,15 +839,15 @@ const main = async (): Promise<void> => {
834
839
const redisClient =
835
840
perpMarketRedisMap . get ( normedMarketIndex ) . client ;
836
841
if ( parseInt ( adjustedDepth as string ) === 5 ) {
837
- redisL2 = await redisClient . client . get (
842
+ redisL2 = await redisClient . getRaw (
838
843
`last_update_orderbook_perp_${ normedMarketIndex } _depth_5`
839
844
) ;
840
845
} else if ( parseInt ( adjustedDepth as string ) === 20 ) {
841
- redisL2 = await redisClient . client . get (
846
+ redisL2 = await redisClient . getRaw (
842
847
`last_update_orderbook_perp_${ normedMarketIndex } _depth_20`
843
848
) ;
844
849
} else if ( parseInt ( adjustedDepth as string ) === 100 ) {
845
- redisL2 = await redisClient . client . get (
850
+ redisL2 = await redisClient . getRaw (
846
851
`last_update_orderbook_perp_${ normedMarketIndex } _depth_100`
847
852
) ;
848
853
}
@@ -870,15 +875,15 @@ const main = async (): Promise<void> => {
870
875
const redisClient =
871
876
spotMarketRedisMap . get ( normedMarketIndex ) . client ;
872
877
if ( parseInt ( adjustedDepth as string ) === 5 ) {
873
- redisL2 = await redisClient . client . get (
878
+ redisL2 = await redisClient . getRaw (
874
879
`last_update_orderbook_spot_${ normedMarketIndex } _depth_5`
875
880
) ;
876
881
} else if ( parseInt ( adjustedDepth as string ) === 20 ) {
877
- redisL2 = await redisClient . client . get (
882
+ redisL2 = await redisClient . getRaw (
878
883
`last_update_orderbook_spot_${ normedMarketIndex } _depth_20`
879
884
) ;
880
885
} else if ( parseInt ( adjustedDepth as string ) === 100 ) {
881
- redisL2 = await redisClient . client . get (
886
+ redisL2 = await redisClient . getRaw (
882
887
`last_update_orderbook_spot_${ normedMarketIndex } _depth_100`
883
888
) ;
884
889
}
@@ -976,7 +981,7 @@ const main = async (): Promise<void> => {
976
981
const redisClient = (
977
982
marketTypeStr === 'spot' ? spotMarketRedisMap : perpMarketRedisMap
978
983
) . get ( normedMarketIndex ) . client ;
979
- const redisL3 = await redisClient . client . get (
984
+ const redisL3 = await redisClient . getRaw (
980
985
`last_update_orderbook_l3_${ marketTypeStr } _${ normedMarketIndex } `
981
986
) ;
982
987
if (
0 commit comments