refactor write contracts

This commit is contained in:
Iuri Matias 2018-05-16 12:48:17 -04:00
parent d8a1894803
commit b7fc3f9054
3 changed files with 43 additions and 31 deletions

View File

@ -102,9 +102,8 @@ class CodeGenerator {
}); });
this.events.setCommandHandler('code-generator:contract', (contractName, cb) => { this.events.setCommandHandler('code-generator:contract', (contractName, cb) => {
self.events.request('contracts:contract', contractName, (contractJSON) => { let contract = self.contractsManager.contracts[contractName];
self.buildContractJS(contractName, contractJSON.toString(), cb); self.buildContractJS(contractName, self.generateContractJSON(contractName, contract), cb);
});
}); });
} }
@ -261,24 +260,28 @@ class CodeGenerator {
return result; return result;
} }
generateContractJSON(className, contract) {
let contractJSON = {};
contractJSON.contract_name = className;
contractJSON.address = contract.deployedAddress;
contractJSON.code = contract.code;
contractJSON.runtime_bytecode = contract.runtimeBytecode;
contractJSON.real_runtime_bytecode = contract.realRuntimeBytecode;
contractJSON.swarm_hash = contract.swarmHash;
contractJSON.gas_estimates = contract.gasEstimates;
contractJSON.function_hashes = contract.functionHashes;
contractJSON.abi = contract.abiDefinition;
return contractJSON;
}
generateContractsJSON() { generateContractsJSON() {
let contracts = {}; let contracts = {};
for (let className in this.contractsManager.contracts) { for (let className in this.contractsManager.contracts) {
let contract = this.contractsManager.contracts[className]; let contract = this.contractsManager.contracts[className];
let contractJSON = {}; contracts[className] = this.generateContractJSON(className, contract);
contractJSON.contract_name = className;
contractJSON.address = contract.deployedAddress;
contractJSON.code = contract.code;
contractJSON.runtime_bytecode = contract.runtimeBytecode;
contractJSON.real_runtime_bytecode = contract.realRuntimeBytecode;
contractJSON.swarm_hash = contract.swarmHash;
contractJSON.gas_estimates = contract.gasEstimates;
contractJSON.function_hashes = contract.functionHashes;
contractJSON.abi = contract.abiDefinition;
contracts[className] = contractJSON;
} }
return contracts; return contracts;
@ -334,7 +337,7 @@ class CodeGenerator {
let contractCode = ""; let contractCode = "";
contractCode += "import web3 from 'Embark/web3';\n"; contractCode += "import web3 from 'Embark/web3';\n";
contractCode += "import EmbarkJS from 'Embark/EmbarkJS';\n"; contractCode += "import EmbarkJS from 'Embark/EmbarkJS';\n";
contractCode += "let " + contractName + "JSONConfig = " + contractJSON + ";\n"; contractCode += "let " + contractName + "JSONConfig = " + JSON.stringify(contractJSON) + ";\n";
contractCode += "let " + contractName + " = new EmbarkJS.Contract(" + contractName + "JSONConfig);\n"; contractCode += "let " + contractName + " = new EmbarkJS.Contract(" + contractName + "JSONConfig);\n";
contractCode += "\n__embarkContext.execWhenReady(function() {\n"; contractCode += "\n__embarkContext.execWhenReady(function() {\n";
@ -343,7 +346,7 @@ class CodeGenerator {
contractCode += "\n});\n"; contractCode += "\n});\n";
contractCode += "export default " + contractName + ";\n"; contractCode += "export default " + contractName + ";\n";
cb(null, contractCode); cb(contractCode);
} }
buildWeb3JS(cb) { buildWeb3JS(cb) {

View File

@ -25,6 +25,11 @@ class ContractsManager {
this.events.on(constants.events.contractConfigChanged, (newContracts) => { this.events.on(constants.events.contractConfigChanged, (newContracts) => {
this.contractsConfig = newContracts; this.contractsConfig = newContracts;
}); });
const self = this;
this.events.setCommandHandler('contracts:list', (cb) => {
cb(self.listContracts());
});
} }
build(done) { build(done) {

View File

@ -28,7 +28,7 @@ class Pipeline {
async.waterfall([ async.waterfall([
function buildTheContracts(next) { function buildTheContracts(next) {
self.buildContracts(contractsJSON, next); self.buildContracts(next);
}, },
function buildWeb3(next) { function buildWeb3(next) {
self.buildWeb3JS(next); self.buildWeb3JS(next);
@ -45,13 +45,15 @@ class Pipeline {
next(); next();
}, },
function writeContracts(next) { function writeContracts(next) {
async.each(Object.keys(contractsJSON), (contractName, eachCb) => { self.events.request('contracts:list', (contracts) => {
self.events.request('code-generator:contract', contractName, (contractCode) => { async.each(contracts, (contract, eachCb) => {
let filePath = fs.dappPath(".embark", contractName + '.js'); self.events.request('code-generator:contract', contract.className, (contractCode) => {
importsList["Embark/contracts/" + contractName] = filePath; let filePath = fs.dappPath(".embark", contract.className + '.js');
fs.writeFile(filePath, contractCode, eachCb); importsList["Embark/contracts/" + contract.className] = filePath;
}); fs.writeFile(filePath, contractCode, eachCb);
}, next); });
}, next);
});
}, },
function assetFileWrite(next) { function assetFileWrite(next) {
async.eachOf(self.assetFiles, function (files, targetFile, cb) { async.eachOf(self.assetFiles, function (files, targetFile, cb) {
@ -176,15 +178,17 @@ class Pipeline {
); );
} }
buildContracts(contractsJSON, JSON, callback) { buildContracts(callback) {
const self = this;
fs.mkdirp(fs.dappPath(this.buildDir, 'contracts'), (err) => { fs.mkdirp(fs.dappPath(this.buildDir, 'contracts'), (err) => {
if (err) { if (err) {
return callback(err); return callback(err);
} }
async.each(Object.keys(contractsJSON), (className, eachCb) => { self.events.request('contracts:list', (contracts) => {
let contract = contractsJSON[className]; async.each(contracts, (contract, eachCb) => {
fs.writeJson(fs.dappPath(this.buildDir, 'contracts', className + ".json"), contract, {spaces: 2}, eachCb); fs.writeJson(fs.dappPath(this.buildDir, 'contracts', contract.className + ".json"), contract, {spaces: 2}, eachCb);
}, callback); }, callback);
});
}); });
} }