diff options
-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 | ||||
-rw-r--r-- | test/unit/config-manager-test.js | 82 | ||||
-rw-r--r-- | ui/app/actions.js | 19 | ||||
-rw-r--r-- | ui/app/reducers/metamask.js | 7 |
6 files changed, 194 insertions, 2 deletions
diff --git a/app/scripts/lib/config-manager.js b/app/scripts/lib/config-manager.js index d7df5343b..8d250e555 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,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 4f03ace8f..613484ffb 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), @@ -240,6 +245,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) diff --git a/test/unit/config-manager-test.js b/test/unit/config-manager-test.js index 8974a6bc5..eba663975 100644 --- a/test/unit/config-manager-test.js +++ b/test/unit/config-manager-test.js @@ -3,6 +3,7 @@ const extend = require('xtend') const STORAGE_KEY = 'metamask-persistance-key' var configManagerGen = require('../lib/mock-config-manager') var configManager +const rp = require('request-promise') describe('config-manager', function() { @@ -11,6 +12,86 @@ describe('config-manager', function() { configManager = configManagerGen() }) + describe('currency conversions', function() { + + describe('#getCurrentFiat', function() { + it('should return false if no previous key exists', function() { + var result = configManager.getCurrentFiat() + assert.ok(!result) + }) + }) + + describe('#setCurrentFiat', function() { + it('should make getCurrentFiat return true once set', function() { + assert.equal(configManager.getCurrentFiat(), false) + configManager.setCurrentFiat('usd') + var result = configManager.getCurrentFiat() + assert.equal(result, 'usd') + }) + + it('should work with other currencies as well', function() { + assert.equal(configManager.getCurrentFiat(), false) + configManager.setCurrentFiat('jpy') + var result = configManager.getCurrentFiat() + assert.equal(result, 'jpy') + }) + }) + + describe('#getConversionRate', function() { + it('should return false if non-existent', function() { + var result = configManager.getConversionRate() + assert.ok(!result) + }) + }) + + describe('#updateConversionRate', function() { + it('should retrieve an update for ETH to USD and set it in memory', function(done) { + this.timeout(15000) + assert.equal(configManager.getConversionRate(), false) + var promise = new Promise( + function (resolve, reject) { + configManager.setCurrentFiat('usd') + configManager.updateConversionRate().then(function() { + resolve() + }) + }) + + promise.then(function() { + var result = configManager.getConversionRate() + assert.equal(typeof result, 'number') + done() + }).catch(function(err) { + console.log(err) + }) + + }) + + it('should work for JPY as well.', function() { + this.timeout(15000) + assert.equal(configManager.getConversionRate(), false) + var promise = new Promise( + function (resolve, reject) { + configManager.setCurrentFiat('jpy') + configManager.updateConversionRate().then(function() { + resolve() + }) + }) + + promise.then(function() { + var result = configManager.getConversionRate() + assert.equal(typeof result, 'number') + done() + }).catch(function(err) { + console.log(err) + }) + }) + + xit('should activate every time the currency is changed.', function() { + + }) + }) + }) + describe('confirmation', function() { describe('#getConfirmed', function() { @@ -215,4 +296,3 @@ describe('config-manager', function() { }) }) }) - diff --git a/ui/app/actions.js b/ui/app/actions.js index 82a319907..a5846f0d2 100644 --- a/ui/app/actions.js +++ b/ui/app/actions.js @@ -55,6 +55,8 @@ var actions = { SHOW_CONF_MSG_PAGE: 'SHOW_CONF_MSG_PAGE', REVEAL_ACCOUNT: 'REVEAL_ACCOUNT', revealAccount: revealAccount, + SET_CURRENT_FIAT: 'SET_CURRENT_FIAT', + setCurrentFiat: setCurrentFiat, // account detail screen SHOW_SEND_PAGE: 'SHOW_SEND_PAGE', showSendPage: showSendPage, @@ -214,6 +216,23 @@ function revealAccount () { } } +function setCurrentFiat (fiat) { + return (dispatch) => { + dispatch(this.showLoadingIndication()) + _accountManager.setCurrentFiat(fiat, (data, err) => { + dispatch(this.hideLoadingIndication()) + dispatch({ + type: this.SET_CURRENT_FIAT, + value: { + currentFiat: data.currentFiat, + conversionRate: data.conversionRate, + conversionDate: data.conversionDate, + }, + }) + }) + } +} + function signMsg (msgData) { return (dispatch) => { dispatch(actions.showLoadingIndication()) diff --git a/ui/app/reducers/metamask.js b/ui/app/reducers/metamask.js index 8d4520fb1..eba59ab45 100644 --- a/ui/app/reducers/metamask.js +++ b/ui/app/reducers/metamask.js @@ -114,6 +114,13 @@ function reduceMetamask (state, action) { var identities = extend(metamaskState.identities, id) return extend(metamaskState, { identities }) + case actions.SET_CURRENT_FIAT: + return extend(metamaskState, { + currentFiat: action.value.currentFiat, + conversionRate: action.value.conversionRate, + conversionDate: action.value.conversionDate, + }) + default: return metamaskState |