inline _estimateFees()

This commit is contained in:
poma 2021-09-03 16:09:26 +03:00
parent a8e504054e
commit c6652baa7c
No known key found for this signature in database
GPG Key ID: BA20CB01FE165657
2 changed files with 9 additions and 27 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "tx-manager", "name": "tx-manager",
"version": "0.4.0", "version": "0.4.1",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

View File

@ -409,28 +409,6 @@ class Transaction {
return this._wallet.getTransactionCount('latest') return this._wallet.getTransactionCount('latest')
} }
/**
* Fetches baseFee from chain and calculate fee params
*
* @returns {Promise<object>}
* @private
*/
async _estimateFees() {
const block = await this._provider.getBlock('latest')
let maxFeePerGas = null,
maxPriorityFeePerGas = null
if (block && block.baseFeePerGas) {
maxPriorityFeePerGas = parseUnits(this.config.PRIORITY_FEE_GWEI.toString(), 'gwei')
maxFeePerGas = block.baseFeePerGas
.mul(100 + this.config.BASE_FEE_RESERVE_PERCENTAGE)
.div(100)
.add(maxPriorityFeePerGas)
}
return { maxFeePerGas, maxPriorityFeePerGas }
}
/** /**
* Choose network gas params * Choose network gas params
* *
@ -438,15 +416,19 @@ class Transaction {
* @private * @private
*/ */
async _getGasParams() { async _getGasParams() {
const { maxFeePerGas, maxPriorityFeePerGas } = await this._estimateFees()
const maxGasPrice = parseUnits(this.config.MAX_GAS_PRICE.toString(), 'gwei') const maxGasPrice = parseUnits(this.config.MAX_GAS_PRICE.toString(), 'gwei')
const block = await this._provider.getBlock('latest')
// Check network support for EIP-1559 // Check network support for EIP-1559
if (maxFeePerGas && maxPriorityFeePerGas) { if (block && block.baseFeePerGas) {
const maxPriorityFeePerGas = parseUnits(this.config.PRIORITY_FEE_GWEI.toString(), 'gwei')
const maxFeePerGas = block.baseFeePerGas
.mul(100 + this.config.BASE_FEE_RESERVE_PERCENTAGE)
.div(100)
.add(maxPriorityFeePerGas)
return { return {
maxFeePerGas: min(maxFeePerGas, maxGasPrice).toHexString(), maxFeePerGas: min(maxFeePerGas, maxGasPrice).toHexString(),
maxPriorityFeePerGas: maxPriorityFeePerGas.toHexString(), maxPriorityFeePerGas: min(maxPriorityFeePerGas, maxGasPrice).toHexString(),
type: 2, type: 2,
} }
} else { } else {