From 854475668460b7d0b57cbad7a8f672288bed2220 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 4 Aug 2015 08:18:04 -0400 Subject: [PATCH] manage chain when deploying contracts --- boilerplate/chains.json | 1 + lib/deploy.js | 85 +++++++++++++++++++++++------------------ lib/index.js | 1 + 3 files changed, 50 insertions(+), 37 deletions(-) create mode 100644 boilerplate/chains.json diff --git a/boilerplate/chains.json b/boilerplate/chains.json new file mode 100644 index 00000000..0967ef42 --- /dev/null +++ b/boilerplate/chains.json @@ -0,0 +1 @@ +{} diff --git a/lib/deploy.js b/lib/deploy.js index 53ddb644..81fcf08a 100644 --- a/lib/deploy.js +++ b/lib/deploy.js @@ -58,6 +58,7 @@ Deploy.prototype.deploy_contracts = function(env) { className = all_contracts[k]; contract = this.contractDB[className]; + if (contract.address !== undefined) { this.deployedContracts[className] = contract.address; @@ -65,48 +66,58 @@ Deploy.prototype.deploy_contracts = function(env) { console.log("contract " + className + " at " + contract.address); } else { - contractObject = web3.eth.contract(contract.compiled.info.abiDefinition); + var chainContract = this.chainManager.getContract(contract.compiled.code); - realArgs = []; - for (var l = 0; l < contract.args.length; l++) { - arg = contract.args[l]; - if (arg[0] === "$") { - realArgs.push(this.deployedContracts[arg.substr(1)]); - } else { - realArgs.push(arg); - } - } - - contractParams = realArgs; - contractParams.push({ - from: primaryAddress, - data: contract.compiled.code, - gas: contract.gasLimit, - gasPrice: contract.gasPrice - }); - - console.log('trying to obtain ' + className + ' address...'); - - while((receipt = this.deploy_contract(contractObject, contractParams)) === false) { - console.log("timeout... failed to deploy contract.. retrying..."); - } - - var contractAddress = receipt.contractAddress; - - if (web3.eth.getCode(contractAddress) === "0x") { - console.log("========="); - console.log("contract was deployed at " + contractAddress + " but doesn't seem to be working"); - console.log("try adjusting your gas values"); - console.log("========="); + if (chainContract != undefined) { + console.log("contract " + className + " is unchanged and already deployed at " + chainContract.address); } else { + + contractObject = web3.eth.contract(contract.compiled.info.abiDefinition); + + realArgs = []; + for (var l = 0; l < contract.args.length; l++) { + arg = contract.args[l]; + if (arg[0] === "$") { + realArgs.push(this.deployedContracts[arg.substr(1)]); + } else { + realArgs.push(arg); + } + } + + contractParams = realArgs; + contractParams.push({ + from: primaryAddress, + data: contract.compiled.code, + gas: contract.gasLimit, + gasPrice: contract.gasPrice + }); + + console.log('trying to obtain ' + className + ' address...'); + + while((receipt = this.deploy_contract(contractObject, contractParams)) === false) { + console.log("timeout... failed to deploy contract.. retrying..."); + } + + var contractAddress = receipt.contractAddress; + + if (web3.eth.getCode(contractAddress) === "0x") { + console.log("========="); + console.log("contract was deployed at " + contractAddress + " but doesn't seem to be working"); + console.log("try adjusting your gas values"); + console.log("========="); + } + else { + console.log("deployed " + className + " at " + contractAddress); + } + + this.deployedContracts[className] = contractAddress; + this.chainManager.addContract(className, contract.compiled.code, contractAddress); + this.chainManager.save(); + console.log("deployed " + className + " at " + contractAddress); + this.execute_cmds(contract.onDeploy); } - - this.deployedContracts[className] = contractAddress; - - console.log("deployed " + className + " at " + contractAddress); - this.execute_cmds(contract.onDeploy); } } diff --git a/lib/index.js b/lib/index.js index b6ce5cc1..95618130 100644 --- a/lib/index.js +++ b/lib/index.js @@ -16,6 +16,7 @@ var Deploy = require('./deploy.js'); var Release = require('./ipfs.js'); var Config = require('./config/config.js'); var Compiler = require('./compiler.js'); +var ChainManager = require('./chain_manager.js'); Embark = { init: function() {