From 0409fc1afcdd253f1ee66f730b48f2a6cd2635c8 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 2 Feb 2018 09:43:50 -0500 Subject: [PATCH 1/4] move UI part of executionContext change and set to the caller --- src/app.js | 20 +++++++++++++++++++- src/app/tabs/run-tab.js | 29 ++++++++++++++++++++++++----- src/execution-context.js | 31 +++++++++++-------------------- 3 files changed, 54 insertions(+), 26 deletions(-) diff --git a/src/app.js b/src/app.js index bb12028c75..9c22c1bb0c 100644 --- a/src/app.js +++ b/src/app.js @@ -784,7 +784,25 @@ function run () { runCompiler() if (queryParams.get().context) { - executionContext.setContext(queryParams.get().context, queryParams.get().endpointurl) + executionContext.setContext(queryParams.get().context, queryParams.get().endpointurl, + () => { + + modalDialogCustom.confirm(null, 'Are you sure you want to connect to an ethereum node?', () => { + let context = queryParams.get().context; + let endPointUrl = queryParams.get().endPointUrl; + + if (!endPointUrl) { + endPointUrl = 'http://localhost:8545' + } + modalDialogCustom.prompt(null, 'Web3 Provider Endpoint', endPointUrl, (target) => { + executionContext.setProviderFromEndpoint(target, context) + }, () => {}) + }, () => {}) + + }, + (alertMsg) => { + modalDialogCustom.alert(alertMsg) + }) } if (queryParams.get().debugtx) { diff --git a/src/app/tabs/run-tab.js b/src/app/tabs/run-tab.js index 02df26e3d1..789f150c4b 100644 --- a/src/app/tabs/run-tab.js +++ b/src/app/tabs/run-tab.js @@ -238,13 +238,32 @@ function runTab (container, appAPI, appEvents, opts) { // DROPDOWN var selectExEnv = el.querySelector('#selectExEnvOptions') + + function setFinalContext() { + // set the final context. Cause it is possible that this is not the one we've originaly selected + selectExEnv.value = executionContext.getProvider() + fillAccountsList(appAPI, el) + events.trigger('clearInstance', []) + } + selectExEnv.addEventListener('change', function (event) { executionContext.executionContextChange(selectExEnv.options[selectExEnv.selectedIndex].value, null, () => { - // set the final context. Cause it is possible that this is not the one we've originaly selected - selectExEnv.value = executionContext.getProvider() - fillAccountsList(appAPI, el) - events.trigger('clearInstance', []) - }) + + modalDialogCustom.confirm(null, 'Are you sure you want to connect to an ethereum node?', () => { + let endPointUrl = null; + let context = selectExEnv.options[selectExEnv.selectedIndex].value; + + if (!endPointUrl) { + endPointUrl = 'http://localhost:8545' + } + modalDialogCustom.prompt(null, 'Web3 Provider Endpoint', endPointUrl, (target) => { + executionContext.setProviderFromEndpoint(target, context, setFinalContext) + }, setFinalContext) + }, setFinalContext) + + }, (alertMsg) => { + modalDialogCustom.alert(alertMsg) + }, setFinalContext) }) selectExEnv.value = executionContext.getProvider() fillAccountsList(appAPI, el) diff --git a/src/execution-context.js b/src/execution-context.js index 72a822a734..2ef37353ce 100644 --- a/src/execution-context.js +++ b/src/execution-context.js @@ -145,23 +145,13 @@ function ExecutionContext () { return vm } - this.setContext = function (context, endPointUrl) { + this.setContext = function (context, endPointUrl, confirmCb, infoCb) { executionContext = context - this.executionContextChange(context, endPointUrl) + this.executionContextChange(context, endPointUrl, confirmCb, infoCb) } - this.executionContextChange = function (context, endPointUrl, cb) { + this.executionContextChange = function (context, endPointUrl, confirmCb, infoCb, cb) { if (!cb) cb = () => {} - function runPrompt () { - if (!endPointUrl) { - endPointUrl = 'http://localhost:8545' - } - modalDialogCustom.prompt(null, 'Web3 Provider Endpoint', endPointUrl, (target) => { - setProviderFromEndpoint(target, context, cb) - }, () => { - cb() - }) - } if (context === 'vm') { executionContext = context @@ -169,7 +159,7 @@ function ExecutionContext () { vm.stateManager.checkpoint() }) self.event.trigger('contextChanged', ['vm']) - cb() + return cb() } if (context === 'injected') { @@ -177,20 +167,18 @@ function ExecutionContext () { var alertMsg = 'No injected Web3 provider found. ' alertMsg += 'Make sure your provider (e.g. MetaMask) is active and running ' alertMsg += '(when recently activated you may have to reload the page).' - modalDialogCustom.alert(alertMsg) - cb() + infoCb(alertMsg); + return cb() } else { executionContext = context web3.setProvider(injectedProvider) self.event.trigger('contextChanged', ['injected']) - cb() + return cb() } } if (context === 'web3') { - modalDialogCustom.confirm(null, 'Are you sure you want to connect to an ethereum node?', - () => { runPrompt(endPointUrl) }, () => { cb() } - ) + confirmCb(cb); } } @@ -213,7 +201,9 @@ function ExecutionContext () { } }, 15000) + // TODO: not used here anymore and needs to be moved function setProviderFromEndpoint (endpoint, context, cb) { + if (!cb) cb = () => {} var oldProvider = web3.currentProvider if (endpoint === 'ipc') { @@ -234,6 +224,7 @@ function ExecutionContext () { cb() } } + this.setProviderFromEndpoint = setProviderFromEndpoint; } module.exports = new ExecutionContext() From 60c75c53a13e20c65dc3143a18cde03e95e9396a Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 2 Feb 2018 09:50:43 -0500 Subject: [PATCH 2/4] refactor context and endPointUrl vars --- src/app.js | 8 +++----- src/app/tabs/run-tab.js | 11 +++-------- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/src/app.js b/src/app.js index 9c22c1bb0c..86c2cd041c 100644 --- a/src/app.js +++ b/src/app.js @@ -784,13 +784,11 @@ function run () { runCompiler() if (queryParams.get().context) { - executionContext.setContext(queryParams.get().context, queryParams.get().endpointurl, + let context = queryParams.get().context; + let endPointUrl = queryParams.get().endPointUrl; + executionContext.setContext(context, endpointurl, () => { - modalDialogCustom.confirm(null, 'Are you sure you want to connect to an ethereum node?', () => { - let context = queryParams.get().context; - let endPointUrl = queryParams.get().endPointUrl; - if (!endPointUrl) { endPointUrl = 'http://localhost:8545' } diff --git a/src/app/tabs/run-tab.js b/src/app/tabs/run-tab.js index 789f150c4b..f66cbf943d 100644 --- a/src/app/tabs/run-tab.js +++ b/src/app/tabs/run-tab.js @@ -247,16 +247,11 @@ function runTab (container, appAPI, appEvents, opts) { } selectExEnv.addEventListener('change', function (event) { - executionContext.executionContextChange(selectExEnv.options[selectExEnv.selectedIndex].value, null, () => { + let context = selectExEnv.options[selectExEnv.selectedIndex].value; + executionContext.executionContextChange(context, null, () => { modalDialogCustom.confirm(null, 'Are you sure you want to connect to an ethereum node?', () => { - let endPointUrl = null; - let context = selectExEnv.options[selectExEnv.selectedIndex].value; - - if (!endPointUrl) { - endPointUrl = 'http://localhost:8545' - } - modalDialogCustom.prompt(null, 'Web3 Provider Endpoint', endPointUrl, (target) => { + modalDialogCustom.prompt(null, 'Web3 Provider Endpoint', 'http://localhost:8545', (target) => { executionContext.setProviderFromEndpoint(target, context, setFinalContext) }, setFinalContext) }, setFinalContext) From 0fcea104a4320cc889329f649f7aa40c6ebe670b Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 2 Feb 2018 09:54:15 -0500 Subject: [PATCH 3/4] move ui modal dialog from setProviderFromEndPoint to caller; remove ui dependency from execution-context --- src/app/tabs/run-tab.js | 7 ++++++- src/execution-context.js | 5 +---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/app/tabs/run-tab.js b/src/app/tabs/run-tab.js index f66cbf943d..04bef97669 100644 --- a/src/app/tabs/run-tab.js +++ b/src/app/tabs/run-tab.js @@ -252,7 +252,12 @@ function runTab (container, appAPI, appEvents, opts) { modalDialogCustom.confirm(null, 'Are you sure you want to connect to an ethereum node?', () => { modalDialogCustom.prompt(null, 'Web3 Provider Endpoint', 'http://localhost:8545', (target) => { - executionContext.setProviderFromEndpoint(target, context, setFinalContext) + executionContext.setProviderFromEndpoint(target, context, (alertMsg) => { + if (alertMsg) { + modalDialogCustom.alert(alertMsg) + } + setFinalContext() + }) }, setFinalContext) }, setFinalContext) diff --git a/src/execution-context.js b/src/execution-context.js index 2ef37353ce..3b23ce9919 100644 --- a/src/execution-context.js +++ b/src/execution-context.js @@ -8,7 +8,6 @@ var ethUtil = require('ethereumjs-util') var StateManager = require('ethereumjs-vm/lib/stateManager') var Web3VMProvider = remixLib.vm.Web3VMProvider var rlp = ethUtil.rlp -var modalDialogCustom = require('./app/ui/modal-dialog-custom') var injectedProvider @@ -203,7 +202,6 @@ function ExecutionContext () { // TODO: not used here anymore and needs to be moved function setProviderFromEndpoint (endpoint, context, cb) { - if (!cb) cb = () => {} var oldProvider = web3.currentProvider if (endpoint === 'ipc') { @@ -220,8 +218,7 @@ function ExecutionContext () { web3.setProvider(oldProvider) var alertMsg = 'Not possible to connect to the Web3 provider. ' alertMsg += 'Make sure the provider is running and a connection is open (via IPC or RPC).' - modalDialogCustom.alert(alertMsg) - cb() + cb(alertMsg) } } this.setProviderFromEndpoint = setProviderFromEndpoint; From c3e3624a8ce0c733e09b006daf00608e7f196e13 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 2 Feb 2018 10:03:44 -0500 Subject: [PATCH 4/4] lint is king --- src/app.js | 19 +++++++++---------- src/app/tabs/run-tab.js | 22 ++++++++++------------ src/execution-context.js | 6 +++--- 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/app.js b/src/app.js index 86c2cd041c..73f2c21cd8 100644 --- a/src/app.js +++ b/src/app.js @@ -784,19 +784,18 @@ function run () { runCompiler() if (queryParams.get().context) { - let context = queryParams.get().context; - let endPointUrl = queryParams.get().endPointUrl; - executionContext.setContext(context, endpointurl, + let context = queryParams.get().context + let endPointUrl = queryParams.get().endPointUrl + executionContext.setContext(context, endPointUrl, () => { modalDialogCustom.confirm(null, 'Are you sure you want to connect to an ethereum node?', () => { - if (!endPointUrl) { - endPointUrl = 'http://localhost:8545' - } - modalDialogCustom.prompt(null, 'Web3 Provider Endpoint', endPointUrl, (target) => { - executionContext.setProviderFromEndpoint(target, context) - }, () => {}) + if (!endPointUrl) { + endPointUrl = 'http://localhost:8545' + } + modalDialogCustom.prompt(null, 'Web3 Provider Endpoint', endPointUrl, (target) => { + executionContext.setProviderFromEndpoint(target, context) }, () => {}) - + }, () => {}) }, (alertMsg) => { modalDialogCustom.alert(alertMsg) diff --git a/src/app/tabs/run-tab.js b/src/app/tabs/run-tab.js index 04bef97669..ee2837f0e9 100644 --- a/src/app/tabs/run-tab.js +++ b/src/app/tabs/run-tab.js @@ -239,7 +239,7 @@ function runTab (container, appAPI, appEvents, opts) { // DROPDOWN var selectExEnv = el.querySelector('#selectExEnvOptions') - function setFinalContext() { + function setFinalContext () { // set the final context. Cause it is possible that this is not the one we've originaly selected selectExEnv.value = executionContext.getProvider() fillAccountsList(appAPI, el) @@ -247,20 +247,18 @@ function runTab (container, appAPI, appEvents, opts) { } selectExEnv.addEventListener('change', function (event) { - let context = selectExEnv.options[selectExEnv.selectedIndex].value; + let context = selectExEnv.options[selectExEnv.selectedIndex].value executionContext.executionContextChange(context, null, () => { - modalDialogCustom.confirm(null, 'Are you sure you want to connect to an ethereum node?', () => { - modalDialogCustom.prompt(null, 'Web3 Provider Endpoint', 'http://localhost:8545', (target) => { - executionContext.setProviderFromEndpoint(target, context, (alertMsg) => { - if (alertMsg) { - modalDialogCustom.alert(alertMsg) - } - setFinalContext() - }) - }, setFinalContext) + modalDialogCustom.prompt(null, 'Web3 Provider Endpoint', 'http://localhost:8545', (target) => { + executionContext.setProviderFromEndpoint(target, context, (alertMsg) => { + if (alertMsg) { + modalDialogCustom.alert(alertMsg) + } + setFinalContext() + }) }, setFinalContext) - + }, setFinalContext) }, (alertMsg) => { modalDialogCustom.alert(alertMsg) }, setFinalContext) diff --git a/src/execution-context.js b/src/execution-context.js index 3b23ce9919..c4e77bdba6 100644 --- a/src/execution-context.js +++ b/src/execution-context.js @@ -166,7 +166,7 @@ function ExecutionContext () { var alertMsg = 'No injected Web3 provider found. ' alertMsg += 'Make sure your provider (e.g. MetaMask) is active and running ' alertMsg += '(when recently activated you may have to reload the page).' - infoCb(alertMsg); + infoCb(alertMsg) return cb() } else { executionContext = context @@ -177,7 +177,7 @@ function ExecutionContext () { } if (context === 'web3') { - confirmCb(cb); + confirmCb(cb) } } @@ -221,7 +221,7 @@ function ExecutionContext () { cb(alertMsg) } } - this.setProviderFromEndpoint = setProviderFromEndpoint; + this.setProviderFromEndpoint = setProviderFromEndpoint } module.exports = new ExecutionContext()