get_fork_safe_latest_block

Documentation for eth_defi.provider.broken_provider.get_fork_safe_latest_block function.

get_fork_safe_latest_block(web3)

Get the latest block identifier that is safe for Anvil mainnet forks.

  • For Anvil mainnet forks, returns web3.eth.block_number (an integer) instead of "latest" to prevent the upstream RPC from resolving "latest" to the actual chain tip (which may be beyond the fork block or the upstream’s available window).

  • For non-fork Anvil (test backend), returns "latest" as there is no upstream RPC.

  • For non-Anvil providers, returns "latest".

This is needed because web3.py v7 forwards "latest" to the upstream RPC when Anvil needs to fetch state not cached at fork time. The upstream then resolves "latest" to its own chain tip, causing BlockOutOfRangeError if the upstream has a limited block window.

See get_safe_cached_latest_block_number() which uses this function.

Parameters

web3 (web3.main.Web3) –

Return type

Union[Literal[‘latest’, ‘earliest’, ‘pending’, ‘safe’, ‘finalized’], eth_typing.evm.BlockNumber, eth_typing.evm.Hash32, eth_typing.encoding.HexStr, int]