Refactor `analyzeGasUsage` to return results (#8487)

`analyzeGasUsage` now returns the results of the analysis rather than
setting them directly on `txMeta`. The caller is now responsible for
mutating `txMeta` instead. Functionally this should be identical to
before.
feature/default_network_editable
Mark Stacey 5 years ago committed by GitHub
parent 157cc98c3a
commit c2b588975c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      app/scripts/controllers/transactions/index.js
  2. 20
      app/scripts/controllers/transactions/tx-gas-utils.js

@ -300,7 +300,14 @@ export default class TransactionController extends EventEmitter {
txMeta.gasLimitSpecified = Boolean(txParams.gas) txMeta.gasLimitSpecified = Boolean(txParams.gas)
return txMeta return txMeta
} }
return await this.txGasUtil.analyzeGasUsage(txMeta)
const { blockGasLimit, estimatedGasHex, simulationFails } = await this.txGasUtil.analyzeGasUsage(txMeta)
if (simulationFails) {
txMeta.simulationFails = simulationFails
} else {
this.txGasUtil.setTxGas(txMeta, blockGasLimit, estimatedGasHex)
}
return txMeta
} }
/** /**

@ -3,6 +3,15 @@ import { hexToBn, BnMultiplyByFraction, bnToHex } from '../../lib/util'
import log from 'loglevel' import log from 'loglevel'
import { addHexPrefix } from 'ethereumjs-util' import { addHexPrefix } from 'ethereumjs-util'
/**
* Result of gas analysis, including either a gas estimate for a successful analysis, or
* debug information for a failed analysis.
* @typedef {Object} GasAnalysisResult
* @property {string} blockGasLimit - The gas limit of the block used for the analysis
* @property {string} estimatedGasHex - The estimated gas, in hexidecimal
* @property {Object} simulationFails - Debug information about why an analysis failed
*/
/** /**
tx-gas-utils are gas utility methods for Transaction manager tx-gas-utils are gas utility methods for Transaction manager
its passed ethquery its passed ethquery
@ -18,25 +27,24 @@ export default class TxGasUtil {
/** /**
@param {Object} txMeta - the txMeta object @param {Object} txMeta - the txMeta object
@returns {Object} - the txMeta object with the gas written to the txParams @returns {GasAnalysisResult} The result of the gas analysis
*/ */
async analyzeGasUsage (txMeta) { async analyzeGasUsage (txMeta) {
const block = await this.query.getBlockByNumber('latest', false) const block = await this.query.getBlockByNumber('latest', false)
let estimatedGasHex let estimatedGasHex
let simulationFails
try { try {
estimatedGasHex = await this.estimateTxGas(txMeta, block.gasLimit) estimatedGasHex = await this.estimateTxGas(txMeta, block.gasLimit)
} catch (err) { } catch (err) {
log.warn(err) log.warn(err)
txMeta.simulationFails = { simulationFails = {
reason: err.message, reason: err.message,
errorKey: err.errorKey, errorKey: err.errorKey,
debug: { blockNumber: block.number, blockGasLimit: block.gasLimit }, debug: { blockNumber: block.number, blockGasLimit: block.gasLimit },
} }
return txMeta
} }
this.setTxGas(txMeta, block.gasLimit, estimatedGasHex)
return txMeta return { blockGasLimit: block.gasLimit, estimatedGasHex, simulationFails }
} }
/** /**

Loading…
Cancel
Save