From 5300aa479b019186643f0e0d1697ecb3ecec6318 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 19 Jan 2018 13:57:35 -0500 Subject: [PATCH] better gas estimation --- lib/contracts/contracts.js | 46 +++++++++++++++++++------------------- lib/contracts/deploy.js | 9 ++++++++ 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/lib/contracts/contracts.js b/lib/contracts/contracts.js index 952fbdbb..104435fa 100644 --- a/lib/contracts/contracts.js +++ b/lib/contracts/contracts.js @@ -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; diff --git a/lib/contracts/deploy.js b/lib/contracts/deploy.js index 23183583..73026b0f 100644 --- a/lib/contracts/deploy.js +++ b/lib/contracts/deploy.js @@ -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);