Work in progress: Vibe coding a Python web3 app that:
- Connects to the Ethereum Sepolia testnet using
web3.pyto query the ETH balance of a user-provided address via the CLI. - Connects to the Solana Devnet using
solana-pyto query the SOL balance of a user-provided address via the CLI.
This project demonstrates basic Python programming, blockchain interaction, and API development/documentation with FastAPI.
This is a learning exercise where I used AI to help guide me through the process of developing a web3 app that queries the blockchain. As someone actively learning Python, this was extremely helpful and conducive to learning. To that end, I made sure to add comments on every line to document what the code does.
- Connects to the Sepolia testnet via a QuickNode endpoint.
- Prompts users to input an Ethereum address and validates it using
web3.py's checksum address conversion. - Queries and displays the ETH balance of the provided address.
- Includes a FastAPI back-end with endpoints documented.
- Uses Coinbase Wallet for testing with Sepolia test ETH.
- Implements error handling for invalid addresses and connection issues.
- Python 3: The core programming language.
web3.py(v7.12.0): Python library for Ethereum blockchain interaction.python-dotenv: Loads environment variables (e.g., QuickNode API URL) from a.envfile.- FastAPI: Provides a framework for automated API documentaton for all endpoints.
- Coinbase Wallet: Non-custodial wallet for managing Sepolia testnet addresses and test ETH.
- QuickNode: Provides the Sepolia testnet endpoint for blockchain access.
- Render: Hosting platform for deploying the FastAPI backend for public API access.
- Python 3.6+ installed.
- A Coinbase Wallet set to the Sepolia testnet.
- Test ETH in your Coinbase Wallet’s Sepolia address obtained from a faucet (like PoW Faucet).
- If you don't have a test wallet loaded with test ETH, any valid Ethereum address will work.
- A QuickNode account with a Sepolia endpoint URL.
-
Clone the repo:
git clone https://github.com/nicoalba/python-web3-app.git cd python-web3-app -
Set up a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install web3 python-dotenv fastapi uvicorn
-
Configure the environment variables:
-
Create a
.envfile in the project root. -
Add your QuickNode Sepolia endpoint URL:
WEB3_PROVIDER_URL=https://your-quicknode-sepolia-url
You can get your URL by signing up at QuickNode and creating a Sepolia endpoint.
-
-
Set up Coinbase Wallet:
- Install the Coinbase Wallet browser extension.
- Switch to the Sepolia testnet in the wallet.
- Fund your Sepolia address with test ETH from the QuickNode Ethereum Faucet.
-
Run the script:
python eth-sol-web3.py
-
Follow the prompt and enter a valid Ethereum or Solana address.
The script will:
- Load the QuickNode or Solana endpoint from the
.envfile. - Connect to the Sepolia testnet or Solana Devnet and display the latest block number.
- Validate the address and display the ETH or SOL balance.
- Load the QuickNode or Solana endpoint from the
Looking for .env file at: /path/to/python-web3-app/.env
web3.py version: 7.12.0
WEB3_PROVIDER_URL: https://omniscient-distinguished-sailboat...
Connected to blockchain!
Latest block number: 8693694
Enter an Ethereum address to check balance: 0xYourAddress
ETH balance of 0xYourAddress: 0.1 ETH
- Ethereum (Sepolia):
0x742d35Cc6634C0532925a3b844Bc454e4438f44e - Solana (Devnet):
4Nd1mGJqD1bNQcb3KgVsC4MQpRxHBaHCErBp9aZ9bJfF
-
Run the FastAPI server:
uvicorn eth-web3-app:app --reload
-
Go to http://localhost:8000/docs in a browser to view interactive API documentation:
The FastAPI backend is deployed on Render for public access. Go to https://python-web3-app.onrender.com/docs to explore the interactive API documentation and test the endpoints:
GET /block-number: Get the latest Sepolia testnet block number.GET /balance/{address}: Query the ETH balance of a valid Ethereum address.GET /solana-balance/{address}: Query the SOL balance of a valid Solana address.
To test the API using curl:
curl https://python-web3-app.onrender.com/block-number
curl https://python-web3-app.onrender.com/balance/0xYourValidAddressNote: The free-tier Render instance spins down after 15 minutes of inactivity, causing a 30-sec delay on the next request.
- Add ERC-20 token balance queries for Sepolia test tokens.
- Configure with CORS for cross-origin requests.
