2015-06-28 02:20:07 +00:00
|
|
|
var web3 = require('web3');
|
|
|
|
var fs = require('fs');
|
|
|
|
var grunt = require('grunt');
|
|
|
|
var readYaml = require('read-yaml');
|
2015-06-28 13:51:58 +00:00
|
|
|
var Config = require('./config/config.js');
|
2015-08-01 22:04:22 +00:00
|
|
|
|
|
|
|
// Ugly, but sleep lib has issues on osx
|
|
|
|
sleep = function sleep(ms) {
|
|
|
|
var start = new Date().getTime();
|
|
|
|
while (new Date().getTime() < start + ms);
|
|
|
|
}
|
2015-06-28 02:20:07 +00:00
|
|
|
|
2015-07-03 08:59:33 +00:00
|
|
|
Deploy = function(env, contractFiles, blockchainConfig, contractsConfig) {
|
|
|
|
//this.blockchainConfig = (new Config.Blockchain()).loadConfigFile('config/blockchain.yml').config(env);
|
|
|
|
this.blockchainConfig = blockchainConfig;
|
2015-06-28 02:20:07 +00:00
|
|
|
|
2015-07-03 08:59:33 +00:00
|
|
|
//this.contractsManager = (new Config.Contracts(contractFiles, blockchainConfig)).loadConfigFile('config/contracts.yml');
|
|
|
|
this.contractsManager = contractsConfig;
|
|
|
|
this.contractsConfig = this.contractsManager.config(env);
|
|
|
|
this.deployedContracts = {};
|
2015-06-28 02:20:07 +00:00
|
|
|
|
|
|
|
try {
|
2015-07-03 08:59:33 +00:00
|
|
|
web3.setProvider(new web3.providers.HttpProvider("http://" + this.blockchainConfig.rpcHost + ":" + this.blockchainConfig.rpcPort));
|
2015-06-28 02:20:07 +00:00
|
|
|
primaryAddress = web3.eth.coinbase;
|
|
|
|
web3.eth.defaultAccount = primaryAddress;
|
2015-07-04 02:41:39 +00:00
|
|
|
} catch (e) {
|
|
|
|
throw new Error("==== can't connect to " + this.blockchainConfig.rpcHost + ":" + this.blockchainConfig.rpcPort + " check if an ethereum node is running");
|
2015-06-28 02:20:07 +00:00
|
|
|
}
|
|
|
|
|
2015-08-01 15:19:21 +00:00
|
|
|
console.log("primary account address is : " + primaryAddress);
|
2015-07-04 02:41:39 +00:00
|
|
|
};
|
2015-06-28 02:20:07 +00:00
|
|
|
|
2015-08-01 22:04:22 +00:00
|
|
|
Deploy.prototype.deploy_contract = function(contractObject, contractParams) {
|
|
|
|
var transactionHash = contractObject["new"].apply(contractObject, contractParams).transactionHash;
|
|
|
|
var receipt = null;
|
|
|
|
var time = 0;
|
|
|
|
while ((receipt = web3.eth.getTransactionReceipt(transactionHash)) === null || receipt.contractAddress === null) {
|
|
|
|
sleep(1000);
|
|
|
|
time += 1;
|
|
|
|
if (time >= 20) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return receipt;
|
|
|
|
}
|
|
|
|
|
2015-07-03 08:59:33 +00:00
|
|
|
Deploy.prototype.deploy_contracts = function(env) {
|
|
|
|
this.contractsManager.compileContracts(env);
|
|
|
|
all_contracts = this.contractsManager.all_contracts;
|
|
|
|
this.contractDB = this.contractsManager.contractDB;
|
|
|
|
contractDependencies = this.contractsManager.contractDependencies;
|
2015-06-28 02:20:07 +00:00
|
|
|
|
2015-07-03 08:59:33 +00:00
|
|
|
this.deployedContracts = {};
|
2015-06-28 02:20:07 +00:00
|
|
|
|
2015-07-03 08:59:33 +00:00
|
|
|
for (k = 0; k < all_contracts.length; k++) {
|
2015-06-28 02:20:07 +00:00
|
|
|
className = all_contracts[k];
|
2015-07-03 08:59:33 +00:00
|
|
|
contract = this.contractDB[className];
|
|
|
|
|
2015-07-06 12:19:25 +00:00
|
|
|
if (contract.address !== undefined) {
|
|
|
|
this.deployedContracts[className] = contract.address;
|
|
|
|
|
2015-07-09 11:31:37 +00:00
|
|
|
//console.log("contract " + className + " at " + contractAddress);
|
|
|
|
console.log("contract " + className + " at " + contract.address);
|
2015-06-28 02:20:07 +00:00
|
|
|
}
|
2015-07-06 12:19:25 +00:00
|
|
|
else {
|
|
|
|
contractObject = web3.eth.contract(contract.compiled.info.abiDefinition);
|
|
|
|
|
|
|
|
realArgs = [];
|
|
|
|
for (var l = 0; l < contract.args.length; l++) {
|
|
|
|
arg = contract.args[l];
|
|
|
|
if (arg[0] === "$") {
|
|
|
|
realArgs.push(this.deployedContracts[arg.substr(1)]);
|
|
|
|
} else {
|
|
|
|
realArgs.push(arg);
|
|
|
|
}
|
|
|
|
}
|
2015-07-03 08:59:33 +00:00
|
|
|
|
2015-07-06 12:19:25 +00:00
|
|
|
contractParams = realArgs;
|
|
|
|
contractParams.push({
|
|
|
|
from: primaryAddress,
|
|
|
|
data: contract.compiled.code,
|
|
|
|
gas: contract.gasLimit,
|
|
|
|
gasPrice: contract.gasPrice
|
|
|
|
});
|
2015-07-03 08:59:33 +00:00
|
|
|
|
2015-07-09 12:23:04 +00:00
|
|
|
console.log('trying to obtain ' + className + ' address...');
|
2015-08-01 22:04:22 +00:00
|
|
|
|
|
|
|
while((receipt = this.deploy_contract(contractObject, contractParams)) === false) {
|
|
|
|
console.log("timeout... failed to deploy contract.. retrying...");
|
2015-07-09 12:23:04 +00:00
|
|
|
}
|
2015-08-01 22:04:22 +00:00
|
|
|
|
2015-07-09 12:23:04 +00:00
|
|
|
var contractAddress = receipt.contractAddress;
|
2015-08-01 23:34:26 +00:00
|
|
|
|
|
|
|
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("=========");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log("deployed " + className + " at " + contractAddress);
|
|
|
|
}
|
|
|
|
|
2015-07-09 12:23:04 +00:00
|
|
|
this.deployedContracts[className] = contractAddress;
|
|
|
|
|
|
|
|
console.log("deployed " + className + " at " + contractAddress);
|
2015-07-09 11:31:37 +00:00
|
|
|
}
|
2015-07-03 08:59:33 +00:00
|
|
|
}
|
|
|
|
|
2015-07-04 02:41:39 +00:00
|
|
|
};
|
2015-07-03 08:59:33 +00:00
|
|
|
|
|
|
|
Deploy.prototype.generate_abi_file = function() {
|
|
|
|
var result;
|
|
|
|
|
|
|
|
result = "web3.setProvider(new web3.providers.HttpProvider('http://" + this.blockchainConfig.rpcHost + ":" + this.blockchainConfig.rpcPort + "'));";
|
|
|
|
result += "web3.eth.defaultAccount = web3.eth.accounts[0];";
|
|
|
|
|
|
|
|
for(className in this.deployedContracts) {
|
|
|
|
var deployedContract = this.deployedContracts[className];
|
|
|
|
var contract = this.contractDB[className];
|
|
|
|
|
2015-07-04 21:02:01 +00:00
|
|
|
var abi = JSON.stringify(contract.compiled.info.abiDefinition);
|
2015-07-09 12:23:04 +00:00
|
|
|
var contractAddress = deployedContract;
|
2015-07-09 11:43:53 +00:00
|
|
|
|
2015-07-09 08:49:04 +00:00
|
|
|
console.log('address is ' + contractAddress);
|
|
|
|
|
2015-07-07 10:51:03 +00:00
|
|
|
result += className + "Abi = " + abi + ";";
|
|
|
|
result += className + "Contract = web3.eth.contract(" + className + "Abi);";
|
|
|
|
result += className + " = " + className + "Contract.at('" + contractAddress + "');";
|
2015-06-28 02:20:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-03 08:59:33 +00:00
|
|
|
return result;
|
2015-07-04 02:41:39 +00:00
|
|
|
};
|
2015-06-28 02:20:07 +00:00
|
|
|
|
2015-07-03 08:59:33 +00:00
|
|
|
Deploy.prototype.generate_and_write_abi_file = function(destFile) {
|
|
|
|
var result = this.generate_abi_file();
|
|
|
|
grunt.file.write(destFile, result);
|
2015-07-04 02:41:39 +00:00
|
|
|
};
|
2015-06-28 02:20:07 +00:00
|
|
|
|
2015-07-04 02:41:39 +00:00
|
|
|
module.exports = Deploy;
|
2015-06-28 02:20:07 +00:00
|
|
|
|