OpenParable
Skip to content
← Back to skills

Get Block Number

Development
get-block-number

Get Block Number is an Agent Skill for development published in the tradingstrategy-ai/web3-ethereum-defi repository on GitHub: a SKILL.md instruction file an AI agent can load to do this task.

Get the latest block number for a blockchain using Web3.py and JSON-RPC environment variables

View on GitHub ↗
QUICK START

How to use this skill

Bring this guide into your coding agent with a prompt tailored to the tool you use.

  1. Open your project in Codex.
  2. Copy and paste the prompt below.
  3. Review files and risks before approving any action.
Prompt to paste
Inspect the Agent Skill at https://github.com/tradingstrategy-ai/web3-ethereum-defi/blob/HEAD/.claude/skills/get-block-number/SKILL.md and all supporting files. Report provenance, permissions, scripts, dependencies, and security risks. Propose an exact installation into .agents/skills/get-block-number, but wait for my approval before writing files or running scripts.

Copying this prompt does not install or run the skill. Review third-party files before use.

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

  1. 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