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

View File

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