Skip to content

Commit

Permalink
gaba: integrate CurrencyRateController
Browse files Browse the repository at this point in the history
  • Loading branch information
bitpshr committed Nov 27, 2018
1 parent d59ac28 commit ec8285d
Show file tree
Hide file tree
Showing 11 changed files with 513 additions and 386 deletions.
205 changes: 0 additions & 205 deletions app/scripts/controllers/currency.js

This file was deleted.

2 changes: 1 addition & 1 deletion app/scripts/controllers/token-rates.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TokenRatesController {
async updateExchangeRates () {
if (!this.isActive) { return }
const contractExchangeRates = {}
const nativeCurrency = this.currency ? this.currency.getState().nativeCurrency.toUpperCase() : 'ETH'
const nativeCurrency = this.currency ? this.currency.state.nativeCurrency.toUpperCase() : 'ETH'
const pairs = this._tokens.map(token => `pairs[]=${token.address}/${nativeCurrency}`)
const query = pairs.join('&')
if (this._tokens.length > 0) {
Expand Down
4 changes: 3 additions & 1 deletion app/scripts/lib/ComposableObservableStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ class ComposableObservableStore extends ObservableStore {
getFlatState () {
let flatState = {}
for (const key in this.config) {
flatState = { ...flatState, ...this.config[key].getState() }
const controller = this.config[key]
const state = controller.getState ? controller.getState() : controller.state
flatState = { ...flatState, ...state }
}
return flatState
}
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/lib/getObjStructure.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = getObjStructure

// {
// "data": {
// "CurrencyController": {
// "CurrencyRateController": {
// "conversionDate": "number",
// "conversionRate": "number",
// "currentCurrency": "string"
Expand Down
34 changes: 12 additions & 22 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const setupMultiplex = require('./lib/stream-utils.js').setupMultiplex
const KeyringController = require('eth-keyring-controller')
const NetworkController = require('./controllers/network')
const PreferencesController = require('./controllers/preferences')
const CurrencyController = require('./controllers/currency')
const NoticeController = require('./notice-controller')
const ShapeShiftController = require('./controllers/shapeshift')
const AddressBookController = require('./controllers/address-book')
Expand Down Expand Up @@ -54,6 +53,7 @@ const HW_WALLETS_KEYRINGS = [TrezorKeyring.type, LedgerBridgeKeyring.type]
const EthQuery = require('eth-query')
const ethUtil = require('ethereumjs-util')
const sigUtil = require('eth-sig-util')
const { CurrencyRateController } = require('gaba')

module.exports = class MetamaskController extends EventEmitter {

Expand Down Expand Up @@ -95,12 +95,7 @@ module.exports = class MetamaskController extends EventEmitter {
network: this.networkController,
})

// currency controller
this.currencyController = new CurrencyController({
initState: initState.CurrencyController,
})
this.currencyController.updateConversionRate()
this.currencyController.scheduleConversionInterval()
this.currencyRateController = new CurrencyRateController(initState.CurrencyRateController)

// infura controller
this.infuraController = new InfuraController({
Expand All @@ -118,7 +113,7 @@ module.exports = class MetamaskController extends EventEmitter {

// token exchange rate tracker
this.tokenRatesController = new TokenRatesController({
currency: this.currencyController.store,
currency: this.currencyRateController,
preferences: this.preferencesController.store,
})

Expand Down Expand Up @@ -200,8 +195,7 @@ module.exports = class MetamaskController extends EventEmitter {
})
this.networkController.on('networkDidChange', () => {
this.balancesController.updateAllBalances()
var currentCurrency = this.currencyController.getCurrentCurrency()
this.setCurrentCurrency(currentCurrency, function () {})
this.setCurrentCurrency(this.currencyRateController.state.currentCurrency, function () {})
})
this.balancesController.updateAllBalances()

Expand Down Expand Up @@ -236,7 +230,7 @@ module.exports = class MetamaskController extends EventEmitter {
KeyringController: this.keyringController.store,
PreferencesController: this.preferencesController.store,
AddressBookController: this.addressBookController.store,
CurrencyController: this.currencyController.store,
CurrencyRateController: this.currencyRateController,
NoticeController: this.noticeController.store,
ShapeShiftController: this.shapeshiftController.store,
NetworkController: this.networkController.store,
Expand All @@ -256,7 +250,7 @@ module.exports = class MetamaskController extends EventEmitter {
PreferencesController: this.preferencesController.store,
RecentBlocksController: this.recentBlocksController.store,
AddressBookController: this.addressBookController.store,
CurrencyController: this.currencyController.store,
CurrencyRateController: this.currencyRateController,
NoticeController: this.noticeController.memStore,
ShapeshiftController: this.shapeshiftController.store,
InfuraController: this.infuraController.store,
Expand Down Expand Up @@ -1439,18 +1433,14 @@ module.exports = class MetamaskController extends EventEmitter {
* @param {string} currencyCode - The code of the preferred currency.
* @param {Function} cb - A callback function returning currency info.
*/
setCurrentCurrency (currencyCode, cb) {
async setCurrentCurrency (currencyCode, cb) {
const { ticker } = this.networkController.getNetworkConfig()
try {
this.currencyController.setNativeCurrency(ticker)
this.currencyController.setCurrentCurrency(currencyCode)
this.currencyController.updateConversionRate()
const data = {
nativeCurrency: ticker || 'ETH',
conversionRate: this.currencyController.getConversionRate(),
currentCurrency: this.currencyController.getCurrentCurrency(),
conversionDate: this.currencyController.getConversionDate(),
}
this.currencyRateController.configure({
nativeCurrency: ticker,
currentCurrency: currencyCode,
})
const data = await this.currencyRateController.updateExchangeRate()
cb(null, data)
} catch (err) {
cb(err)
Expand Down
22 changes: 22 additions & 0 deletions app/scripts/migrations/030.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const clone = require('clone')

const version = 30

module.exports = {
version,

migrate: function (originalData) {
const versionedData = clone(originalData)
versionedData.meta.version = version
try {
const state = versionedData.data
if (state.CurrencyController) {
state.CurrencyRateController = { ...state.CurrencyController }
delete state.CurrencyController
}
} catch (err) {
console.warn(`MetaMask Migration #${version}` + err.stack)
}
return Promise.resolve(versionedData)
},
}
1 change: 1 addition & 0 deletions app/scripts/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ module.exports = [
require('./027'),
require('./028'),
require('./029'),
require('./030'),
]
Loading

0 comments on commit ec8285d

Please sign in to comment.