Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GABA: Integrate CurrencyRateController #5844

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a temporary patch to ComposableObservableStore to provide GABA API compatibility. Once we're fully on GABA, this entire module goes away and we'll be using gaba/ComposableController instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a comment here outlining the two cases we're handling and noting the conditions under which we'd remove this logic?

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's worth noting that #5843 is concurrently introducing migration 30

cc @danjm


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