Push new conversion rate updates to the clients.
This commit is contained in:
		
							parent
							
								
									dc63fd9428
								
							
						
					
					
						commit
						b91516a1c1
					
				| @ -1,13 +1,19 @@ | ||||
| import logger from '../logger'; | ||||
| import axios from 'axios'; | ||||
| import { IConversionRates } from '../mempool.interfaces'; | ||||
| 
 | ||||
| class FiatConversion { | ||||
|   private conversionRates = { | ||||
|   private conversionRates: IConversionRates = { | ||||
|     'USD': 0 | ||||
|   }; | ||||
|   private ratesChangedCallback: ((rates: IConversionRates) => void) | undefined; | ||||
| 
 | ||||
|   constructor() { } | ||||
| 
 | ||||
|   public setProgressChangedCallback(fn: (rates: IConversionRates) => void) { | ||||
|     this.ratesChangedCallback = fn; | ||||
|   } | ||||
| 
 | ||||
|   public startService() { | ||||
|     logger.info('Starting currency rates service'); | ||||
|     setInterval(this.updateCurrency.bind(this), 1000 * 60 * 60); | ||||
| @ -25,6 +31,9 @@ class FiatConversion { | ||||
|       this.conversionRates = { | ||||
|         'USD': usd.price, | ||||
|       }; | ||||
|       if (this.ratesChangedCallback) { | ||||
|         this.ratesChangedCallback(this.conversionRates); | ||||
|       } | ||||
|     } catch (e) { | ||||
|       logger.err('Error updating fiat conversion rates: ' + e); | ||||
|     } | ||||
|  | ||||
| @ -1,6 +1,6 @@ | ||||
| import logger from '../logger'; | ||||
| import * as WebSocket from 'ws'; | ||||
| import { BlockExtended, TransactionExtended, WebsocketResponse, MempoolBlock, OptimizedStatistic, ILoadingIndicators } from '../mempool.interfaces'; | ||||
| import { BlockExtended, TransactionExtended, WebsocketResponse, MempoolBlock, OptimizedStatistic, ILoadingIndicators, IConversionRates } from '../mempool.interfaces'; | ||||
| import blocks from './blocks'; | ||||
| import memPool from './mempool'; | ||||
| import backendInfo from './backend-info'; | ||||
| @ -131,6 +131,19 @@ class WebsocketHandler { | ||||
|     }); | ||||
|   } | ||||
| 
 | ||||
|   handleNewConversionRates(conversionRates: IConversionRates) { | ||||
|     if (!this.wss) { | ||||
|       throw new Error('WebSocket.Server is not set'); | ||||
|     } | ||||
| 
 | ||||
|     this.wss.clients.forEach((client: WebSocket) => { | ||||
|       if (client.readyState !== WebSocket.OPEN) { | ||||
|         return; | ||||
|       } | ||||
|       client.send(JSON.stringify({ conversions: conversionRates })); | ||||
|     }); | ||||
|   } | ||||
| 
 | ||||
|   getInitData(_blocks?: BlockExtended[]) { | ||||
|     if (!_blocks) { | ||||
|       _blocks = blocks.getBlocks().slice(-8); | ||||
|  | ||||
| @ -136,6 +136,7 @@ class Server { | ||||
|     blocks.setNewBlockCallback(websocketHandler.handleNewBlock.bind(websocketHandler)); | ||||
|     memPool.setMempoolChangedCallback(websocketHandler.handleMempoolChange.bind(websocketHandler)); | ||||
|     donations.setNotfyDonationStatusCallback(websocketHandler.handleNewDonation.bind(websocketHandler)); | ||||
|     fiatConversion.setProgressChangedCallback(websocketHandler.handleNewConversionRates.bind(websocketHandler)); | ||||
|     loadingIndicators.setProgressChangedCallback(websocketHandler.handleLoadingChanged.bind(websocketHandler)); | ||||
|   } | ||||
| 
 | ||||
|  | ||||
| @ -137,3 +137,4 @@ interface RequiredParams { | ||||
| } | ||||
| 
 | ||||
| export interface ILoadingIndicators { [name: string]: number; } | ||||
| export interface IConversionRates { [currency: string]: number; } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user