From b7fc3f9054a197f499eb869cd5a226d345715348 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 16 May 2018 12:48:17 -0400 Subject: [PATCH] refactor write contracts --- lib/contracts/code_generator.js | 39 ++++++++++++++++++--------------- lib/contracts/contracts.js | 5 +++++ lib/pipeline/pipeline.js | 30 ++++++++++++++----------- 3 files changed, 43 insertions(+), 31 deletions(-) diff --git a/lib/contracts/code_generator.js b/lib/contracts/code_generator.js index 0c70a38b1..2a4e80190 100644 --- a/lib/contracts/code_generator.js +++ b/lib/contracts/code_generator.js @@ -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) { diff --git a/lib/contracts/contracts.js b/lib/contracts/contracts.js index 5b8a9329f..81b5ed37b 100644 --- a/lib/contracts/contracts.js +++ b/lib/contracts/contracts.js @@ -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) { diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index 3bdb27359..2239f0eff 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -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); + }); }); }