embark-area-51/lib/contracts/deploy.js

230 lines
8.5 KiB
JavaScript
Raw Normal View History

2017-03-29 17:50:05 +00:00
let async = require('async');
2017-03-29 17:50:05 +00:00
let RunCode = require('../core/runCode.js');
2017-03-29 17:50:05 +00:00
let DeployTracker = require('./deploy_tracker.js');
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
});
}
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);
}
}
2017-03-30 11:12:39 +00:00
return realArgs;
}
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-03-30 11:12:39 +00:00
if (contract.address !== undefined) {
2016-09-27 04:55:35 +00:00
2017-03-30 11:12:39 +00:00
realArgs = self.determineArguments(params || contract.args);
2016-09-27 04:55:35 +00:00
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-03-30 11:12:39 +00:00
let trackedContract = self.deployTracker.getContract(contract.className, contract.realRuntimeBytecode, contract.args);
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());
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-03-30 11:12:39 +00:00
if (contract.onDeploy !== undefined) {
self.logger.info('executing onDeploy commands');
let codeGenerator = new CodeGenerator({contractsManager: self.contractsManager});
2015-10-09 17:20:35 +00:00
2017-12-20 16:32:11 +00:00
let cmds = "";
cmds += codeGenerator.generateContractCode(contract);
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"));
}
cmds += onDeployCode.join(';\n');
2017-12-20 16:32:11 +00:00
RunCode.doEval(cmds, self.web3);
2017-03-30 11:12:39 +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();
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
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;
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));
}
let toReplace = linkReference + "_".repeat(40 - linkReference.length);
if (deployedAddress === undefined) {
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-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],
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-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;
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;
self.logger.contractsState(self.contractsManager.contractsState());
2017-03-30 11:12:39 +00:00
return callback(null, transaction.address);
}
});
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;