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

180 lines
6.2 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});
2017-10-13 09:56:42 +00:00
let code = codeGenerator.generateContracts(false, true, true);
2017-03-30 11:12:39 +00:00
let cmds = contract.onDeploy.join(';\n');
2015-10-09 17:20:35 +00:00
RunCode.doEval(code + "\n" + 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;
let toReplace = linkReference + "_".repeat(40 - linkReference.length);
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);
done();
}
);
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;