diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/scripts/lib/config-manager.js | 47 | ||||
-rw-r--r-- | app/scripts/lib/controllers/currency.js | 70 | ||||
-rw-r--r-- | app/scripts/lib/idStore.js | 3 | ||||
-rw-r--r-- | app/scripts/metamask-controller.js | 47 | ||||
-rw-r--r-- | app/scripts/migrations/009.js | 40 | ||||
-rw-r--r-- | app/scripts/migrations/index.js | 1 |
6 files changed, 133 insertions, 75 deletions
diff --git a/app/scripts/lib/config-manager.js b/app/scripts/lib/config-manager.js index 357e081b1..a9b86ca8c 100644 --- a/app/scripts/lib/config-manager.js +++ b/app/scripts/lib/config-manager.js @@ -250,53 +250,6 @@ ConfigManager.prototype.getTOSHash = function () { return data.TOSHash } -ConfigManager.prototype.setCurrentFiat = function (currency) { - var data = this.getData() - data.fiatCurrency = currency - this.setData(data) -} - -ConfigManager.prototype.getCurrentFiat = function () { - var data = this.getData() - return data.fiatCurrency || 'USD' -} - -ConfigManager.prototype.updateConversionRate = function () { - var data = this.getData() - return fetch(`https://www.cryptonator.com/api/ticker/eth-${data.fiatCurrency}`) - .then(response => response.json()) - .then((parsedResponse) => { - this.setConversionPrice(parsedResponse.ticker.price) - this.setConversionDate(parsedResponse.timestamp) - }).catch((err) => { - console.warn('MetaMask - Failed to query currency conversion.') - 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 (data.conversionRate) || 0 -} - -ConfigManager.prototype.getConversionDate = function () { - var data = this.getData() - return (data.conversionDate) || 'N/A' -} - ConfigManager.prototype.getShapeShiftTxList = function () { var data = this.getData() var shapeShiftTxList = data.shapeShiftTxList ? data.shapeShiftTxList : [] diff --git a/app/scripts/lib/controllers/currency.js b/app/scripts/lib/controllers/currency.js new file mode 100644 index 000000000..c4904f8ac --- /dev/null +++ b/app/scripts/lib/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 diff --git a/app/scripts/lib/idStore.js b/app/scripts/lib/idStore.js index e4cbca456..ac395440d 100644 --- a/app/scripts/lib/idStore.js +++ b/app/scripts/lib/idStore.js @@ -97,9 +97,6 @@ IdentityStore.prototype.getState = function () { isDisclaimerConfirmed: configManager.getConfirmedDisclaimer(), selectedAddress: configManager.getSelectedAccount(), shapeShiftTxList: configManager.getShapeShiftTxList(), - currentFiat: configManager.getCurrentFiat(), - conversionRate: configManager.getConversionRate(), - conversionDate: configManager.getConversionDate(), gasMultiplier: configManager.getGasMultiplier(), })) } diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 785bb7b33..2f1623c7b 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -12,6 +12,7 @@ const MetaMaskProvider = require('web3-provider-engine/zero.js') const setupMultiplex = require('./lib/stream-utils.js').setupMultiplex const KeyringController = require('./keyring-controller') const PreferencesController = require('./lib/controllers/preferences') +const CurrencyController = require('./lib/controllers/currency') const NoticeController = require('./notice-controller') const MessageManager = require('./lib/message-manager') const TxManager = require('./transaction-manager') @@ -41,13 +42,19 @@ module.exports = class MetamaskController extends EventEmitter { this.configManager = new ConfigManager({ store: this.store, }) - this.configManager.updateConversionRate() // preferences controller this.preferencesController = new PreferencesController({ initState: initState.PreferencesController, }) + // currency controller + this.currencyController = new CurrencyController({ + initState: initState.CurrencyController, + }) + this.currencyController.updateConversionRate() + this.currencyController.scheduleConversionInterval() + // rpc provider this.provider = this.initializeProvider(opts) this.provider.on('block', this.logBlock.bind(this)) @@ -97,8 +104,6 @@ module.exports = class MetamaskController extends EventEmitter { this.checkTOSChange() - this.scheduleConversionInterval() - // TEMPORARY UNTIL FULL DEPRECATION: this.idStoreMigrator = new IdStoreMigrator({ configManager: this.configManager, @@ -114,11 +119,15 @@ module.exports = class MetamaskController extends EventEmitter { this.txManager.store.subscribe((state) => { this.store.updateState({ TransactionManager: state }) }) + this.currencyController.store.subscribe((state) => { + this.store.updateState({ CurrencyController: state }) + }) // manual mem state subscriptions this.ethStore.subscribe(this.sendUpdate.bind(this)) this.networkStore.subscribe(this.sendUpdate.bind(this)) this.keyringController.memStore.subscribe(this.sendUpdate.bind(this)) + this.currencyController.store.subscribe(this.sendUpdate.bind(this)) this.txManager.memStore.subscribe(this.sendUpdate.bind(this)) this.messageManager.memStore.subscribe(this.sendUpdate.bind(this)) } @@ -189,15 +198,13 @@ module.exports = class MetamaskController extends EventEmitter { this.messageManager.memStore.getState(), this.keyringController.memStore.getState(), this.preferencesController.store.getState(), + this.currencyController.store.getState(), this.noticeController.memStore.getState(), // config manager this.configManager.getConfig(), { shapeShiftTxList: this.configManager.getShapeShiftTxList(), lostAccounts: this.configManager.getLostAccounts(), - currentFiat: this.configManager.getCurrentFiat(), - conversionRate: this.configManager.getConversionRate(), - conversionDate: this.configManager.getConversionDate(), isDisclaimerConfirmed: this.configManager.getConfirmedDisclaimer(), seedWords: this.configManager.getSeedWords(), } @@ -223,7 +230,7 @@ module.exports = class MetamaskController extends EventEmitter { useEtherscanProvider: this.useEtherscanProvider.bind(this), agreeToDisclaimer: this.agreeToDisclaimer.bind(this), resetDisclaimer: this.resetDisclaimer.bind(this), - setCurrentFiat: this.setCurrentFiat.bind(this), + setCurrentCurrency: this.setCurrentCurrency.bind(this), setTOSHash: this.setTOSHash.bind(this), checkTOSChange: this.checkTOSChange.bind(this), setGasMultiplier: this.setGasMultiplier.bind(this), @@ -550,29 +557,19 @@ module.exports = class MetamaskController extends EventEmitter { this.verifyNetwork() } - setCurrentFiat (fiat, cb) { + setCurrentCurrency (currencyCode, cb) { try { - this.configManager.setCurrentFiat(fiat) - this.configManager.updateConversionRate() - this.scheduleConversionInterval() + this.currencyController.setCurrentCurrency(currencyCode) + this.currencyController.updateConversionRate() const data = { - conversionRate: this.configManager.getConversionRate(), - currentFiat: this.configManager.getCurrentFiat(), - conversionDate: this.configManager.getConversionDate(), + conversionRate: this.currencyController.getConversionRate(), + currentFiat: this.currencyController.getCurrentCurrency(), + conversionDate: this.currencyController.getConversionDate(), } - cb(data) + cb(null, data) } catch (err) { - cb(null, err) - } - } - - scheduleConversionInterval () { - if (this.conversionInterval) { - clearInterval(this.conversionInterval) + cb(err) } - this.conversionInterval = setInterval(() => { - this.configManager.updateConversionRate() - }, 300000) } buyEth (address, amount) { diff --git a/app/scripts/migrations/009.js b/app/scripts/migrations/009.js new file mode 100644 index 000000000..61b8b7fa2 --- /dev/null +++ b/app/scripts/migrations/009.js @@ -0,0 +1,40 @@ +const version = 9 + +/* + +This migration breaks out the CurrencyController substate + +*/ + +const merge = require('deep-merge') + +module.exports = { + version, + + migrate: function (versionedData) { + versionedData.meta.version = version + try { + const state = versionedData.data + const newState = transformState(state) + versionedData.data = newState + } catch (err) { + console.warn(`MetaMask Migration #${version}` + err.stack) + } + return Promise.resolve(versionedData) + }, +} + +function transformState (state) { + const newState = merge(state, { + CurrencyController: { + currentCurrency: state.currentFiat || 'USD', + conversionRate: state.conversionRate, + conversionDate: state.conversionDate, + }, + }) + delete newState.currentFiat + delete newState.conversionRate + delete newState.conversionDate + + return newState +} diff --git a/app/scripts/migrations/index.js b/app/scripts/migrations/index.js index a25d3b044..22bf008ba 100644 --- a/app/scripts/migrations/index.js +++ b/app/scripts/migrations/index.js @@ -19,4 +19,5 @@ module.exports = [ require('./006'), require('./007'), require('./008'), + require('./009'), ] |