@@ -10,78 +10,125 @@ const GRPC_ENDPOINT = process.env.GRPC_ENDPOINT;
1010const TOKEN = process . env . TOKEN ;
1111
1212async function initializeGrpcDriftClientV2 ( ) {
13- const connection = new Connection ( 'https://api.mainnet-beta.solana.com' ) ;
14- const wallet = new Wallet ( new Keypair ( ) ) ;
15- dotenv . config ( { path : '../' } ) ;
16- const config : DriftClientConfig = {
17- connection,
18- wallet,
19- programID : new PublicKey ( DRIFT_PROGRAM_ID ) ,
20- accountSubscription : {
21- type : 'grpc' ,
22- grpcConfigs : {
23- endpoint : GRPC_ENDPOINT ,
24- token : TOKEN ,
25- commitmentLevel : 'confirmed' as unknown as CommitmentLevel ,
26- channelOptions : {
27- 'grpc.keepalive_time_ms' : 10_000 ,
28- 'grpc.keepalive_timeout_ms' : 1_000 ,
29- 'grpc.keepalive_permit_without_calls' : 1 ,
30- } ,
31- } ,
32- driftClientAccountSubscriber : grpcDriftClientAccountSubscriberV2 ,
33- } ,
34- perpMarketIndexes : [ 0 , 1 , 2 ] , // Example market indexes
35- spotMarketIndexes : [ 0 , 1 , 2 ] , // Example market indexes
36- oracleInfos : [ ] , // Add oracle information if needed
37- } ;
13+ const connection = new Connection ( 'https://api.mainnet-beta.solana.com' ) ;
14+ const wallet = new Wallet ( new Keypair ( ) ) ;
15+ dotenv . config ( { path : '../' } ) ;
16+ const config : DriftClientConfig = {
17+ connection,
18+ wallet,
19+ programID : new PublicKey ( DRIFT_PROGRAM_ID ) ,
20+ accountSubscription : {
21+ type : 'grpc' ,
22+ grpcConfigs : {
23+ endpoint : GRPC_ENDPOINT ,
24+ token : TOKEN ,
25+ commitmentLevel : 'confirmed' as unknown as CommitmentLevel ,
26+ channelOptions : {
27+ 'grpc.keepalive_time_ms' : 10_000 ,
28+ 'grpc.keepalive_timeout_ms' : 1_000 ,
29+ 'grpc.keepalive_permit_without_calls' : 1 ,
30+ } ,
31+ } ,
32+ driftClientAccountSubscriber : grpcDriftClientAccountSubscriberV2 ,
33+ } ,
34+ perpMarketIndexes : [ 0 , 1 , 2 ] , // Example market indexes
35+ spotMarketIndexes : [ 0 , 1 , 2 ] , // Example market indexes
36+ oracleInfos : [ ] , // Add oracle information if needed
37+ } ;
3838
39- const driftClient = new DriftClient ( config ) ;
39+ const driftClient = new DriftClient ( config ) ;
4040
41- let perpMarketUpdateCount = 0 ;
42- let spotMarketUpdateCount = 0 ;
43- let oraclePriceUpdateCount = 0 ;
44- let userAccountUpdateCount = 0 ;
41+ let perpMarketUpdateCount = 0 ;
42+ let spotMarketUpdateCount = 0 ;
43+ let oraclePriceUpdateCount = 0 ;
44+ let userAccountUpdateCount = 0 ;
4545
46- const updatePromise = new Promise < void > ( ( resolve ) => {
47- driftClient . accountSubscriber . eventEmitter . on ( 'perpMarketAccountUpdate' , ( data ) => {
48- console . log ( 'Perp market account update:' , decodeName ( data . name ) ) ;
49- perpMarketUpdateCount ++ ;
50- if ( perpMarketUpdateCount >= 10 && spotMarketUpdateCount >= 10 && oraclePriceUpdateCount >= 10 && userAccountUpdateCount >= 2 ) {
51- resolve ( ) ;
52- }
53- } ) ;
46+ const updatePromise = new Promise < void > ( ( resolve ) => {
47+ driftClient . accountSubscriber . eventEmitter . on (
48+ 'perpMarketAccountUpdate' ,
49+ ( data ) => {
50+ console . log ( 'Perp market account update:' , decodeName ( data . name ) ) ;
51+ const perpMarketData = driftClient . getPerpMarketAccount (
52+ data . marketIndex
53+ ) ;
54+ console . log (
55+ 'Perp market data market index:' ,
56+ perpMarketData ?. marketIndex
57+ ) ;
58+ perpMarketUpdateCount ++ ;
59+ if (
60+ perpMarketUpdateCount >= 10 &&
61+ spotMarketUpdateCount >= 10 &&
62+ oraclePriceUpdateCount >= 10 &&
63+ userAccountUpdateCount >= 2
64+ ) {
65+ resolve ( ) ;
66+ }
67+ }
68+ ) ;
5469
55- driftClient . accountSubscriber . eventEmitter . on ( 'spotMarketAccountUpdate' , ( data ) => {
56- console . log ( 'Spot market account update:' , decodeName ( data . name ) ) ;
57- spotMarketUpdateCount ++ ;
58- if ( perpMarketUpdateCount >= 10 && spotMarketUpdateCount >= 10 && oraclePriceUpdateCount >= 10 && userAccountUpdateCount >= 2 ) {
59- resolve ( ) ;
60- }
61- } ) ;
70+ driftClient . accountSubscriber . eventEmitter . on (
71+ 'spotMarketAccountUpdate' ,
72+ ( data ) => {
73+ console . log ( 'Spot market account update:' , decodeName ( data . name ) ) ;
74+ const spotMarketData = driftClient . getSpotMarketAccount (
75+ data . marketIndex
76+ ) ;
77+ console . log (
78+ 'Spot market data market index:' ,
79+ spotMarketData ?. marketIndex
80+ ) ;
81+ spotMarketUpdateCount ++ ;
82+ if (
83+ perpMarketUpdateCount >= 10 &&
84+ spotMarketUpdateCount >= 10 &&
85+ oraclePriceUpdateCount >= 10 &&
86+ userAccountUpdateCount >= 2
87+ ) {
88+ resolve ( ) ;
89+ }
90+ }
91+ ) ;
6292
63- driftClient . accountSubscriber . eventEmitter . on ( 'oraclePriceUpdate' , ( data ) => {
64- console . log ( 'Oracle price update:' , data . toBase58 ( ) ) ;
65- oraclePriceUpdateCount ++ ;
66- if ( perpMarketUpdateCount >= 10 && spotMarketUpdateCount >= 10 && oraclePriceUpdateCount >= 10 && userAccountUpdateCount >= 2 ) {
67- resolve ( ) ;
68- }
69- } ) ;
93+ driftClient . accountSubscriber . eventEmitter . on (
94+ 'oraclePriceUpdate' ,
95+ ( data ) => {
96+ console . log ( 'Oracle price update:' , data . toBase58 ( ) ) ;
97+ oraclePriceUpdateCount ++ ;
98+ if (
99+ perpMarketUpdateCount >= 10 &&
100+ spotMarketUpdateCount >= 10 &&
101+ oraclePriceUpdateCount >= 10 &&
102+ userAccountUpdateCount >= 2
103+ ) {
104+ resolve ( ) ;
105+ }
106+ }
107+ ) ;
70108
71- driftClient . accountSubscriber . eventEmitter . on ( 'userAccountUpdate' , ( data ) => {
72- console . log ( 'User account update:' , decodeName ( data . name ) ) ;
73- userAccountUpdateCount ++ ;
74- if ( perpMarketUpdateCount >= 10 && spotMarketUpdateCount >= 10 && oraclePriceUpdateCount >= 10 && userAccountUpdateCount >= 2 ) {
75- resolve ( ) ;
76- }
77- } ) ;
78- } ) ;
109+ driftClient . accountSubscriber . eventEmitter . on (
110+ 'userAccountUpdate' ,
111+ ( data ) => {
112+ console . log ( 'User account update:' , decodeName ( data . name ) ) ;
113+ userAccountUpdateCount ++ ;
79114
80- await driftClient . subscribe ( ) ;
81- console . log ( 'DriftClient initialized and listening for updates.' ) ;
115+ if (
116+ perpMarketUpdateCount >= 10 &&
117+ spotMarketUpdateCount >= 10 &&
118+ oraclePriceUpdateCount >= 10 &&
119+ userAccountUpdateCount >= 2
120+ ) {
121+ resolve ( ) ;
122+ }
123+ }
124+ ) ;
125+ } ) ;
82126
83- await updatePromise ;
84- console . log ( 'Received required number of updates.' ) ;
127+ await driftClient . subscribe ( ) ;
128+ console . log ( 'DriftClient initialized and listening for updates.' ) ;
129+
130+ await updatePromise ;
131+ console . log ( 'Received required number of updates.' ) ;
85132}
86133
87134initializeGrpcDriftClientV2 ( ) . catch ( console . error ) ;
0 commit comments