Switch to batch mempool/txs/:txid endpoint
This commit is contained in:
		
							parent
							
								
									202d4122b4
								
							
						
					
					
						commit
						db715a1dba
					
				| @ -5,8 +5,6 @@ import { AbstractBitcoinApi } from './bitcoin-api-abstract-factory'; | |||||||
| import { IEsploraApi } from './esplora-api.interface'; | import { IEsploraApi } from './esplora-api.interface'; | ||||||
| import logger from '../../logger'; | import logger from '../../logger'; | ||||||
| 
 | 
 | ||||||
| import JsonStream from 'JSONStream'; |  | ||||||
| 
 |  | ||||||
| const axiosConnection = axios.create({ | const axiosConnection = axios.create({ | ||||||
|   httpAgent: new http.Agent({ keepAlive: true, }) |   httpAgent: new http.Agent({ keepAlive: true, }) | ||||||
| }); | }); | ||||||
| @ -74,22 +72,30 @@ class ElectrsApi implements AbstractBitcoinApi { | |||||||
|   async $getMempoolTransactions(expectedCount: number): Promise<IEsploraApi.Transaction[]> { |   async $getMempoolTransactions(expectedCount: number): Promise<IEsploraApi.Transaction[]> { | ||||||
|     const transactions: IEsploraApi.Transaction[] = []; |     const transactions: IEsploraApi.Transaction[] = []; | ||||||
|     let count = 0; |     let count = 0; | ||||||
|     return new Promise((resolve, reject) => { |     let done = false; | ||||||
|       axiosConnection.get(config.ESPLORA.REST_API_URL + '/mempool/txs', { ...this.activeAxiosConfig, timeout: 60000, responseType: 'stream' }).then(response => { |     let last_txid = ''; | ||||||
|         response.data.pipe(JsonStream.parse('*')).on('data', transaction => { |     while (!done) { | ||||||
|  |       try { | ||||||
|  |         const result = await this.$queryWrapper<IEsploraApi.Transaction[]>(config.ESPLORA.REST_API_URL + '/mempool/txs' + (last_txid ? '/' + last_txid : '')); | ||||||
|  |         if (result) { | ||||||
|  |           for (const tx of result) { | ||||||
|  |             transactions.push(tx); | ||||||
|             count++; |             count++; | ||||||
|           if (count % 10000 === 0) { |  | ||||||
|             logger.info(`Fetched ${count} of ${expectedCount} mempool transactions from esplora`); |  | ||||||
|           } |           } | ||||||
|           transactions.push(transaction); |           logger.info(`Fetched ${count} of ${expectedCount} mempool transactions from esplora`); | ||||||
|         }).on('end', () => { |           if (result.length > 0) { | ||||||
|           logger.info(`Fetched all ${count} of ${expectedCount} mempool transactions from esplora`); |             last_txid = result[result.length - 1].txid; | ||||||
|           resolve(transactions); |           } else { | ||||||
|         }).on('error', (err) => { |             done = true; | ||||||
|           reject(err); |           } | ||||||
|         }); |         } else { | ||||||
|       }); |           done = true; | ||||||
|     }); |         } | ||||||
|  |       } catch(err) { | ||||||
|  |         logger.err('failed to fetch bulk mempool transactions from esplora'); | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |     return transactions; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   $getTransactionHex(txId: string): Promise<string> { |   $getTransactionHex(txId: string): Promise<string> { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user