From a8222df1eb957acd96eaf9f8ea15a9af8b15ea09 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 7 Feb 2018 19:41:11 -0500 Subject: [PATCH] support ws providers --- lib/contracts/code_templates/web3-connector.js.ejs | 7 +++++-- test/code_generator.js | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/contracts/code_templates/web3-connector.js.ejs b/lib/contracts/code_templates/web3-connector.js.ejs index 6d7c4d54..4e61fb0f 100644 --- a/lib/contracts/code_templates/web3-connector.js.ejs +++ b/lib/contracts/code_templates/web3-connector.js.ejs @@ -7,8 +7,11 @@ __reduce(<%- connectionList %>,function(prev, value, next) { if (value === '$WEB3' && (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined')) { web3.setProvider(web3.givenProvider); } else if (value !== '$WEB3' && (typeof Web3 !== 'undefined' && ((typeof web3 === 'undefined') || (typeof web3 !== 'undefined' && (!web3.isConnected || (web3.isConnected && !web3.isConnected())))))) { - - web3.setProvider(new Web3.providers.HttpProvider(value)); + if (value.indexOf('ws://') >= 0) { + web3.setProvider(new Web3.providers.WebsocketProvider(value)); + } else { + web3.setProvider(new Web3.providers.HttpProvider(value)); + } } else if (value === '$WEB3') { return next(null, ''); } diff --git a/test/code_generator.js b/test/code_generator.js index e7dbc9c1..c293e3c4 100644 --- a/test/code_generator.js +++ b/test/code_generator.js @@ -10,7 +10,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) {\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\n web3.setProvider(new Web3.providers.HttpProvider(value));\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};\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) {\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'; assert.equal(generator.generateProvider(), providerCode); });