Skip to content

Commit

Permalink
fix: issue with fee history for some nodes (#1812)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Jan 4, 2024
1 parent b617e43 commit 9fb5b72
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/ape/utils/basemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def __ape_extra_attributes__(self) -> Iterator[ExtraModelAttributes]:
def __getattr__(self, name: str) -> Any:
"""
An overridden ``__getattr__`` implementation that takes into
account :meth:`~ape.utils.basemodel.BaseModel.__ape_extra_attributes__`.
account :meth:`~ape.utils.basemodel.ExtraAttributesMixin.__ape_extra_attributes__`.
"""

private_attrs = self.__pydantic_private__ or {}
Expand Down
14 changes: 11 additions & 3 deletions src/ape_ethereum/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from web3.middleware.validation import MAX_EXTRADATA_LENGTH
from web3.providers import AutoProvider
from web3.providers.auto import load_provider_from_environment
from web3.types import RPCEndpoint, TxParams
from web3.types import FeeHistory, RPCEndpoint, TxParams

from ape.api import BlockAPI, ProviderAPI, ReceiptAPI, TransactionAPI
from ape.api.networks import LOCAL_NETWORK_NAME
Expand Down Expand Up @@ -161,8 +161,8 @@ def base_fee(self) -> int:
return 0

try:
fee_history = self.web3.eth.fee_history(1, BlockNumber(latest_block_number))
except ValueError as exc:
fee_history = self._get_fee_history(latest_block_number)
except Exception as exc:
# Use the less-accurate approach (OK for testing).
logger.debug(
"Failed using `web3.eth.fee_history` for network "
Expand All @@ -181,6 +181,14 @@ def base_fee(self) -> int:

return pending_base_fee

def _get_fee_history(self, block_number: int) -> FeeHistory:
# NOTE: Have to `reward_percentiles=[]` because of this bug:
# https://github.com/ethereum/web3.py/issues/3184
try:
return self.web3.eth.fee_history(1, BlockNumber(block_number), reward_percentiles=[])
except MethodUnavailable as err:
raise APINotImplementedError(str(err)) from err

def _get_last_base_fee(self) -> int:
block = self.get_block("latest")
base_fee = getattr(block, "base_fee", None)
Expand Down
10 changes: 10 additions & 0 deletions tests/functional/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,13 @@ def custom_make_request(rpc, params):
match="RPC method 'ape_thisDoesNotExist' is not implemented by this node instance.",
):
eth_tester_provider._make_request("ape_thisDoesNotExist")


def test_base_fee(eth_tester_provider):
actual = eth_tester_provider.base_fee
assert actual > 0

# NOTE: Mostly doing this to ensure we are calling the fee history
# RPC correctly. There was a bug where we were not.
with pytest.raises(APINotImplementedError):
_ = eth_tester_provider._get_fee_history(0)

0 comments on commit 9fb5b72

Please sign in to comment.