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

query network version when metamask is back on line #3004

Closed
wants to merge 7 commits 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
11 changes: 9 additions & 2 deletions app/scripts/controllers/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const createEventEmitterProxy = require('../lib/events-proxy.js')
const RPC_ADDRESS_LIST = require('../config.js').network
const DEFAULT_RPC = RPC_ADDRESS_LIST['rinkeby']
const INFURA_PROVIDER_TYPES = ['ropsten', 'rinkeby', 'kovan', 'mainnet']
const noop = function () {}

module.exports = class NetworkController extends EventEmitter {

Expand Down Expand Up @@ -58,15 +59,21 @@ module.exports = class NetworkController extends EventEmitter {
return this.getNetworkState() === 'loading'
}

lookupNetwork () {
lookupNetwork (cb = noop){
// Prevent firing when provider is not defined.
if (!this.ethQuery || !this.ethQuery.sendAsync) {
return log.warn('NetworkController - lookupNetwork aborted due to missing ethQuery')
}
this.ethQuery.sendAsync({ method: 'net_version' }, (err, network) => {
if (err) return this.setNetworkState('loading')
if (err) {
this.setNetworkState('loading')
console.error(err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be log.error to use our loglevel utility.

cb(err)
return
}
log.info('web3.getNetwork returned ' + network)
this.setNetworkState(network)
cb(null, network)
})
}

Expand Down
16 changes: 16 additions & 0 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ module.exports = class MetamaskController extends EventEmitter {
// network store
this.networkController = new NetworkController(initState.NetworkController)

// setup onLine lister for restarting the provider
if (this.platform.addOnLineListener) {
Copy link
Contributor

Choose a reason for hiding this comment

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

"online" is one word, I'd rather we didn't camel case it into two. Non blocking.

this.platform.addOnLineListener(() => {
// guard against premature firring of the event listener
if (this.provider) {
// start the block tracker up
this.provider.stop()
this.provider.start()
}
// look up the network
this.networkController.lookupNetwork(() => {
this.sendUpdate()
})
})
}
// config manager
this.configManager = new ConfigManager({
store: this.store,
Expand Down Expand Up @@ -91,6 +106,7 @@ module.exports = class MetamaskController extends EventEmitter {

// rpc provider
this.provider = this.initializeProvider()

this.blockTracker = this.provider._blockTracker

this.recentBlocksController = new RecentBlocksController({
Expand Down
8 changes: 8 additions & 0 deletions app/scripts/platforms/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ class ExtensionPlatform {
cb(e)
}
}

isOnline () {
return navigator.onLine
Copy link
Member

@kumavis kumavis Mar 13, 2018

Choose a reason for hiding this comment

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

sadly, this is the web api's camelization

}

addOnLineListener (cb) {
window.addEventListener('online', cb)
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah camel cased correctly here. Incorrect capitalization kinda driving me nuts, going to change it myself.

}
}

module.exports = ExtensionPlatform