From 7686ad561f6f29afe8c72478c090670f0d67c844 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 11 May 2018 08:20:03 -0400 Subject: [PATCH 1/3] detect web3 version and get accounts correctly --- lib/contracts/code_templates/utils.js.ejs | 17 +++++++++++++++++ .../code_templates/web3-connector.js.ejs | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/contracts/code_templates/utils.js.ejs b/lib/contracts/code_templates/utils.js.ejs index b88c7b37..25bb65fd 100644 --- a/lib/contracts/code_templates/utils.js.ejs +++ b/lib/contracts/code_templates/utils.js.ejs @@ -34,3 +34,20 @@ function __reduce(arr, memo, iteratee, cb) { }); })(0, memo); }; + +function __isNewWeb3_1() { + return (typeof(web3.version) === "string"); +}; + +function __getAccounts(cb) { + if (__isNewWeb3_1()) { + web3.eth.getAccounts().then(function(accounts) { + cb(null, accounts); + }).catch(function(err) { + cb(err); + }); + return; + } + web3.eth.getAccounts(cb); +}; + diff --git a/lib/contracts/code_templates/web3-connector.js.ejs b/lib/contracts/code_templates/web3-connector.js.ejs index 4e61fb0f..be3e3351 100644 --- a/lib/contracts/code_templates/web3-connector.js.ejs +++ b/lib/contracts/code_templates/web3-connector.js.ejs @@ -16,7 +16,7 @@ __reduce(<%- connectionList %>,function(prev, value, next) { return next(null, ''); } - web3.eth.getAccounts(function(err, account) { + __getAccounts(function(err, account) { if (err) { next(null, true) } else { @@ -24,7 +24,7 @@ __reduce(<%- connectionList %>,function(prev, value, next) { } }); }, function(err, _result) { - web3.eth.getAccounts(function(err, accounts) { + __getAccounts(function(err, accounts) { web3.eth.defaultAccount = accounts[0]; <%- done %> }); From 3a62f6a1213e33bef1b5d3004a9d1f57ce62b967 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 11 May 2018 10:23:52 -0400 Subject: [PATCH 2/3] update default dev networkid to be the same as --dev option chainId --- lib/cmds/blockchain/blockchain.js | 2 +- templates/boilerplate/config/blockchain.json | 3 ++- templates/demo/config/blockchain.json | 3 ++- test_apps/contracts_app/blockchain.json | 3 ++- test_apps/test_app/config/blockchain.json | 3 ++- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/cmds/blockchain/blockchain.js b/lib/cmds/blockchain/blockchain.js index 1e01893e..1b7e16d9 100644 --- a/lib/cmds/blockchain/blockchain.js +++ b/lib/cmds/blockchain/blockchain.js @@ -23,7 +23,7 @@ var Blockchain = function(options) { rpcHost: this.blockchainConfig.rpcHost || 'localhost', rpcPort: this.blockchainConfig.rpcPort || 8545, rpcCorsDomain: this.blockchainConfig.rpcCorsDomain || false, - networkId: this.blockchainConfig.networkId || 12301, + networkId: this.blockchainConfig.networkId || 1337, port: this.blockchainConfig.port || 30303, nodiscover: this.blockchainConfig.nodiscover || false, mine: this.blockchainConfig.mine || false, diff --git a/templates/boilerplate/config/blockchain.json b/templates/boilerplate/config/blockchain.json index a7f31878..b72f107a 100644 --- a/templates/boilerplate/config/blockchain.json +++ b/templates/boilerplate/config/blockchain.json @@ -2,9 +2,10 @@ "development": { "enabled": true, "networkType": "custom", + "networkId": "1337", + "isDev": true, "genesisBlock": "config/development/genesis.json", "datadir": ".embark/development/datadir", - "isDev": true, "mineWhenNeeded": true, "nodiscover": true, "maxpeers": 0, diff --git a/templates/demo/config/blockchain.json b/templates/demo/config/blockchain.json index 5743c38d..8e299188 100644 --- a/templates/demo/config/blockchain.json +++ b/templates/demo/config/blockchain.json @@ -2,9 +2,10 @@ "development": { "enabled": true, "networkType": "custom", + "networkId": "1337", + "isDev": true, "genesisBlock": "config/development/genesis.json", "datadir": ".embark/development/datadir", - "isDev": true, "mineWhenNeeded": true, "nodiscover": true, "maxpeers": 0, diff --git a/test_apps/contracts_app/blockchain.json b/test_apps/contracts_app/blockchain.json index a574d400..ee5b1b96 100644 --- a/test_apps/contracts_app/blockchain.json +++ b/test_apps/contracts_app/blockchain.json @@ -2,9 +2,10 @@ "development": { "enabled": true, "networkType": "custom", + "networkId": "1337", + "isDev": true, "genesisBlock": "development/genesis.json", "datadir": ".embark/development/datadir", - "isDev": true, "mineWhenNeeded": true, "nodiscover": true, "maxpeers": 0, diff --git a/test_apps/test_app/config/blockchain.json b/test_apps/test_app/config/blockchain.json index 2b9baa50..d86d2c76 100644 --- a/test_apps/test_app/config/blockchain.json +++ b/test_apps/test_app/config/blockchain.json @@ -2,9 +2,10 @@ "development": { "enabled": true, "networkType": "custom", + "networkId": "1337", + "isDev": true, "genesisBlock": "config/development/genesis.json", "datadir": ".embark/development/datadir", - "isDev": true, "mineWhenNeeded": true, "nodiscover": true, "maxpeers": 0, From 9214eab7ad8bbe1be0fbd99aa6f30fb247c28eee Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 11 May 2018 12:13:49 -0400 Subject: [PATCH 3/3] update tests --- test/code_generator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/code_generator.js b/test/code_generator.js index fa2181b0..d5f5cdcf 100644 --- a/test/code_generator.js +++ b/test/code_generator.js @@ -14,7 +14,7 @@ describe('embark.CodeGenerator', 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};\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 web3.eth.getAccounts(function(err, account) {\n if (err) {\n next(null, true)\n } else {\n next(null, false)\n }\n });\n}, function(err, _result) {\n web3.eth.getAccounts(function(err, accounts) {\n web3.eth.defaultAccount = accounts[0];\n done();\n });\n});\n\n })\n});\n\n'; + 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 }).catch(function(err) {\n cb(err);\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); });