diff options
author | Kevin Serrano <kevgagser@gmail.com> | 2017-03-02 02:04:25 +0800 |
---|---|---|
committer | Kevin Serrano <kevgagser@gmail.com> | 2017-03-02 02:04:25 +0800 |
commit | e66035254f1778ec8e614cabecaf1a9493509888 (patch) | |
tree | ab0ab43a9792075dfd2e1a23a95ea35a86d6d962 /app/scripts/controllers/currency.js | |
parent | 0a3849ec8477b4a61c50d715bd72b3e8f1ea8b66 (diff) | |
parent | 6fb33853f2ceeb6ef48988a7ee9334dfc87a8223 (diff) | |
download | tangerine-wallet-browser-e66035254f1778ec8e614cabecaf1a9493509888.tar tangerine-wallet-browser-e66035254f1778ec8e614cabecaf1a9493509888.tar.gz tangerine-wallet-browser-e66035254f1778ec8e614cabecaf1a9493509888.tar.bz2 tangerine-wallet-browser-e66035254f1778ec8e614cabecaf1a9493509888.tar.lz tangerine-wallet-browser-e66035254f1778ec8e614cabecaf1a9493509888.tar.xz tangerine-wallet-browser-e66035254f1778ec8e614cabecaf1a9493509888.tar.zst tangerine-wallet-browser-e66035254f1778ec8e614cabecaf1a9493509888.zip |
More conflict resolution.
Diffstat (limited to 'app/scripts/controllers/currency.js')
-rw-r--r-- | app/scripts/controllers/currency.js | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/app/scripts/controllers/currency.js b/app/scripts/controllers/currency.js new file mode 100644 index 000000000..c4904f8ac --- /dev/null +++ b/app/scripts/controllers/currency.js @@ -0,0 +1,70 @@ +const ObservableStore = require('obs-store') +const extend = require('xtend') + +// every ten minutes +const POLLING_INTERVAL = 600000 + +class CurrencyController { + + constructor (opts = {}) { + const initState = extend({ + currentCurrency: 'USD', + conversionRate: 0, + conversionDate: 'N/A', + }, opts.initState) + this.store = new ObservableStore(initState) + } + + // + // PUBLIC METHODS + // + + getCurrentCurrency () { + return this.store.getState().currentCurrency + } + + setCurrentCurrency (currentCurrency) { + this.store.updateState({ currentCurrency }) + } + + getConversionRate () { + return this.store.getState().conversionRate + } + + setConversionRate (conversionRate) { + this.store.updateState({ conversionRate }) + } + + getConversionDate () { + return this.store.getState().conversionDate + } + + setConversionDate (conversionDate) { + this.store.updateState({ conversionDate }) + } + + updateConversionRate () { + const currentCurrency = this.getCurrentCurrency() + return fetch(`https://www.cryptonator.com/api/ticker/eth-${currentCurrency}`) + .then(response => response.json()) + .then((parsedResponse) => { + this.setConversionRate(Number(parsedResponse.ticker.price)) + this.setConversionDate(Number(parsedResponse.timestamp)) + }).catch((err) => { + console.warn('MetaMask - Failed to query currency conversion.') + this.setConversionRate(0) + this.setConversionDate('N/A') + }) + } + + scheduleConversionInterval () { + if (this.conversionInterval) { + clearInterval(this.conversionInterval) + } + this.conversionInterval = setInterval(() => { + this.updateConversionRate() + }, POLLING_INTERVAL) + } +} + +module.exports = CurrencyController |