embark/lib/deploy.js

63 lines
2.0 KiB
JavaScript
Raw Normal View History

2016-08-13 10:48:00 -04:00
var async = require('async');
2016-08-14 08:04:34 -04:00
var Compiler = require('./compiler.js');
2016-08-13 10:48:00 -04:00
2016-09-16 23:56:25 -04:00
var Deploy = function(web3, contractsManager, logger) {
2016-08-14 08:04:34 -04:00
this.web3 = web3;
this.contractsManager = contractsManager;
2016-09-16 23:56:25 -04:00
this.logger = logger;
2016-08-14 08:04:34 -04:00
};
Deploy.prototype.deployContract = function(contract, params, callback) {
2016-09-16 23:56:25 -04:00
var self = this;
2016-08-14 08:04:34 -04:00
var contractObject = this.web3.eth.contract(contract.abiDefinition);
2015-10-09 13:20:35 -04:00
2016-08-14 08:04:34 -04:00
var contractParams = params || contract.args;
2016-08-13 10:48:00 -04:00
contractParams.push({
2016-08-14 08:04:34 -04:00
from: this.web3.eth.coinbase,
data: contract.code,
2016-08-13 10:48:00 -04:00
gas: contract.gasLimit,
gasPrice: contract.gasPrice
2016-05-29 20:14:27 -04:00
});
2016-08-14 08:04:34 -04:00
self.logger.info("deploying " + contract.className);
2016-08-14 08:04:34 -04:00
contractParams.push(function(err, transaction) {
self.logger.contractsState(self.contractsManager.contractsState());
2016-08-14 08:04:34 -04:00
if (err) {
2016-09-17 12:28:26 -04:00
self.logger.error("error deploying contract: " + contract.className);
self.logger.error(err.toString());
self.logger.contractsState(self.contractsManager.contractsState());
2016-08-14 08:04:34 -04:00
callback(new Error(err));
} else if (transaction.address !== undefined) {
self.logger.info(contract.className + " deployed at " + transaction.address);
2016-08-14 08:04:34 -04:00
contract.deployedAddress = transaction.address;
self.logger.contractsState(self.contractsManager.contractsState());
2016-08-14 08:04:34 -04:00
callback(null, transaction.address);
}
});
2015-08-04 08:18:04 -04:00
2016-08-13 10:48:00 -04:00
contractObject["new"].apply(contractObject, contractParams);
};
2016-08-14 08:04:34 -04:00
Deploy.prototype.deployAll = function(done) {
var self = this;
2016-09-17 12:28:26 -04:00
this.logger.info("deploying contracts");
2016-08-14 08:04:34 -04:00
async.eachOfSeries(this.contractsManager.listContracts(),
function(contract, key, callback) {
2016-09-17 12:28:26 -04:00
self.logger.trace(arguments);
2016-08-14 08:04:34 -04:00
self.deployContract(contract, null, callback);
},
function(err, results) {
2016-09-16 23:56:25 -04:00
self.logger.info("finished");
2016-09-17 12:28:26 -04:00
self.logger.trace(arguments);
2016-08-14 08:04:34 -04:00
done();
}
);
};
module.exports = Deploy;