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

361 lines
14 KiB
JavaScript
Raw Normal View History

2017-03-29 17:50:05 +00:00
let async = require('async');
2018-01-05 20:10:47 +00:00
//require("../utils/debug_util.js")(__filename, 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.events = options.events;
2017-03-30 11:12:39 +00:00
this.env = options.env;
2018-01-05 20:10:47 +00:00
this.chainConfig = options.chainConfig;
this.plugins = options.plugins;
2018-01-13 16:38:10 +00:00
this.gasLimit = options.gasLimit;
2018-01-05 20:10:47 +00:00
}
2017-03-30 11:12:39 +00:00
2018-01-05 20:10:47 +00:00
initTracker(cb) {
2017-03-30 11:12:39 +00:00
this.deployTracker = new DeployTracker({
2018-01-05 20:10:47 +00:00
logger: this.logger, chainConfig: this.chainConfig, web3: this.web3, env: this.env
}, cb);
}
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 if (Array.isArray(arg)) {
let subRealArgs = [];
for (let sub_arg of arg) {
if (sub_arg[0] === "$") {
contractName = sub_arg.substr(1);
referedContract = this.contractsManager.getContract(contractName);
subRealArgs.push(referedContract.deployedAddress);
} else {
subRealArgs.push(sub_arg);
}
}
realArgs.push(subRealArgs);
2017-03-30 11:12:39 +00:00
} 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.events.emit('contractsState', self.contractsManager.contractsState());
2017-03-30 11:12:39 +00:00
return callback();
}
2016-09-25 01:10:47 +00:00
realArgs = self.determineArguments(params || contract.args);
2016-09-27 04:55:35 +00:00
if (contract.address !== undefined) {
2018-03-05 00:18:39 +00:00
try {
this.web3.utils.toChecksumAddress(contract.address);
} catch(e) {
self.logger.error("error deploying " + contract.className);
self.logger.error(e.message);
contract.error = e.message;
self.events.emit('contractsState', self.contractsManager.contractsState());
return callback(e.message);
}
2017-03-30 11:12:39 +00:00
contract.deployedAddress = contract.address;
self.logger.info(contract.className.bold.cyan + " already deployed at ".green + contract.address.bold.cyan);
2018-01-05 20:10:47 +00:00
if (this.deployTracker) {
self.deployTracker.trackContract(contract.className, contract.realRuntimeBytecode, realArgs, contract.address);
self.deployTracker.save();
}
self.events.emit('contractsState', self.contractsManager.contractsState());
2017-03-30 11:12:39 +00:00
return callback();
}
2016-10-02 21:57:33 +00:00
2018-01-05 20:10:47 +00:00
if (!this.deployTracker) {
return self.contractToDeploy(contract, params, callback);
}
let trackedContract = self.deployTracker.getContract(contract.className, contract.realRuntimeBytecode, realArgs);
2018-01-05 20:10:47 +00:00
if (!trackedContract) {
return self.contractToDeploy(contract, params, callback);
}
this.web3.eth.getCode(trackedContract.address, function(_getCodeErr, codeInChain) {
if (codeInChain !== "0x") {
self.contractAlreadyDeployed(contract, trackedContract, callback);
} else {
self.contractToDeploy(contract, params, callback);
}
});
}
2017-02-18 21:53:49 +00:00
2018-01-05 20:10:47 +00:00
contractAlreadyDeployed(contract, trackedContract, callback) {
const self = this;
self.logger.info(contract.className.bold.cyan + " already deployed at ".green + trackedContract.address.bold.cyan);
contract.deployedAddress = trackedContract.address;
self.events.emit('contractsState', self.contractsManager.contractsState());
2018-01-05 20:10:47 +00:00
// always run contractCode so other functionality like 'afterDeploy' can also work
let codeGenerator = new CodeGenerator({contractsManager: self.contractsManager});
2018-01-13 16:38:10 +00:00
let contractCode = codeGenerator.generateContractCode(contract, self.gasLimit);
2018-01-05 20:10:47 +00:00
RunCode.doEval(contractCode, self.web3);
return callback();
}
contractToDeploy(contract, params, callback) {
const self = this;
let realArgs = self.determineArguments(params || contract.args);
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.events.emit('contractsState', self.contractsManager.contractsState());
// always run contractCode so other functionality like 'afterDeploy' can also work
let codeGenerator = new CodeGenerator({contractsManager: self.contractsManager});
2018-01-13 16:38:10 +00:00
let contractCode = codeGenerator.generateContractCode(contract, self.gasLimit);
RunCode.doEval(contractCode, self.web3);
2018-01-05 20:10:47 +00:00
if (contract.onDeploy !== undefined) {
self.logger.info('executing onDeploy commands');
2016-08-14 12:04:34 +00:00
2018-01-13 16:38:10 +00:00
let contractCode = codeGenerator.generateContractCode(contract, self.gasLimit);
RunCode.doEval(contractCode, self.web3);
2018-01-05 20:10:47 +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;
2017-12-20 19:09:35 +00:00
});
2018-01-05 20:10:47 +00:00
return realCmd;
});
2017-12-20 19:09:35 +00:00
2018-01-05 20:10:47 +00:00
if (withErrors) {
contract.error = "onDeployCmdError";
return callback(new Error("error running onDeploy"));
}
2017-12-20 19:09:35 +00:00
2018-01-05 20:10:47 +00:00
// TODO: convert to for to avoid repeated callback
for(let cmd of onDeployCode) {
self.logger.info("executing: " + cmd);
try {
RunCode.doEval(cmd, self.web3);
} catch(e) {
if (e.message.indexOf("invalid opcode") >= 0) {
self.logger.error('the transaction was rejected; this usually happens due to a throw or a require, it can also happen due to an invalid operation');
}
2018-01-05 20:10:47 +00:00
return callback(new Error(e));
}
2017-03-30 11:12:39 +00:00
}
2018-01-05 20:10:47 +00:00
}
2018-01-05 20:10:47 +00:00
callback();
});
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;
2018-01-18 19:41:33 +00:00
let accounts = [];
2017-03-30 11:12:39 +00:00
let contractParams = (params || contract.args).slice();
2018-01-18 19:41:33 +00:00
let contractCode = contract.code;
let deploymentAccount = self.web3.eth.defaultAccount;
let deployObject;
async.waterfall([
function getAccounts(next) {
self.web3.eth.getAccounts(function (err, _accounts) {
if (err) {
2018-01-18 19:46:53 +00:00
return next(new Error(err));
2018-01-18 19:41:33 +00:00
}
accounts = _accounts;
// applying deployer account configuration, if any
if (typeof contract.fromIndex == 'number') {
deploymentAccount = accounts[contract.fromIndex];
2018-01-20 01:38:28 +00:00
if (deploymentAccount === undefined) {
return next("error deploying " + contract.className + ": no account found at index " + contract.fromIndex + " check the config");
}
}
if (typeof contract.from == 'string' && typeof contract.fromIndex != 'undefined') {
self.logger.warn('Both "from" and "fromIndex" are defined for contract "'+contract.className+'". Using "from" as deployer account.');
}
if (typeof contract.from == 'string') {
deploymentAccount = contract.from;
}
2018-01-18 19:41:33 +00:00
deploymentAccount = deploymentAccount || accounts[0];
next();
});
},
function doLinking(next) {
// Applying linked contracts
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;
}
if (linkReference.length > 40) {
return next(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 next(new Error(contract.className + " needs " + libraryName + " but an address was not found, did you deploy it or configured an address?"));
}
contractCode = contractCode.replace(new RegExp(toReplace, "g"), deployedAddress);
}
2018-01-18 19:41:33 +00:00
// saving code changes back to contract object
contract.code = contractCode;
next();
},
function applyBeforeDeploy(next) {
let beforeDeployPlugins = self.plugins.getPluginsFor('beforeDeploy');
//self.logger.info("applying beforeDeploy plugins...", beforeDeployPlugins.length);
async.eachSeries(beforeDeployPlugins, (plugin, eachPluginCb) => {
self.logger.info("running beforeDeploy plugin " + plugin.name + " .");
// calling each beforeDeploy handler declared by the plugin
async.eachSeries(plugin.beforeDeploy, (beforeDeployFn, eachCb) => {
beforeDeployFn({
embarkDeploy: self,
pluginConfig: plugin.pluginConfig,
deploymentAccount: deploymentAccount,
contract: contract,
callback:
(function(resObj){
contract.code = resObj.contractCode;
eachCb();
})
});
}, () => {
2018-01-18 19:41:33 +00:00
//self.logger.info('All beforeDeploy handlers of the plugin has processed.');
eachPluginCb();
});
2018-01-18 19:41:33 +00:00
}, () => {
//self.logger.info('All beforeDeploy plugins has been processed.');
contractCode = contract.code;
next();
});
},
function createDeployObject(next) {
let contractObject = new self.web3.eth.Contract(contract.abiDefinition);
try {
deployObject = contractObject.deploy({arguments: contractParams, data: "0x" + contractCode});
} catch(e) {
2018-03-02 22:48:30 +00:00
if (e.message.indexOf('Invalid number of parameters for "undefined"') >= 0) {
return next(new Error("attempted to deploy " + contract.className + " without specifying parameters"));
2018-01-18 19:41:33 +00:00
} else {
return next(new Error(e));
}
2018-01-05 20:10:47 +00:00
}
2018-01-18 19:41:33 +00:00
next();
},
2018-01-19 18:57:35 +00:00
function estimateCorrectGas(next) {
if (contract.gas === 'auto') {
return deployObject.estimateGas().then((gasValue) => {
contract.gas = gasValue;
next();
}).catch(next);
}
next();
},
2018-01-18 19:41:33 +00:00
function deployTheContract(next) {
self.logger.info("deploying " + contract.className.bold.cyan + " with ".green + contract.gas + " gas".green);
deployObject.send({
from: deploymentAccount,
gas: contract.gas,
gasPrice: contract.gasPrice
}).on('receipt', function(receipt) {
if (receipt.contractAddress !== undefined) {
self.logger.info(contract.className.bold.cyan + " deployed at ".green + receipt.contractAddress.bold.cyan);
contract.deployedAddress = receipt.contractAddress;
contract.transactionHash = receipt.transactionHash;
self.events.emit('contractsState', self.contractsManager.contractsState());
2018-01-18 19:41:33 +00:00
return next(null, receipt.contractAddress);
}
self.events.emit('contractsState', self.contractsManager.contractsState());
2018-01-18 19:41:33 +00:00
}).on('error', function(error) {
self.events.emit('contractsState', self.contractsManager.contractsState());
2018-01-18 19:41:33 +00:00
return next(new Error("error deploying =" + contract.className + "= due to error: " + error.message));
});
}
], callback);
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;