You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
4.2 KiB
129 lines
4.2 KiB
7 years ago
|
const EthQuery = require('ethjs-query')
|
||
|
const {
|
||
|
hexToBn,
|
||
|
BnMultiplyByFraction,
|
||
|
bnToHex,
|
||
7 years ago
|
} = require('../../lib/util')
|
||
7 years ago
|
const { addHexPrefix } = require('ethereumjs-util')
|
||
7 years ago
|
const SIMPLE_GAS_COST = '0x5208' // Hex for 21000, cost of a simple send.
|
||
8 years ago
|
|
||
7 years ago
|
/**
|
||
|
tx-gas-utils are gas utility methods for Transaction manager
|
||
8 years ago
|
its passed ethquery
|
||
8 years ago
|
and used to do things like calculate gas of a tx.
|
||
7 years ago
|
@param {Object} provider - A network provider.
|
||
8 years ago
|
*/
|
||
8 years ago
|
|
||
7 years ago
|
class TxGasUtil {
|
||
7 years ago
|
|
||
7 years ago
|
constructor (provider) {
|
||
|
this.query = new EthQuery(provider)
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
7 years ago
|
/**
|
||
|
@param txMeta {Object} - the txMeta object
|
||
|
@returns {object} the txMeta object with the gas written to the txParams
|
||
|
*/
|
||
7 years ago
|
async analyzeGasUsage (txMeta) {
|
||
|
const block = await this.query.getBlockByNumber('latest', true)
|
||
7 years ago
|
let estimatedGasHex
|
||
|
try {
|
||
|
estimatedGasHex = await this.estimateTxGas(txMeta, block.gasLimit)
|
||
|
} catch (err) {
|
||
7 years ago
|
const simulationFailed = (
|
||
|
err.message.includes('Transaction execution error.') ||
|
||
|
err.message.includes('gas required exceeds allowance or always failing transaction')
|
||
|
)
|
||
7 years ago
|
if (simulationFailed) {
|
||
7 years ago
|
txMeta.simulationFails = true
|
||
|
return txMeta
|
||
|
}
|
||
|
}
|
||
7 years ago
|
this.setTxGas(txMeta, block.gasLimit, estimatedGasHex)
|
||
7 years ago
|
return txMeta
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
Estimates the tx's gas usage
|
||
|
@param txMeta {Object} - the txMeta object
|
||
|
@param blockGasLimitHex {string} - hex string of the block's gas limit
|
||
|
@returns {string} the estimated gas limit as a hex string
|
||
|
*/
|
||
7 years ago
|
async estimateTxGas (txMeta, blockGasLimitHex) {
|
||
8 years ago
|
const txParams = txMeta.txParams
|
||
7 years ago
|
|
||
8 years ago
|
// check if gasLimit is already specified
|
||
8 years ago
|
txMeta.gasLimitSpecified = Boolean(txParams.gas)
|
||
7 years ago
|
|
||
|
// if it is, use that value
|
||
|
if (txMeta.gasLimitSpecified) {
|
||
|
return txParams.gas
|
||
8 years ago
|
}
|
||
7 years ago
|
|
||
|
// if recipient has no code, gas is 21k max:
|
||
|
const recipient = txParams.to
|
||
|
const hasRecipient = Boolean(recipient)
|
||
7 years ago
|
let code
|
||
|
if (recipient) code = await this.query.getCode(recipient)
|
||
|
|
||
7 years ago
|
if (hasRecipient && (!code || code === '0x')) {
|
||
|
txParams.gas = SIMPLE_GAS_COST
|
||
7 years ago
|
txMeta.simpleSend = true // Prevents buffer addition
|
||
7 years ago
|
return SIMPLE_GAS_COST
|
||
|
}
|
||
|
|
||
|
// if not, fall back to block gasLimit
|
||
|
const blockGasLimitBN = hexToBn(blockGasLimitHex)
|
||
|
const saferGasLimitBN = BnMultiplyByFraction(blockGasLimitBN, 19, 20)
|
||
|
txParams.gas = bnToHex(saferGasLimitBN)
|
||
|
|
||
7 years ago
|
// run tx
|
||
|
return await this.query.estimateGas(txParams)
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
Writes the gas on the txParams in the txMeta
|
||
|
@param txMeta {Object} - the txMeta object to write to
|
||
|
@param blockGasLimitHex {string} - the block gas limit hex
|
||
|
@param estimatedGasHex {string} - the estimated gas hex
|
||
|
*/
|
||
7 years ago
|
setTxGas (txMeta, blockGasLimitHex, estimatedGasHex) {
|
||
7 years ago
|
txMeta.estimatedGas = addHexPrefix(estimatedGasHex)
|
||
8 years ago
|
const txParams = txMeta.txParams
|
||
8 years ago
|
|
||
8 years ago
|
// if gasLimit was specified and doesnt OOG,
|
||
|
// use original specified amount
|
||
7 years ago
|
if (txMeta.gasLimitSpecified || txMeta.simpleSend) {
|
||
8 years ago
|
txMeta.estimatedGas = txParams.gas
|
||
8 years ago
|
return
|
||
|
}
|
||
|
// if gasLimit not originally specified,
|
||
|
// try adding an additional gas buffer to our estimation for safety
|
||
8 years ago
|
const recommendedGasHex = this.addGasBuffer(txMeta.estimatedGas, blockGasLimitHex)
|
||
8 years ago
|
txParams.gas = recommendedGasHex
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
Adds a gas buffer with out exceeding the block gas limit
|
||
|
|
||
|
@param initialGasLimitHex {string} - the initial gas limit to add the buffer too
|
||
|
@param blockGasLimitHex {string} - the block gas limit
|
||
|
@returns {string} the buffered gas limit as a hex string
|
||
|
*/
|
||
8 years ago
|
addGasBuffer (initialGasLimitHex, blockGasLimitHex) {
|
||
8 years ago
|
const initialGasLimitBn = hexToBn(initialGasLimitHex)
|
||
|
const blockGasLimitBn = hexToBn(blockGasLimitHex)
|
||
8 years ago
|
const upperGasLimitBn = blockGasLimitBn.muln(0.9)
|
||
8 years ago
|
const bufferedGasLimitBn = initialGasLimitBn.muln(1.5)
|
||
8 years ago
|
|
||
8 years ago
|
// if initialGasLimit is above blockGasLimit, dont modify it
|
||
8 years ago
|
if (initialGasLimitBn.gt(upperGasLimitBn)) return bnToHex(initialGasLimitBn)
|
||
8 years ago
|
// if bufferedGasLimit is below blockGasLimit, use bufferedGasLimit
|
||
8 years ago
|
if (bufferedGasLimitBn.lt(upperGasLimitBn)) return bnToHex(bufferedGasLimitBn)
|
||
8 years ago
|
// otherwise use blockGasLimit
|
||
8 years ago
|
return bnToHex(upperGasLimitBn)
|
||
8 years ago
|
}
|
||
7 years ago
|
}
|
||
|
|
||
|
module.exports = TxGasUtil
|