@@ -12,7 +12,7 @@ import memPool from './api/mempool';
1212import blocks from './api/blocks' ;
1313import projectedBlocks from './api/projected-blocks' ;
1414import statistics from './api/statistics' ;
15- import { IBlock , IMempool } from './interfaces' ;
15+ import { IBlock , IMempool , ITransaction , IMempoolStats } from './interfaces' ;
1616
1717import routes from './routes' ;
1818import fiatConversion from './api/fiat-conversion' ;
@@ -57,7 +57,7 @@ class MempoolSpace {
5757
5858 private async runMempoolIntervalFunctions ( ) {
5959 await blocks . updateBlocks ( ) ;
60- await memPool . getMemPoolInfo ( ) ;
60+ await memPool . updateMemPoolInfo ( ) ;
6161 await memPool . updateMempool ( ) ;
6262 setTimeout ( this . runMempoolIntervalFunctions . bind ( this ) , config . MEMPOOL_REFRESH_RATE_MS ) ;
6363 }
@@ -79,7 +79,6 @@ class MempoolSpace {
7979 this . wss . on ( 'connection' , ( client : WebSocket ) => {
8080 let theBlocks = blocks . getBlocks ( ) ;
8181 theBlocks = theBlocks . concat ( [ ] ) . splice ( theBlocks . length - config . INITIAL_BLOCK_AMOUNT ) ;
82-
8382 const formatedBlocks = theBlocks . map ( ( b ) => blocks . formatBlock ( b ) ) ;
8483
8584 client . send ( JSON . stringify ( {
@@ -94,6 +93,14 @@ class MempoolSpace {
9493 client . on ( 'message' , async ( message : any ) => {
9594 try {
9695 const parsedMessage = JSON . parse ( message ) ;
96+
97+ if ( parsedMessage . action === 'want' ) {
98+ client [ 'want-stats' ] = parsedMessage . data . indexOf ( 'stats' ) > - 1 ;
99+ client [ 'want-blocks' ] = parsedMessage . data . indexOf ( 'blocks' ) > - 1 ;
100+ client [ 'want-projected-blocks' ] = parsedMessage . data . indexOf ( 'projected-blocks' ) > - 1 ;
101+ client [ 'want-live-2h-chart' ] = parsedMessage . data . indexOf ( 'live-2h-chart' ) > - 1 ;
102+ }
103+
97104 if ( parsedMessage . action === 'track-tx' && parsedMessage . txId && / ^ [ a - f A - F 0 - 9 ] { 64 } $ / . test ( parsedMessage . txId ) ) {
98105 const tx = await memPool . getRawTransaction ( parsedMessage . txId ) ;
99106 if ( tx ) {
@@ -168,26 +175,29 @@ class MempoolSpace {
168175 return ;
169176 }
170177
178+ const response = { } ;
179+
171180 if ( client [ 'trackingTx' ] === true && client [ 'blockHeight' ] === 0 ) {
172- if ( block . tx . some ( ( tx ) => tx === client [ 'txId' ] ) ) {
181+ if ( block . tx . some ( ( tx : ITransaction ) => tx === client [ 'txId' ] ) ) {
173182 client [ 'blockHeight' ] = block . height ;
174183 }
175184 }
176185
177- client . send ( JSON . stringify ( {
178- 'block' : formattedBlocks ,
179- 'track-tx' : {
180- tracking : client [ 'trackingTx' ] || false ,
181- blockHeight : client [ 'blockHeight' ] ,
182- }
183- } ) ) ;
186+ response [ 'track-tx' ] = {
187+ tracking : client [ 'trackingTx' ] || false ,
188+ blockHeight : client [ 'blockHeight' ] ,
189+ } ;
190+
191+ response [ 'block' ] = formattedBlocks ;
192+
193+ client . send ( JSON . stringify ( response ) ) ;
184194 } ) ;
185195 } ) ;
186196
187197 memPool . setMempoolChangedCallback ( ( newMempool : IMempool ) => {
188198 projectedBlocks . updateProjectedBlocks ( newMempool ) ;
189199
190- let pBlocks = projectedBlocks . getProjectedBlocks ( ) ;
200+ const pBlocks = projectedBlocks . getProjectedBlocks ( ) ;
191201 const mempoolInfo = memPool . getMempoolInfo ( ) ;
192202 const txPerSecond = memPool . getTxPerSecond ( ) ;
193203 const vBytesPerSecond = memPool . getVBytesPerSecond ( ) ;
@@ -197,20 +207,41 @@ class MempoolSpace {
197207 return ;
198208 }
199209
200- if ( client [ 'trackingTx' ] && client [ 'blockHeight' ] === 0 ) {
201- pBlocks = projectedBlocks . getProjectedBlocks ( client [ 'txId' ] ) ;
202- }
210+ const response = { } ;
203211
204- client . send ( JSON . stringify ( {
205- 'projectedBlocks' : pBlocks ,
206- 'mempoolInfo' : mempoolInfo ,
207- 'txPerSecond' : txPerSecond ,
208- 'vBytesPerSecond' : vBytesPerSecond ,
209- 'track-tx' : {
212+ if ( client [ 'want-stats' ] ) {
213+ response [ 'mempoolInfo' ] = mempoolInfo ;
214+ response [ 'txPerSecond' ] = txPerSecond ;
215+ response [ 'vBytesPerSecond' ] = vBytesPerSecond ;
216+ response [ 'track-tx' ] = {
210217 tracking : client [ 'trackingTx' ] || false ,
211218 blockHeight : client [ 'blockHeight' ] ,
212- }
213- } ) ) ;
219+ } ;
220+ }
221+
222+ if ( client [ 'want-projected-blocks' ] && client [ 'trackingTx' ] && client [ 'blockHeight' ] === 0 ) {
223+ response [ 'projectedBlocks' ] = projectedBlocks . getProjectedBlocks ( client [ 'txId' ] ) ;
224+ } else if ( client [ 'want-projected-blocks' ] ) {
225+ response [ 'projectedBlocks' ] = pBlocks ;
226+ }
227+
228+ if ( Object . keys ( response ) . length ) {
229+ client . send ( JSON . stringify ( response ) ) ;
230+ }
231+ } ) ;
232+ } ) ;
233+
234+ statistics . setNewStatisticsEntryCallback ( ( stats : IMempoolStats ) => {
235+ this . wss . clients . forEach ( ( client : WebSocket ) => {
236+ if ( client . readyState !== WebSocket . OPEN ) {
237+ return ;
238+ }
239+
240+ if ( client [ 'want-live-2h-chart' ] ) {
241+ client . send ( JSON . stringify ( {
242+ 'live-2h-chart' : stats
243+ } ) ) ;
244+ }
214245 } ) ;
215246 } ) ;
216247 }
@@ -220,7 +251,6 @@ class MempoolSpace {
220251 . get ( config . API_ENDPOINT + 'transactions/height/:id' , routes . $getgetTransactionsForBlock )
221252 . get ( config . API_ENDPOINT + 'transactions/projected/:id' , routes . getgetTransactionsForProjectedBlock )
222253 . get ( config . API_ENDPOINT + 'fees/recommended' , routes . getRecommendedFees )
223- . get ( config . API_ENDPOINT + 'statistics/live' , routes . getLiveResult )
224254 . get ( config . API_ENDPOINT + 'statistics/2h' , routes . get2HStatistics )
225255 . get ( config . API_ENDPOINT + 'statistics/24h' , routes . get24HStatistics )
226256 . get ( config . API_ENDPOINT + 'statistics/1w' , routes . get1WHStatistics )
0 commit comments