aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/keyring-controller.js
diff options
context:
space:
mode:
authorDan Finlay <somniac@me.com>2016-11-23 06:42:46 +0800
committerGitHub <noreply@github.com>2016-11-23 06:42:46 +0800
commitc8176c9439cebf2cf65efbc73a633f8e1b643128 (patch)
tree5129bd47c58d3550ce41e6e8ed97f78dfadfad08 /app/scripts/keyring-controller.js
parente14efa8f175e17bd671e0c38b3e743716ea4aa19 (diff)
parentbc39cd7b894ddf0f3724d4af3cfc30c2638e0939 (diff)
downloadtangerine-wallet-browser-c8176c9439cebf2cf65efbc73a633f8e1b643128.tar
tangerine-wallet-browser-c8176c9439cebf2cf65efbc73a633f8e1b643128.tar.gz
tangerine-wallet-browser-c8176c9439cebf2cf65efbc73a633f8e1b643128.tar.bz2
tangerine-wallet-browser-c8176c9439cebf2cf65efbc73a633f8e1b643128.tar.lz
tangerine-wallet-browser-c8176c9439cebf2cf65efbc73a633f8e1b643128.tar.xz
tangerine-wallet-browser-c8176c9439cebf2cf65efbc73a633f8e1b643128.tar.zst
tangerine-wallet-browser-c8176c9439cebf2cf65efbc73a633f8e1b643128.zip
Merge pull request #875 from MetaMask/i868-estimateGasTooHigh-dev
I868 estimate gas too high dev
Diffstat (limited to 'app/scripts/keyring-controller.js')
-rw-r--r--app/scripts/keyring-controller.js66
1 files changed, 61 insertions, 5 deletions
diff --git a/app/scripts/keyring-controller.js b/app/scripts/keyring-controller.js
index 34d81ace3..7020c188a 100644
--- a/app/scripts/keyring-controller.js
+++ b/app/scripts/keyring-controller.js
@@ -1,4 +1,5 @@
const async = require('async')
+const bind = require('ap').partial
const ethUtil = require('ethereumjs-util')
const ethBinToOps = require('eth-bin-to-ops')
const EthQuery = require('eth-query')
@@ -367,7 +368,7 @@ module.exports = class KeyringController extends EventEmitter {
// calculate metadata for tx
async.parallel([
analyzeForDelegateCall,
- estimateGas,
+ analyzeGasUsage,
], didComplete)
// perform static analyis on the target contract code
@@ -390,14 +391,69 @@ module.exports = class KeyringController extends EventEmitter {
}
}
- function estimateGas (cb) {
- query.estimateGas(txParams, function (err, result) {
+ function analyzeGasUsage (cb) {
+ query.getBlockByNumber('latest', true, function (err, block) {
if (err) return cb(err)
- txData.estimatedGas = self.addGasBuffer(result)
- cb()
+ async.waterfall([
+ bind(estimateGas, txData, block.gasLimit),
+ bind(checkForGasError, txData),
+ bind(setTxGas, txData, block.gasLimit),
+ ], cb)
})
}
+ function estimateGas(txData, blockGasLimitHex, cb) {
+ const txParams = txData.txParams
+ // check if gasLimit is already specified
+ txData.gasLimitSpecified = Boolean(txParams.gas)
+ // if not, fallback to block gasLimit
+ if (!txData.gasLimitSpecified) {
+ txParams.gas = blockGasLimitHex
+ }
+ // run tx, see if it will OOG
+ query.estimateGas(txParams, cb)
+ }
+
+ function checkForGasError(txData, estimatedGasHex) {
+ txData.estimatedGas = estimatedGasHex
+ // all gas used - must be an error
+ if (estimatedGasHex === txData.txParams.gas) {
+ txData.simulationFails = true
+ }
+ cb()
+ }
+
+ function setTxGas(txData, blockGasLimitHex) {
+ const txParams = txData.txParams
+ // if OOG, nothing more to do
+ if (txData.simulationFails) {
+ cb()
+ return
+ }
+ // if gasLimit was specified and doesnt OOG,
+ // use original specified amount
+ if (txData.gasLimitSpecified) {
+ txData.estimatedGas = txParams.gas
+ cb()
+ return
+ }
+ // if gasLimit not originally specified,
+ // try adding an additional gas buffer to our estimation for safety
+ const estimatedGasBn = new BN(ethUtil.stripHexPrefix(txData.estimatedGas), 16)
+ const blockGasLimitBn = new BN(ethUtil.stripHexPrefix(blockGasLimitHex), 16)
+ const estimationWithBuffer = self.addGasBuffer(estimatedGasBn)
+ // added gas buffer is too high
+ if (estimationWithBuffer.gt(blockGasLimitBn)) {
+ txParams.gas = txData.estimatedGas
+ // added gas buffer is safe
+ } else {
+ const gasWithBufferHex = ethUtil.intToHex(estimationWithBuffer)
+ txParams.gas = gasWithBufferHex
+ }
+ cb()
+ return
+ }
+
function didComplete (err) {
if (err) return cb(err)
configManager.addTx(txData)