embark/lib/modules/deployment/contract_deployer.js

277 lines
11 KiB
JavaScript
Raw Normal View History

2017-03-30 02:50:05 +09:00
let async = require('async');
2018-01-05 15:10:47 -05:00
//require("../utils/debug_util.js")(__filename, async);
2018-07-27 13:09:07 -04:00
let utils = require('../../utils/utils.js');
class ContractDeployer {
2017-03-30 20:12:39 +09:00
constructor(options) {
const self = this;
2017-03-30 20:12:39 +09:00
this.logger = options.logger;
this.events = options.events;
this.plugins = options.plugins;
self.events.setCommandHandler('deploy:contract', (contract, cb) => {
self.checkAndDeployContract(contract, null, cb);
});
2018-01-05 15:10:47 -05:00
}
2017-03-30 20:12:39 +09:00
2018-05-21 12:29:18 -04:00
// TODO: determining the arguments could also be in a module since it's not
// part of ta 'normal' contract deployment
determineArguments(suppliedArgs, contract, callback) {
2018-05-21 13:11:13 -04:00
const self = this;
2017-03-30 20:12:39 +09:00
let args = suppliedArgs;
if (!Array.isArray(args)) {
args = [];
let abi = contract.abiDefinition.find((abi) => abi.type === 'constructor');
for (let input of abi.inputs) {
let inputValue = suppliedArgs[input.name];
if (!inputValue) {
2018-05-08 17:49:46 -04:00
this.logger.error(__("{{inputName}} has not been defined for {{className}} constructor", {inputName: input.name, className: contract.className}));
}
args.push(inputValue || "");
}
}
2018-05-22 16:16:52 -04:00
async.map(args, (arg, nextEachCb) => {
2017-03-30 20:12:39 +09:00
if (arg[0] === "$") {
2018-05-21 13:30:45 -04:00
let contractName = arg.substr(1);
2018-05-21 13:11:13 -04:00
self.events.request('contracts:contract', contractName, (referedContract) => {
2018-05-22 16:16:52 -04:00
nextEachCb(null, referedContract.deployedAddress);
2018-05-21 13:11:13 -04:00
});
} else if (Array.isArray(arg)) {
2018-05-22 16:16:52 -04:00
async.map(arg, (sub_arg, nextSubEachCb) => {
if (sub_arg[0] === "$") {
2018-05-21 13:30:45 -04:00
let contractName = sub_arg.substr(1);
self.events.request('contracts:contract', contractName, (referedContract) => {
2018-05-22 16:16:52 -04:00
nextSubEachCb(null, referedContract.deployedAddress);
2018-05-21 13:30:45 -04:00
});
} else {
2018-05-22 16:16:52 -04:00
nextSubEachCb(null, sub_arg);
}
2018-05-22 16:16:52 -04:00
}, (err, subRealArgs) => {
nextEachCb(null, subRealArgs);
2018-05-21 13:30:45 -04:00
});
2017-03-30 20:12:39 +09:00
} else {
2018-05-22 16:16:52 -04:00
nextEachCb(null, arg);
2017-03-30 20:12:39 +09:00
}
}, callback);
}
2017-03-30 20:12:39 +09:00
checkAndDeployContract(contract, params, callback) {
let self = this;
contract.error = false;
if (contract.deploy === false) {
2018-05-20 20:26:15 -04:00
self.events.emit("deploy:contract:undeployed", contract);
2017-03-30 20:12:39 +09:00
return callback();
}
2016-09-24 21:10:47 -04:00
async.waterfall([
2018-07-27 16:03:15 -04:00
function requestBlockchainConnector(callback) {
self.events.request("blockchain:object", (blockchain) => {
self.blockchain = blockchain;
callback();
});
},
2018-05-21 12:29:18 -04:00
function _determineArguments(next) {
self.determineArguments(params || contract.args, contract, (err, realArgs) => {
if (err) {
return next(err);
}
2018-05-21 12:29:18 -04:00
contract.realArgs = realArgs;
next();
});
},
function deployIt(next) {
if (contract.address !== undefined) {
try {
utils.toChecksumAddress(contract.address);
} catch(e) {
self.logger.error(__("error deploying %s", contract.className));
self.logger.error(e.message);
contract.error = e.message;
self.events.emit("deploy:contract:error", contract);
return next(e.message);
}
contract.deployedAddress = contract.address;
self.logger.info(contract.className.bold.cyan + __(" already deployed at ").green + contract.address.bold.cyan);
self.events.emit("deploy:contract:deployed", contract);
return next();
}
2016-10-02 17:57:33 -04:00
2018-06-01 13:44:49 -04:00
// TODO find a better way to do that
if (process.env.isTest) {
2018-06-01 13:53:09 -04:00
return self.deployContract(contract, next);
2018-06-01 13:44:49 -04:00
}
// TODO: this should be a plugin API instead, if not existing, it should by default deploy the contract
self.events.request("deploy:contract:shouldDeploy", contract, function(trackedContract) {
if (!trackedContract) {
2018-05-22 15:37:04 -04:00
return self.deployContract(contract, next);
}
2018-05-18 22:40:47 -04:00
self.blockchain.getCode(trackedContract.address, function(_getCodeErr, codeInChain) {
if (codeInChain !== "0x") {
self.contractAlreadyDeployed(contract, trackedContract, next);
} else {
2018-05-22 15:37:04 -04:00
self.deployContract(contract, next);
}
});
});
}
], callback);
2018-01-05 15:10:47 -05:00
}
2017-02-18 16:53:49 -05:00
2018-01-05 15:10:47 -05:00
contractAlreadyDeployed(contract, trackedContract, callback) {
const self = this;
2018-05-08 17:49:46 -04:00
self.logger.info(contract.className.bold.cyan + __(" already deployed at ").green + trackedContract.address.bold.cyan);
2018-01-05 15:10:47 -05:00
contract.deployedAddress = trackedContract.address;
2018-05-20 19:59:35 -04:00
self.events.emit("deploy:contract:deployed", contract);
2018-01-05 15:10:47 -05:00
2018-05-23 11:16:13 -04:00
// TODO: can be moved into a afterDeploy event
// just need to figure out the gasLimit coupling issue
self.events.request('code-generator:contract:vanilla', contract, contract._gasLimit, (contractCode) => {
2018-05-23 11:16:56 -04:00
self.events.request('runcode:eval', contractCode);
2018-05-23 11:16:13 -04:00
return callback();
});
2018-01-05 15:10:47 -05:00
}
2018-05-22 15:37:04 -04:00
deployContract(contract, callback) {
2017-03-30 20:12:39 +09:00
let self = this;
2018-01-18 14:41:33 -05:00
let accounts = [];
2018-05-22 15:37:04 -04:00
let contractParams = (contract.realArgs || contract.args).slice();
2018-01-18 14:41:33 -05:00
let contractCode = contract.code;
2018-05-18 18:31:47 -04:00
let deploymentAccount = self.blockchain.defaultAccount();
2018-01-18 14:41:33 -05:00
let deployObject;
async.waterfall([
// TODO: can potentially go to a beforeDeploy plugin
2018-01-18 14:41:33 -05:00
function getAccounts(next) {
2018-05-18 18:31:47 -04:00
self.blockchain.getAccounts(function (err, _accounts) {
2018-01-18 14:41:33 -05:00
if (err) {
2018-01-18 14:46:53 -05:00
return next(new Error(err));
2018-01-18 14:41:33 -05:00
}
accounts = _accounts;
// applying deployer account configuration, if any
2018-06-20 14:06:15 -04:00
if (typeof contract.fromIndex === 'number') {
deploymentAccount = accounts[contract.fromIndex];
2018-01-19 20:38:28 -05:00
if (deploymentAccount === undefined) {
2018-05-08 17:49:46 -04:00
return next(__("error deploying") + " " + contract.className + ": " + __("no account found at index") + " " + contract.fromIndex + __(" check the config"));
2018-01-19 20:38:28 -05:00
}
}
2018-06-20 14:06:15 -04:00
if (typeof contract.from === 'string' && typeof contract.fromIndex !== 'undefined') {
2018-05-08 17:49:46 -04:00
self.logger.warn(__('Both "from" and "fromIndex" are defined for contract') + ' "' + contract.className + '". ' + __('Using "from" as deployer account.'));
}
2018-06-20 14:06:15 -04:00
if (typeof contract.from === 'string') {
deploymentAccount = contract.from;
}
2018-01-18 14:41:33 -05:00
deploymentAccount = deploymentAccount || accounts[0];
next();
});
},
function doLinking(next) {
self.events.request('contracts:list', (_err, contracts) => {
for (let contractObj of contracts) {
let filename = contractObj.filename;
let deployedAddress = contractObj.deployedAddress;
if (deployedAddress) {
deployedAddress = deployedAddress.substr(2);
}
let linkReference = '__' + filename + ":" + contractObj.className;
if (contractCode.indexOf(linkReference.substr(0, 38)) < 0) { // substr to simulate the cut that solc does
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 {{contractName}}", {linkReference: linkReference, filename: filename, contractName: contractObj.className})));
}
let toReplace = linkReference + "_".repeat(40 - linkReference.length);
if (deployedAddress === undefined) {
let libraryName = contractObj.className;
return next(new Error(__("{{contractName}} needs {{libraryName}} but an address was not found, did you deploy it or configured an address?", {contractName: contract.className, libraryName: libraryName})));
}
contractCode = contractCode.replace(new RegExp(toReplace, "g"), deployedAddress);
2018-01-18 14:41:33 -05:00
}
// saving code changes back to contract object
contract.code = contractCode;
next();
});
2018-01-18 14:41:33 -05:00
},
function applyBeforeDeploy(next) {
2018-05-30 08:00:31 -04:00
self.plugins.emitAndRunActionsForEvent('deploy:contract:beforeDeploy', {contract: contract}, next);
2018-01-18 14:41:33 -05:00
},
function getGasPriceForNetwork(next) {
self.events.request("blockchain:gasPrice", (gasPrice) => {
contract.gasPrice = contract.gasPrice || gasPrice;
next();
});
},
2018-01-18 14:41:33 -05:00
function createDeployObject(next) {
let contractObject = self.blockchain.ContractObject({abi: contract.abiDefinition});
2018-01-18 14:41:33 -05:00
try {
2018-04-13 15:48:19 -04:00
const dataCode = contractCode.startsWith('0x') ? contractCode : "0x" + contractCode;
2018-05-18 20:26:21 -04:00
deployObject = self.blockchain.deployContractObject(contractObject, {arguments: contractParams, data: dataCode});
2018-01-18 14:41:33 -05:00
} catch(e) {
2018-03-02 17:48:30 -05:00
if (e.message.indexOf('Invalid number of parameters for "undefined"') >= 0) {
2018-05-08 17:49:46 -04:00
return next(new Error(__("attempted to deploy %s without specifying parameters", contract.className)));
2018-01-18 14:41:33 -05:00
} else {
return next(new Error(e));
}
2018-01-05 15:10:47 -05:00
}
2018-01-18 14:41:33 -05:00
next();
},
2018-01-19 13:57:35 -05:00
function estimateCorrectGas(next) {
if (contract.gas === 'auto') {
return self.blockchain.estimateDeployContractGas(deployObject, (err, gasValue) => {
if (err) {
return next(err);
}
2018-01-19 13:57:35 -05:00
contract.gas = gasValue;
next();
});
2018-01-19 13:57:35 -05:00
}
next();
},
2018-01-18 14:41:33 -05:00
function deployTheContract(next) {
let estimatedCost = contract.gas * contract.gasPrice;
self.logger.info(__("deploying") + " " + contract.className.bold.cyan + " " + __("with").green + " " + contract.gas + " " + __("gas at the price of").green + " " + contract.gasPrice + " " + __("Wei, estimated cost:").green + " " + estimatedCost + " Wei".green);
2018-01-18 14:41:33 -05:00
2018-05-18 20:26:21 -04:00
self.blockchain.deployContractFromObject(deployObject, {
2018-01-18 14:41:33 -05:00
from: deploymentAccount,
gas: contract.gas,
gasPrice: contract.gasPrice
2018-05-18 20:26:21 -04:00
}, function(error, receipt) {
if (error) {
2018-05-22 15:37:04 -04:00
contract.error = error.message;
self.events.emit("deploy:contract:error", contract);
2018-05-18 20:26:21 -04:00
return next(new Error("error deploying =" + contract.className + "= due to error: " + error.message));
2018-01-18 14:41:33 -05:00
}
self.logger.info(contract.className.bold.cyan + " " + __("deployed at").green + " " + receipt.contractAddress.bold.cyan + " " + __("using").green + " " + receipt.gasUsed + " " + __("gas").green);
2018-05-18 20:26:21 -04:00
contract.deployedAddress = receipt.contractAddress;
contract.transactionHash = receipt.transactionHash;
2018-06-28 14:01:44 -04:00
receipt.className = contract.className;
2018-05-22 15:37:04 -04:00
self.events.emit("deploy:contract:receipt", receipt);
self.events.emit("deploy:contract:deployed", contract);
// TODO: can be moved into a afterDeploy event
// just need to figure out the gasLimit coupling issue
self.events.request('code-generator:contract:vanilla', contract, contract._gasLimit, (contractCode) => {
2018-05-22 15:37:04 -04:00
self.events.request('runcode:eval', contractCode);
2018-05-28 19:40:55 -04:00
self.plugins.runActionsForEvent('deploy:contract:deployed', {contract: contract}, () => {
2018-05-22 15:37:04 -04:00
return next(null, receipt);
});
});
2018-01-18 14:41:33 -05:00
});
}
], callback);
2017-03-30 20:12:39 +09:00
}
}
2016-08-14 08:04:34 -04:00
module.exports = ContractDeployer;