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.
84 lines
2.7 KiB
84 lines
2.7 KiB
7 years ago
|
const EthQuery = require('ethjs-query')
|
||
|
const {
|
||
|
hexToBn,
|
||
|
BnMultiplyByFraction,
|
||
|
bnToHex,
|
||
|
} = require('./util')
|
||
8 years ago
|
|
||
|
/*
|
||
|
tx-utils are 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.
|
||
|
*/
|
||
8 years ago
|
|
||
7 years ago
|
module.exports = class txProvideUtil {
|
||
7 years ago
|
constructor (provider) {
|
||
|
this.query = new EthQuery(provider)
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
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) {
|
||
|
if (err.message.includes('Transaction execution error.')) {
|
||
|
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
|
async estimateTxGas (txMeta, blockGasLimitHex) {
|
||
8 years ago
|
const txParams = txMeta.txParams
|
||
8 years ago
|
// check if gasLimit is already specified
|
||
8 years ago
|
txMeta.gasLimitSpecified = Boolean(txParams.gas)
|
||
8 years ago
|
// if not, fallback to block gasLimit
|
||
8 years ago
|
if (!txMeta.gasLimitSpecified) {
|
||
8 years ago
|
const blockGasLimitBN = hexToBn(blockGasLimitHex)
|
||
8 years ago
|
const saferGasLimitBN = BnMultiplyByFraction(blockGasLimitBN, 19, 20)
|
||
8 years ago
|
txParams.gas = bnToHex(saferGasLimitBN)
|
||
8 years ago
|
}
|
||
7 years ago
|
// run tx
|
||
|
return await this.query.estimateGas(txParams)
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
setTxGas (txMeta, blockGasLimitHex, estimatedGasHex) {
|
||
8 years ago
|
txMeta.estimatedGas = estimatedGasHex
|
||
|
const txParams = txMeta.txParams
|
||
8 years ago
|
|
||
8 years ago
|
// if gasLimit was specified and doesnt OOG,
|
||
|
// use original specified amount
|
||
8 years ago
|
if (txMeta.gasLimitSpecified) {
|
||
|
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
|
||
|
}
|
||
|
|
||
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
|
}
|
||
8 years ago
|
|
||
7 years ago
|
async validateTxParams (txParams) {
|
||
|
if (('value' in txParams) && txParams.value.indexOf('-') === 0) {
|
||
|
throw new Error(`Invalid transaction value of ${txParams.value} not a positive number.`)
|
||
|
}
|
||
8 years ago
|
}
|
||
7 years ago
|
}
|