2017-03-29 17:50:05 +00:00
|
|
|
let async = require('async');
|
2017-02-19 18:17:28 +00:00
|
|
|
|
2017-03-29 17:50:05 +00:00
|
|
|
let RunCode = require('../core/runCode.js');
|
2017-02-19 18:17:28 +00:00
|
|
|
|
2017-03-29 17:50:05 +00:00
|
|
|
let DeployTracker = require('./deploy_tracker.js');
|
2017-08-03 23:29:09 +00:00
|
|
|
let CodeGenerator = require('./code_generator.js');
|
2016-09-25 01:10:47 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
class Deploy {
|
|
|
|
constructor(options) {
|
|
|
|
this.web3 = options.web3;
|
|
|
|
this.contractsManager = options.contractsManager;
|
|
|
|
this.logger = options.logger;
|
|
|
|
this.env = options.env;
|
|
|
|
|
|
|
|
this.deployTracker = new DeployTracker({
|
|
|
|
logger: options.logger, chainConfig: options.chainConfig, web3: options.web3, env: this.env
|
|
|
|
});
|
2016-10-21 03:31:42 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
determineArguments(suppliedArgs) {
|
|
|
|
let realArgs = [], l, arg, contractName, referedContract;
|
|
|
|
|
|
|
|
for (l = 0; l < suppliedArgs.length; l++) {
|
|
|
|
arg = suppliedArgs[l];
|
|
|
|
if (arg[0] === "$") {
|
|
|
|
contractName = arg.substr(1);
|
|
|
|
referedContract = this.contractsManager.getContract(contractName);
|
|
|
|
realArgs.push(referedContract.deployedAddress);
|
|
|
|
} else {
|
|
|
|
realArgs.push(arg);
|
|
|
|
}
|
|
|
|
}
|
2016-09-28 01:13:54 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
return realArgs;
|
2016-09-28 01:13:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
checkAndDeployContract(contract, params, callback) {
|
|
|
|
let self = this;
|
|
|
|
let realArgs;
|
|
|
|
contract.error = false;
|
|
|
|
|
|
|
|
if (contract.deploy === false) {
|
|
|
|
self.logger.contractsState(self.contractsManager.contractsState());
|
|
|
|
return callback();
|
|
|
|
}
|
2016-09-25 01:10:47 +00:00
|
|
|
|
2017-12-28 13:27:20 +00:00
|
|
|
realArgs = self.determineArguments(params || contract.args);
|
2016-09-27 04:55:35 +00:00
|
|
|
|
2017-12-28 13:27:20 +00:00
|
|
|
if (contract.address !== undefined) {
|
2017-03-30 11:12:39 +00:00
|
|
|
contract.deployedAddress = contract.address;
|
|
|
|
self.deployTracker.trackContract(contract.className, contract.realRuntimeBytecode, realArgs, contract.address);
|
2016-09-25 01:10:47 +00:00
|
|
|
self.deployTracker.save();
|
|
|
|
self.logger.contractsState(self.contractsManager.contractsState());
|
2017-03-30 11:12:39 +00:00
|
|
|
return callback();
|
|
|
|
}
|
2016-10-02 21:57:33 +00:00
|
|
|
|
2017-12-28 13:27:20 +00:00
|
|
|
let trackedContract = self.deployTracker.getContract(contract.className, contract.realRuntimeBytecode, realArgs);
|
2017-02-18 21:53:49 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
if (trackedContract && this.web3.eth.getCode(trackedContract.address) !== "0x") {
|
|
|
|
self.logger.info(contract.className.bold.cyan + " already deployed at ".green + trackedContract.address.bold.cyan);
|
|
|
|
contract.deployedAddress = trackedContract.address;
|
|
|
|
self.logger.contractsState(self.contractsManager.contractsState());
|
2017-12-27 16:40:21 +00:00
|
|
|
|
|
|
|
// always run contractCode so other functionality like 'afterDeploy' can also work
|
|
|
|
let codeGenerator = new CodeGenerator({contractsManager: self.contractsManager});
|
|
|
|
let contractCode = codeGenerator.generateContractCode(contract);
|
|
|
|
RunCode.doEval(contractCode, self.web3);
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
return callback();
|
|
|
|
} else {
|
2016-10-02 21:57:33 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
realArgs = self.determineArguments(params || contract.args);
|
2016-08-13 14:48:00 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
this.deployContract(contract, realArgs, function (err, address) {
|
|
|
|
if (err) {
|
|
|
|
return callback(new Error(err));
|
|
|
|
}
|
|
|
|
self.deployTracker.trackContract(contract.className, contract.realRuntimeBytecode, realArgs, address);
|
|
|
|
self.deployTracker.save();
|
|
|
|
self.logger.contractsState(self.contractsManager.contractsState());
|
2016-08-14 12:04:34 +00:00
|
|
|
|
2017-12-27 16:40:21 +00:00
|
|
|
// always run contractCode so other functionality like 'afterDeploy' can also work
|
|
|
|
let codeGenerator = new CodeGenerator({contractsManager: self.contractsManager});
|
|
|
|
let contractCode = codeGenerator.generateContractCode(contract);
|
|
|
|
RunCode.doEval(contractCode, self.web3);
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
if (contract.onDeploy !== undefined) {
|
|
|
|
self.logger.info('executing onDeploy commands');
|
2015-10-09 17:20:35 +00:00
|
|
|
|
2017-12-28 13:27:20 +00:00
|
|
|
let contractCode = codeGenerator.generateContractCode(contract);
|
|
|
|
RunCode.doEval(contractCode, self.web3);
|
2017-12-20 19:09:35 +00:00
|
|
|
|
|
|
|
let withErrors = false;
|
|
|
|
let regex = /\$\w+/g;
|
|
|
|
let onDeployCode = contract.onDeploy.map((cmd) => {
|
|
|
|
let realCmd = cmd.replace(regex, (match) => {
|
|
|
|
let referedContractName = match.slice(1);
|
|
|
|
let referedContract = self.contractsManager.getContract(referedContractName);
|
|
|
|
if (!referedContract) {
|
|
|
|
self.logger.error('error executing onDeploy for ' + contract.className);
|
|
|
|
self.logger.error(referedContractName + ' does not exist');
|
|
|
|
self.logger.error("error running onDeploy: " + cmd);
|
|
|
|
withErrors = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (referedContract && referedContract.deploy === false) {
|
|
|
|
self.logger.error('error executing onDeploy for ' + contract.className);
|
|
|
|
self.logger.error(referedContractName + " exists but has been set to not deploy");
|
|
|
|
self.logger.error("error running onDeploy: " + cmd);
|
|
|
|
withErrors = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (referedContract && !referedContract.deployedAddress) {
|
|
|
|
self.logger.error('error executing onDeploy for ' + contract.className);
|
|
|
|
self.logger.error("couldn't find a valid address for " + referedContractName + ". has it been deployed?");
|
|
|
|
self.logger.error("error running onDeploy: " + cmd);
|
|
|
|
withErrors = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return referedContract.deployedAddress;
|
|
|
|
});
|
|
|
|
return realCmd;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (withErrors) {
|
|
|
|
contract.error = "onDeployCmdError";
|
|
|
|
return callback(new Error("error running onDeploy"));
|
|
|
|
}
|
|
|
|
|
2017-12-28 13:27:20 +00:00
|
|
|
// TODO: convert to for to avoid repeated callback
|
|
|
|
for(let cmd of onDeployCode) {
|
|
|
|
self.logger.info("executing: " + cmd);
|
|
|
|
try {
|
|
|
|
RunCode.doEval(cmd, self.web3);
|
|
|
|
} catch(e) {
|
|
|
|
if (e.message.indexOf("invalid opcode") >= 0) {
|
|
|
|
self.logger.error('the transaction was rejected; this usually happens due to a throw or a require, it can also happen due to an invalid operation');
|
|
|
|
}
|
|
|
|
return callback(new Error(e));
|
|
|
|
}
|
|
|
|
}
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
2015-06-28 02:20:07 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
callback();
|
|
|
|
});
|
2016-10-30 23:14:38 +00:00
|
|
|
}
|
2016-10-02 20:57:13 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
2016-08-14 12:04:34 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
deployContract(contract, params, callback) {
|
|
|
|
let self = this;
|
|
|
|
let contractObject = this.web3.eth.contract(contract.abiDefinition);
|
|
|
|
|
|
|
|
let contractParams = (params || contract.args).slice();
|
2016-09-22 22:24:01 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
this.web3.eth.getAccounts(function (err, accounts) {
|
2016-10-02 20:57:13 +00:00
|
|
|
if (err) {
|
2016-10-30 23:17:31 +00:00
|
|
|
return callback(new Error(err));
|
2016-10-02 20:57:13 +00:00
|
|
|
}
|
2017-03-30 11:12:39 +00:00
|
|
|
|
2017-07-16 16:10:17 +00:00
|
|
|
let contractCode = contract.code;
|
|
|
|
let contractsList = self.contractsManager.listContracts();
|
|
|
|
for (let contractObj of contractsList) {
|
|
|
|
let filename = contractObj.filename;
|
|
|
|
let deployedAddress = contractObj.deployedAddress;
|
|
|
|
if (deployedAddress) {
|
|
|
|
deployedAddress = deployedAddress.substr(2);
|
|
|
|
}
|
|
|
|
let linkReference = '__' + filename + ":" + contractObj.className;
|
2017-12-19 16:13:27 +00:00
|
|
|
if (contractCode.indexOf(linkReference) < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-12-15 21:39:13 +00:00
|
|
|
if (linkReference.length > 40) {
|
|
|
|
return callback(new Error(linkReference + " is too long, try reducing the path of the contract (" + filename + ") and/or its name " + contractObj.className));
|
|
|
|
}
|
2017-07-16 16:10:17 +00:00
|
|
|
let toReplace = linkReference + "_".repeat(40 - linkReference.length);
|
2017-12-19 16:13:27 +00:00
|
|
|
if (deployedAddress === undefined) {
|
2017-12-15 21:18:19 +00:00
|
|
|
let libraryName = contractObj.className;
|
|
|
|
return callback(new Error(contract.className + " needs " + libraryName + " but an address was not found, did you deploy it or configured an address?"));
|
|
|
|
}
|
2017-07-16 17:38:12 +00:00
|
|
|
contractCode = contractCode.replace(new RegExp(toReplace, "g"), deployedAddress);
|
2017-07-16 16:10:17 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
// TODO: probably needs to be defaultAccount
|
|
|
|
// TODO: it wouldn't necessary be the first address
|
|
|
|
// use defined blockchain address or first address
|
|
|
|
contractParams.push({
|
|
|
|
//from: this.web3.eth.coinbase,
|
|
|
|
from: accounts[0],
|
2017-07-16 16:10:17 +00:00
|
|
|
data: "0x" + contractCode,
|
2017-03-30 11:12:39 +00:00
|
|
|
gas: contract.gas,
|
|
|
|
gasPrice: contract.gasPrice
|
|
|
|
});
|
|
|
|
|
|
|
|
self.logger.info("deploying " + contract.className.bold.cyan + " with ".green + contract.gas + " gas".green);
|
2017-07-16 16:10:17 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
contractParams.push(function (err, transaction) {
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
self.logger.error("error deploying contract: " + contract.className.cyan);
|
|
|
|
let errMsg = err.toString();
|
|
|
|
if (errMsg === 'Error: The contract code couldn\'t be stored, please check your gas amount.') {
|
|
|
|
errMsg = 'The contract code couldn\'t be stored, out of gas or constructor error';
|
|
|
|
}
|
|
|
|
self.logger.error(errMsg);
|
|
|
|
contract.error = errMsg;
|
2017-12-14 20:03:08 +00:00
|
|
|
self.logger.contractsState(self.contractsManager.contractsState());
|
2017-03-30 11:12:39 +00:00
|
|
|
return callback(new Error(err));
|
|
|
|
} else if (transaction.address !== undefined) {
|
|
|
|
self.logger.info(contract.className.bold.cyan + " deployed at ".green + transaction.address.bold.cyan);
|
|
|
|
contract.deployedAddress = transaction.address;
|
|
|
|
contract.transactionHash = transaction.transactionHash;
|
2017-12-14 20:03:08 +00:00
|
|
|
self.logger.contractsState(self.contractsManager.contractsState());
|
2017-03-30 11:12:39 +00:00
|
|
|
return callback(null, transaction.address);
|
|
|
|
}
|
|
|
|
});
|
2017-12-14 20:03:08 +00:00
|
|
|
self.logger.contractsState(self.contractsManager.contractsState());
|
2017-03-30 11:12:39 +00:00
|
|
|
|
|
|
|
contractObject["new"].apply(contractObject, contractParams);
|
2016-10-02 20:57:13 +00:00
|
|
|
});
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
deployAll(done) {
|
|
|
|
let self = this;
|
|
|
|
this.logger.info("deploying contracts");
|
|
|
|
|
|
|
|
async.eachOfSeries(this.contractsManager.listContracts(),
|
|
|
|
function (contract, key, callback) {
|
|
|
|
self.logger.trace(arguments);
|
|
|
|
self.checkAndDeployContract(contract, null, callback);
|
|
|
|
},
|
2017-10-14 10:12:54 +00:00
|
|
|
function (err, _results) {
|
2017-03-30 11:12:39 +00:00
|
|
|
if (err) {
|
|
|
|
self.logger.error("error deploying contracts");
|
|
|
|
self.logger.error(err.message);
|
|
|
|
self.logger.debug(err.stack);
|
|
|
|
}
|
|
|
|
self.logger.info("finished deploying contracts");
|
|
|
|
self.logger.trace(arguments);
|
2017-12-20 19:09:35 +00:00
|
|
|
done(err);
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
|
|
|
);
|
2015-08-04 12:18:04 +00:00
|
|
|
|
2017-03-30 11:38:14 +00:00
|
|
|
}
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
2016-08-14 12:04:34 +00:00
|
|
|
|
|
|
|
module.exports = Deploy;
|