diff options
Diffstat (limited to 'app/scripts')
-rw-r--r-- | app/scripts/lib/config-manager.js | 52 | ||||
-rw-r--r-- | app/scripts/lib/idStore.js | 3 | ||||
-rw-r--r-- | app/scripts/metamask-controller.js | 33 |
3 files changed, 87 insertions, 1 deletions
diff --git a/app/scripts/lib/config-manager.js b/app/scripts/lib/config-manager.js index 727cd46fc..9c37a2c6f 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 @@ -274,6 +275,57 @@ 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) + console.log('=================') + console.log('Updated currency!') + console.log('=================') + console.log(this.getConversionRate()) + console.log(this.getCurrentFiat()) + console.log(parsedResponse) + }).catch((err) => { + console.error('Error in conversion.', err) + }) +} + +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 +} + +ConfigManager.prototype.getConversionDate = function () { + var data = this.getData() + return ('conversionDate' in data) && data.conversionDate +} + ConfigManager.prototype.setShouldntShowWarning = function (confirmed) { var data = this.getData() data.isEthConfirmed = confirmed diff --git a/app/scripts/lib/idStore.js b/app/scripts/lib/idStore.js index c6ac55a03..9d8012d54 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 dd43ac2fc..45305ee8f 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -20,7 +20,10 @@ module.exports = class MetamaskController { this.ethStore = new EthStore(this.provider) this.idStore.setStore(this.ethStore) this.messageManager = messageManager - this.publicConfigStore = this.initPublicConfigStore() + 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), @@ -241,6 +246,32 @@ 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(() => { + console.log('started update conversion rate.') + this.configManager.updateConversionRate() + }, 300000) + } + agreeToEthWarning (cb) { try { this.configManager.setShouldntShowWarning(true) |