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
|
|
|
|
*/
|
2018-11-13 20:57:43 +01:00
|
|
|
constructor ({ interval = DEFAULT_INTERVAL, currency, preferences } = {}) {
|
2018-04-16 17:21:06 +02:00
|
|
|
this.store = new ObservableStore()
|
2018-11-13 20:57:43 +01:00
|
|
|
this.currency = currency
|
2018-04-16 17:21:06 +02:00
|
|
|
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-13 20:57:43 +01:00
|
|
|
const nativeCurrency = this.currency ? this.currency.getState().nativeCurrency.toUpperCase() : 'ETH'
|
|
|
|
const pairs = this._tokens.map(token => `pairs[]=${token.address}/${nativeCurrency}`)
|
|
|
|
const query = pairs.join('&')
|
|
|
|
if (this._tokens.length > 0) {
|
|
|
|
try {
|
|
|
|
const response = await fetch(`https://exchanges.balanc3.net/pie?${query}&autoConversion=true`)
|
|
|
|
const { prices = [] } = await response.json()
|
|
|
|
prices.forEach(({ pair, price }) => {
|
|
|
|
contractExchangeRates[pair.split('/')[0]] = typeof price === 'number' ? price : 0
|
|
|
|
})
|
|
|
|
} catch (error) {
|
|
|
|
log.warn(`MetaMask - TokenRatesController exchange rate fetch failed.`, error)
|
|
|
|
}
|
2018-04-16 17:21:06 +02:00
|
|
|
}
|
|
|
|
this.store.putState({ contractExchangeRates })
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|