embark/lib/deploy.js

89 lines
2.9 KiB
JavaScript

var async = require('async');
var Compiler = require('./compiler.js');
var DeployTracker = require('./deploy_tracker.js');
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
});
};
Deploy.prototype.checkAndDeployContract = function(contract, params, callback) {
var self = this;
var trackedChain = self.deployTracker.getContract(contract.className, contract.code, contract.args);
// TODO: need to also check getCode
if (trackedChain) {
self.logger.info(contract.className + " already deployed " + trackedChain.address);
contract.deployedAddress = trackedChain.address;
self.logger.contractsState(self.contractsManager.contractsState());
callback();
} else {
this.deployContract(contract, params, function(err, address) {
self.logger.info("not deployed");
self.deployTracker.trackContract(contract.className, contract.code, contract.args, address);
self.deployTracker.save();
self.logger.contractsState(self.contractsManager.contractsState());
callback();
});
}
};
Deploy.prototype.deployContract = function(contract, params, callback) {
var self = this;
var contractObject = this.web3.eth.contract(contract.abiDefinition);
var contractParams = (params || contract.args).slice();
contractParams.push({
from: this.web3.eth.coinbase,
data: contract.code,
gas: contract.gas,
gasPrice: contract.gasPrice
});
self.logger.info("deploying " + contract.className);
contractParams.push(function(err, transaction) {
self.logger.contractsState(self.contractsManager.contractsState());
if (err) {
self.logger.error("error deploying contract: " + contract.className);
self.logger.error(err.toString());
callback(new Error(err));
} else if (transaction.address !== undefined) {
self.logger.info(contract.className + " deployed at " + transaction.address);
contract.deployedAddress = transaction.address;
callback(null, transaction.address);
}
});
contractObject["new"].apply(contractObject, contractParams);
};
Deploy.prototype.deployAll = function(done) {
var 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);
},
function(err, results) {
self.logger.info("finished");
self.logger.trace(arguments);
done();
}
);
};
module.exports = Deploy;