get_almost_latest_block_number
Documentation for eth_defi.provider.broken_provider.get_almost_latest_block_number function.
- get_almost_latest_block_number(web3)
Get the latest block number with workarounds for low quality JSON-RPC service providers.
Use this method instead of
web3.eth.block_number.Because low quality providers may lose the block of this block number on the subsequent API calls, we add some number of delay or confirmations to the chain tip, specified by
get_block_tip_latency().Includes a monotonicity safety check: if the block number drops by more than
MAX_BLOCK_REGRESSIONblocks compared to the last observed value, raisesBlockNumberOutOfRange. This catches scenarios where the RPC provider silently starts returning data for the wrong chain after a failover.Providers with known issues
LlamaNodes
Ankr
Example:
from eth_defi.provider.broken_provider import get_almost_latest_block_number # We cannot query the chain head on Ankr or LlamaNodes, # so get the almost head if not block_number: block_number = get_almost_latest_block_number(web3) timestamp = fetch_block_timestamp(web3, block_number) token = fetch_erc20_details(web3, asset.address) amount = token.fetch_balance_of(address, block_identifier=block_number)
- Raises
BlockNumberOutOfRange – If the block number regresses by more than
MAX_BLOCK_REGRESSION.- Parameters
web3 (web3.main.Web3) –
- Return type