diff --git a/lib/config/contracts.js b/lib/config/contracts.js index d95b45770..5a42e00ac 100644 --- a/lib/config/contracts.js +++ b/lib/config/contracts.js @@ -122,6 +122,7 @@ ContractsConfig.prototype.compileContracts = function(env) { contract.gasLimit = contractConfig.gas_limit || contract.gasLimit; contract.args = contractConfig.args || []; contract.address = contractConfig.address; + contract.onDeploy = contractConfig.onDeploy || []; if (contractConfig.instanceOf !== undefined) { contract.types.push('instance'); diff --git a/lib/deploy.js b/lib/deploy.js index 158421bd4..3650cd0a5 100644 --- a/lib/deploy.js +++ b/lib/deploy.js @@ -104,11 +104,27 @@ Deploy.prototype.deploy_contracts = function(env) { this.deployedContracts[className] = contractAddress; console.log("deployed " + className + " at " + contractAddress); + this.execute_cmds(contract.onDeploy); } } }; +Deploy.prototype.execute_cmds = function(cmds) { + if (cmds.length === 0) return; + + eval(this.generate_abi_file()); + for (var i = 0; i < cmds.length; i++) { + var cmd = cmds[i]; + + // need to initialize all variables of deployed contracts making them + // available to deployment + + console.log("executing: " + cmd); + eval(cmd); + } +} + Deploy.prototype.generate_abi_file = function() { var result; diff --git a/test/deploy.js b/test/deploy.js index 4673dcf95..e0655bbd2 100644 --- a/test/deploy.js +++ b/test/deploy.js @@ -2,6 +2,7 @@ var Config = require('../lib/config/config.js'); var Deploy = require('../lib/deploy.js'); var Compiler = require('../lib/compiler.js'); var assert = require('assert'); +var web3 = require('web3'); setDeployConfig = function(config) { var _blockchainConfig = (new Config.Blockchain()).loadConfigFile(config.blockchain); @@ -119,6 +120,45 @@ describe('embark.deploy', function() { }); + describe('contracts deploy script', function() { + var files = [ + 'test/support/contracts/data_source.sol', + 'test/support/contracts/manager.sol' + ]; + + describe('#deploy_contracts', function() { + var deploy = setDeployConfig({ + files: files, + blockchain: 'test/support/blockchain.yml', + contracts: 'test/support/arguments3.yml' + }); + deploy.deploy_contracts("development"); + + it("should deploy contracts", function() { + var all_contracts = ['DataSource', 'Manager']; + for(var i=0; i < all_contracts.length; i++) { + var className = all_contracts[i]; + + assert.equal(deploy.deployedContracts.hasOwnProperty(className), true); + } + }); + + it("should execute deploy changes", function() { + web3.setProvider(new web3.providers.HttpProvider('http://localhost:8101')); + web3.eth.defaultAccount = web3.eth.accounts[0]; + + data_source_abi = deploy.contractDB['DataSource'].compiled.info.abiDefinition; + data_source_address = deploy.deployedContracts['DataSource']; + + DataSource = web3.eth.contract(data_source_abi).at(data_source_address); + + assert.equal(DataSource.storeData().toNumber(), 5); + }); + + }); + + }); + describe('contracts with addresses defined', function() { var files = [ 'test/support/contracts/simple_storage.sol' diff --git a/test/support/arguments3.yml b/test/support/arguments3.yml new file mode 100644 index 000000000..9ade4a5ce --- /dev/null +++ b/test/support/arguments3.yml @@ -0,0 +1,11 @@ +development: + DataSource: + args: + Manager: + stubs: + - DataSource + args: + - $DataSource + onDeploy: + - DataSource.set(5) +staging: diff --git a/test/support/contracts/data_source.sol b/test/support/contracts/data_source.sol new file mode 100644 index 000000000..90b0c7843 --- /dev/null +++ b/test/support/contracts/data_source.sol @@ -0,0 +1,11 @@ +contract DataSource { + uint public storeData; + + function DataSource() { + } + + function set(uint num) { + storeData = num; + } + +} diff --git a/test/support/contracts/manager.sol b/test/support/contracts/manager.sol new file mode 100644 index 000000000..78e390ba2 --- /dev/null +++ b/test/support/contracts/manager.sol @@ -0,0 +1,8 @@ +contract Manager { + address data; + + function Manager(address dataAddress) { + data = dataAddress; + } + +}