2020-01-09 04:34:58 +01:00
|
|
|
import ObservableStore from 'obs-store'
|
|
|
|
import log from 'loglevel'
|
|
|
|
import { normalize as normalizeAddress } from 'eth-sig-util'
|
|
|
|
import ethUtil from 'ethereumjs-util'
|
2019-04-30 18:49:58 +02:00
|
|
|
|
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 () {
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!this.isActive) {
|
|
|
|
return
|
|
|
|
}
|
2018-04-16 17:21:06 +02:00
|
|
|
const contractExchangeRates = {}
|
2019-06-01 00:14:22 +02:00
|
|
|
const nativeCurrency = this.currency ? this.currency.state.nativeCurrency.toLowerCase() : 'eth'
|
2020-02-15 21:34:12 +01:00
|
|
|
const pairs = this._tokens.map((token) => token.address).join(',')
|
2019-04-11 00:37:15 +02:00
|
|
|
const query = `contract_addresses=${pairs}&vs_currencies=${nativeCurrency}`
|
2018-11-13 20:57:43 +01:00
|
|
|
if (this._tokens.length > 0) {
|
|
|
|
try {
|
2019-04-11 00:37:15 +02:00
|
|
|
const response = await fetch(`https://api.coingecko.com/api/v3/simple/token_price/ethereum?${query}`)
|
|
|
|
const prices = await response.json()
|
2020-02-15 21:34:12 +01:00
|
|
|
this._tokens.forEach((token) => {
|
2019-04-30 18:49:58 +02:00
|
|
|
const price = prices[token.address.toLowerCase()] || prices[ethUtil.toChecksumAddress(token.address)]
|
2019-04-11 00:37:15 +02:00
|
|
|
contractExchangeRates[normalizeAddress(token.address)] = price ? price[nativeCurrency] : 0
|
2018-11-13 20:57:43 +01:00
|
|
|
})
|
|
|
|
} 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)
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!interval) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this._handle = setInterval(() => {
|
|
|
|
this.updateExchangeRates()
|
|
|
|
}, interval)
|
2018-04-16 17:21:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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()
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!preferences) {
|
|
|
|
return
|
|
|
|
}
|
2018-04-16 17:21:06 +02:00
|
|
|
this._preferences = preferences
|
|
|
|
this.tokens = preferences.getState().tokens
|
2019-11-20 01:03:20 +01:00
|
|
|
preferences.subscribe(({ tokens = [] }) => {
|
|
|
|
this.tokens = tokens
|
|
|
|
})
|
2018-04-16 17:21:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export default TokenRatesController
|