Skip to content

Commit 3757e41

Browse files
committed
support deploy commands on contracts config
1 parent 834b259 commit 3757e41

File tree

6 files changed

+87
-0
lines changed

6 files changed

+87
-0
lines changed

lib/config/contracts.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ ContractsConfig.prototype.compileContracts = function(env) {
122122
contract.gasLimit = contractConfig.gas_limit || contract.gasLimit;
123123
contract.args = contractConfig.args || [];
124124
contract.address = contractConfig.address;
125+
contract.onDeploy = contractConfig.onDeploy || [];
125126

126127
if (contractConfig.instanceOf !== undefined) {
127128
contract.types.push('instance');

lib/deploy.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,27 @@ Deploy.prototype.deploy_contracts = function(env) {
104104
this.deployedContracts[className] = contractAddress;
105105

106106
console.log("deployed " + className + " at " + contractAddress);
107+
this.execute_cmds(contract.onDeploy);
107108
}
108109
}
109110

110111
};
111112

113+
Deploy.prototype.execute_cmds = function(cmds) {
114+
if (cmds.length === 0) return;
115+
116+
eval(this.generate_abi_file());
117+
for (var i = 0; i < cmds.length; i++) {
118+
var cmd = cmds[i];
119+
120+
// need to initialize all variables of deployed contracts making them
121+
// available to deployment
122+
123+
console.log("executing: " + cmd);
124+
eval(cmd);
125+
}
126+
}
127+
112128
Deploy.prototype.generate_abi_file = function() {
113129
var result;
114130

test/deploy.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var Config = require('../lib/config/config.js');
22
var Deploy = require('../lib/deploy.js');
33
var Compiler = require('../lib/compiler.js');
44
var assert = require('assert');
5+
var web3 = require('web3');
56

67
setDeployConfig = function(config) {
78
var _blockchainConfig = (new Config.Blockchain()).loadConfigFile(config.blockchain);
@@ -119,6 +120,45 @@ describe('embark.deploy', function() {
119120

120121
});
121122

123+
describe('contracts deploy script', function() {
124+
var files = [
125+
'test/support/contracts/data_source.sol',
126+
'test/support/contracts/manager.sol'
127+
];
128+
129+
describe('#deploy_contracts', function() {
130+
var deploy = setDeployConfig({
131+
files: files,
132+
blockchain: 'test/support/blockchain.yml',
133+
contracts: 'test/support/arguments3.yml'
134+
});
135+
deploy.deploy_contracts("development");
136+
137+
it("should deploy contracts", function() {
138+
var all_contracts = ['DataSource', 'Manager'];
139+
for(var i=0; i < all_contracts.length; i++) {
140+
var className = all_contracts[i];
141+
142+
assert.equal(deploy.deployedContracts.hasOwnProperty(className), true);
143+
}
144+
});
145+
146+
it("should execute deploy changes", function() {
147+
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8101'));
148+
web3.eth.defaultAccount = web3.eth.accounts[0];
149+
150+
data_source_abi = deploy.contractDB['DataSource'].compiled.info.abiDefinition;
151+
data_source_address = deploy.deployedContracts['DataSource'];
152+
153+
DataSource = web3.eth.contract(data_source_abi).at(data_source_address);
154+
155+
assert.equal(DataSource.storeData().toNumber(), 5);
156+
});
157+
158+
});
159+
160+
});
161+
122162
describe('contracts with addresses defined', function() {
123163
var files = [
124164
'test/support/contracts/simple_storage.sol'

test/support/arguments3.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
development:
2+
DataSource:
3+
args:
4+
Manager:
5+
stubs:
6+
- DataSource
7+
args:
8+
- $DataSource
9+
onDeploy:
10+
- DataSource.set(5)
11+
staging:
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
contract DataSource {
2+
uint public storeData;
3+
4+
function DataSource() {
5+
}
6+
7+
function set(uint num) {
8+
storeData = num;
9+
}
10+
11+
}

test/support/contracts/manager.sol

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
contract Manager {
2+
address data;
3+
4+
function Manager(address dataAddress) {
5+
data = dataAddress;
6+
}
7+
8+
}

0 commit comments

Comments
 (0)