diff --git a/src/app/tabs/run-tab.js b/src/app/tabs/run-tab.js index e1639fbfc1..0732aead8f 100644 --- a/src/app/tabs/run-tab.js +++ b/src/app/tabs/run-tab.js @@ -165,6 +165,20 @@ var css = csjs` padding-left: 10px; display: inline; } + .network { + display: flex; + justify-content: flex-end; + align-items: center; + position: absolute; + color: grey; + width: 100%; + height: 100%; + padding-right: 28px; + pointer-events: none; + } + .networkItem { + margin-right: 5px; + } ` module.exports = runTab @@ -378,33 +392,49 @@ function contractDropdown (appAPI, appEvents, instanceContainer) { ------------------------------------------------ */ function settings (appAPI, appEvents) { // SETTINGS HTML + var net = yo`` + const updateNetwork = () => { + executionContext.detectNetwork((err, { id, name } = {}) => { + if (err) { + console.error(err) + net.innerHTML = 'can\'t detect network ' + } else { + net.innerHTML = ` ${name} (${id || '-'})` + } + }) + } + setInterval(updateNetwork, 5000) var el = yo`
Environment
- +
+ ${net} + + +
Account
diff --git a/src/execution-context.js b/src/execution-context.js index c9dcaafa90..c157eea1fc 100644 --- a/src/execution-context.js +++ b/src/execution-context.js @@ -74,6 +74,8 @@ vm.stateManager.checkpoint() var web3VM = new Web3VMProvider() web3VM.setVM(vm) +var mainNetGenesisHash = '0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3' + /* trigger contextChanged, web3EndpointChanged */ @@ -94,6 +96,34 @@ function ExecutionContext () { return this.isVM() ? web3VM : web3 } + this.detectNetwork = function (callback) { + if (this.isVM()) { + callback(null, { id: '-', name: 'VM' }) + } else { + this.web3().version.getNetwork((err, id) => { + var name = null + if (err) name = 'Unknown' + // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md + else if (id === '1') name = 'Main' + else if (id === '2') name = 'Morden (deprecated)' + else if (id === '3') name = 'Ropsten' + else if (id === '4') name = 'Rinkeby' + else if (id === '42') name = 'Kovan' + else name = 'Custom' + + if (id === '1') { + this.web3().eth.getBlock(0, (error, block) => { + if (error) console.log('cant query first block') + if (block && block.hash !== mainNetGenesisHash) name = 'Custom' + callback(err, { id, name }) + }) + } else { + callback(err, { id, name }) + } + }) + } + } + this.internalWeb3 = function () { return web3 }