get-block-number
Get the latest block number for a blockchain using Web3.py and JSON-RPC environment variables
When & Why to Use This Skill
This Claude skill provides a streamlined way to retrieve the latest block number from any EVM-compatible blockchain (such as Ethereum, Arbitrum, Base, or Polygon) using Web3.py. By leveraging JSON-RPC environment variables, it allows developers and analysts to fetch real-time network height data directly through a Python-driven interface, ensuring accuracy by executing live scripts rather than relying on cached information.
Use Cases
- Network Health Monitoring: Quickly verify if a blockchain network is operational and identify the current head of the chain for synchronization checks.
- Smart Contract Debugging: Determine the exact block height during transaction execution to troubleshoot timing issues or event logs in development and production environments.
- Data Indexing & Automation: Use as a foundational step for scripts that need to define a range for fetching logs, filtering blockchain events, or updating dApp dashboards.
- Multi-chain Management: Easily switch between different networks (Ethereum, Polygon, etc.) by simply providing the chain name, making it ideal for cross-chain developers.
| name | get-block-number |
|---|---|
| description | Get the latest block number for a blockchain using Web3.py and JSON-RPC environment variables |
Get latest block number
This skill retrieves the latest block number from a blockchain using the configured JSON-RPC environment variables and Web3.py.
ALWAYS USE SCRIPT. NEVER RELY ON THE HISTORICAL INFORMATION OR GUESS.
Required inputs
- Chain name: The blockchain to query (e.g., Ethereum, Arbitrum, Base, Polygon)
Running the script
Generate and run a Python script to fetch the block number. Run it Python commadn line inline, don't write a new file.
import os
from web3 import Web3
from eth_defi.provider.multi_provider import create_multi_provider_web3
# Replace {CHAIN} with the uppercase chain name
json_rpc_url = os.environ.get("JSON_RPC_{CHAIN}")
if not json_rpc_url:
raise ValueError("JSON_RPC_{CHAIN} environment variable not set")
web3 = create_multi_provider_web3(json_rpc_url)
block_number = web3.eth.block_number
print(f"Latest block number: {block_number}")
Run the script with:
source .local-test.env && poetry run python <script_path>
Display output
Return the block number to the user in a clear format, e.g.:
Chain: Ethereum
Latest block number: 19,234,567