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
|
|
|
|
*/
|
2020-05-06 00:19:38 +02:00
|
|
|
export default class TokenRatesController {
|
2020-08-19 18:27:05 +02:00
|
|
|
|
2018-04-16 17:21:06 +02:00
|
|
|
/**
|
|
|
|
* Creates a TokenRatesController
|
|
|
|
*
|
|
|
|
* @param {Object} [config] - Options to configure controller
|
|
|
|
*/
|
2020-06-05 20:36:55 +02:00
|
|
|
constructor ({ 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
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates exchange rates for all tokens
|
|
|
|
*/
|
|
|
|
async updateExchangeRates () {
|
|
|
|
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 {
|
2020-04-15 19:23:27 +02:00
|
|
|
const response = await window.fetch(`https://api.coingecko.com/api/v3/simple/token_price/ethereum?${query}`)
|
2019-04-11 00:37:15 +02:00
|
|
|
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 {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-06-05 20:36:55 +02:00
|
|
|
|
|
|
|
start (interval = DEFAULT_INTERVAL) {
|
|
|
|
this._handle && clearInterval(this._handle)
|
|
|
|
if (!interval) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this._handle = setInterval(() => {
|
|
|
|
this.updateExchangeRates()
|
|
|
|
}, interval)
|
|
|
|
this.updateExchangeRates()
|
|
|
|
}
|
|
|
|
|
|
|
|
stop () {
|
|
|
|
this._handle && clearInterval(this._handle)
|
|
|
|
}
|
2018-04-16 17:21:06 +02:00
|
|
|
}
|