From 47f313b12c9a5f428986f52355e4b809159a6191 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 4 Apr 2017 06:37:50 -0400 Subject: [PATCH] generate json files --- lib/contracts/abi.js | 25 +++++++++++++++++++++++++ lib/contracts/deploy_tracker.js | 2 +- lib/core/engine.js | 14 ++++++++------ lib/pipeline/pipeline.js | 13 ++++++++++++- 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/lib/contracts/abi.js b/lib/contracts/abi.js index b167f618..b8b29016 100644 --- a/lib/contracts/abi.js +++ b/lib/contracts/abi.js @@ -113,6 +113,31 @@ class ABIGenerator { return result; } + + generateContractsJSON() { + let contracts = {}; + + for (let className in this.contractsManager.contracts) { + let contract = this.contractsManager.contracts[className]; + let contractJSON = {}; + + let abi = JSON.stringify(contract.abiDefinition); + let gasEstimates = JSON.stringify(contract.gasEstimates); + + contractJSON.contract_name = className; + 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; + } } module.exports = ABIGenerator; diff --git a/lib/contracts/deploy_tracker.js b/lib/contracts/deploy_tracker.js index c7168b0e..38874c11 100644 --- a/lib/contracts/deploy_tracker.js +++ b/lib/contracts/deploy_tracker.js @@ -54,7 +54,7 @@ class DeployTracker { if (this.chainConfig === false) { return; } - fs.writeJSONSync("./chains.json", this.chainConfig); + fs.writeJSONSync("./chains.json", this.chainConfig, {spaces: 2}); } } diff --git a/lib/core/engine.js b/lib/core/engine.js index 2edc10a4..aecee563 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -80,9 +80,10 @@ class Engine { logger: this.logger, plugins: this.plugins }); - this.events.on('abi', function (abi) { + this.events.on('abi', function (abi, contractsJSON) { self.currentAbi = abi; - pipeline.build(abi); + self.contractsJSON = contractsJSON; + pipeline.build(abi, contractsJSON); self.events.emit('outputDone'); }); // TODO: still need to redeploy contracts because the original contracts @@ -90,7 +91,7 @@ class Engine { //this.events.on('file-event', function(fileType, path) { // if (fileType === 'asset') { // self.config.reloadConfig(); - // pipeline.build(self.abi, path); + // pipeline.build(self.abi, self.contractsJSON, path); // self.events.emit('outputDone'); // } //}); @@ -109,10 +110,11 @@ class Engine { let embarkJSABI = abiGenerator.generateABI({useEmbarkJS: true}); let vanillaABI = abiGenerator.generateABI({useEmbarkJS: false}); let vanillaContractsABI = abiGenerator.generateContracts(false); + let contractsJSON = abiGenerator.generateContractsJSON(); - self.events.emit('abi-contracts-vanila', vanillaContractsABI); - self.events.emit('abi-vanila', vanillaABI); - self.events.emit('abi', embarkJSABI); + self.events.emit('abi-contracts-vanila', vanillaContractsABI, contractsJSON); + self.events.emit('abi-vanila', vanillaABI, contractsJSON); + self.events.emit('abi', embarkJSABI, contractsJSON); }; this.events.on('contractsDeployed', generateABICode); this.events.on('blockchainDisabled', generateABICode); diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index 2a91e210..d5af6cfb 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -11,7 +11,7 @@ class Pipeline { this.plugins = options.plugins; } - build(abi, path) { + build(abi, contractsJSON, path) { let self = this; for (let targetFile in this.assetFiles) { @@ -76,6 +76,17 @@ class Pipeline { fs.writeFileSync(this.buildDir + targetFile, content); } } + + this.buildContracts(contractsJSON); + } + + buildContracts(contractsJSON) { + fs.mkdirpSync(this.buildDir + 'contracts'); + + for (let className in contractsJSON) { + let contract = contractsJSON[className]; + fs.writeJSONSync(this.buildDir + 'contracts/' + className + ".json", contract, {spaces: 2}); + } } }