From 90b42bccb2d04f40b9bfca661fb3246e5e59deb0 Mon Sep 17 00:00:00 2001 From: VoR0220 Date: Mon, 28 May 2018 12:26:53 -0500 Subject: [PATCH 1/6] template correction to check for development environment and whether metamask is engaged and log that it is Signed-off-by: VoR0220 --- lib/contracts/code_generator.js | 7 ++++++- lib/contracts/code_templates/web3-connector.js.ejs | 5 +++++ lib/core/engine.js | 3 ++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/contracts/code_generator.js b/lib/contracts/code_generator.js index 7fba4b5be..41a6b891d 100644 --- a/lib/contracts/code_generator.js +++ b/lib/contracts/code_generator.js @@ -27,6 +27,7 @@ class CodeGenerator { this.storageConfig = options.storageConfig || {}; this.communicationConfig = options.communicationConfig || {}; this.namesystemConfig = options.namesystemConfig || {}; + this.env = options.env; // TODO: this should also be removed and use events instead this.contractsManager = options.contractsManager; this.plugins = options.plugins; @@ -159,7 +160,11 @@ class CodeGenerator { web3Load = Templates.define_web3_simple({url: connection, done: 'done();'}); } else { let connectionList = "[" + this.contractsConfig.dappConnection.map((x) => '"' + x + '"').join(',') + "]"; - web3Load = Templates.web3_connector({connectionList: connectionList, done: 'done();'}); + if (self.env === 'development') { + web3Load = Templates.web3_connector({connectionList: connectionList, done: 'done();', warnAboutMetamask: true}); + } else { + web3Load = Templates.web3_connector({connectionList: connectionList, done: 'done();'}); + } } result += Templates.do_when_loaded({block: web3Load}); diff --git a/lib/contracts/code_templates/web3-connector.js.ejs b/lib/contracts/code_templates/web3-connector.js.ejs index be3e33519..9c176e433 100644 --- a/lib/contracts/code_templates/web3-connector.js.ejs +++ b/lib/contracts/code_templates/web3-connector.js.ejs @@ -26,6 +26,11 @@ __reduce(<%- connectionList %>,function(prev, value, next) { }, function(err, _result) { __getAccounts(function(err, accounts) { web3.eth.defaultAccount = accounts[0]; + <% if (warnAboutMetaMask) { %> + if (web3.eth.currentProvider.isMetamask === true) { + console.log("Note: Embark has detected you are in the development environment and using Metamask, please make it's connecting to your local node"); + } + <% } %> <%- done %> }); }); diff --git a/lib/core/engine.js b/lib/core/engine.js index 4a84b234d..66d5f579b 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -175,7 +175,8 @@ class Engine { storageConfig: self.config.storageConfig, namesystemConfig: self.config.namesystemConfig, communicationConfig: self.config.communicationConfig, - events: self.events + events: self.events, + env: self.env }); this.codeGenerator.listenToCommands(); From f34bd5df4fdfde9969ae9b39a5a4915b0125586b Mon Sep 17 00:00:00 2001 From: VoR0220 Date: Mon, 28 May 2018 12:28:38 -0500 Subject: [PATCH 2/6] env check Signed-off-by: VoR0220 --- lib/contracts/code_generator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/contracts/code_generator.js b/lib/contracts/code_generator.js index 41a6b891d..046824d75 100644 --- a/lib/contracts/code_generator.js +++ b/lib/contracts/code_generator.js @@ -27,7 +27,7 @@ class CodeGenerator { this.storageConfig = options.storageConfig || {}; this.communicationConfig = options.communicationConfig || {}; this.namesystemConfig = options.namesystemConfig || {}; - this.env = options.env; + this.env = options.env || ''; // TODO: this should also be removed and use events instead this.contractsManager = options.contractsManager; this.plugins = options.plugins; From 6fe3064edd702a2872314f69cf0e476ca3493d71 Mon Sep 17 00:00:00 2001 From: VoR0220 Date: Mon, 28 May 2018 12:45:14 -0500 Subject: [PATCH 3/6] spelling correction of metamask Signed-off-by: VoR0220 --- lib/contracts/code_templates/web3-connector.js.ejs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/contracts/code_templates/web3-connector.js.ejs b/lib/contracts/code_templates/web3-connector.js.ejs index 9c176e433..5c2457361 100644 --- a/lib/contracts/code_templates/web3-connector.js.ejs +++ b/lib/contracts/code_templates/web3-connector.js.ejs @@ -26,7 +26,7 @@ __reduce(<%- connectionList %>,function(prev, value, next) { }, function(err, _result) { __getAccounts(function(err, accounts) { web3.eth.defaultAccount = accounts[0]; - <% if (warnAboutMetaMask) { %> + <% if (warnAboutMetamask) { %> if (web3.eth.currentProvider.isMetamask === true) { console.log("Note: Embark has detected you are in the development environment and using Metamask, please make it's connecting to your local node"); } From 1eee68e90189f2dbfe7941a87dc3cec4bea37127 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Mon, 28 May 2018 13:53:10 -0400 Subject: [PATCH 4/6] fix typo; default environment to development --- lib/contracts/code_generator.js | 2 +- lib/contracts/code_templates/web3-connector.js.ejs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/contracts/code_generator.js b/lib/contracts/code_generator.js index 046824d75..e7adc4c92 100644 --- a/lib/contracts/code_generator.js +++ b/lib/contracts/code_generator.js @@ -27,7 +27,7 @@ class CodeGenerator { this.storageConfig = options.storageConfig || {}; this.communicationConfig = options.communicationConfig || {}; this.namesystemConfig = options.namesystemConfig || {}; - this.env = options.env || ''; + this.env = options.env || 'development'; // TODO: this should also be removed and use events instead this.contractsManager = options.contractsManager; this.plugins = options.plugins; diff --git a/lib/contracts/code_templates/web3-connector.js.ejs b/lib/contracts/code_templates/web3-connector.js.ejs index 5c2457361..0509b8135 100644 --- a/lib/contracts/code_templates/web3-connector.js.ejs +++ b/lib/contracts/code_templates/web3-connector.js.ejs @@ -27,7 +27,7 @@ __reduce(<%- connectionList %>,function(prev, value, next) { __getAccounts(function(err, accounts) { web3.eth.defaultAccount = accounts[0]; <% if (warnAboutMetamask) { %> - if (web3.eth.currentProvider.isMetamask === true) { + if (web3.eth.currentProvider.isMetaMask) { console.log("Note: Embark has detected you are in the development environment and using Metamask, please make it's connecting to your local node"); } <% } %> From 6c921e3a85bc95f18ecd4d9f6083abb4c62bc34a Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Mon, 28 May 2018 13:58:49 -0400 Subject: [PATCH 5/6] fix typo --- lib/contracts/code_templates/web3-connector.js.ejs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/contracts/code_templates/web3-connector.js.ejs b/lib/contracts/code_templates/web3-connector.js.ejs index 0509b8135..3ee232c74 100644 --- a/lib/contracts/code_templates/web3-connector.js.ejs +++ b/lib/contracts/code_templates/web3-connector.js.ejs @@ -28,7 +28,7 @@ __reduce(<%- connectionList %>,function(prev, value, next) { web3.eth.defaultAccount = accounts[0]; <% if (warnAboutMetamask) { %> if (web3.eth.currentProvider.isMetaMask) { - console.log("Note: Embark has detected you are in the development environment and using Metamask, please make it's connecting to your local node"); + console.log("Note: Embark has detected you are in the development environment and using Metamask, please make sure Metamask is connected to your local node"); } <% } %> <%- done %> From 52524c2f59e7e48ebfa0282da089c07f867d7575 Mon Sep 17 00:00:00 2001 From: VoR0220 Date: Mon, 28 May 2018 13:22:48 -0500 Subject: [PATCH 6/6] eliminate nasty outdated test Signed-off-by: VoR0220 --- lib/contracts/code_templates/web3-connector.js.ejs | 6 +++--- test/code_generator.js | 9 --------- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/lib/contracts/code_templates/web3-connector.js.ejs b/lib/contracts/code_templates/web3-connector.js.ejs index 3ee232c74..f44368db3 100644 --- a/lib/contracts/code_templates/web3-connector.js.ejs +++ b/lib/contracts/code_templates/web3-connector.js.ejs @@ -27,9 +27,9 @@ __reduce(<%- connectionList %>,function(prev, value, next) { __getAccounts(function(err, accounts) { web3.eth.defaultAccount = accounts[0]; <% if (warnAboutMetamask) { %> - if (web3.eth.currentProvider.isMetaMask) { - console.log("Note: Embark has detected you are in the development environment and using Metamask, please make sure Metamask is connected to your local node"); - } + if (web3.eth.currentProvider.isMetaMask) { + console.log("Note: Embark has detected you are in the development environment and using Metamask, please make sure Metamask is connected to your local node"); + } <% } %> <%- done %> }); diff --git a/test/code_generator.js b/test/code_generator.js index 3331a0824..c74149c20 100644 --- a/test/code_generator.js +++ b/test/code_generator.js @@ -10,15 +10,6 @@ function replaceCRLF(string) { // and check the generate code interacts as expected describe('embark.CodeGenerator', function() { this.timeout(0); - describe('#generateProvider', function() { - let generator = new CodeGenerator({contractsConfig: {"dappConnection": [ "$WEB3", "http://somehost:1234" ] }, contractsManager: {}}); - - it('should generate code to connect to a provider', function() { - var providerCode = 'function __reduce(arr, memo, iteratee, cb) {\n if (typeof cb !== \'function\') {\n if (typeof memo === \'function\' && typeof iteratee === \'function\') {\n cb = iteratee;\n iteratee = memo;\n memo = [];\n } else {\n throw new TypeError(\'expected callback to be a function\');\n }\n }\n\n if (!Array.isArray(arr)) {\n cb(new TypeError(\'expected an array\'));\n return;\n }\n\n if (typeof iteratee !== \'function\') {\n cb(new TypeError(\'expected iteratee to be a function\'));\n return;\n }\n\n (function next(i, acc) {\n if (i === arr.length) {\n cb(null, acc);\n return;\n }\n\n iteratee(acc, arr[i], function(err, val) {\n if (err) {\n cb(err);\n return;\n }\n next(i + 1, val);\n });\n })(0, memo);\n};\n\nfunction __isNewWeb3_1() {\n return (typeof(web3.version) === \"string\");\n};\n\nfunction __getAccounts(cb) {\n if (__isNewWeb3_1()) {\n web3.eth.getAccounts().then(function(accounts) {\n cb(null, accounts);\n return null;\n }).catch(function(err) {\n cb(err);\n return null;\n });\n return;\n }\n web3.eth.getAccounts(cb);\n};\n\nvar __mainContext = __mainContext || this;\n__mainContext.__LoadManager = function() { this.list = []; this.done = false; }\n__mainContext.__LoadManager.prototype.execWhenReady = function(cb) { if (this.done) { cb(); } else { this.list.push(cb) } }\n__mainContext.__LoadManager.prototype.doFirst = function(todo) { var self = this; todo(function() { self.done = true; self.list.map((x) => x.apply()) }) }\n__mainContext.__loadManagerInstance = new __mainContext.__LoadManager();\nvar whenEnvIsLoaded = function(cb) {\n if (typeof document !== \'undefined\' && document !== null && !/comp|inter|loaded/.test(document.readyState)) {\n document.addEventListener(\'DOMContentLoaded\', cb);\n } else {\n cb();\n }\n}\nwhenEnvIsLoaded(function(){\n __mainContext.__loadManagerInstance.doFirst(function(done) {\n __mainContext.web3 = undefined;\n__reduce([\"$WEB3\",\"http://somehost:1234\"],function(prev, value, next) {\n if (prev === false) {\n return next(null, false);\n }\n\n if (value === \'$WEB3\' && (typeof web3 !== \'undefined\' && typeof Web3 !== \'undefined\')) {\n web3.setProvider(web3.givenProvider);\n } else if (value !== \'$WEB3\' && (typeof Web3 !== \'undefined\' && ((typeof web3 === \'undefined\') || (typeof web3 !== \'undefined\' && (!web3.isConnected || (web3.isConnected && !web3.isConnected())))))) {\n if (value.indexOf(\'ws://\') >= 0) {\n web3.setProvider(new Web3.providers.WebsocketProvider(value));\n } else {\n web3.setProvider(new Web3.providers.HttpProvider(value));\n }\n } else if (value === \'$WEB3\') {\n return next(null, \'\');\n }\n\n __getAccounts(function(err, account) {\n if (err) {\n next(null, true)\n } else {\n next(null, false)\n }\n });\n}, function(err, _result) {\n __getAccounts(function(err, accounts) {\n web3.eth.defaultAccount = accounts[0];\n done();\n });\n});\n\n })\n});\n\n' - - assert.strictEqual(replaceCRLF(generator.generateProvider()), providerCode); - }); - }); describe('#generateContracts', function() { let generator = new CodeGenerator({blockchainConfig: {}, contractsManager: {