1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Merge pull request #5659 from MetaMask/token-rates-bugfix

token-rates - protect against bad token data
This commit is contained in:
kumavis 2018-11-03 21:57:21 -04:00 committed by GitHub
commit 14852669ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,5 @@
const ObservableStore = require('obs-store')
const { warn } = require('loglevel')
const log = require('loglevel')
// By default, poll every 3 minutes
const DEFAULT_INTERVAL = 180 * 1000
@ -26,8 +26,11 @@ class TokenRatesController {
async updateExchangeRates () {
if (!this.isActive) { return }
const contractExchangeRates = {}
for (const i in this._tokens) {
const address = this._tokens[i].address
// copy array to ensure its not modified during iteration
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)
}
this.store.putState({ contractExchangeRates })
@ -44,7 +47,7 @@ class TokenRatesController {
const json = await response.json()
return json && json.length ? json[0].averagePrice : 0
} 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
}
}