From 7f408d56c29543e273c5ae33b1a6c9e69b57f087 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 29 Aug 2018 15:33:33 -0400 Subject: [PATCH] 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;