better gas estimation

This commit is contained in:
Iuri Matias 2018-01-19 13:57:35 -05:00
parent a6013b518c
commit 5300aa479b
2 changed files with 32 additions and 23 deletions

View File

@ -57,7 +57,7 @@ class ContractsManager {
contract.filename = compiledContract.filename; contract.filename = compiledContract.filename;
contract.gas = (contractConfig && contractConfig.gas) || self.contractsConfig.gas || 'auto'; contract.gas = (contractConfig && contractConfig.gas) || self.contractsConfig.gas || 'auto';
self.adjustGas(contract); //self.adjustGas(contract);
contract.gasPrice = contract.gasPrice || self.contractsConfig.gasPrice; contract.gasPrice = contract.gasPrice || self.contractsConfig.gasPrice;
contract.type = 'file'; contract.type = 'file';
@ -270,28 +270,28 @@ class ContractsManager {
return data; return data;
} }
adjustGas(contract) { //adjustGas(contract) {
let maxGas, adjustedGas; // let maxGas, adjustedGas;
if (contract.gas === 'auto') { // if (contract.gas === 'auto') {
if (contract.deploy || contract.deploy === undefined) { // if (contract.deploy || contract.deploy === undefined) {
if (contract.gasEstimates.creation !== undefined) { // if (contract.gasEstimates.creation !== undefined) {
// TODO: should sum it instead // // TODO: should sum it instead
maxGas = Math.max(contract.gasEstimates.creation[0], contract.gasEstimates.creation[1], 500000); // maxGas = Math.max(contract.gasEstimates.creation[0], contract.gasEstimates.creation[1], 500000);
} else { // } else {
maxGas = 500000; // maxGas = 500000;
} // }
} else { // } else {
maxGas = 500000; // maxGas = 500000;
} // }
// TODO: put a check so it doesn't go over the block limit // // TODO: put a check so it doesn't go over the block limit
adjustedGas = Math.round(maxGas * 1.40); // adjustedGas = Math.round(maxGas * 1.40);
adjustedGas += 25000; // adjustedGas += 25000;
contract.gas = adjustedGas; // contract.gas = adjustedGas;
if (this.gasLimit && this.gasLimit > contract.gas) { // if (this.gasLimit && this.gasLimit > contract.gas) {
contract.gas = this.gasLimit; // contract.gas = this.gasLimit;
} // }
} // }
} //}
} }
module.exports = ContractsManager; module.exports = ContractsManager;

View File

@ -262,6 +262,15 @@ class Deploy {
} }
next(); next();
}, },
function estimateCorrectGas(next) {
if (contract.gas === 'auto') {
return deployObject.estimateGas().then((gasValue) => {
contract.gas = gasValue;
next();
}).catch(next);
}
next();
},
function deployTheContract(next) { function deployTheContract(next) {
self.logger.info("deploying " + contract.className.bold.cyan + " with ".green + contract.gas + " gas".green); self.logger.info("deploying " + contract.className.bold.cyan + " with ".green + contract.gas + " gas".green);