implement plugin support: implement support for client-side web3 providers and contracts plugins

This commit is contained in:
Iuri Matias 2016-12-06 21:33:31 -05:00
parent 747ddc1815
commit ccdfaf61f2
7 changed files with 133 additions and 29 deletions

View File

@ -1,39 +1,59 @@
var Plugins = require('./plugins.js');
var ABIGenerator = function(blockchainConfig, contractsManager) { var ABIGenerator = function(options) {
this.blockchainConfig = blockchainConfig; this.blockchainConfig = options.blockchainConfig || {};
this.contractsManager = contractsManager; this.contractsManager = options.contractsManager;
this.rpcHost = blockchainConfig.rpcHost; this.rpcHost = options.blockchainConfig.rpcHost;
this.rpcPort = blockchainConfig.rpcPort; this.rpcPort = options.blockchainConfig.rpcPort;
this.plugins = options.plugins || new Plugins({});
}; };
ABIGenerator.prototype.generateProvider = function() { ABIGenerator.prototype.generateProvider = function() {
var self = this;
var result = ""; var result = "";
result += "\nif (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {"; var providerPlugins = this.plugins.getPluginsFor('clientWeb3Provider');
result += '\n\tweb3 = new Web3(web3.currentProvider);';
result += "\n} else if (typeof Web3 !== 'undefined') {"; if (providerPlugins.length > 0) {
result += '\n\tweb3 = new Web3(new Web3.providers.HttpProvider("http://' + this.rpcHost + ':' + this.rpcPort + '"));'; providerPlugins.forEach(function(plugin) {
result += '\n}'; result += plugin.generateProvider(self);
result += "\nweb3.eth.defaultAccount = web3.eth.accounts[0];"; });
} else {
result += "\nif (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {";
result += '\n\tweb3 = new Web3(web3.currentProvider);';
result += "\n} else if (typeof Web3 !== 'undefined') {";
result += '\n\tweb3 = new Web3(new Web3.providers.HttpProvider("http://' + this.rpcHost + ':' + this.rpcPort + '"));';
result += '\n}';
result += "\nweb3.eth.defaultAccount = web3.eth.accounts[0];";
}
return result; return result;
}; };
ABIGenerator.prototype.generateContracts = function(useEmbarkJS) { ABIGenerator.prototype.generateContracts = function(useEmbarkJS) {
var self = this;
var result = "\n"; var result = "\n";
for(var className in this.contractsManager.contracts) { var contractsPlugins = this.plugins.getPluginsFor('contractGeneration');
var contract = this.contractsManager.contracts[className];
var abi = JSON.stringify(contract.abiDefinition); if (contractsPlugins.length > 0) {
var gasEstimates = JSON.stringify(contract.gasEstimates); contractsPlugins.forEach(function(plugin) {
result += plugin.generateContracts({contracts: self.contractsManager.contracts});
});
} else {
for(var className in this.contractsManager.contracts) {
var contract = this.contractsManager.contracts[className];
if (useEmbarkJS) { var abi = JSON.stringify(contract.abiDefinition);
result += "\n" + className + " = new EmbarkJS.Contract({abi: " + abi + ", address: '" + contract.deployedAddress + "', code: '" + contract.code + "', gasEstimates: " + gasEstimates + "});"; var gasEstimates = JSON.stringify(contract.gasEstimates);
} else {
result += "\n" + className + "Abi = " + abi + ";"; if (useEmbarkJS) {
result += "\n" + className + "Contract = web3.eth.contract(" + className + "Abi);"; result += "\n" + className + " = new EmbarkJS.Contract({abi: " + abi + ", address: '" + contract.deployedAddress + "', code: '" + contract.code + "', gasEstimates: " + gasEstimates + "});";
result += "\n" + className + " = " + className + "Contract.at('" + contract.deployedAddress + "');"; } else {
result += "\n" + className + "Abi = " + abi + ";";
result += "\n" + className + "Contract = web3.eth.contract(" + className + "Abi);";
result += "\n" + className + " = " + className + "Contract.at('" + contract.deployedAddress + "');";
}
} }
} }

View File

@ -81,7 +81,7 @@ Deploy.prototype.checkAndDeployContract = function(contract, params, callback) {
// a callback // a callback
if (contract.onDeploy !== undefined) { if (contract.onDeploy !== undefined) {
self.logger.info('executing onDeploy commands'); self.logger.info('executing onDeploy commands');
var abiGenerator = new ABIGenerator({}, self.contractsManager); var abiGenerator = new ABIGenerator({contractsManager: self.contractsManager});
web3 = self.web3; web3 = self.web3;
var abi = abiGenerator.generateContracts(false); var abi = abiGenerator.generateContracts(false);
eval(abi); // jshint ignore:line eval(abi); // jshint ignore:line

View File

@ -6,6 +6,7 @@ var grunt = require('grunt');
var mkdirp = require('mkdirp'); var mkdirp = require('mkdirp');
var colors = require('colors'); var colors = require('colors');
var chokidar = require('chokidar'); var chokidar = require('chokidar');
var path = require('path');
var Cmd = require('./cmd.js'); var Cmd = require('./cmd.js');
var Deploy = require('./deploy.js'); var Deploy = require('./deploy.js');
@ -24,6 +25,7 @@ var ServicesMonitor = require('./services.js');
var Console = require('./console.js'); var Console = require('./console.js');
var IPFS = require('./ipfs.js'); var IPFS = require('./ipfs.js');
var Swarm = require('./swarm.js'); var Swarm = require('./swarm.js');
var Plugins = require('./plugins.js');
var Embark = { var Embark = {
@ -43,6 +45,8 @@ var Embark = {
this.config = new Config({env: env}); this.config = new Config({env: env});
this.config.loadConfigFiles(options); this.config.loadConfigFiles(options);
this.logger = new Logger({logLevel: 'debug'}); this.logger = new Logger({logLevel: 'debug'});
this.plugins = new Plugins({plugins: this.config.embarkConfig.plugins});
this.plugins.loadPlugins();
}, },
redeploy: function(env) { redeploy: function(env) {
@ -132,6 +136,13 @@ var Embark = {
Embark.redeploy(); Embark.redeploy();
}); });
callback(); callback();
},
function displayLoadedPlugins(callback) {
var pluginList = self.plugins.pluginList;
if (pluginList.length > 0) {
self.logger.info("loaded plugins: " + pluginList.join(", "));
}
callback();
} }
], function(err, result) { ], function(err, result) {
if (err) { if (err) {
@ -239,7 +250,7 @@ var Embark = {
}); });
}, },
function generateABI(contractsManager, callback) { function generateABI(contractsManager, callback) {
var abiGenerator = new ABIGenerator(self.config.blockchainConfig, contractsManager); var abiGenerator = new ABIGenerator({blockchainConfig: self.config.blockchainConfig, contractsManager: contractsManager, plugins: self.plugins});
callback(null, abiGenerator.generateABI({useEmbarkJS: true})); callback(null, abiGenerator.generateABI({useEmbarkJS: true}));
} }
], function(err, result) { ], function(err, result) {
@ -265,13 +276,13 @@ var Embark = {
}); });
}, },
function generateConsoleABI(contractsManager, callback) { function generateConsoleABI(contractsManager, callback) {
var abiGenerator = new ABIGenerator(self.config.blockchainConfig, contractsManager); var abiGenerator = new ABIGenerator({blockchainConfig: self.config.blockchainConfig, contractsManager: contractsManager});
var consoleABI = abiGenerator.generateABI({useEmbarkJS: false}); var consoleABI = abiGenerator.generateABI({useEmbarkJS: false});
Embark.console.runCode(consoleABI); Embark.console.runCode(consoleABI);
callback(null, contractsManager); callback(null, contractsManager);
}, },
function generateABI(contractsManager, callback) { function generateABI(contractsManager, callback) {
var abiGenerator = new ABIGenerator(self.config.blockchainConfig, contractsManager); var abiGenerator = new ABIGenerator({blockchainConfig: self.config.blockchainConfig, contractsManager: contractsManager, plugins: self.plugins});
callback(null, abiGenerator.generateABI({useEmbarkJS: true})); callback(null, abiGenerator.generateABI({useEmbarkJS: true}));
}, },
function buildPipeline(abi, callback) { function buildPipeline(abi, callback) {

42
lib/plugin.js Normal file
View File

@ -0,0 +1,42 @@
// TODO: pass other params like blockchainConfig, contract files, etc..
var Plugin = function(options) {
this.name = options.name;
this.pluginModule = options.pluginModule;
this.clientWeb3Providers = [];
this.contractsGenerators = [];
this.pluginTypes = [];
};
Plugin.prototype.loadPlugin = function() {
(this.pluginModule.call(this, this));
};
// TODO: add deploy provider
Plugin.prototype.registerClientWeb3Provider = function(cb) {
this.clientWeb3Providers.push(cb);
this.pluginTypes.push('clientWeb3Provider');
};
Plugin.prototype.registerContractsGeneration = function(cb) {
this.contractsGenerators.push(cb);
this.pluginTypes.push('contractGeneration');
};
Plugin.prototype.has = function(pluginType) {
return this.pluginTypes.indexOf(pluginType) >= 0;
};
Plugin.prototype.generateProvider = function(args) {
return this.clientWeb3Providers.map(function(cb) {
return cb.call(this, args);
}).join("\n");
};
Plugin.prototype.generateContracts = function(args) {
return this.contractsGenerators.map(function(cb) {
return cb.call(this, args);
}).join("\n");
};
module.exports = Plugin;

31
lib/plugins.js Normal file
View File

@ -0,0 +1,31 @@
var Plugin = require('./plugin.js');
var path = require('path');
var Plugins = function(options) {
this.pluginList = options.plugins || [];
this.plugins = [];
};
Plugins.prototype.loadPlugins = function() {
var i, plugin;
for (i = 0; i < this.pluginList.length; i++) {
plugin = this.pluginList[i];
this.loadPlugin(plugin);
}
};
Plugins.prototype.loadPlugin = function(pluginName) {
var plugin = require(path.join(process.env.PWD, 'node_modules', pluginName));
var pluginWrapper = new Plugin({name: pluginName, pluginModule: plugin});
pluginWrapper.loadPlugin();
this.plugins.push(pluginWrapper);
};
Plugins.prototype.getPluginsFor = function(pluginType) {
return this.plugins.filter(function(plugin) {
return plugin.has(pluginType);
});
};
module.exports = Plugins;

View File

@ -54,7 +54,7 @@ Test.prototype.deployAll = function(contractsConfig, cb) {
}); });
}, },
function generateABI(contractsManager, callback) { function generateABI(contractsManager, callback) {
var abiGenerator = new ABIGenerator({}, contractsManager); var abiGenerator = new ABIGenerator({contractsManager: contractsManager});
var ABI = abiGenerator.generateContracts(false); var ABI = abiGenerator.generateContracts(false);
callback(null, ABI); callback(null, ABI);
} }

View File

@ -7,7 +7,7 @@ var assert = require('assert');
describe('embark.ABIGenerator', function() { describe('embark.ABIGenerator', function() {
describe('#generateProvider', function() { describe('#generateProvider', function() {
var generator = new ABIGenerator({rpcHost: 'somehost', rpcPort: '1234'}, {}); var generator = new ABIGenerator({blockchainConfig: {rpcHost: 'somehost', rpcPort: '1234'}, contractsManager: {}});
it('should generate code to connect to a provider', function() { it('should generate code to connect to a provider', function() {
var providerCode = "\nif (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {\n\tweb3 = new Web3(web3.currentProvider);\n} else if (typeof Web3 !== 'undefined') {\n\tweb3 = new Web3(new Web3.providers.HttpProvider(\"http://somehost:1234\"));\n}\nweb3.eth.defaultAccount = web3.eth.accounts[0];"; var providerCode = "\nif (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {\n\tweb3 = new Web3(web3.currentProvider);\n} else if (typeof Web3 !== 'undefined') {\n\tweb3 = new Web3(new Web3.providers.HttpProvider(\"http://somehost:1234\"));\n}\nweb3.eth.defaultAccount = web3.eth.accounts[0];";
@ -17,7 +17,7 @@ describe('embark.ABIGenerator', function() {
}); });
describe('#generateContracts', function() { describe('#generateContracts', function() {
var generator = new ABIGenerator({}, { var generator = new ABIGenerator({blockchainConfig: {}, contractsManager: {
contracts: { contracts: {
SimpleStorage: { SimpleStorage: {
abiDefinition: [{"constant":true,"inputs":[],"name":"storedData","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"retVal","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"initialValue","type":"uint256"}],"type":"constructor"}], abiDefinition: [{"constant":true,"inputs":[],"name":"storedData","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"retVal","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"initialValue","type":"uint256"}],"type":"constructor"}],
@ -32,7 +32,7 @@ describe('embark.ABIGenerator', function() {
code: '123456' code: '123456'
} }
} }
}); }});
describe('with EmbarkJS', function() { describe('with EmbarkJS', function() {
var withEmbarkJS = true; var withEmbarkJS = true;