forked from ethereum/web3.py
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: utility for estimating maxPriorityFeePerGas via eth_feeHistory
Refactor idea from PR ethereum#2259 into sync and async fee utility methods. Change params passed into eth_feeHistory to values that allowed for better results when we tested locally. Add a min and max to the estimated fee history so that we don't allow unsuspecting users to contribute to fee bloating. Max and min values keep the priority fee within a range that healthy blocks should accept, so these transactions would be accepted when fee prices settle from high-fee periods.
- Loading branch information
Showing
5 changed files
with
87 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Calculate a default ``maxPriorityFeePerGas`` using ``eth_feeHistory`` when ``eth_maxPriorityFeePerGas`` is not available, since the latter is not a part of the Ethereum JSON-RPC specs and only supported by certain clients. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from typing import ( | ||
TYPE_CHECKING, | ||
) | ||
|
||
from web3.types import ( | ||
FeeHistory, | ||
Wei, | ||
) | ||
|
||
if TYPE_CHECKING: | ||
from web3.eth import ( | ||
AsyncEth, # noqa: F401 | ||
Eth, # noqa: F401 | ||
) | ||
|
||
PRIORITY_FEE_MAX = Wei(1500000000) # 1.5 gwei | ||
PRIORITY_FEE_MIN = Wei(1000000000) # 1 gwei | ||
|
||
# 5th percentile fee history from the last 10 blocks | ||
PRIORITY_FEE_HISTORY_PARAMS = (10, 'pending', [5.0]) | ||
|
||
|
||
def _fee_history_priority_fee_estimate(fee_history: FeeHistory) -> Wei: | ||
# grab only non-zero fees and average against only that list | ||
non_empty_block_fees = [fee[0] for fee in fee_history['reward'] if fee[0] != 0] | ||
|
||
# prevent division by zero in the extremely unlikely case that all fees within the polled fee | ||
# history range for the specified percentile are 0 | ||
divisor = len(non_empty_block_fees) if len(non_empty_block_fees) != 0 else 1 | ||
|
||
priority_fee_average_for_percentile = Wei( | ||
round(sum(non_empty_block_fees) / divisor) | ||
) | ||
|
||
return ( # keep estimated priority fee within a max / min range | ||
PRIORITY_FEE_MAX if priority_fee_average_for_percentile > PRIORITY_FEE_MAX else | ||
PRIORITY_FEE_MIN if priority_fee_average_for_percentile < PRIORITY_FEE_MIN else | ||
priority_fee_average_for_percentile | ||
) | ||
|
||
|
||
def fee_history_priority_fee(eth: "Eth") -> Wei: | ||
# This is a tested internal call so no need for type hinting. We can keep better consistency | ||
# between the sync and async calls by unpacking PRIORITY_FEE_HISTORY_PARAMS as constants here. | ||
fee_history = eth.fee_history(*PRIORITY_FEE_HISTORY_PARAMS) # type: ignore | ||
return _fee_history_priority_fee_estimate(fee_history) | ||
|
||
|
||
async def async_fee_history_priority_fee(async_eth: "AsyncEth") -> Wei: | ||
# This is a tested internal call so no need for type hinting. We can keep better consistency | ||
# between the sync and async calls by unpacking PRIORITY_FEE_HISTORY_PARAMS as constants here. | ||
fee_history = await async_eth.fee_history(*PRIORITY_FEE_HISTORY_PARAMS) # type: ignore | ||
return _fee_history_priority_fee_estimate(fee_history) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters