support deploying a single contract with custom arguments in the tests
This commit is contained in:
parent
cf3cfe51ca
commit
60d860f27a
12
lib/index.js
12
lib/index.js
|
@ -9,6 +9,7 @@ var grunt = require('grunt');
|
||||||
//var Tests = require('./test.js');
|
//var Tests = require('./test.js');
|
||||||
var Blockchain = require('./blockchain.js');
|
var Blockchain = require('./blockchain.js');
|
||||||
var Deploy = require('./deploy.js');
|
var Deploy = require('./deploy.js');
|
||||||
|
var SingleDeploy = require('./single_deploy.js');
|
||||||
var Release = require('./ipfs.js');
|
var Release = require('./ipfs.js');
|
||||||
var Config = require('./config/config.js');
|
var Config = require('./config/config.js');
|
||||||
var Compiler = require('./compiler.js');
|
var Compiler = require('./compiler.js');
|
||||||
|
@ -65,6 +66,17 @@ Embark = {
|
||||||
chain.execGeth(args);
|
chain.execGeth(args);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
deployContract: function(contractFiles, className, args, cb) {
|
||||||
|
var compiledContracts = this.compiler.compile_solidity(contractFiles);
|
||||||
|
|
||||||
|
var deploy = new SingleDeploy(compiledContracts, this.web3);
|
||||||
|
deploy.deploy_a_contract(className, args, function() {
|
||||||
|
var result = "";
|
||||||
|
result += deploy.generate_abi_file();
|
||||||
|
cb(result);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
release: Release,
|
release: Release,
|
||||||
|
|
||||||
initTests: function() {
|
initTests: function() {
|
||||||
|
|
|
@ -0,0 +1,111 @@
|
||||||
|
var web3 = require('web3');
|
||||||
|
var fs = require('fs');
|
||||||
|
var grunt = require('grunt');
|
||||||
|
var readYaml = require('read-yaml');
|
||||||
|
var Compiler = require('./compiler.js');
|
||||||
|
var Config = require('./config/config.js');
|
||||||
|
var BigNumber = require('bignumber.js');
|
||||||
|
|
||||||
|
// this is a temporary module to deploy a single contract, will be refactored
|
||||||
|
|
||||||
|
SingleDeploy = function(compiledContracts, _web3) {
|
||||||
|
if (_web3 !== undefined) {
|
||||||
|
web3 = _web3;
|
||||||
|
}
|
||||||
|
this.compiledContracts = compiledContracts;
|
||||||
|
};
|
||||||
|
|
||||||
|
SingleDeploy.waitForContract = function(transactionHash, cb) {
|
||||||
|
web3.eth.getTransactionReceipt(transactionHash, function(e, receipt) {
|
||||||
|
if (!e && receipt && receipt.contractAddress !== undefined) {
|
||||||
|
cb(receipt.contractAddress);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Deploy.waitForContract(transactionHash, cb);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
SingleDeploy.prototype.deploy_contract = function(contractObject, contractParams, cb) {
|
||||||
|
var callback = function(e, contract) {
|
||||||
|
if(!e && contract.address !== undefined) {
|
||||||
|
cb(contract.address);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Deploy.waitForContract(contract.transactionHash, cb);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
contractParams.push(callback);
|
||||||
|
contractObject["new"].apply(contractObject, contractParams);
|
||||||
|
};
|
||||||
|
|
||||||
|
Deploy.prototype.deploy_a_contract = function(className, args, cb) {
|
||||||
|
var contract = this.compiledContracts[className];
|
||||||
|
var contractObject = web3.eth.contract(contract.compiled.info.abiDefinition);
|
||||||
|
|
||||||
|
contractParams = args.slice();
|
||||||
|
contractParams.push({
|
||||||
|
from: primaryAddress,
|
||||||
|
data: contract.compiled.code,
|
||||||
|
gas: contract.gasLimit,
|
||||||
|
gasPrice: contract.gasPrice
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('trying to obtain ' + className + ' address...');
|
||||||
|
|
||||||
|
this.deploy_contract(contractObject, contractParams, function(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("=========");
|
||||||
|
}
|
||||||
|
|
||||||
|
cb();
|
||||||
|
});
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
SingleDeploy.prototype.generate_provider_file = function() {
|
||||||
|
var result = "";
|
||||||
|
|
||||||
|
result += "if (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {";
|
||||||
|
result += 'web3 = new Web3(web3.currentProvider);';
|
||||||
|
result += "} else if (typeof Web3 !== 'undefined') {";
|
||||||
|
result += 'web3 = new Web3(new Web3.providers.HttpProvider("http://' + this.blockchainConfig.rpcHost + ':' + this.blockchainConfig.rpcPort + '"));';
|
||||||
|
result += '}';
|
||||||
|
result += "web3.eth.defaultAccount = web3.eth.accounts[0];";
|
||||||
|
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
SingleDeploy.prototype.generate_abi_file = function() {
|
||||||
|
var result = "";
|
||||||
|
|
||||||
|
result += 'blockchain = '+JSON.stringify(this.blockchainConfig)+';';
|
||||||
|
|
||||||
|
for(className in this.deployedContracts) {
|
||||||
|
var deployedContract = this.deployedContracts[className];
|
||||||
|
var contract = this.contractDB[className];
|
||||||
|
|
||||||
|
var abi = JSON.stringify(contract.compiled.info.abiDefinition);
|
||||||
|
var contractAddress = deployedContract;
|
||||||
|
|
||||||
|
console.log('address is ' + contractAddress);
|
||||||
|
|
||||||
|
result += className + "Abi = " + abi + ";";
|
||||||
|
result += className + "Contract = web3.eth.contract(" + className + "Abi);";
|
||||||
|
result += className + " = " + className + "Contract.at('" + contractAddress + "');";
|
||||||
|
}
|
||||||
|
result += 'contractDB = '+JSON.stringify(this.contractDB)+';'
|
||||||
|
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
SingleDeploy.prototype.generate_and_write_abi_file = function(destFile) {
|
||||||
|
var result = this.generate_abi_file();
|
||||||
|
grunt.file.write(destFile, result);
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = SingleDeploy;
|
|
@ -34,5 +34,13 @@ Test.prototype.deployAll = function(cb) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Test.prototype.deployContract = function(className, args, cb) {
|
||||||
|
var web3 = this.web3;
|
||||||
|
Embark.deployContract(this.contractFiles, className, args, function(abi) {
|
||||||
|
eval(abi);
|
||||||
|
cb();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = Test;
|
module.exports = Test;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue