Skip to content

Commit 60d860f

Browse files
committed
support deploying a single contract with custom arguments in the tests
1 parent cf3cfe5 commit 60d860f

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

lib/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var grunt = require('grunt');
99
//var Tests = require('./test.js');
1010
var Blockchain = require('./blockchain.js');
1111
var Deploy = require('./deploy.js');
12+
var SingleDeploy = require('./single_deploy.js');
1213
var Release = require('./ipfs.js');
1314
var Config = require('./config/config.js');
1415
var Compiler = require('./compiler.js');
@@ -65,6 +66,17 @@ Embark = {
6566
chain.execGeth(args);
6667
},
6768

69+
deployContract: function(contractFiles, className, args, cb) {
70+
var compiledContracts = this.compiler.compile_solidity(contractFiles);
71+
72+
var deploy = new SingleDeploy(compiledContracts, this.web3);
73+
deploy.deploy_a_contract(className, args, function() {
74+
var result = "";
75+
result += deploy.generate_abi_file();
76+
cb(result);
77+
});
78+
},
79+
6880
release: Release,
6981

7082
initTests: function() {

lib/single_deploy.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
var web3 = require('web3');
2+
var fs = require('fs');
3+
var grunt = require('grunt');
4+
var readYaml = require('read-yaml');
5+
var Compiler = require('./compiler.js');
6+
var Config = require('./config/config.js');
7+
var BigNumber = require('bignumber.js');
8+
9+
// this is a temporary module to deploy a single contract, will be refactored
10+
11+
SingleDeploy = function(compiledContracts, _web3) {
12+
if (_web3 !== undefined) {
13+
web3 = _web3;
14+
}
15+
this.compiledContracts = compiledContracts;
16+
};
17+
18+
SingleDeploy.waitForContract = function(transactionHash, cb) {
19+
web3.eth.getTransactionReceipt(transactionHash, function(e, receipt) {
20+
if (!e && receipt && receipt.contractAddress !== undefined) {
21+
cb(receipt.contractAddress);
22+
}
23+
else {
24+
Deploy.waitForContract(transactionHash, cb);
25+
}
26+
});
27+
};
28+
29+
SingleDeploy.prototype.deploy_contract = function(contractObject, contractParams, cb) {
30+
var callback = function(e, contract) {
31+
if(!e && contract.address !== undefined) {
32+
cb(contract.address);
33+
}
34+
else {
35+
Deploy.waitForContract(contract.transactionHash, cb);
36+
}
37+
};
38+
39+
contractParams.push(callback);
40+
contractObject["new"].apply(contractObject, contractParams);
41+
};
42+
43+
Deploy.prototype.deploy_a_contract = function(className, args, cb) {
44+
var contract = this.compiledContracts[className];
45+
var contractObject = web3.eth.contract(contract.compiled.info.abiDefinition);
46+
47+
contractParams = args.slice();
48+
contractParams.push({
49+
from: primaryAddress,
50+
data: contract.compiled.code,
51+
gas: contract.gasLimit,
52+
gasPrice: contract.gasPrice
53+
});
54+
55+
console.log('trying to obtain ' + className + ' address...');
56+
57+
this.deploy_contract(contractObject, contractParams, function(contractAddress) {
58+
if (web3.eth.getCode(contractAddress) === "0x") {
59+
console.log("=========");
60+
console.log("contract was deployed at " + contractAddress + " but doesn't seem to be working");
61+
console.log("try adjusting your gas values");
62+
console.log("=========");
63+
}
64+
65+
cb();
66+
});
67+
68+
};
69+
70+
SingleDeploy.prototype.generate_provider_file = function() {
71+
var result = "";
72+
73+
result += "if (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {";
74+
result += 'web3 = new Web3(web3.currentProvider);';
75+
result += "} else if (typeof Web3 !== 'undefined') {";
76+
result += 'web3 = new Web3(new Web3.providers.HttpProvider("http://' + this.blockchainConfig.rpcHost + ':' + this.blockchainConfig.rpcPort + '"));';
77+
result += '}';
78+
result += "web3.eth.defaultAccount = web3.eth.accounts[0];";
79+
80+
return result;
81+
};
82+
83+
SingleDeploy.prototype.generate_abi_file = function() {
84+
var result = "";
85+
86+
result += 'blockchain = '+JSON.stringify(this.blockchainConfig)+';';
87+
88+
for(className in this.deployedContracts) {
89+
var deployedContract = this.deployedContracts[className];
90+
var contract = this.contractDB[className];
91+
92+
var abi = JSON.stringify(contract.compiled.info.abiDefinition);
93+
var contractAddress = deployedContract;
94+
95+
console.log('address is ' + contractAddress);
96+
97+
result += className + "Abi = " + abi + ";";
98+
result += className + "Contract = web3.eth.contract(" + className + "Abi);";
99+
result += className + " = " + className + "Contract.at('" + contractAddress + "');";
100+
}
101+
result += 'contractDB = '+JSON.stringify(this.contractDB)+';'
102+
103+
return result;
104+
};
105+
106+
SingleDeploy.prototype.generate_and_write_abi_file = function(destFile) {
107+
var result = this.generate_abi_file();
108+
grunt.file.write(destFile, result);
109+
};
110+
111+
module.exports = SingleDeploy;

lib/test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,13 @@ Test.prototype.deployAll = function(cb) {
3434
});
3535
}
3636

37+
Test.prototype.deployContract = function(className, args, cb) {
38+
var web3 = this.web3;
39+
Embark.deployContract(this.contractFiles, className, args, function(abi) {
40+
eval(abi);
41+
cb();
42+
});
43+
};
44+
3745
module.exports = Test;
3846

0 commit comments

Comments
 (0)