token-rates - protect against bad token data

feature/default_network_editable
kumavis 6 years ago
parent e683684e8d
commit 2c1bca1ab0
  1. 11
      app/scripts/controllers/token-rates.js

@ -1,5 +1,5 @@
const ObservableStore = require('obs-store') const ObservableStore = require('obs-store')
const { warn } = require('loglevel') const log = require('loglevel')
// By default, poll every 3 minutes // By default, poll every 3 minutes
const DEFAULT_INTERVAL = 180 * 1000 const DEFAULT_INTERVAL = 180 * 1000
@ -26,8 +26,11 @@ class TokenRatesController {
async updateExchangeRates () { async updateExchangeRates () {
if (!this.isActive) { return } if (!this.isActive) { return }
const contractExchangeRates = {} const contractExchangeRates = {}
for (const i in this._tokens) { // copy array to ensure its not modified during iteration
const address = this._tokens[i].address const tokens = this._tokens.slice()
for (const token of tokens) {
if (!token) return log.error(`TokenRatesController - invalid tokens state:\n${JSON.stringify(tokens, null, 2)}`)
const address = token.address
contractExchangeRates[address] = await this.fetchExchangeRate(address) contractExchangeRates[address] = await this.fetchExchangeRate(address)
} }
this.store.putState({ contractExchangeRates }) this.store.putState({ contractExchangeRates })
@ -44,7 +47,7 @@ class TokenRatesController {
const json = await response.json() const json = await response.json()
return json && json.length ? json[0].averagePrice : 0 return json && json.length ? json[0].averagePrice : 0
} catch (error) { } catch (error) {
warn(`MetaMask - TokenRatesController exchange rate fetch failed for ${address}.`, error) log.warn(`MetaMask - TokenRatesController exchange rate fetch failed for ${address}.`, error)
return 0 return 0
} }
} }

Loading…
Cancel
Save