support deploy commands on contracts config

This commit is contained in:
Iuri Matias 2015-08-02 19:43:37 -04:00
parent 834b25908b
commit 3757e41ea0
6 changed files with 87 additions and 0 deletions

View File

@ -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');

View File

@ -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;

View File

@ -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'

View File

@ -0,0 +1,11 @@
development:
DataSource:
args:
Manager:
stubs:
- DataSource
args:
- $DataSource
onDeploy:
- DataSource.set(5)
staging:

View File

@ -0,0 +1,11 @@
contract DataSource {
uint public storeData;
function DataSource() {
}
function set(uint num) {
storeData = num;
}
}

View File

@ -0,0 +1,8 @@
contract Manager {
address data;
function Manager(address dataAddress) {
data = dataAddress;
}
}