diff --git a/src/contracts.js b/src/blockchain.js similarity index 72% rename from src/contracts.js rename to src/blockchain.js index e86016f..a13a64a 100644 --- a/src/contracts.js +++ b/src/blockchain.js @@ -1,3 +1,88 @@ +import {reduce} from 'async' + +function isNewWeb3_1() { + return (typeof(web3.version) === "string"); +}; + +function getAccounts(cb) { + if (isNewWeb3_1()) { + return web3.eth.getAccounts().then(function(accounts) { + return cb(null, accounts); + }).catch(function(err) { + return cb(err); + }); + } + web3.eth.getAccounts(cb); +}; + +let Blockchain = {}; + +Blockchain.connect = function(connectionList, opts, doneCb) { + const self = this; + this.web3 = null; + this.doFirst(function(cb) { + 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) { + return next(null, !!err) + }); + }, function(err, _result) { + self.web3 = web3; + getAccounts(function(err, accounts) { + if (opts.warnAboutMetamask) { + if (web3.eth.currentProvider && web3.eth.currentProvider.isMetaMask) { + 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) { + web3.eth.defaultAccount = accounts[0]; + } + cb(); + doneCb(err); + }); + }); + }) +} + +Blockchain.execWhenReady = function(cb) { + 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, []); + } + }) +} + let Contract = function (options) { var self = this; var i, abiElement; @@ -23,6 +108,11 @@ let Contract = function (options) { let originalMethods = Object.keys(ContractClass); + Blockchain.execWhenReady(function() { + ContractClass.setProvider(web3.currentProvider); + ContractClass.options.from = web3.eth.defaultAccount; + }); + 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 +321,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 acc07a5..553cd8e 100644 --- a/src/index.js +++ b/src/index.js @@ -1,24 +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'; var EmbarkJS = { onReady: function (cb) { - if (typeof (__embarkContext) === 'undefined') { + if (self.Blockchain.done) { return cb(); } - return __embarkContext.execWhenReady(cb); + 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() { + throw new Error('EmbarkJS.Contract is deprecated: please use EmbarkJS.Blockchain.Contract instead'); +}; -EmbarkJS.isNewWeb3 = Contract.isNewWeb3; +EmbarkJS.isNewWeb3 = Blockchain.Contract.isNewWeb3; export default EmbarkJS;