embark/lib/deploy.js

170 lines
5.6 KiB
JavaScript
Raw Normal View History

2016-08-13 14:48:00 +00:00
var async = require('async');
2016-08-14 12:04:34 +00:00
var Compiler = require('./compiler.js');
2016-09-25 01:10:47 +00:00
var DeployTracker = require('./deploy_tracker.js');
2016-10-02 21:57:33 +00:00
var ABIGenerator = require('./abi.js');
var web3;
2016-09-25 01:10:47 +00:00
var Deploy = function(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-30 23:38:40 +00:00
Deploy.prototype.determineArguments = function(suppliedArgs) {
var 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);
}
}
return realArgs;
2016-10-30 23:38:40 +00:00
};
2016-09-25 01:10:47 +00:00
Deploy.prototype.checkAndDeployContract = function(contract, params, callback) {
var self = this;
2016-10-22 19:29:41 +00:00
var suppliedArgs;
var realArgs;
var arg;
var l;
var contractName;
var referedContract;
2016-10-21 11:16:15 +00:00
contract.error = false;
if (contract.deploy === false) {
self.logger.contractsState(self.contractsManager.contractsState());
return callback();
}
if (contract.address !== undefined) {
2016-10-30 23:38:40 +00:00
realArgs = self.determineArguments(params || contract.args);
contract.deployedAddress = contract.address;
self.deployTracker.trackContract(contract.className, contract.code, realArgs, contract.address);
self.deployTracker.save();
self.logger.contractsState(self.contractsManager.contractsState());
return callback();
}
2016-09-25 06:30:03 +00:00
var trackedContract = self.deployTracker.getContract(contract.className, contract.code, contract.args);
2016-09-25 01:10:47 +00:00
2016-09-25 06:30:03 +00:00
if (trackedContract && this.web3.eth.getCode(trackedContract.address) !== "0x") {
self.logger.info(contract.className + " already deployed " + trackedContract.address);
contract.deployedAddress = trackedContract.address;
2016-09-25 01:10:47 +00:00
self.logger.contractsState(self.contractsManager.contractsState());
2016-10-29 17:55:06 +00:00
return callback();
2016-09-25 01:10:47 +00:00
} else {
2016-09-27 04:55:35 +00:00
2016-10-30 23:38:40 +00:00
realArgs = self.determineArguments(params || contract.args);
2016-09-27 04:55:35 +00:00
this.deployContract(contract, realArgs, function(err, address) {
2016-10-30 23:22:09 +00:00
if (err) {
return callback(new Error(err));
}
2016-09-27 04:55:35 +00:00
self.deployTracker.trackContract(contract.className, contract.code, realArgs, address);
2016-09-25 01:10:47 +00:00
self.deployTracker.save();
self.logger.contractsState(self.contractsManager.contractsState());
2016-10-02 21:57:33 +00:00
2016-10-22 21:29:06 +00:00
// TODO: replace evals with separate process so it's isolated and with
2016-10-02 21:57:33 +00:00
// a callback
if (contract.onDeploy !== undefined) {
self.logger.info('executing onDeploy commands');
var abiGenerator = new ABIGenerator({}, self.contractsManager);
web3 = self.web3;
var abi = abiGenerator.generateContracts(false);
2016-10-22 21:29:06 +00:00
eval(abi); // jshint ignore:line
2016-10-02 21:57:33 +00:00
var cmds = contract.onDeploy.join(';\n');
2016-10-22 21:29:06 +00:00
eval(cmds); // jshint ignore:line
2016-10-02 21:57:33 +00:00
}
2016-09-25 01:10:47 +00:00
callback();
});
}
2016-08-13 14:48:00 +00:00
2016-08-14 12:04:34 +00:00
};
Deploy.prototype.deployContract = function(contract, params, callback) {
2016-09-17 03:56:25 +00:00
var self = this;
2016-08-14 12:04:34 +00:00
var contractObject = this.web3.eth.contract(contract.abiDefinition);
2015-10-09 17:20:35 +00:00
2016-09-25 01:10:47 +00:00
var contractParams = (params || contract.args).slice();
2016-10-22 19:29:41 +00:00
this.web3.eth.getAccounts(function(err, accounts) {
2016-10-30 23:14:38 +00:00
if (err) {
return callback(new Error(err));
}
2016-10-02 20:57:13 +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-02-03 12:01:59 +00:00
data: "0x" + contract.code,
2016-10-02 20:57:13 +00:00
gas: contract.gas,
gasPrice: contract.gasPrice
});
2016-08-14 12:04:34 +00:00
self.logger.info("deploying " + contract.className + " with " + contract.gas + " gas");
2016-10-02 20:57:13 +00:00
contractParams.push(function(err, transaction) {
self.logger.contractsState(self.contractsManager.contractsState());
2016-10-02 20:57:13 +00:00
if (err) {
self.logger.error("error deploying contract: " + contract.className);
2016-10-22 15:48:47 +00:00
var errMsg = err.toString();
2016-10-22 15:57:36 +00:00
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';
2016-10-22 15:48:47 +00:00
}
self.logger.error(errMsg);
contract.error = errMsg;
2016-10-30 23:17:31 +00:00
return callback(new Error(err));
2016-10-02 20:57:13 +00:00
} else if (transaction.address !== undefined) {
self.logger.info(contract.className + " deployed at " + transaction.address);
contract.deployedAddress = transaction.address;
contract.transactionHash = transaction.transactionHash;
2016-10-30 23:17:31 +00:00
return callback(null, transaction.address);
2016-10-02 20:57:13 +00:00
}
});
2015-08-04 12:18:04 +00:00
2016-10-02 20:57:13 +00:00
contractObject["new"].apply(contractObject, contractParams);
});
};
2016-08-14 12:04:34 +00:00
Deploy.prototype.deployAll = function(done) {
var self = this;
2016-09-17 16:28:26 +00:00
this.logger.info("deploying contracts");
2016-08-14 12:04:34 +00:00
async.eachOfSeries(this.contractsManager.listContracts(),
function(contract, key, callback) {
2016-09-17 16:28:26 +00:00
self.logger.trace(arguments);
2016-09-25 01:10:47 +00:00
self.checkAndDeployContract(contract, null, callback);
2016-08-14 12:04:34 +00:00
},
function(err, results) {
2016-10-30 23:28:45 +00:00
if (err) {
self.logger.error("error deploying contracts");
self.logger.error(err.message);
self.logger.debug(err.stack);
}
2016-09-17 03:56:25 +00:00
self.logger.info("finished");
2016-09-17 16:28:26 +00:00
self.logger.trace(arguments);
2016-08-14 12:04:34 +00:00
done();
}
);
};
module.exports = Deploy;