Make sure exchange API response format is valid before using it
This commit is contained in:
		
							parent
							
								
									b0d05ba787
								
							
						
					
					
						commit
						4bb812a7e6
					
				@ -13,7 +13,11 @@ class BitfinexApi implements PriceFeed {
 | 
			
		||||
 | 
			
		||||
  public async $fetchPrice(currency): Promise<number> {
 | 
			
		||||
    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> {
 | 
			
		||||
 | 
			
		||||
@ -13,7 +13,11 @@ class BitflyerApi implements PriceFeed {
 | 
			
		||||
 | 
			
		||||
  public async $fetchPrice(currency): Promise<number> {
 | 
			
		||||
    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> {
 | 
			
		||||
 | 
			
		||||
@ -13,7 +13,11 @@ class CoinbaseApi implements PriceFeed {
 | 
			
		||||
 | 
			
		||||
  public async $fetchPrice(currency): Promise<number> {
 | 
			
		||||
    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> {
 | 
			
		||||
 | 
			
		||||
@ -13,7 +13,11 @@ class GeminiApi implements PriceFeed {
 | 
			
		||||
 | 
			
		||||
  public async $fetchPrice(currency): Promise<number> {
 | 
			
		||||
    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> {
 | 
			
		||||
 | 
			
		||||
@ -23,7 +23,14 @@ class KrakenApi implements PriceFeed {
 | 
			
		||||
 | 
			
		||||
  public async $fetchPrice(currency): Promise<number> {
 | 
			
		||||
    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> {
 | 
			
		||||
 | 
			
		||||
@ -127,8 +127,12 @@ class PriceUpdater {
 | 
			
		||||
 | 
			
		||||
      // Compute average price, non weighted
 | 
			
		||||
      prices = prices.filter(price => price > 0);
 | 
			
		||||
      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)}`);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user