2020-12-16 22:14:49 +01:00
|
|
|
import { ObservableStore } from '@metamask/obs-store'
|
2020-01-09 04:34:58 +01:00
|
|
|
import log from 'loglevel'
|
|
|
|
import { normalize as normalizeAddress } from 'eth-sig-util'
|
|
|
|
import ethUtil from 'ethereumjs-util'
|
2021-01-19 17:41:57 +01:00
|
|
|
import getFetchWithTimeout from '../../../shared/modules/fetch-with-timeout'
|
|
|
|
|
|
|
|
const fetchWithTimeout = getFetchWithTimeout(30000)
|
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 {
|
2018-04-16 17:21:06 +02:00
|
|
|
/**
|
|
|
|
* Creates a TokenRatesController
|
|
|
|
*
|
|
|
|
* @param {Object} [config] - Options to configure controller
|
|
|
|
*/
|
2020-11-03 00:41:28 +01: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
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
async updateExchangeRates() {
|
2018-04-16 17:21:06 +02:00
|
|
|
const contractExchangeRates = {}
|
2020-11-03 00:41:28 +01: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 {
|
2021-01-19 17:41:57 +01:00
|
|
|
const response = await fetchWithTimeout(
|
2020-11-03 00:41:28 +01:00
|
|
|
`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) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const price =
|
|
|
|
prices[token.address.toLowerCase()] ||
|
|
|
|
prices[ethUtil.toChecksumAddress(token.address)]
|
|
|
|
contractExchangeRates[normalizeAddress(token.address)] = price
|
|
|
|
? price[nativeCurrency]
|
|
|
|
: 0
|
2018-11-13 20:57:43 +01:00
|
|
|
})
|
|
|
|
} catch (error) {
|
2020-11-03 00:41:28 +01:00
|
|
|
log.warn(
|
|
|
|
`MetaMask - TokenRatesController exchange rate fetch failed.`,
|
|
|
|
error,
|
|
|
|
)
|
2018-11-13 20:57:43 +01:00
|
|
|
}
|
2018-04-16 17:21:06 +02:00
|
|
|
}
|
|
|
|
this.store.putState({ contractExchangeRates })
|
|
|
|
}
|
|
|
|
|
2020-10-22 19:06:44 +02:00
|
|
|
/* eslint-disable accessor-pairs */
|
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
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
set preferences(preferences) {
|
2018-04-16 17:21:06 +02:00
|
|
|
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
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
set tokens(tokens) {
|
2018-04-16 17:21:06 +02:00
|
|
|
this._tokens = tokens
|
|
|
|
this.updateExchangeRates()
|
|
|
|
}
|
2020-10-22 19:06:44 +02:00
|
|
|
/* eslint-enable accessor-pairs */
|
2020-06-05 20:36:55 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
start(interval = DEFAULT_INTERVAL) {
|
2020-06-05 20:36:55 +02:00
|
|
|
this._handle && clearInterval(this._handle)
|
|
|
|
if (!interval) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this._handle = setInterval(() => {
|
|
|
|
this.updateExchangeRates()
|
|
|
|
}, interval)
|
|
|
|
this.updateExchangeRates()
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
stop() {
|
2020-06-05 20:36:55 +02:00
|
|
|
this._handle && clearInterval(this._handle)
|
|
|
|
}
|
2018-04-16 17:21:06 +02:00
|
|
|
}
|