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) => {
self.events.request('contracts:contract', contractName, (contractJSON) => {
self.buildContractJS(contractName, contractJSON.toString(), cb);
});
let contract = self.contractsManager.contracts[contractName];
self.buildContractJS(contractName, self.generateContractJSON(contractName, contract), cb);
});
}
@ -261,24 +260,28 @@ class CodeGenerator {
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() {
let contracts = {};
for (let className in this.contractsManager.contracts) {
let contract = this.contractsManager.contracts[className];
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;
contracts[className] = contractJSON;
contracts[className] = this.generateContractJSON(className, contract);
}
return contracts;
@ -334,7 +337,7 @@ class CodeGenerator {
let contractCode = "";
contractCode += "import web3 from 'Embark/web3';\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 += "\n__embarkContext.execWhenReady(function() {\n";
@ -343,7 +346,7 @@ class CodeGenerator {
contractCode += "\n});\n";
contractCode += "export default " + contractName + ";\n";
cb(null, contractCode);
cb(contractCode);
}
buildWeb3JS(cb) {

View File

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

View File

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