Skip to content

Commit 232f6fc

Browse files
committed
support list of fallbacks for dapp web3 connection, support separate connection config for deployment
1 parent f882b34 commit 232f6fc

File tree

9 files changed

+111
-29
lines changed

9 files changed

+111
-29
lines changed

lib/contracts/abi.js

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,54 @@
11
class ABIGenerator {
22
constructor(options) {
33
this.blockchainConfig = options.blockchainConfig || {};
4+
this.contractsConfig = options.contractsConfig || {};
45
this.storageConfig = options.storageConfig || {};
56
this.communicationConfig = options.communicationConfig || {};
67
this.contractsManager = options.contractsManager;
78
this.rpcHost = options.blockchainConfig && options.blockchainConfig.rpcHost;
89
this.rpcPort = options.blockchainConfig && options.blockchainConfig.rpcPort;
910
this.plugins = options.plugins;
11+
this.events = options.events;
1012
}
1113

12-
generateProvider() {
14+
listenToCommands() {
15+
let self = this;
16+
17+
this.events.setCommandHandler('abi-vanila', function(cb) {
18+
let vanillaABI = self.generateABI({useEmbarkJS: false});
19+
let contractsJSON = self.generateContractsJSON();
20+
21+
cb(vanillaABI, contractsJSON);
22+
});
23+
24+
this.events.setCommandHandler('abi', function(cb) {
25+
let embarkJSABI = self.generateABI({useEmbarkJS: true});
26+
let contractsJSON = self.generateContractsJSON();
27+
28+
cb(embarkJSABI, contractsJSON);
29+
});
30+
31+
this.events.setCommandHandler('abi-contracts-vanila', function(cb) {
32+
let vanillaContractsABI = self.generateContracts(false);
33+
let contractsJSON = self.generateContractsJSON();
34+
35+
cb(vanillaContractsABI, contractsJSON);
36+
});
37+
38+
this.events.setCommandHandler('abi-vanila-deployment', function(cb) {
39+
let vanillaABI = self.generateABI({useEmbarkJS: false, deployment: true});
40+
let contractsJSON = self.generateContractsJSON();
41+
42+
cb(vanillaABI, contractsJSON);
43+
});
44+
}
45+
46+
generateProvider(isDeployment) {
1347
let self = this;
1448
let result = "";
1549
let providerPlugins;
1650

51+
// TODO: check contractsConfig for enabled
1752
if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) {
1853
return "";
1954
}
@@ -35,12 +70,32 @@ class ABIGenerator {
3570
result += plugin.generateProvider(self) + "\n";
3671
});
3772
} else {
38-
result += "\nwhenEnvIsLoaded(function() {";
39-
result += "\nif (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {";
40-
result += '\n\tweb3 = new Web3(web3.currentProvider);';
41-
result += "\n} else if (typeof Web3 !== 'undefined') {";
42-
result += '\n\tweb3 = new Web3(new Web3.providers.HttpProvider("http://' + this.rpcHost + ':' + this.rpcPort + '"));';
43-
result += '\n}';
73+
result += "\nwhenEnvIsLoaded(function() {\n";
74+
75+
if (isDeployment) {
76+
77+
let connection = "http://" + this.contractsConfig.deployment.host + ":" + this.contractsConfig.deployment.port;
78+
result += '\n\tweb3 = new Web3(new Web3.providers.HttpProvider("' + connection + '"));';
79+
} else {
80+
81+
let connectionCode = this.contractsConfig.dappConnection.map(function(connection) {
82+
let code = "";
83+
if (connection === '$WEB3') {
84+
code += "if (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {";
85+
code += '\n\tweb3 = new Web3(web3.currentProvider);';
86+
code += '\n}';
87+
} else {
88+
code += "if (typeof Web3 !== 'undefined' && !web3.isConnected()) {";
89+
code += '\n\tweb3 = new Web3(new Web3.providers.HttpProvider("' + connection + '"));';
90+
code += '\n}';
91+
}
92+
return code;
93+
});
94+
95+
result += connectionCode.join(' else ');
96+
}
97+
98+
4499
result += "\nweb3.eth.defaultAccount = web3.eth.accounts[0];";
45100
result += '\n})';
46101
}
@@ -133,7 +188,7 @@ class ABIGenerator {
133188
generateABI(options) {
134189
let result = "";
135190

136-
result += this.generateProvider();
191+
result += this.generateProvider(options.deployment);
137192
result += this.generateContracts(options.useEmbarkJS);
138193
result += this.generateStorageInitialization(options.useEmbarkJS);
139194
result += this.generateCommunicationInitialization(options.useEmbarkJS);

lib/core/config.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,19 @@ Config.prototype.loadBlockchainConfigFile = function() {
7676
Config.prototype.loadContractsConfigFile = function() {
7777

7878
var configObject = {
79-
"deployment": {
79+
"versions": {
80+
"web3.js": "0.19.1",
81+
"solc": "0.4.11"
82+
},
83+
"deployment": {
8084
"host": "localhost",
8185
"port": 8545,
8286
"type": "rpc"
83-
}
87+
},
88+
"dappConnection": [
89+
"$WEB3",
90+
"localhost:8545"
91+
]
8492
};
8593

8694
var configPlugins = this.plugins.getPluginsFor('contractsConfig');

lib/core/engine.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@ class Engine {
8080
logger: this.logger,
8181
plugins: this.plugins
8282
});
83-
this.events.on('abi', function (abi, contractsJSON) {
84-
self.currentAbi = abi;
85-
self.contractsJSON = contractsJSON;
86-
pipeline.build(abi, contractsJSON, null, function() {
87-
self.events.emit('outputDone');
83+
this.events.on('code-generator-ready', function () {
84+
self.events.request('abi', function (abi, contractsJSON) {
85+
self.currentAbi = abi;
86+
self.contractsJSON = contractsJSON;
87+
pipeline.build(abi, contractsJSON, null, function() {
88+
self.events.emit('outputDone');
89+
});
8890
});
8991
});
9092
// TODO: still need to redeploy contracts because the original contracts
@@ -103,19 +105,16 @@ class Engine {
103105
let generateABICode = function (contractsManager) {
104106
let abiGenerator = new ABIGenerator({
105107
blockchainConfig: self.config.blockchainConfig,
108+
contractsConfig: self.config.contractsConfig,
106109
contractsManager: contractsManager,
107110
plugins: self.plugins,
108111
storageConfig: self.config.storageConfig,
109-
communicationConfig: self.config.communicationConfig
112+
communicationConfig: self.config.communicationConfig,
113+
events: self.events
110114
});
111-
let embarkJSABI = abiGenerator.generateABI({useEmbarkJS: true});
112-
let vanillaABI = abiGenerator.generateABI({useEmbarkJS: false});
113-
let vanillaContractsABI = abiGenerator.generateContracts(false);
114-
let contractsJSON = abiGenerator.generateContractsJSON();
115+
abiGenerator.listenToCommands();
115116

116-
self.events.emit('abi-contracts-vanila', vanillaContractsABI, contractsJSON);
117-
self.events.emit('abi-vanila', vanillaABI, contractsJSON);
118-
self.events.emit('abi', embarkJSABI, contractsJSON);
117+
self.events.emit('code-generator-ready');
119118
};
120119
this.events.on('contractsDeployed', generateABICode);
121120
this.events.on('blockchainDisabled', generateABICode);

lib/core/events.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
var EventEmitter = require('events');
22

3+
EventEmitter.prototype.request = function(requestName, cb) {
4+
this.emit('request:' + requestName, cb);
5+
};
6+
7+
EventEmitter.prototype.setCommandHandler = function(requestName, cb) {
8+
this.on('request:' + requestName, function(_cb) {
9+
cb.call(this, _cb);
10+
});
11+
};
12+
313
module.exports = EventEmitter;

lib/core/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Test.prototype.deployAll = function(contractsConfig, cb) {
7272
callback();
7373
},
7474
function deploy(callback) {
75-
self.engine.events.on('abi-contracts-vanila', function(vanillaABI) {
75+
self.engine.events.setHandler('abi-contracts-vanila', function(vanillaABI) {
7676
callback(null, vanillaABI);
7777
});
7878
self.engine.deployManager.deployContracts(function(err, result) {

lib/dashboard/console.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ class Console {
88
}
99

1010
runCode(code) {
11-
RunCode.doEval(code); // jshint ignore:line
11+
try {
12+
RunCode.doEval(code); // jshint ignore:line
13+
} catch(e) {
14+
console.log(code);
15+
console.debug(e);
16+
}
1217
}
1318

1419
processEmbarkCmd (cmd) {

lib/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ class Embark {
7979
env: engine.env
8080
});
8181
dashboard.start(function () {
82-
engine.events.on('abi-vanila', function (abi) {
83-
dashboard.console.runCode(abi);
82+
engine.events.on('code-generator-ready', function () {
83+
engine.events.request('abi-vanila-deployment', function (abi) {
84+
dashboard.console.runCode(abi);
85+
});
8486
});
8587

8688
engine.logger.info('dashboard start');

test_app/app/contracts/another_storage.sol

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
pragma solidity ^0.4.7;
1+
pragma solidity ^0.4.11;
22
contract AnotherStorage {
33
address public simpleStorageAddress;
4+
address simpleStorageAddress2;
45

56
function AnotherStorage(address addr) {
67
simpleStorageAddress = addr;

test_app/config/contracts.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
},
1212
"dappConnection": [
1313
"$WEB3",
14-
"localhost:8545"
14+
"http://localhost:8550",
15+
"http://localhost:8545",
16+
"http://localhost:8550"
1517
],
1618
"gas": "auto",
1719
"contracts": {

0 commit comments

Comments
 (0)