Merge pull request #2770 from mempool/nymkappa/bugfix/price-update-invalid-response
Make sure exchange API response format is valid before using it
This commit is contained in:
		
						commit
						83fc60d6ee
					
				| @ -13,7 +13,11 @@ class BitfinexApi implements PriceFeed { | |||||||
| 
 | 
 | ||||||
|   public async $fetchPrice(currency): Promise<number> { |   public async $fetchPrice(currency): Promise<number> { | ||||||
|     const response = await query(this.url + currency); |     const response = await query(this.url + currency); | ||||||
|     return response ? parseInt(response['last_price'], 10) : -1; |     if (response && response['last_price']) { | ||||||
|  |       return parseInt(response['last_price'], 10); | ||||||
|  |     } else { | ||||||
|  |       return -1; | ||||||
|  |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   public async $fetchRecentPrice(currencies: string[], type: 'hour' | 'day'): Promise<PriceHistory> { |   public async $fetchRecentPrice(currencies: string[], type: 'hour' | 'day'): Promise<PriceHistory> { | ||||||
|  | |||||||
| @ -13,7 +13,11 @@ class BitflyerApi implements PriceFeed { | |||||||
| 
 | 
 | ||||||
|   public async $fetchPrice(currency): Promise<number> { |   public async $fetchPrice(currency): Promise<number> { | ||||||
|     const response = await query(this.url + currency); |     const response = await query(this.url + currency); | ||||||
|     return response ? parseInt(response['ltp'], 10) : -1; |     if (response && response['ltp']) { | ||||||
|  |       return parseInt(response['ltp'], 10); | ||||||
|  |     } else { | ||||||
|  |       return -1; | ||||||
|  |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   public async $fetchRecentPrice(currencies: string[], type: 'hour' | 'day'): Promise<PriceHistory> { |   public async $fetchRecentPrice(currencies: string[], type: 'hour' | 'day'): Promise<PriceHistory> { | ||||||
|  | |||||||
| @ -13,7 +13,11 @@ class CoinbaseApi implements PriceFeed { | |||||||
| 
 | 
 | ||||||
|   public async $fetchPrice(currency): Promise<number> { |   public async $fetchPrice(currency): Promise<number> { | ||||||
|     const response = await query(this.url + currency); |     const response = await query(this.url + currency); | ||||||
|     return response ? parseInt(response['data']['amount'], 10) : -1; |     if (response && response['data'] && response['data']['amount']) { | ||||||
|  |       return parseInt(response['data']['amount'], 10); | ||||||
|  |     } else { | ||||||
|  |       return -1; | ||||||
|  |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   public async $fetchRecentPrice(currencies: string[], type: 'hour' | 'day'): Promise<PriceHistory> { |   public async $fetchRecentPrice(currencies: string[], type: 'hour' | 'day'): Promise<PriceHistory> { | ||||||
|  | |||||||
| @ -13,7 +13,11 @@ class GeminiApi implements PriceFeed { | |||||||
| 
 | 
 | ||||||
|   public async $fetchPrice(currency): Promise<number> { |   public async $fetchPrice(currency): Promise<number> { | ||||||
|     const response = await query(this.url + currency); |     const response = await query(this.url + currency); | ||||||
|     return response ? parseInt(response['last'], 10) : -1; |     if (response && response['last']) { | ||||||
|  |       return parseInt(response['last'], 10); | ||||||
|  |     } else { | ||||||
|  |       return -1; | ||||||
|  |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   public async $fetchRecentPrice(currencies: string[], type: 'hour' | 'day'): Promise<PriceHistory> { |   public async $fetchRecentPrice(currencies: string[], type: 'hour' | 'day'): Promise<PriceHistory> { | ||||||
|  | |||||||
| @ -23,7 +23,14 @@ class KrakenApi implements PriceFeed { | |||||||
| 
 | 
 | ||||||
|   public async $fetchPrice(currency): Promise<number> { |   public async $fetchPrice(currency): Promise<number> { | ||||||
|     const response = await query(this.url + currency); |     const response = await query(this.url + currency); | ||||||
|     return response ? parseInt(response['result'][this.getTicker(currency)]['c'][0], 10) : -1; |     const ticker = this.getTicker(currency); | ||||||
|  |     if (response && response['result'] && response['result'][ticker] && | ||||||
|  |       response['result'][ticker]['c'] && response['result'][ticker]['c'].length > 0 | ||||||
|  |     ) { | ||||||
|  |       return parseInt(response['result'][ticker]['c'][0], 10); | ||||||
|  |     } else { | ||||||
|  |       return -1; | ||||||
|  |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   public async $fetchRecentPrice(currencies: string[], type: 'hour' | 'day'): Promise<PriceHistory> { |   public async $fetchRecentPrice(currencies: string[], type: 'hour' | 'day'): Promise<PriceHistory> { | ||||||
|  | |||||||
| @ -130,7 +130,11 @@ class PriceUpdater { | |||||||
| 
 | 
 | ||||||
|       // Compute average price, non weighted
 |       // Compute average price, non weighted
 | ||||||
|       prices = prices.filter(price => price > 0); |       prices = prices.filter(price => price > 0); | ||||||
|       this.latestPrices[currency] = Math.round((prices.reduce((partialSum, a) => partialSum + a, 0)) / prices.length); |       if (prices.length === 0) { | ||||||
|  |         this.latestPrices[currency] = -1; | ||||||
|  |       } else { | ||||||
|  |         this.latestPrices[currency] = Math.round((prices.reduce((partialSum, a) => partialSum + a, 0)) / prices.length); | ||||||
|  |       } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     logger.info(`Latest BTC fiat averaged price: ${JSON.stringify(this.latestPrices)}`); |     logger.info(`Latest BTC fiat averaged price: ${JSON.stringify(this.latestPrices)}`); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user