2018-05-18 21:15:49 +00:00
|
|
|
const Web3 = require('web3');
|
2018-05-23 12:40:09 +00:00
|
|
|
const async = require('async');
|
2018-05-18 18:58:05 +00:00
|
|
|
const Provider = require('./provider.js');
|
2018-07-27 20:54:33 +00:00
|
|
|
const utils = require('../../utils/utils');
|
2018-07-31 13:59:27 +00:00
|
|
|
const constants = require('../../constants');
|
2018-08-14 14:09:59 +00:00
|
|
|
const embarkJsUtils = require('embarkjs').Utils;
|
2018-05-23 13:33:32 +00:00
|
|
|
|
|
|
|
const WEB3_READY = 'web3Ready';
|
2018-05-18 20:51:03 +00:00
|
|
|
|
2018-07-20 16:37:50 +00:00
|
|
|
// TODO: consider another name, this is the blockchain connector
|
2018-07-27 19:36:16 +00:00
|
|
|
class BlockchainConnector {
|
2018-07-27 20:54:33 +00:00
|
|
|
constructor(embark, options) {
|
2018-07-20 12:21:23 +00:00
|
|
|
const self = this;
|
2018-05-18 20:51:03 +00:00
|
|
|
this.plugins = options.plugins;
|
2018-07-27 20:54:33 +00:00
|
|
|
this.logger = embark.logger;
|
|
|
|
this.events = embark.events;
|
|
|
|
this.contractsConfig = embark.config.contractsConfig;
|
|
|
|
this.blockchainConfig = embark.config.blockchainConfig;
|
2018-05-18 21:15:49 +00:00
|
|
|
this.web3 = options.web3;
|
2018-05-23 15:52:07 +00:00
|
|
|
this.isDev = options.isDev;
|
2018-05-23 13:54:13 +00:00
|
|
|
this.web3Endpoint = '';
|
2018-05-23 13:33:32 +00:00
|
|
|
this.isWeb3Ready = false;
|
2018-10-09 20:43:38 +00:00
|
|
|
this.wait = options.wait;
|
2018-05-18 18:58:05 +00:00
|
|
|
|
2018-07-20 12:21:23 +00:00
|
|
|
self.events.setCommandHandler("blockchain:web3:isReady", (cb) => {
|
|
|
|
cb(self.isWeb3Ready);
|
|
|
|
});
|
2018-07-19 20:31:28 +00:00
|
|
|
|
2018-07-27 20:03:15 +00:00
|
|
|
self.events.setCommandHandler("blockchain:object", (cb) => {
|
|
|
|
cb(self);
|
|
|
|
});
|
|
|
|
|
2018-05-18 21:15:49 +00:00
|
|
|
if (!this.web3) {
|
|
|
|
this.initWeb3();
|
2018-05-23 13:54:13 +00:00
|
|
|
} else {
|
|
|
|
this.isWeb3Ready = true;
|
2018-05-18 21:15:49 +00:00
|
|
|
}
|
2018-10-09 20:43:38 +00:00
|
|
|
|
2018-07-20 12:21:23 +00:00
|
|
|
this.registerServiceCheck();
|
2018-05-19 01:08:32 +00:00
|
|
|
this.registerRequests();
|
2018-05-23 15:16:56 +00:00
|
|
|
this.registerWeb3Object();
|
2018-07-20 12:21:23 +00:00
|
|
|
this.registerEvents();
|
2018-07-19 20:31:28 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 21:34:54 +00:00
|
|
|
initWeb3(cb) {
|
2018-05-23 13:33:32 +00:00
|
|
|
if (!cb) {
|
|
|
|
cb = function(){};
|
|
|
|
}
|
|
|
|
if (this.isWeb3Ready) {
|
2018-07-17 13:13:12 +00:00
|
|
|
this.events.emit(WEB3_READY);
|
2018-05-23 13:33:32 +00:00
|
|
|
return cb();
|
|
|
|
}
|
2018-07-19 20:31:28 +00:00
|
|
|
|
2018-05-18 18:58:05 +00:00
|
|
|
const self = this;
|
2018-05-18 21:15:49 +00:00
|
|
|
this.web3 = new Web3();
|
2018-05-18 18:58:05 +00:00
|
|
|
|
2018-10-09 20:43:38 +00:00
|
|
|
// TODO find a better way
|
|
|
|
if (self.wait) {
|
|
|
|
return cb();
|
|
|
|
}
|
|
|
|
|
|
|
|
let {type, host, port, accounts, protocol} = this.contractsConfig.deployment;
|
|
|
|
|
|
|
|
if (!BlockchainConnector.ACCEPTED_TYPES.includes(type)) {
|
|
|
|
this.logger.error(__("contracts config error: unknown deployment type %s", type));
|
|
|
|
this.logger.error(__("Accepted types are: %s", BlockchainConnector.ACCEPTED_TYPES.join(', ')));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'vm') {
|
2018-10-10 15:34:56 +00:00
|
|
|
const sim = this._getSimulator();
|
|
|
|
this.provider = sim.provider(this.contractsConfig.deployment);
|
|
|
|
this.web3.setProvider(this.provider);
|
|
|
|
this._emitWeb3Ready();
|
|
|
|
return cb();
|
2018-05-18 21:15:49 +00:00
|
|
|
}
|
2018-06-15 18:35:50 +00:00
|
|
|
|
2018-10-09 20:43:38 +00:00
|
|
|
protocol = (type === "rpc") ? protocol : 'ws';
|
2018-07-19 20:31:28 +00:00
|
|
|
|
2018-10-09 20:43:38 +00:00
|
|
|
this.web3Endpoint = utils.buildUrl(protocol, host, port);
|
2018-07-19 20:31:28 +00:00
|
|
|
|
|
|
|
const providerOptions = {
|
|
|
|
web3: this.web3,
|
2018-10-09 20:43:38 +00:00
|
|
|
accountsConfig: accounts,
|
2018-07-19 20:31:28 +00:00
|
|
|
blockchainConfig: this.blockchainConfig,
|
|
|
|
logger: this.logger,
|
|
|
|
isDev: this.isDev,
|
2018-10-09 20:43:38 +00:00
|
|
|
type: type,
|
2018-07-19 20:31:28 +00:00
|
|
|
web3Endpoint: self.web3Endpoint
|
|
|
|
};
|
|
|
|
this.provider = new Provider(providerOptions);
|
|
|
|
|
2018-07-20 12:21:23 +00:00
|
|
|
self.events.request("processes:launch", "blockchain", () => {
|
|
|
|
self.provider.startWeb3Provider(() => {
|
2018-09-28 10:09:23 +00:00
|
|
|
this.getNetworkId()
|
2018-07-31 13:59:27 +00:00
|
|
|
.then(id => {
|
|
|
|
let networkId = self.blockchainConfig.networkId;
|
|
|
|
if (!networkId && constants.blockchain.networkIds[self.blockchainConfig.networkType]) {
|
|
|
|
networkId = constants.blockchain.networkIds[self.blockchainConfig.networkType];
|
|
|
|
}
|
2018-08-23 14:29:39 +00:00
|
|
|
if (networkId && id.toString() !== networkId.toString()) {
|
2018-07-31 13:59:27 +00:00
|
|
|
self.logger.warn(__('Connected to a blockchain node on network {{realId}} while your config specifies {{configId}}', {realId: id, configId: networkId}));
|
|
|
|
self.logger.warn(__('Make sure you started the right blockchain node'));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(console.error);
|
2018-07-20 12:21:23 +00:00
|
|
|
self.provider.fundAccounts(() => {
|
2018-10-09 20:43:38 +00:00
|
|
|
self._emitWeb3Ready();
|
2018-07-19 20:31:28 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-09 20:43:38 +00:00
|
|
|
_emitWeb3Ready() {
|
|
|
|
this.isWeb3Ready = true;
|
|
|
|
this.events.emit(WEB3_READY);
|
|
|
|
this.registerWeb3Object();
|
|
|
|
}
|
|
|
|
|
|
|
|
_getSimulator() {
|
|
|
|
try {
|
|
|
|
return require('ganache-cli');
|
|
|
|
} catch (e) {
|
|
|
|
const moreInfo = 'For more information see https://github.com/trufflesuite/ganache-cli';
|
|
|
|
if (e.code === 'MODULE_NOT_FOUND') {
|
|
|
|
this.logger.error(__('Simulator not found; Please install it with "%s"', 'npm install ganache-cli --save'));
|
|
|
|
this.logger.error(moreInfo);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
this.logger.error("==============");
|
|
|
|
this.logger.error(__("Tried to load Ganache CLI (testrpc), but an error occurred. This is a problem with Ganache CLI"));
|
|
|
|
this.logger.error(moreInfo);
|
|
|
|
this.logger.error("==============");
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-20 12:21:23 +00:00
|
|
|
registerEvents() {
|
2018-07-26 17:11:38 +00:00
|
|
|
const self = this;
|
|
|
|
self.events.on('check:wentOffline:Ethereum', () => {
|
2018-09-25 16:41:22 +00:00
|
|
|
self.logger.warn('Ethereum went offline: stopping web3 provider...');
|
2018-07-26 17:11:38 +00:00
|
|
|
self.provider.stop();
|
|
|
|
|
|
|
|
// once the node goes back online, we can restart the provider
|
|
|
|
self.events.once('check:backOnline:Ethereum', () => {
|
2018-09-25 16:41:22 +00:00
|
|
|
self.logger.warn('Ethereum back online: starting web3 provider...');
|
2018-07-26 17:11:38 +00:00
|
|
|
self.provider.startWeb3Provider(() => {
|
2018-09-25 16:41:22 +00:00
|
|
|
self.logger.warn('web3 provider restarted after ethereum node came back online');
|
2018-07-26 17:11:38 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-05-18 21:15:49 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 13:33:32 +00:00
|
|
|
onReady(callback) {
|
|
|
|
if (this.isWeb3Ready) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.events.once(WEB3_READY, () => {
|
2018-06-22 12:52:15 +00:00
|
|
|
callback();
|
|
|
|
});
|
2018-05-23 13:33:32 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 21:15:49 +00:00
|
|
|
registerServiceCheck() {
|
|
|
|
const self = this;
|
2018-05-30 15:02:01 +00:00
|
|
|
const NO_NODE = 'noNode';
|
2018-05-18 21:15:49 +00:00
|
|
|
|
2018-06-01 15:58:11 +00:00
|
|
|
this.events.request("services:register", 'Ethereum', function (cb) {
|
2018-05-23 12:40:09 +00:00
|
|
|
async.waterfall([
|
|
|
|
function checkNodeConnection(next) {
|
2018-10-17 10:27:12 +00:00
|
|
|
if (!self.provider || !self.provider.connected()) {
|
2018-07-20 12:21:23 +00:00
|
|
|
return next(NO_NODE, {name: "No Blockchain node found", status: 'off'});
|
|
|
|
}
|
|
|
|
next();
|
2018-05-23 12:40:09 +00:00
|
|
|
},
|
|
|
|
function checkVersion(next) {
|
|
|
|
// TODO: web3_clientVersion method is currently not implemented in web3.js 1.0
|
|
|
|
self.web3._requestManager.send({method: 'web3_clientVersion', params: []}, (err, version) => {
|
|
|
|
if (err) {
|
2018-07-20 12:21:23 +00:00
|
|
|
self.isWeb3Ready = false;
|
2018-09-21 21:51:53 +00:00
|
|
|
return next(null, {name: "Ethereum node not found", status: 'off'});
|
2018-05-23 12:40:09 +00:00
|
|
|
}
|
|
|
|
if (version.indexOf("/") < 0) {
|
2018-07-20 12:21:23 +00:00
|
|
|
self.events.emit(WEB3_READY);
|
|
|
|
self.isWeb3Ready = true;
|
2018-05-23 12:40:09 +00:00
|
|
|
return next(null, {name: version, status: 'on'});
|
|
|
|
}
|
|
|
|
let nodeName = version.split("/")[0];
|
|
|
|
let versionNumber = version.split("/")[1].split("-")[0];
|
|
|
|
let name = nodeName + " " + versionNumber + " (Ethereum)";
|
|
|
|
|
2018-07-20 12:21:23 +00:00
|
|
|
self.events.emit(WEB3_READY);
|
|
|
|
self.isWeb3Ready = true;
|
2018-05-23 12:40:09 +00:00
|
|
|
return next(null, {name: name, status: 'on'});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
], (err, statusObj) => {
|
2018-05-30 15:02:01 +00:00
|
|
|
if (err && err !== NO_NODE) {
|
|
|
|
return cb(err);
|
2018-05-18 21:15:49 +00:00
|
|
|
}
|
2018-05-23 12:40:09 +00:00
|
|
|
cb(statusObj);
|
2018-05-18 21:15:49 +00:00
|
|
|
});
|
2018-05-30 16:00:44 +00:00
|
|
|
}, 5000, 'off');
|
2018-05-18 20:51:03 +00:00
|
|
|
}
|
2018-05-18 21:15:49 +00:00
|
|
|
|
2018-05-19 01:08:32 +00:00
|
|
|
registerRequests() {
|
2018-05-18 22:31:47 +00:00
|
|
|
const self = this;
|
|
|
|
|
2018-10-05 17:15:01 +00:00
|
|
|
this.events.setCommandHandler("blockchain:get", function(cb) {
|
|
|
|
cb(self.web3);
|
|
|
|
});
|
|
|
|
|
2018-05-18 22:31:47 +00:00
|
|
|
this.events.setCommandHandler("blockchain:defaultAccount:get", function(cb) {
|
2018-07-13 20:41:15 +00:00
|
|
|
cb(self.defaultAccount());
|
2018-05-18 22:31:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.events.setCommandHandler("blockchain:defaultAccount:set", function(account, cb) {
|
|
|
|
self.setDefaultAccount(account);
|
|
|
|
cb();
|
|
|
|
});
|
2018-05-19 01:08:32 +00:00
|
|
|
|
2018-10-10 16:09:56 +00:00
|
|
|
this.events.setCommandHandler("blockchain:getAccounts", function(cb) {
|
|
|
|
self.getAccounts(cb);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.events.setCommandHandler("blockchain:getBalance", function(address, cb) {
|
|
|
|
self.getBalance(address, cb);
|
|
|
|
});
|
|
|
|
|
2018-05-19 01:08:32 +00:00
|
|
|
this.events.setCommandHandler("blockchain:block:byNumber", function(blockNumber, cb) {
|
|
|
|
self.getBlock(blockNumber, cb);
|
|
|
|
});
|
|
|
|
|
2018-06-14 19:22:50 +00:00
|
|
|
this.events.setCommandHandler("blockchain:gasPrice", function(cb) {
|
2018-07-18 12:08:32 +00:00
|
|
|
self.getGasPrice(cb);
|
2018-06-14 19:22:50 +00:00
|
|
|
});
|
|
|
|
|
2018-09-28 10:09:23 +00:00
|
|
|
this.events.setCommandHandler("blockchain:networkId", function(cb) {
|
|
|
|
self.getNetworkId().then(cb);
|
|
|
|
});
|
|
|
|
|
2018-07-13 20:41:15 +00:00
|
|
|
this.events.setCommandHandler("blockchain:contract:create", function(params, cb) {
|
|
|
|
cb(self.ContractObject(params));
|
|
|
|
});
|
2018-05-18 22:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defaultAccount() {
|
|
|
|
return this.web3.eth.defaultAccount;
|
|
|
|
}
|
|
|
|
|
|
|
|
setDefaultAccount(account) {
|
|
|
|
this.web3.eth.defaultAccount = account;
|
|
|
|
}
|
|
|
|
|
|
|
|
getAccounts(cb) {
|
|
|
|
this.web3.eth.getAccounts(cb);
|
|
|
|
}
|
|
|
|
|
2018-10-10 16:09:56 +00:00
|
|
|
getBalance(address, cb) {
|
|
|
|
this.web3.eth.getBalance(address, cb);
|
|
|
|
}
|
|
|
|
|
2018-05-18 22:41:04 +00:00
|
|
|
getCode(address, cb) {
|
|
|
|
this.web3.eth.getCode(address, cb);
|
|
|
|
}
|
|
|
|
|
2018-05-18 22:50:20 +00:00
|
|
|
getBlock(blockNumber, cb) {
|
|
|
|
this.web3.eth.getBlock(blockNumber, cb);
|
|
|
|
}
|
|
|
|
|
2018-06-14 19:22:50 +00:00
|
|
|
getGasPrice(cb) {
|
2018-07-20 12:21:23 +00:00
|
|
|
const self = this;
|
|
|
|
this.onReady(() => {
|
|
|
|
self.web3.eth.getGasPrice(cb);
|
|
|
|
});
|
2018-06-14 19:22:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 10:09:23 +00:00
|
|
|
getNetworkId() {
|
|
|
|
return this.web3.eth.net.getId();
|
|
|
|
}
|
|
|
|
|
2018-05-18 23:00:36 +00:00
|
|
|
ContractObject(params) {
|
2018-07-13 20:41:15 +00:00
|
|
|
return new this.web3.eth.Contract(params.abi, params.address);
|
2018-05-18 23:00:36 +00:00
|
|
|
}
|
|
|
|
|
2018-05-19 00:26:21 +00:00
|
|
|
deployContractObject(contractObject, params) {
|
|
|
|
return contractObject.deploy({arguments: params.arguments, data: params.data});
|
|
|
|
}
|
|
|
|
|
|
|
|
estimateDeployContractGas(deployObject, cb) {
|
|
|
|
return deployObject.estimateGas().then((gasValue) => {
|
|
|
|
cb(null, gasValue);
|
|
|
|
}).catch(cb);
|
|
|
|
}
|
|
|
|
|
2018-08-13 17:04:42 +00:00
|
|
|
deployContractFromObject(deployContractObject, params, cb) {
|
2018-08-14 14:09:59 +00:00
|
|
|
embarkJsUtils.secureSend(this.web3, deployContractObject, {
|
2018-05-19 00:26:21 +00:00
|
|
|
from: params.from, gas: params.gas, gasPrice: params.gasPrice
|
2018-09-26 21:39:11 +00:00
|
|
|
}, true, cb);
|
2018-05-20 13:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
determineDefaultAccount(cb) {
|
|
|
|
const self = this;
|
2018-08-13 17:04:42 +00:00
|
|
|
self.getAccounts(function(err, accounts) {
|
2018-05-20 13:08:03 +00:00
|
|
|
if (err) {
|
|
|
|
self.logger.error(err);
|
|
|
|
return cb(new Error(err));
|
|
|
|
}
|
|
|
|
let accountConfig = self.blockchainConfig.account;
|
|
|
|
let selectedAccount = accountConfig && accountConfig.address;
|
|
|
|
self.setDefaultAccount(selectedAccount || accounts[0]);
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-23 15:16:56 +00:00
|
|
|
registerWeb3Object() {
|
|
|
|
// doesn't feel quite right, should be a cmd or plugin method
|
|
|
|
// can just be a command without a callback
|
2018-08-29 10:03:06 +00:00
|
|
|
this.events.emit("runcode:register", "web3", this.web3, false);
|
2018-05-23 15:16:56 +00:00
|
|
|
}
|
2018-08-10 20:26:05 +00:00
|
|
|
|
|
|
|
subscribeToPendingTransactions() {
|
|
|
|
const self = this;
|
|
|
|
this.onReady(() => {
|
|
|
|
if (self.logsSubscription) {
|
|
|
|
self.logsSubscription.unsubscribe();
|
|
|
|
}
|
|
|
|
self.logsSubscription = self.web3.eth
|
|
|
|
.subscribe('newBlockHeaders', () => {})
|
|
|
|
.on("data", function (blockHeader) {
|
|
|
|
self.events.emit('block:header', blockHeader);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-05-18 20:51:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 20:43:38 +00:00
|
|
|
BlockchainConnector.ACCEPTED_TYPES = ['rpc', 'ws', 'vm'];
|
|
|
|
|
2018-07-27 19:36:16 +00:00
|
|
|
module.exports = BlockchainConnector;
|
2018-05-18 20:51:03 +00:00
|
|
|
|