Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor analyzeGasUsage to return results #8487

Merged
merged 1 commit into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion app/scripts/controllers/transactions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,14 @@ export default class TransactionController extends EventEmitter {
txMeta.gasLimitSpecified = Boolean(txParams.gas)
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
}

/**
Expand Down
20 changes: 14 additions & 6 deletions app/scripts/controllers/transactions/tx-gas-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ import { hexToBn, BnMultiplyByFraction, bnToHex } from '../../lib/util'
import log from 'loglevel'
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
its passed ethquery
Expand All @@ -18,25 +27,24 @@ export default class TxGasUtil {

/**
@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) {
const block = await this.query.getBlockByNumber('latest', false)
let estimatedGasHex
let simulationFails
try {
estimatedGasHex = await this.estimateTxGas(txMeta, block.gasLimit)
} catch (err) {
log.warn(err)
txMeta.simulationFails = {
simulationFails = {
reason: err.message,
errorKey: err.errorKey,
debug: { blockNumber: block.number, blockGasLimit: block.gasLimit },
}

return txMeta
}
this.setTxGas(txMeta, block.gasLimit, estimatedGasHex)
return txMeta

return { blockGasLimit: block.gasLimit, estimatedGasHex, simulationFails }
}

/**
Expand Down