From 634df1c08e09a3375fae8ac9aad639d57fb44c2b Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Mon, 27 Aug 2018 16:46:46 -0400 Subject: [PATCH 1/5] add connect function --- src/index.js | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/src/index.js b/src/index.js index acc07a5..4cdf4c3 100644 --- a/src/index.js +++ b/src/index.js @@ -4,12 +4,107 @@ import Messages from './messages.js'; import Contract from './contracts.js'; import Utils from './utils.js'; +function __reduce(arr, memo, iteratee, cb) { + if (typeof cb !== 'function') { + if (typeof memo === 'function' && typeof iteratee === 'function') { + cb = iteratee; + iteratee = memo; + memo = []; + } else { + throw new TypeError('expected callback to be a function'); + } + } + + if (!Array.isArray(arr)) { + cb(new TypeError('expected an array')); + return; + } + + if (typeof iteratee !== 'function') { + cb(new TypeError('expected iteratee to be a function')); + return; + } + + (function next(i, acc) { + if (i === arr.length) { + cb(null, acc); + return; + } + + iteratee(acc, arr[i], function(err, val) { + if (err) { + cb(err); + return; + } + next(i + 1, val); + }); + })(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); + return null; + }).catch(function(err) { + cb(err); + return null; + }); + return; + } + web3.eth.getAccounts(cb); +}; + var EmbarkJS = { onReady: function (cb) { if (typeof (__embarkContext) === 'undefined') { return cb(); } return __embarkContext.execWhenReady(cb); + }, + + connect: function(connectionList, opts, doneCb) { + __reduce(connectionList, function(prev, value, next) { + if (prev === false) { + return next(null, false); + } + + 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())))))) { + 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, ''); + } + + __getAccounts(function(err, account) { + if (err) { + next(null, true) + } else { + next(null, false) + } + }); + }, function(err, _result) { + __getAccounts(function(err, accounts) { + if (opts.warnAboutMetamask) { + if (web3.eth.currentProvider && web3.eth.currentProvider.isMetaMask) { + console.log("%cNote: Embark has detected you are in the development environment and using Metamask, please make sure Metamask is connected to your local node", "font-size: 2em"); + } + } + if (accounts) { + web3.eth.defaultAccount = accounts[0]; + } + doneCb(); + }); + }); } }; From 7f408d56c29543e273c5ae33b1a6c9e69b57f087 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 29 Aug 2018 15:33:33 -0400 Subject: [PATCH 2/5] add connection manager --- src/{contracts.js => blockchain.js} | 142 +++++++++++++++++++++++++++- src/index.js | 110 ++------------------- 2 files changed, 151 insertions(+), 101 deletions(-) rename src/{contracts.js => blockchain.js} (65%) diff --git a/src/contracts.js b/src/blockchain.js similarity index 65% rename from src/contracts.js rename to src/blockchain.js index e86016f..94ae4c9 100644 --- a/src/contracts.js +++ b/src/blockchain.js @@ -1,3 +1,135 @@ + +function __reduce(arr, memo, iteratee, cb) { + if (typeof cb !== 'function') { + if (typeof memo === 'function' && typeof iteratee === 'function') { + cb = iteratee; + iteratee = memo; + memo = []; + } else { + throw new TypeError('expected callback to be a function'); + } + } + + if (!Array.isArray(arr)) { + cb(new TypeError('expected an array')); + return; + } + + if (typeof iteratee !== 'function') { + cb(new TypeError('expected iteratee to be a function')); + return; + } + + (function next(i, acc) { + if (i === arr.length) { + cb(null, acc); + return; + } + + iteratee(acc, arr[i], function(err, val) { + if (err) { + cb(err); + return; + } + next(i + 1, val); + }); + })(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); + return null; + }).catch(function(err) { + cb(err); + return null; + }); + return; + } + web3.eth.getAccounts(cb); +}; + +let Blockchain = {}; + +Blockchain.connect = function(connectionList, opts, doneCb) { + const self = this; + this.web3 = null; + this.doFirst(function(cb) { + console.dir("connecting..."); + __reduce(connectionList, function(prev, value, next) { + if (prev === false) { + return next(null, false); + } + + 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())))))) { + 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, ''); + } + + __getAccounts(function(err, account) { + if (err) { + next(null, true) + } else { + next(null, false) + } + }); + }, function(err, _result) { + self.web3 = web3; + __getAccounts(function(err, accounts) { + if (opts.warnAboutMetamask) { + if (web3.eth.currentProvider && web3.eth.currentProvider.isMetaMask) { + console.log("%cNote: Embark has detected you are in the development environment and using Metamask, please make sure Metamask is connected to your local node", "font-size: 2em"); + } + } + if (accounts) { + web3.eth.defaultAccount = accounts[0]; + } + console.dir("connected"); + cb(); + doneCb(err); + }); + }); + }) +} + +Blockchain.execWhenReady = function(cb) { + console.dir("adding an Blockchain: execWhenReady"); + if (this.done) { + return cb(this.err); + } + if (!this.list) { + this.list = []; + } + this.list.push(cb) +} + +Blockchain.doFirst = function(todo) { + var self = this; + todo(function(err) { + self.done = true; + self.err = err; + if (self.list) { + self.list.map((x) => x.apply(x, [self.err])); + } + if (self.finalCb) { + self.finalCb.apply(self.finalCb, []); + } + console.dir("all done!"); + }) +} + let Contract = function (options) { var self = this; var i, abiElement; @@ -23,6 +155,12 @@ let Contract = function (options) { let originalMethods = Object.keys(ContractClass); + Blockchain.execWhenReady(function() { + console.dir("executing..."); + ContractClass.setProvider(web3.currentProvider); + ContractClass.options.from = web3.eth.defaultAccount; + }, "Contract"); + ContractClass._jsonInterface.forEach((abi) => { if (originalMethods.indexOf(abi.name) >= 0) { console.log(abi.name + " is a reserved word and cannot be used as a contract method, property or event"); @@ -231,4 +369,6 @@ Contract.prototype.send = function (value, unit, _options) { this.web3.eth.sendTransaction(options); }; -export default Contract; +Blockchain.Contract = Contract; + +export default Blockchain; diff --git a/src/index.js b/src/index.js index 4cdf4c3..99864ac 100644 --- a/src/index.js +++ b/src/index.js @@ -1,119 +1,29 @@ import Storage from './storage.js'; import Names from './names.js'; import Messages from './messages.js'; -import Contract from './contracts.js'; +import Blockchain from './blockchain.js'; import Utils from './utils.js'; -function __reduce(arr, memo, iteratee, cb) { - if (typeof cb !== 'function') { - if (typeof memo === 'function' && typeof iteratee === 'function') { - cb = iteratee; - iteratee = memo; - memo = []; - } else { - throw new TypeError('expected callback to be a function'); - } - } - - if (!Array.isArray(arr)) { - cb(new TypeError('expected an array')); - return; - } - - if (typeof iteratee !== 'function') { - cb(new TypeError('expected iteratee to be a function')); - return; - } - - (function next(i, acc) { - if (i === arr.length) { - cb(null, acc); - return; - } - - iteratee(acc, arr[i], function(err, val) { - if (err) { - cb(err); - return; - } - next(i + 1, val); - }); - })(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); - return null; - }).catch(function(err) { - cb(err); - return null; - }); - return; - } - web3.eth.getAccounts(cb); -}; - var EmbarkJS = { onReady: function (cb) { - if (typeof (__embarkContext) === 'undefined') { + if (self.Blockchain.done) { return cb(); } - return __embarkContext.execWhenReady(cb); - }, - - connect: function(connectionList, opts, doneCb) { - __reduce(connectionList, function(prev, value, next) { - if (prev === false) { - return next(null, false); - } - - 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())))))) { - 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, ''); - } - - __getAccounts(function(err, account) { - if (err) { - next(null, true) - } else { - next(null, false) - } - }); - }, function(err, _result) { - __getAccounts(function(err, accounts) { - if (opts.warnAboutMetamask) { - if (web3.eth.currentProvider && web3.eth.currentProvider.isMetaMask) { - console.log("%cNote: Embark has detected you are in the development environment and using Metamask, please make sure Metamask is connected to your local node", "font-size: 2em"); - } - } - if (accounts) { - web3.eth.defaultAccount = accounts[0]; - } - doneCb(); - }); - }); + this.Blockchain.finalCb = function() { + cb(); + } } }; -EmbarkJS.Contract = Contract; +EmbarkJS.Blockchain = Blockchain; EmbarkJS.Storage = Storage; EmbarkJS.Names = Names; EmbarkJS.Messages = Messages; EmbarkJS.Utils = Utils; +EmbarkJS.Contract = function() { + console.error("deprecated: please use EmbarkJS.Blockchain.Contract instead"); +}; -EmbarkJS.isNewWeb3 = Contract.isNewWeb3; +EmbarkJS.isNewWeb3 = Blockchain.Contract.isNewWeb3; export default EmbarkJS; From b85f5d7e89a3bcc9ee76ae24e944131d9d1a8838 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 29 Aug 2018 17:26:36 -0400 Subject: [PATCH 3/5] clean up and remove __ since the functions are now local to the file --- src/blockchain.js | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/blockchain.js b/src/blockchain.js index 94ae4c9..608c8b8 100644 --- a/src/blockchain.js +++ b/src/blockchain.js @@ -1,5 +1,5 @@ -function __reduce(arr, memo, iteratee, cb) { +function reduce(arr, memo, iteratee, cb) { if (typeof cb !== 'function') { if (typeof memo === 'function' && typeof iteratee === 'function') { cb = iteratee; @@ -36,12 +36,12 @@ function __reduce(arr, memo, iteratee, cb) { })(0, memo); }; -function __isNewWeb3_1() { +function isNewWeb3_1() { return (typeof(web3.version) === "string"); }; -function __getAccounts(cb) { - if (__isNewWeb3_1()) { +function getAccounts(cb) { + if (isNewWeb3_1()) { web3.eth.getAccounts().then(function(accounts) { cb(null, accounts); return null; @@ -60,8 +60,7 @@ Blockchain.connect = function(connectionList, opts, doneCb) { const self = this; this.web3 = null; this.doFirst(function(cb) { - console.dir("connecting..."); - __reduce(connectionList, function(prev, value, next) { + reduce(connectionList, function(prev, value, next) { if (prev === false) { return next(null, false); } @@ -78,7 +77,7 @@ Blockchain.connect = function(connectionList, opts, doneCb) { return next(null, ''); } - __getAccounts(function(err, account) { + getAccounts(function(err, account) { if (err) { next(null, true) } else { @@ -87,7 +86,7 @@ Blockchain.connect = function(connectionList, opts, doneCb) { }); }, function(err, _result) { self.web3 = web3; - __getAccounts(function(err, accounts) { + getAccounts(function(err, accounts) { if (opts.warnAboutMetamask) { if (web3.eth.currentProvider && web3.eth.currentProvider.isMetaMask) { console.log("%cNote: Embark has detected you are in the development environment and using Metamask, please make sure Metamask is connected to your local node", "font-size: 2em"); @@ -96,7 +95,6 @@ Blockchain.connect = function(connectionList, opts, doneCb) { if (accounts) { web3.eth.defaultAccount = accounts[0]; } - console.dir("connected"); cb(); doneCb(err); }); @@ -105,7 +103,6 @@ Blockchain.connect = function(connectionList, opts, doneCb) { } Blockchain.execWhenReady = function(cb) { - console.dir("adding an Blockchain: execWhenReady"); if (this.done) { return cb(this.err); } @@ -126,7 +123,6 @@ Blockchain.doFirst = function(todo) { if (self.finalCb) { self.finalCb.apply(self.finalCb, []); } - console.dir("all done!"); }) } @@ -156,10 +152,9 @@ let Contract = function (options) { let originalMethods = Object.keys(ContractClass); Blockchain.execWhenReady(function() { - console.dir("executing..."); ContractClass.setProvider(web3.currentProvider); ContractClass.options.from = web3.eth.defaultAccount; - }, "Contract"); + }); ContractClass._jsonInterface.forEach((abi) => { if (originalMethods.indexOf(abi.name) >= 0) { From 3ade13179643facb816a24b0413c3ebba414a755 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 31 Aug 2018 15:57:08 -0400 Subject: [PATCH 4/5] replace reduce with async reduce; refactor --- src/blockchain.js | 55 ++++++----------------------------------------- src/index.js | 2 +- 2 files changed, 7 insertions(+), 50 deletions(-) diff --git a/src/blockchain.js b/src/blockchain.js index 608c8b8..fdb606b 100644 --- a/src/blockchain.js +++ b/src/blockchain.js @@ -1,40 +1,4 @@ - -function reduce(arr, memo, iteratee, cb) { - if (typeof cb !== 'function') { - if (typeof memo === 'function' && typeof iteratee === 'function') { - cb = iteratee; - iteratee = memo; - memo = []; - } else { - throw new TypeError('expected callback to be a function'); - } - } - - if (!Array.isArray(arr)) { - cb(new TypeError('expected an array')); - return; - } - - if (typeof iteratee !== 'function') { - cb(new TypeError('expected iteratee to be a function')); - return; - } - - (function next(i, acc) { - if (i === arr.length) { - cb(null, acc); - return; - } - - iteratee(acc, arr[i], function(err, val) { - if (err) { - cb(err); - return; - } - next(i + 1, val); - }); - })(0, memo); -}; +import {reduce} from 'async' function isNewWeb3_1() { return (typeof(web3.version) === "string"); @@ -42,14 +6,11 @@ function isNewWeb3_1() { function getAccounts(cb) { if (isNewWeb3_1()) { - web3.eth.getAccounts().then(function(accounts) { - cb(null, accounts); - return null; + return web3.eth.getAccounts().then(function(accounts) { + return cb(null, accounts); }).catch(function(err) { - cb(err); - return null; + return cb(err); }); - return; } web3.eth.getAccounts(cb); }; @@ -60,7 +21,7 @@ Blockchain.connect = function(connectionList, opts, doneCb) { const self = this; this.web3 = null; this.doFirst(function(cb) { - reduce(connectionList, function(prev, value, next) { + reduce(connectionList, '', function(prev, value, next) { if (prev === false) { return next(null, false); } @@ -78,11 +39,7 @@ Blockchain.connect = function(connectionList, opts, doneCb) { } getAccounts(function(err, account) { - if (err) { - next(null, true) - } else { - next(null, false) - } + return next(null, !!err) }); }, function(err, _result) { self.web3 = web3; diff --git a/src/index.js b/src/index.js index 99864ac..553cd8e 100644 --- a/src/index.js +++ b/src/index.js @@ -21,7 +21,7 @@ EmbarkJS.Names = Names; EmbarkJS.Messages = Messages; EmbarkJS.Utils = Utils; EmbarkJS.Contract = function() { - console.error("deprecated: please use EmbarkJS.Blockchain.Contract instead"); + throw new Error('EmbarkJS.Contract is deprecated: please use EmbarkJS.Blockchain.Contract instead'); }; EmbarkJS.isNewWeb3 = Blockchain.Contract.isNewWeb3; From dbcfc634bbc985f0a9abebe88554c19ce66a17e4 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 31 Aug 2018 15:59:32 -0400 Subject: [PATCH 5/5] update mm message --- src/blockchain.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/blockchain.js b/src/blockchain.js index fdb606b..a13a64a 100644 --- a/src/blockchain.js +++ b/src/blockchain.js @@ -46,7 +46,7 @@ Blockchain.connect = function(connectionList, opts, doneCb) { getAccounts(function(err, accounts) { if (opts.warnAboutMetamask) { if (web3.eth.currentProvider && web3.eth.currentProvider.isMetaMask) { - console.log("%cNote: Embark has detected you are in the development environment and using Metamask, please make sure Metamask is connected to your local node", "font-size: 2em"); + console.warn("%cNote: Embark has detected you are in the development environment and using Metamask, please make sure Metamask is connected to your local node", "font-size: 2em"); } } if (accounts) {