mirror of
https://github.com/embarklabs/embark.git
synced 2025-02-18 16:46:38 +00:00
support list of fallbacks for dapp web3 connection, support separate connection config for deployment
This commit is contained in:
parent
f882b3486b
commit
232f6fc88b
@ -1,19 +1,54 @@
|
|||||||
class ABIGenerator {
|
class ABIGenerator {
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
this.blockchainConfig = options.blockchainConfig || {};
|
this.blockchainConfig = options.blockchainConfig || {};
|
||||||
|
this.contractsConfig = options.contractsConfig || {};
|
||||||
this.storageConfig = options.storageConfig || {};
|
this.storageConfig = options.storageConfig || {};
|
||||||
this.communicationConfig = options.communicationConfig || {};
|
this.communicationConfig = options.communicationConfig || {};
|
||||||
this.contractsManager = options.contractsManager;
|
this.contractsManager = options.contractsManager;
|
||||||
this.rpcHost = options.blockchainConfig && options.blockchainConfig.rpcHost;
|
this.rpcHost = options.blockchainConfig && options.blockchainConfig.rpcHost;
|
||||||
this.rpcPort = options.blockchainConfig && options.blockchainConfig.rpcPort;
|
this.rpcPort = options.blockchainConfig && options.blockchainConfig.rpcPort;
|
||||||
this.plugins = options.plugins;
|
this.plugins = options.plugins;
|
||||||
|
this.events = options.events;
|
||||||
}
|
}
|
||||||
|
|
||||||
generateProvider() {
|
listenToCommands() {
|
||||||
|
let self = this;
|
||||||
|
|
||||||
|
this.events.setCommandHandler('abi-vanila', function(cb) {
|
||||||
|
let vanillaABI = self.generateABI({useEmbarkJS: false});
|
||||||
|
let contractsJSON = self.generateContractsJSON();
|
||||||
|
|
||||||
|
cb(vanillaABI, contractsJSON);
|
||||||
|
});
|
||||||
|
|
||||||
|
this.events.setCommandHandler('abi', function(cb) {
|
||||||
|
let embarkJSABI = self.generateABI({useEmbarkJS: true});
|
||||||
|
let contractsJSON = self.generateContractsJSON();
|
||||||
|
|
||||||
|
cb(embarkJSABI, contractsJSON);
|
||||||
|
});
|
||||||
|
|
||||||
|
this.events.setCommandHandler('abi-contracts-vanila', function(cb) {
|
||||||
|
let vanillaContractsABI = self.generateContracts(false);
|
||||||
|
let contractsJSON = self.generateContractsJSON();
|
||||||
|
|
||||||
|
cb(vanillaContractsABI, contractsJSON);
|
||||||
|
});
|
||||||
|
|
||||||
|
this.events.setCommandHandler('abi-vanila-deployment', function(cb) {
|
||||||
|
let vanillaABI = self.generateABI({useEmbarkJS: false, deployment: true});
|
||||||
|
let contractsJSON = self.generateContractsJSON();
|
||||||
|
|
||||||
|
cb(vanillaABI, contractsJSON);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
generateProvider(isDeployment) {
|
||||||
let self = this;
|
let self = this;
|
||||||
let result = "";
|
let result = "";
|
||||||
let providerPlugins;
|
let providerPlugins;
|
||||||
|
|
||||||
|
// TODO: check contractsConfig for enabled
|
||||||
if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) {
|
if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
@ -35,12 +70,32 @@ class ABIGenerator {
|
|||||||
result += plugin.generateProvider(self) + "\n";
|
result += plugin.generateProvider(self) + "\n";
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
result += "\nwhenEnvIsLoaded(function() {";
|
result += "\nwhenEnvIsLoaded(function() {\n";
|
||||||
result += "\nif (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {";
|
|
||||||
result += '\n\tweb3 = new Web3(web3.currentProvider);';
|
if (isDeployment) {
|
||||||
result += "\n} else if (typeof Web3 !== 'undefined') {";
|
|
||||||
result += '\n\tweb3 = new Web3(new Web3.providers.HttpProvider("http://' + this.rpcHost + ':' + this.rpcPort + '"));';
|
let connection = "http://" + this.contractsConfig.deployment.host + ":" + this.contractsConfig.deployment.port;
|
||||||
result += '\n}';
|
result += '\n\tweb3 = new Web3(new Web3.providers.HttpProvider("' + connection + '"));';
|
||||||
|
} else {
|
||||||
|
|
||||||
|
let connectionCode = this.contractsConfig.dappConnection.map(function(connection) {
|
||||||
|
let code = "";
|
||||||
|
if (connection === '$WEB3') {
|
||||||
|
code += "if (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {";
|
||||||
|
code += '\n\tweb3 = new Web3(web3.currentProvider);';
|
||||||
|
code += '\n}';
|
||||||
|
} else {
|
||||||
|
code += "if (typeof Web3 !== 'undefined' && !web3.isConnected()) {";
|
||||||
|
code += '\n\tweb3 = new Web3(new Web3.providers.HttpProvider("' + connection + '"));';
|
||||||
|
code += '\n}';
|
||||||
|
}
|
||||||
|
return code;
|
||||||
|
});
|
||||||
|
|
||||||
|
result += connectionCode.join(' else ');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
result += "\nweb3.eth.defaultAccount = web3.eth.accounts[0];";
|
result += "\nweb3.eth.defaultAccount = web3.eth.accounts[0];";
|
||||||
result += '\n})';
|
result += '\n})';
|
||||||
}
|
}
|
||||||
@ -133,7 +188,7 @@ class ABIGenerator {
|
|||||||
generateABI(options) {
|
generateABI(options) {
|
||||||
let result = "";
|
let result = "";
|
||||||
|
|
||||||
result += this.generateProvider();
|
result += this.generateProvider(options.deployment);
|
||||||
result += this.generateContracts(options.useEmbarkJS);
|
result += this.generateContracts(options.useEmbarkJS);
|
||||||
result += this.generateStorageInitialization(options.useEmbarkJS);
|
result += this.generateStorageInitialization(options.useEmbarkJS);
|
||||||
result += this.generateCommunicationInitialization(options.useEmbarkJS);
|
result += this.generateCommunicationInitialization(options.useEmbarkJS);
|
||||||
|
@ -76,11 +76,19 @@ Config.prototype.loadBlockchainConfigFile = function() {
|
|||||||
Config.prototype.loadContractsConfigFile = function() {
|
Config.prototype.loadContractsConfigFile = function() {
|
||||||
|
|
||||||
var configObject = {
|
var configObject = {
|
||||||
|
"versions": {
|
||||||
|
"web3.js": "0.19.1",
|
||||||
|
"solc": "0.4.11"
|
||||||
|
},
|
||||||
"deployment": {
|
"deployment": {
|
||||||
"host": "localhost",
|
"host": "localhost",
|
||||||
"port": 8545,
|
"port": 8545,
|
||||||
"type": "rpc"
|
"type": "rpc"
|
||||||
}
|
},
|
||||||
|
"dappConnection": [
|
||||||
|
"$WEB3",
|
||||||
|
"localhost:8545"
|
||||||
|
]
|
||||||
};
|
};
|
||||||
|
|
||||||
var configPlugins = this.plugins.getPluginsFor('contractsConfig');
|
var configPlugins = this.plugins.getPluginsFor('contractsConfig');
|
||||||
|
@ -80,13 +80,15 @@ class Engine {
|
|||||||
logger: this.logger,
|
logger: this.logger,
|
||||||
plugins: this.plugins
|
plugins: this.plugins
|
||||||
});
|
});
|
||||||
this.events.on('abi', function (abi, contractsJSON) {
|
this.events.on('code-generator-ready', function () {
|
||||||
|
self.events.request('abi', function (abi, contractsJSON) {
|
||||||
self.currentAbi = abi;
|
self.currentAbi = abi;
|
||||||
self.contractsJSON = contractsJSON;
|
self.contractsJSON = contractsJSON;
|
||||||
pipeline.build(abi, contractsJSON, null, function() {
|
pipeline.build(abi, contractsJSON, null, function() {
|
||||||
self.events.emit('outputDone');
|
self.events.emit('outputDone');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
// TODO: still need to redeploy contracts because the original contracts
|
// TODO: still need to redeploy contracts because the original contracts
|
||||||
// config is being corrupted
|
// config is being corrupted
|
||||||
//this.events.on('file-event', function(fileType, path) {
|
//this.events.on('file-event', function(fileType, path) {
|
||||||
@ -103,19 +105,16 @@ class Engine {
|
|||||||
let generateABICode = function (contractsManager) {
|
let generateABICode = function (contractsManager) {
|
||||||
let abiGenerator = new ABIGenerator({
|
let abiGenerator = new ABIGenerator({
|
||||||
blockchainConfig: self.config.blockchainConfig,
|
blockchainConfig: self.config.blockchainConfig,
|
||||||
|
contractsConfig: self.config.contractsConfig,
|
||||||
contractsManager: contractsManager,
|
contractsManager: contractsManager,
|
||||||
plugins: self.plugins,
|
plugins: self.plugins,
|
||||||
storageConfig: self.config.storageConfig,
|
storageConfig: self.config.storageConfig,
|
||||||
communicationConfig: self.config.communicationConfig
|
communicationConfig: self.config.communicationConfig,
|
||||||
|
events: self.events
|
||||||
});
|
});
|
||||||
let embarkJSABI = abiGenerator.generateABI({useEmbarkJS: true});
|
abiGenerator.listenToCommands();
|
||||||
let vanillaABI = abiGenerator.generateABI({useEmbarkJS: false});
|
|
||||||
let vanillaContractsABI = abiGenerator.generateContracts(false);
|
|
||||||
let contractsJSON = abiGenerator.generateContractsJSON();
|
|
||||||
|
|
||||||
self.events.emit('abi-contracts-vanila', vanillaContractsABI, contractsJSON);
|
self.events.emit('code-generator-ready');
|
||||||
self.events.emit('abi-vanila', vanillaABI, contractsJSON);
|
|
||||||
self.events.emit('abi', embarkJSABI, contractsJSON);
|
|
||||||
};
|
};
|
||||||
this.events.on('contractsDeployed', generateABICode);
|
this.events.on('contractsDeployed', generateABICode);
|
||||||
this.events.on('blockchainDisabled', generateABICode);
|
this.events.on('blockchainDisabled', generateABICode);
|
||||||
|
@ -1,3 +1,13 @@
|
|||||||
var EventEmitter = require('events');
|
var EventEmitter = require('events');
|
||||||
|
|
||||||
|
EventEmitter.prototype.request = function(requestName, cb) {
|
||||||
|
this.emit('request:' + requestName, cb);
|
||||||
|
};
|
||||||
|
|
||||||
|
EventEmitter.prototype.setCommandHandler = function(requestName, cb) {
|
||||||
|
this.on('request:' + requestName, function(_cb) {
|
||||||
|
cb.call(this, _cb);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = EventEmitter;
|
module.exports = EventEmitter;
|
||||||
|
@ -72,7 +72,7 @@ Test.prototype.deployAll = function(contractsConfig, cb) {
|
|||||||
callback();
|
callback();
|
||||||
},
|
},
|
||||||
function deploy(callback) {
|
function deploy(callback) {
|
||||||
self.engine.events.on('abi-contracts-vanila', function(vanillaABI) {
|
self.engine.events.setHandler('abi-contracts-vanila', function(vanillaABI) {
|
||||||
callback(null, vanillaABI);
|
callback(null, vanillaABI);
|
||||||
});
|
});
|
||||||
self.engine.deployManager.deployContracts(function(err, result) {
|
self.engine.deployManager.deployContracts(function(err, result) {
|
||||||
|
@ -8,7 +8,12 @@ class Console {
|
|||||||
}
|
}
|
||||||
|
|
||||||
runCode(code) {
|
runCode(code) {
|
||||||
|
try {
|
||||||
RunCode.doEval(code); // jshint ignore:line
|
RunCode.doEval(code); // jshint ignore:line
|
||||||
|
} catch(e) {
|
||||||
|
console.log(code);
|
||||||
|
console.debug(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
processEmbarkCmd (cmd) {
|
processEmbarkCmd (cmd) {
|
||||||
|
@ -79,9 +79,11 @@ class Embark {
|
|||||||
env: engine.env
|
env: engine.env
|
||||||
});
|
});
|
||||||
dashboard.start(function () {
|
dashboard.start(function () {
|
||||||
engine.events.on('abi-vanila', function (abi) {
|
engine.events.on('code-generator-ready', function () {
|
||||||
|
engine.events.request('abi-vanila-deployment', function (abi) {
|
||||||
dashboard.console.runCode(abi);
|
dashboard.console.runCode(abi);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
engine.logger.info('dashboard start');
|
engine.logger.info('dashboard start');
|
||||||
engine.events.on('servicesState', function (servicesState) {
|
engine.events.on('servicesState', function (servicesState) {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
pragma solidity ^0.4.7;
|
pragma solidity ^0.4.11;
|
||||||
contract AnotherStorage {
|
contract AnotherStorage {
|
||||||
address public simpleStorageAddress;
|
address public simpleStorageAddress;
|
||||||
|
address simpleStorageAddress2;
|
||||||
|
|
||||||
function AnotherStorage(address addr) {
|
function AnotherStorage(address addr) {
|
||||||
simpleStorageAddress = addr;
|
simpleStorageAddress = addr;
|
||||||
|
@ -11,7 +11,9 @@
|
|||||||
},
|
},
|
||||||
"dappConnection": [
|
"dappConnection": [
|
||||||
"$WEB3",
|
"$WEB3",
|
||||||
"localhost:8545"
|
"http://localhost:8550",
|
||||||
|
"http://localhost:8545",
|
||||||
|
"http://localhost:8550"
|
||||||
],
|
],
|
||||||
"gas": "auto",
|
"gas": "auto",
|
||||||
"contracts": {
|
"contracts": {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user