2018-04-16 17:21:06 +02:00
|
|
|
const ObservableStore = require('obs-store')
|
2018-11-03 03:14:40 +01:00
|
|
|
const log = require('loglevel')
|
2018-04-16 17:21:06 +02:00
|
|
|
|
|
|
|
// 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, preferences } = {}) {
|
|
|
|
this.store = new ObservableStore()
|
|
|
|
this.preferences = preferences
|
|
|
|
this.interval = interval
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates exchange rates for all tokens
|
|
|
|
*/
|
|
|
|
async updateExchangeRates () {
|
2018-04-16 23:45:18 +02:00
|
|
|
if (!this.isActive) { return }
|
2018-04-16 17:21:06 +02:00
|
|
|
const contractExchangeRates = {}
|
2018-11-03 03:14:40 +01:00
|
|
|
// 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
|
2018-04-16 17:21:06 +02:00
|
|
|
contractExchangeRates[address] = await this.fetchExchangeRate(address)
|
|
|
|
}
|
|
|
|
this.store.putState({ contractExchangeRates })
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches a token exchange rate by address
|
|
|
|
*
|
|
|
|
* @param {String} address - Token contract address
|
|
|
|
*/
|
|
|
|
async fetchExchangeRate (address) {
|
|
|
|
try {
|
2018-04-24 23:12:57 +02:00
|
|
|
const response = await fetch(`https://metamask.balanc3.net/prices?from=${address}&to=ETH&autoConversion=false&summaryOnly=true`)
|
2018-04-16 17:21:06 +02:00
|
|
|
const json = await response.json()
|
|
|
|
return json && json.length ? json[0].averagePrice : 0
|
2018-04-27 00:31:54 +02:00
|
|
|
} catch (error) {
|
2018-11-03 03:14:40 +01:00
|
|
|
log.warn(`MetaMask - TokenRatesController exchange rate fetch failed for ${address}.`, error)
|
2018-04-27 00:31:54 +02:00
|
|
|
return 0
|
|
|
|
}
|
2018-04-16 17:21:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-18 23:24:36 +02:00
|
|
|
* @type {Number}
|
2018-04-16 17:21:06 +02:00
|
|
|
*/
|
|
|
|
set interval (interval) {
|
|
|
|
this._handle && clearInterval(this._handle)
|
|
|
|
if (!interval) { return }
|
|
|
|
this._handle = setInterval(() => { this.updateExchangeRates() }, interval)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-18 23:24:36 +02:00
|
|
|
* @type {Object}
|
2018-04-16 17:21:06 +02:00
|
|
|
*/
|
|
|
|
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 })
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-18 23:24:36 +02:00
|
|
|
* @type {Array}
|
2018-04-16 17:21:06 +02:00
|
|
|
*/
|
|
|
|
set tokens (tokens) {
|
|
|
|
this._tokens = tokens
|
|
|
|
this.updateExchangeRates()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = TokenRatesController
|