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-06-28 02:20:07 +00:00
|
|
|
|
|
|
|
deployContracts = function(env, contractFiles, destFile) {
|
|
|
|
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
|
|
|
|
|
2015-06-28 13:51:58 +00:00
|
|
|
blockchainConfig = (new Config.Blockchain()).loadConfigFile('config/blockchain.yml').config(env);
|
2015-06-28 02:20:07 +00:00
|
|
|
|
2015-06-28 20:11:42 +00:00
|
|
|
contractsManager = (new Config.Contracts(contractFiles, blockchainConfig)).loadConfigFile('config/contracts.yml');
|
|
|
|
contractsConfig = contractsManager.config(env);
|
2015-06-28 02:20:07 +00:00
|
|
|
|
|
|
|
try {
|
2015-06-28 13:51:58 +00:00
|
|
|
web3.setProvider(new web3.providers.HttpProvider("http://" + blockchainConfig.rpcHost + ":" + blockchainConfig.rpcPort));
|
2015-06-28 02:20:07 +00:00
|
|
|
primaryAddress = web3.eth.coinbase;
|
|
|
|
web3.eth.defaultAccount = primaryAddress;
|
|
|
|
} catch (_error) {
|
|
|
|
e = _error;
|
2015-06-28 13:51:58 +00:00
|
|
|
console.log("==== can't connect to " + blockchainConfig.rpcHost + ":" + blockchainConfig.rpcPort + " check if an ethereum node is running");
|
2015-06-28 02:20:07 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("address is : " + primaryAddress);
|
|
|
|
|
2015-06-28 13:51:58 +00:00
|
|
|
result = "web3.setProvider(new web3.providers.HttpProvider('http://" + blockchainConfig.rpcHost + ":" + blockchainConfig.rpcPort + "'));";
|
2015-06-28 02:20:07 +00:00
|
|
|
result += "web3.eth.defaultAccount = web3.eth.accounts[0];";
|
|
|
|
|
2015-07-01 23:49:49 +00:00
|
|
|
contractsManager.compileContracts(env);
|
2015-06-28 20:11:42 +00:00
|
|
|
all_contracts = contractsManager.all_contracts;
|
|
|
|
contractDB = contractsManager.contractDB;
|
2015-07-01 23:49:49 +00:00
|
|
|
contractDependencies = contractsManager.contractDependencies;
|
2015-06-28 02:20:07 +00:00
|
|
|
|
|
|
|
deployedContracts = {};
|
|
|
|
|
|
|
|
for (k = 0, len2 = all_contracts.length; k < len2; k++) {
|
|
|
|
className = all_contracts[k];
|
|
|
|
contract = contractDB[className];
|
2015-06-28 13:51:58 +00:00
|
|
|
contractGasLimit = (contractsConfig != null ? (ref1 = contractsConfig[className]) != null ? ref1.gasLimit : void 0 : void 0) || blockchainConfig.gasLimit;
|
|
|
|
contractGasPrice = (contractsConfig != null ? (ref2 = contractsConfig[className]) != null ? ref2.gasPrice : void 0 : void 0) || blockchainConfig.gasPrice;
|
2015-06-28 02:20:07 +00:00
|
|
|
args = (contractsConfig != null ? (ref3 = contractsConfig[className]) != null ? ref3.args : void 0 : void 0) || [];
|
|
|
|
contractObject = web3.eth.contract(contract.info.abiDefinition);
|
|
|
|
realArgs = [];
|
|
|
|
for (l = 0, len3 = args.length; l < len3; l++) {
|
|
|
|
arg = args[l];
|
|
|
|
if (arg[0] === "$") {
|
|
|
|
realArgs.push(deployedContracts[arg.substr(1)]);
|
|
|
|
} else {
|
|
|
|
realArgs.push(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
contractParams = realArgs;
|
|
|
|
contractParams.push({
|
|
|
|
from: primaryAddress,
|
|
|
|
data: contract.code,
|
|
|
|
gas: contractGasLimit,
|
|
|
|
gasPrice: contractGasPrice
|
|
|
|
});
|
|
|
|
contractAddress = contractObject["new"].apply(contractObject, contractParams).address;
|
|
|
|
deployedContracts[className] = contractAddress;
|
|
|
|
console.log("address is " + contractAddress);
|
|
|
|
console.log("deployed " + className + " at " + contractAddress);
|
|
|
|
abi = JSON.stringify(contract.info.abiDefinition);
|
|
|
|
result += "var " + className + "Abi = " + abi + ";";
|
|
|
|
result += "var " + className + "Contract = web3.eth.contract(" + className + "Abi);";
|
|
|
|
result += "var " + className + " = " + className + "Contract.at('" + contractAddress + "');";
|
|
|
|
}
|
|
|
|
|
|
|
|
grunt.file.write(destFile, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Deploy = {
|
|
|
|
deployContracts: deployContracts
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Deploy
|
|
|
|
|