Merge pull request #16 from embark-framework/dapp_connection
Move dapp connection logic to EmbarkJS; put Contract inside Blockchain Component
This commit is contained in:
commit
ecfeb27e82
|
@ -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) {
|
let Contract = function (options) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var i, abiElement;
|
var i, abiElement;
|
||||||
|
@ -23,6 +108,11 @@ let Contract = function (options) {
|
||||||
|
|
||||||
let originalMethods = Object.keys(ContractClass);
|
let originalMethods = Object.keys(ContractClass);
|
||||||
|
|
||||||
|
Blockchain.execWhenReady(function() {
|
||||||
|
ContractClass.setProvider(web3.currentProvider);
|
||||||
|
ContractClass.options.from = web3.eth.defaultAccount;
|
||||||
|
});
|
||||||
|
|
||||||
ContractClass._jsonInterface.forEach((abi) => {
|
ContractClass._jsonInterface.forEach((abi) => {
|
||||||
if (originalMethods.indexOf(abi.name) >= 0) {
|
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");
|
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);
|
this.web3.eth.sendTransaction(options);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Contract;
|
Blockchain.Contract = Contract;
|
||||||
|
|
||||||
|
export default Blockchain;
|
15
src/index.js
15
src/index.js
|
@ -1,24 +1,29 @@
|
||||||
import Storage from './storage.js';
|
import Storage from './storage.js';
|
||||||
import Names from './names.js';
|
import Names from './names.js';
|
||||||
import Messages from './messages.js';
|
import Messages from './messages.js';
|
||||||
import Contract from './contracts.js';
|
import Blockchain from './blockchain.js';
|
||||||
import Utils from './utils.js';
|
import Utils from './utils.js';
|
||||||
|
|
||||||
var EmbarkJS = {
|
var EmbarkJS = {
|
||||||
onReady: function (cb) {
|
onReady: function (cb) {
|
||||||
if (typeof (__embarkContext) === 'undefined') {
|
if (self.Blockchain.done) {
|
||||||
return cb();
|
return cb();
|
||||||
}
|
}
|
||||||
return __embarkContext.execWhenReady(cb);
|
this.Blockchain.finalCb = function() {
|
||||||
|
cb();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbarkJS.Contract = Contract;
|
EmbarkJS.Blockchain = Blockchain;
|
||||||
EmbarkJS.Storage = Storage;
|
EmbarkJS.Storage = Storage;
|
||||||
EmbarkJS.Names = Names;
|
EmbarkJS.Names = Names;
|
||||||
EmbarkJS.Messages = Messages;
|
EmbarkJS.Messages = Messages;
|
||||||
EmbarkJS.Utils = Utils;
|
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;
|
export default EmbarkJS;
|
||||||
|
|
Loading…
Reference in New Issue