From 44390f5429ad2fdd112fe454297e6058fa729d75 Mon Sep 17 00:00:00 2001 From: holgerd77 Date: Fri, 29 Sep 2017 10:29:53 +0200 Subject: [PATCH 1/6] Simplify confusing and error-provoking context conditionals in executionContextChange --- src/execution-context.js | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/execution-context.js b/src/execution-context.js index 29a8a9c810..0307b66738 100644 --- a/src/execution-context.js +++ b/src/execution-context.js @@ -128,25 +128,29 @@ function ExecutionContext () { }) } - if (context === 'web3') { - modalDialogCustom.confirm(null, 'Are you sure you want to connect to an ethereum node?', - () => { runPrompt(endPointUrl) }, () => { cb() } - ) - } else if (context === 'injected' && injectedProvider === undefined) { - cb() - } else { - if (context === 'injected') { + if (context === 'vm') { + executionContext = context + vm.stateManager.revert(function () { + vm.stateManager.checkpoint() + }) + self.event.trigger('contextChanged', ['vm']) + } + + if (context === 'injected') { + if (injectedProvider === undefined) { + cb() + } else { executionContext = context web3.setProvider(injectedProvider) self.event.trigger('contextChanged', ['injected']) - } else if (context === 'vm') { - executionContext = context - vm.stateManager.revert(function () { - vm.stateManager.checkpoint() - }) - self.event.trigger('contextChanged', ['vm']) } } + + if (context === 'web3') { + modalDialogCustom.confirm(null, 'Are you sure you want to connect to an ethereum node?', + () => { runPrompt(endPointUrl) }, () => { cb() } + ) + } } this.currentblockGasLimit = function () { From 77a74b3a94198435736ff3382ef92d0f78dcbba9 Mon Sep 17 00:00:00 2001 From: holgerd77 Date: Fri, 29 Sep 2017 10:31:20 +0200 Subject: [PATCH 2/6] Add alert/help msg for injected provider not found when wanting to switch context --- src/execution-context.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/execution-context.js b/src/execution-context.js index 0307b66738..abb215b220 100644 --- a/src/execution-context.js +++ b/src/execution-context.js @@ -138,6 +138,9 @@ function ExecutionContext () { if (context === 'injected') { if (injectedProvider === undefined) { + var alertMsg = 'No injected Web3 provider found. ' + alertMsg += 'Make sure your provider (e.g. MetaMask) is active and running.' + modalDialogCustom.alert(alertMsg) cb() } else { executionContext = context From cdd86a79399606ed014e5ffd6b4f8f6bd33d7638 Mon Sep 17 00:00:00 2001 From: holgerd77 Date: Fri, 29 Sep 2017 10:42:17 +0200 Subject: [PATCH 3/6] Added some missing callback cases --- src/execution-context.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/execution-context.js b/src/execution-context.js index abb215b220..966f0e157b 100644 --- a/src/execution-context.js +++ b/src/execution-context.js @@ -134,6 +134,7 @@ function ExecutionContext () { vm.stateManager.checkpoint() }) self.event.trigger('contextChanged', ['vm']) + cb() } if (context === 'injected') { @@ -146,6 +147,7 @@ function ExecutionContext () { executionContext = context web3.setProvider(injectedProvider) self.event.trigger('contextChanged', ['injected']) + cb() } } From 08fb22eeda8455cdf771698311e8ad3874df03ba Mon Sep 17 00:00:00 2001 From: holgerd77 Date: Fri, 29 Sep 2017 10:44:13 +0200 Subject: [PATCH 4/6] Don't trigger Web3 Provider change when second Provider Endpoint choice dialog was canceled --- src/execution-context.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/execution-context.js b/src/execution-context.js index 966f0e157b..13320e21ec 100644 --- a/src/execution-context.js +++ b/src/execution-context.js @@ -114,16 +114,15 @@ function ExecutionContext () { this.executionContextChange = function (context, endPointUrl, cb) { if (!cb) cb = () => {} function runPrompt () { - executionContext = context if (!endPointUrl) { endPointUrl = 'http://localhost:8545' } modalDialogCustom.prompt(null, 'Web3 Provider Endpoint', endPointUrl, (target) => { + executionContext = context setProviderFromEndpoint(target) self.event.trigger('contextChanged', ['web3']) cb() }, () => { - self.event.trigger('contextChanged', ['web3']) cb() }) } From 098351b64883ba7b7390379435c0efbd71f670b0 Mon Sep 17 00:00:00 2001 From: holgerd77 Date: Fri, 29 Sep 2017 11:53:11 +0200 Subject: [PATCH 5/6] Only connect to Web3 provider if there is a connection, otherwise keep provider setting and show an error message --- src/execution-context.js | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/execution-context.js b/src/execution-context.js index 13320e21ec..520b4c339d 100644 --- a/src/execution-context.js +++ b/src/execution-context.js @@ -118,10 +118,7 @@ function ExecutionContext () { endPointUrl = 'http://localhost:8545' } modalDialogCustom.prompt(null, 'Web3 Provider Endpoint', endPointUrl, (target) => { - executionContext = context - setProviderFromEndpoint(target) - self.event.trigger('contextChanged', ['web3']) - cb() + setProviderFromEndpoint(target, context, cb) }, () => { cb() }) @@ -176,13 +173,26 @@ function ExecutionContext () { } }, 15000) - function setProviderFromEndpoint (endpoint) { + function setProviderFromEndpoint (endpoint, context, cb) { + var oldProvider = web3.currentProvider + if (endpoint === 'ipc') { web3.setProvider(new web3.providers.IpcProvider()) } else { web3.setProvider(new web3.providers.HttpProvider(endpoint)) } - self.event.trigger('web3EndpointChanged') + if (web3.isConnected()) { + executionContext = context + self.event.trigger('contextChanged', ['web3']) + self.event.trigger('web3EndpointChanged') + cb() + } else { + 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() + } } } From 8786773d8ddb46ef50f3ae1713e2a9a85d58ea42 Mon Sep 17 00:00:00 2001 From: holgerd77 Date: Sat, 30 Sep 2017 10:12:26 +0200 Subject: [PATCH 6/6] Added a hint to reload page on injected-provider-not-found msg --- src/execution-context.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/execution-context.js b/src/execution-context.js index 520b4c339d..006509daba 100644 --- a/src/execution-context.js +++ b/src/execution-context.js @@ -136,7 +136,8 @@ function ExecutionContext () { if (context === 'injected') { if (injectedProvider === undefined) { var alertMsg = 'No injected Web3 provider found. ' - alertMsg += 'Make sure your provider (e.g. MetaMask) is active and running.' + 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() } else {