mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-23 02:10:12 +01:00
5ee1291662
Previously all browser globals were allowed to be used anywhere by ESLint because we had set the `env` property to `browser` in the ESLint config. This has made it easy to accidentally use browser globals (e.g. #8338), so it has been removed. Instead we now have a short list of allowed globals. All browser globals are now accessed as properties on `window`. Unfortunately this change resulted in a few different confusing unit test errors, as some of our unit tests setup assumed that a particular global would be used via `window` or `global`. In particular, `window.fetch` didn't work correctly because it wasn't patched by the AbortController polyfill (only `global.fetch` was being patched). The `jsdom-global` package we were using complicated matters by setting all of the JSDOM `window` properties directly on `global`, overwriting the `AbortController` for example. The `helpers.js` test setup module has been simplified somewhat by removing `jsdom-global` and constructing the JSDOM instance manually. The JSDOM window is set on `window`, and a few properties are set on `global` as well as needed by various dependencies. `node-fetch` and the AbortController polyfill/patch now work as expected as well, though `fetch` is only available on `window` now.
91 lines
2.5 KiB
JavaScript
91 lines
2.5 KiB
JavaScript
import ObservableStore from 'obs-store'
|
|
import log from 'loglevel'
|
|
import { normalize as normalizeAddress } from 'eth-sig-util'
|
|
import ethUtil from 'ethereumjs-util'
|
|
|
|
|
|
// By default, poll every 3 minutes
|
|
const DEFAULT_INTERVAL = 180 * 1000
|
|
|
|
/**
|
|
* A controller that polls for token exchange
|
|
* rates based on a user's current token list
|
|
*/
|
|
class TokenRatesController {
|
|
/**
|
|
* Creates a TokenRatesController
|
|
*
|
|
* @param {Object} [config] - Options to configure controller
|
|
*/
|
|
constructor ({ interval = DEFAULT_INTERVAL, currency, preferences } = {}) {
|
|
this.store = new ObservableStore()
|
|
this.currency = currency
|
|
this.preferences = preferences
|
|
this.interval = interval
|
|
}
|
|
|
|
/**
|
|
* Updates exchange rates for all tokens
|
|
*/
|
|
async updateExchangeRates () {
|
|
if (!this.isActive) {
|
|
return
|
|
}
|
|
const contractExchangeRates = {}
|
|
const nativeCurrency = this.currency ? this.currency.state.nativeCurrency.toLowerCase() : 'eth'
|
|
const pairs = this._tokens.map((token) => token.address).join(',')
|
|
const query = `contract_addresses=${pairs}&vs_currencies=${nativeCurrency}`
|
|
if (this._tokens.length > 0) {
|
|
try {
|
|
const response = await window.fetch(`https://api.coingecko.com/api/v3/simple/token_price/ethereum?${query}`)
|
|
const prices = await response.json()
|
|
this._tokens.forEach((token) => {
|
|
const price = prices[token.address.toLowerCase()] || prices[ethUtil.toChecksumAddress(token.address)]
|
|
contractExchangeRates[normalizeAddress(token.address)] = price ? price[nativeCurrency] : 0
|
|
})
|
|
} catch (error) {
|
|
log.warn(`MetaMask - TokenRatesController exchange rate fetch failed.`, error)
|
|
}
|
|
}
|
|
this.store.putState({ contractExchangeRates })
|
|
}
|
|
|
|
/**
|
|
* @type {Number}
|
|
*/
|
|
set interval (interval) {
|
|
this._handle && clearInterval(this._handle)
|
|
if (!interval) {
|
|
return
|
|
}
|
|
this._handle = setInterval(() => {
|
|
this.updateExchangeRates()
|
|
}, interval)
|
|
}
|
|
|
|
/**
|
|
* @type {Object}
|
|
*/
|
|
set preferences (preferences) {
|
|
this._preferences && this._preferences.unsubscribe()
|
|
if (!preferences) {
|
|
return
|
|
}
|
|
this._preferences = preferences
|
|
this.tokens = preferences.getState().tokens
|
|
preferences.subscribe(({ tokens = [] }) => {
|
|
this.tokens = tokens
|
|
})
|
|
}
|
|
|
|
/**
|
|
* @type {Array}
|
|
*/
|
|
set tokens (tokens) {
|
|
this._tokens = tokens
|
|
this.updateExchangeRates()
|
|
}
|
|
}
|
|
|
|
export default TokenRatesController
|