From c1c6079745a2b6dc6588ff37ddecc76b6d58379b Mon Sep 17 00:00:00 2001 From: Danil Kovtonyuk Date: Mon, 23 May 2022 18:47:40 +1000 Subject: [PATCH] fix: use eth_maxPriorityFeePerGas --- src/Transaction.js | 28 ++++++++++++---------------- src/TxManager.js | 3 ++- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/Transaction.js b/src/Transaction.js index de80468..bae4e08 100644 --- a/src/Transaction.js +++ b/src/Transaction.js @@ -1,5 +1,5 @@ const ethers = require('ethers') -const { parseUnits, formatUnits, hexValue } = ethers.utils +const { parseUnits, formatUnits } = ethers.utils const BigNumber = ethers.BigNumber const PromiEvent = require('web3-core-promievent') const { sleep, min, max } = require('./utils') @@ -429,25 +429,21 @@ class Transaction { * @returns {Promise} * @private */ - async _estimatePriorityFee(blockNumber) { - const feeHistoryBlocks = 1 - const feeHistoryPercentile = 50 - const defaultPriorityFee = parseUnits(this.config.PRIORITY_FEE_GWEI.toString(), 'gwei') + async _estimatePriorityFee() { + const defaultPriorityFee = parseUnits(this.config.DEFAULT_PRIORITY_FEE.toString(), 'gwei') try { - const { reward } = await this._provider.send('eth_feeHistory', [ - hexValue(feeHistoryBlocks), - hexValue(blockNumber), - [feeHistoryPercentile], - ]) + const estimatedPriorityFee = await this._provider.send('eth_maxPriorityFeePerGas', []) - const historyPriorityFee = reward[0][0] - - if (historyPriorityFee) { - return max(BigNumber.from(historyPriorityFee), defaultPriorityFee) + if (!estimatedPriorityFee || isNaN(estimatedPriorityFee)) { + return defaultPriorityFee } - return defaultPriorityFee + const bumpedPriorityFee = BigNumber.from(estimatedPriorityFee) + .mul(100 + this.config.PRIORITY_FEE_RESERVE_PERCENTAGE) + .div(100) + + return max(bumpedPriorityFee, defaultPriorityFee) } catch (err) { console.error('_estimatePriorityFee has error:', err.message) return defaultPriorityFee @@ -466,7 +462,7 @@ class Transaction { // Check network support for EIP-1559 if (this.config.ENABLE_EIP1559 && block && block.baseFeePerGas) { - const maxPriorityFeePerGas = await this._estimatePriorityFee(block.number) + const maxPriorityFeePerGas = await this._estimatePriorityFee() const maxFeePerGas = block.baseFeePerGas .mul(100 + this.config.BASE_FEE_RESERVE_PERCENTAGE) diff --git a/src/TxManager.js b/src/TxManager.js index 18d143a..85a774e 100644 --- a/src/TxManager.js +++ b/src/TxManager.js @@ -16,8 +16,9 @@ const defaultConfig = { THROW_ON_REVERT: true, BLOCK_GAS_LIMIT: null, ENABLE_EIP1559: true, - PRIORITY_FEE_GWEI: 3, + DEFAULT_PRIORITY_FEE: 3, BASE_FEE_RESERVE_PERCENTAGE: 50, + PRIORITY_FEE_RESERVE_PERCENTAGE: 10, } class TxManager {