aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/controllers/token-rates.js
diff options
context:
space:
mode:
authorDan Finlay <542863+danfinlay@users.noreply.github.com>2018-04-17 07:49:53 +0800
committerGitHub <noreply@github.com>2018-04-17 07:49:53 +0800
commite4eb69dcc290a7e7eb27cab7ec57a8097345d81e (patch)
treec3c3b50de25ba7aafe6c58a426f0ba6212d63b71 /app/scripts/controllers/token-rates.js
parent6a7fd3c774f50954050d651f7e6b8ff325ce2940 (diff)
parent06689765146c3ca88fc206d93387b05e1db3b81c (diff)
downloadtangerine-wallet-browser-e4eb69dcc290a7e7eb27cab7ec57a8097345d81e.tar
tangerine-wallet-browser-e4eb69dcc290a7e7eb27cab7ec57a8097345d81e.tar.gz
tangerine-wallet-browser-e4eb69dcc290a7e7eb27cab7ec57a8097345d81e.tar.bz2
tangerine-wallet-browser-e4eb69dcc290a7e7eb27cab7ec57a8097345d81e.tar.lz
tangerine-wallet-browser-e4eb69dcc290a7e7eb27cab7ec57a8097345d81e.tar.xz
tangerine-wallet-browser-e4eb69dcc290a7e7eb27cab7ec57a8097345d81e.tar.zst
tangerine-wallet-browser-e4eb69dcc290a7e7eb27cab7ec57a8097345d81e.zip
Merge pull request #3982 from MetaMask/i3981-contract-rates
Fetch token prices based on contract address
Diffstat (limited to 'app/scripts/controllers/token-rates.js')
-rw-r--r--app/scripts/controllers/token-rates.js77
1 files changed, 77 insertions, 0 deletions
diff --git a/app/scripts/controllers/token-rates.js b/app/scripts/controllers/token-rates.js
new file mode 100644
index 000000000..22e3e8154
--- /dev/null
+++ b/app/scripts/controllers/token-rates.js
@@ -0,0 +1,77 @@
+const ObservableStore = require('obs-store')
+
+// 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 () {
+ if (!this.isActive) { return }
+ const contractExchangeRates = {}
+ for (const i in this._tokens) {
+ const address = this._tokens[i].address
+ 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 {
+ const response = await fetch(`https://exchanges.balanc3.net/prices?from=${address}&to=ETH&autoConversion=false&summaryOnly=true`)
+ const json = await response.json()
+ return json && json.length ? json[0].averagePrice : 0
+ } catch (error) { }
+ }
+
+ /**
+ * @type {Number} - Interval used to poll for exchange rates
+ */
+ set interval (interval) {
+ this._handle && clearInterval(this._handle)
+ if (!interval) { return }
+ this._handle = setInterval(() => { this.updateExchangeRates() }, interval)
+ }
+
+ /**
+ * @type {Object} - Preferences controller instance
+ */
+ 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 })
+ }
+
+ /**
+ * @type {Array} - Array of token objects with contract addresses
+ */
+ set tokens (tokens) {
+ this._tokens = tokens
+ this.updateExchangeRates()
+ }
+}
+
+module.exports = TokenRatesController