diff options
Merge branch 'master' into PopupNotifications
Diffstat (limited to 'app/scripts')
-rw-r--r-- | app/scripts/lib/config-manager.js | 48 | ||||
-rw-r--r-- | app/scripts/lib/idStore.js | 3 | ||||
-rw-r--r-- | app/scripts/metamask-controller.js | 30 |
3 files changed, 81 insertions, 0 deletions
diff --git a/app/scripts/lib/config-manager.js b/app/scripts/lib/config-manager.js index d14d3afc3..c56f52e48 100644 --- a/app/scripts/lib/config-manager.js +++ b/app/scripts/lib/config-manager.js @@ -1,6 +1,7 @@ const Migrator = require('pojo-migrator') const MetamaskConfig = require('../config.js') const migrations = require('./migrations') +const rp = require('request-promise') const TESTNET_RPC = MetamaskConfig.network.testnet const MAINNET_RPC = MetamaskConfig.network.mainnet @@ -270,6 +271,53 @@ ConfigManager.prototype.getConfirmed = function () { return ('isConfirmed' in data) && data.isConfirmed } +ConfigManager.prototype.setCurrentFiat = function (currency) { + var data = this.getData() + data.fiatCurrency = currency + this.setData(data) +} + +ConfigManager.prototype.getCurrentFiat = function () { + var data = this.getData() + return ('fiatCurrency' in data) && data.fiatCurrency +} + +ConfigManager.prototype.updateConversionRate = function () { + var data = this.getData() + return rp(`https://www.cryptonator.com/api/ticker/eth-${data.fiatCurrency}`) + .then((response) => { + const parsedResponse = JSON.parse(response) + this.setConversionPrice(parsedResponse.ticker.price) + this.setConversionDate(parsedResponse.timestamp) + }).catch((err) => { + console.error('Error in conversion.', err) + this.setConversionPrice(0) + this.setConversionDate('N/A') + }) +} + +ConfigManager.prototype.setConversionPrice = function(price) { + var data = this.getData() + data.conversionRate = Number(price) + this.setData(data) +} + +ConfigManager.prototype.setConversionDate = function (datestring) { + var data = this.getData() + data.conversionDate = datestring + this.setData(data) +} + +ConfigManager.prototype.getConversionRate = function () { + var data = this.getData() + return (('conversionRate' in data) && data.conversionRate) || 0 +} + +ConfigManager.prototype.getConversionDate = function () { + var data = this.getData() + return (('conversionDate' in data) && data.conversionDate) || 'N/A' +} + ConfigManager.prototype.setShouldntShowWarning = function () { var data = this.getData() if (data.isEthConfirmed) { diff --git a/app/scripts/lib/idStore.js b/app/scripts/lib/idStore.js index f63e03ec6..3272f2d1a 100644 --- a/app/scripts/lib/idStore.js +++ b/app/scripts/lib/idStore.js @@ -100,6 +100,9 @@ IdentityStore.prototype.getState = function () { unconfMsgs: messageManager.unconfirmedMsgs(), messages: messageManager.getMsgList(), selectedAddress: configManager.getSelectedAccount(), + currentFiat: configManager.getCurrentFiat(), + conversionRate: configManager.getConversionRate(), + conversionDate: configManager.getConversionDate(), })) } diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 2c141a402..d21eb5fc1 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -21,6 +21,9 @@ module.exports = class MetamaskController { this.idStore.setStore(this.ethStore) this.messageManager = messageManager this.publicConfigStore = this.initPublicConfigStore() + this.configManager.setCurrentFiat('USD') + this.configManager.updateConversionRate() + this.scheduleConversionInterval() } getState () { @@ -40,7 +43,9 @@ module.exports = class MetamaskController { setProviderType: this.setProviderType.bind(this), useEtherscanProvider: this.useEtherscanProvider.bind(this), agreeToDisclaimer: this.agreeToDisclaimer.bind(this), + setCurrentFiat: this.setCurrentFiat.bind(this), agreeToEthWarning: this.agreeToEthWarning.bind(this), + // forward directly to idStore createNewVault: idStore.createNewVault.bind(idStore), recoverFromSeed: idStore.recoverFromSeed.bind(idStore), @@ -245,6 +250,31 @@ module.exports = class MetamaskController { } } + setCurrentFiat (fiat, cb) { + try { + this.configManager.setCurrentFiat(fiat) + this.configManager.updateConversionRate() + this.scheduleConversionInterval() + const data = { + conversionRate: this.configManager.getConversionRate(), + currentFiat: this.configManager.getCurrentFiat(), + conversionDate: this.configManager.getConversionDate(), + } + cb(data) + } catch (e) { + cb(null, e) + } + } + + scheduleConversionInterval () { + if (this.conversionInterval) { + clearInterval(this.conversionInterval) + } + this.conversionInterval = setInterval(() => { + this.configManager.updateConversionRate() + }, 300000) + } + agreeToEthWarning (cb) { try { this.configManager.setShouldntShowWarning() |