- Published on
Ethereum Blockchain Indexer
- Authors

- Name
- Jayant Nagle
- https://www.x.com/thejnbtc
Title: Ethereum Blockchain Indexer
Github: https://github.com/jayantna/ethereum-indexer
Ethereum Blockchain Indexer
A Node.js application that indexes Ethereum blockchain data into a PostgreSQL database and provides API endpoints to query the indexed data.
Features
- Indexes Ethereum blocks and transactions
- Stores blockchain data in PostgreSQL
- Provides RESTful API endpoints to query indexed data
- Built with Node.js, Express, ethers.js, and PostgreSQL
Prerequisites
- Node.js (v14 or higher)
- PostgreSQL (v12 or higher)
- Alchemy API key
Installation
Clone the repository:
git clone https://github.com/yourusername/ethereum-indexer.git cd ethereum-indexerInstall dependencies:
npm installCreate a
.envfile in the root directory with the following variables:ALCHEMY_API_KEY=your_alchemy_api_key POSTGRES_USER=your_postgres_user POSTGRES_PASSWORD=your_postgres_password POSTGRES_DB=ethereum_indexer POSTGRES_HOST=localhost POSTGRES_PORT=5432 PORT=3000Create the PostgreSQL database:
psql -U postgres CREATE DATABASE ethereum_indexer; \q
Usage
Start the server
npm start
For development with auto-restart:
npm run dev
API Endpoints
GET /health- Check server health and indexing statusGET /blocks/:number- Get block data by block numberGET /transactions/:hash- Get transaction data by transaction hash
Project Structure
ethereum-indexer/
├── src/
│ ├── index.js # Main application entry point
│ ├── db.js # Database connection
│ ├── schema.js # Database schema
│ └── indexer.js # Blockchain indexing logic
├── .env # Environment variables
├── package.json # Project dependencies
└── README.md # Project documentation
How It Works
- The application connects to the Ethereum network using Alchemy's API
- It fetches blocks and transactions from the blockchain
- The data is stored in PostgreSQL tables
- The Express server provides API endpoints to query the indexed data
Database Schema
Blocks Table
number- Block number (primary key)hash- Block hashparent_hash- Parent block hashtimestamp- Block timestampminer- Miner addressgas_used- Gas used in the blockgas_limit- Gas limit of the blockbase_fee_per_gas- Base fee per gas (EIP-1559)indexed_at- Timestamp when the block was indexed
Transactions Table
hash- Transaction hash (primary key)block_number- Block number (foreign key)from_address- Sender addressto_address- Recipient addressvalue- Transaction value in weigas_used- Gas used by the transactiongas_price- Gas price in weinonce- Transaction noncetransaction_index- Index of the transaction in the blockstatus- Transaction status (success/failure)indexed_at- Timestamp when the transaction was indexed
Future Enhancements
- Index contract events (logs)
- Track token transfers (ERC-20, ERC-721)
- Monitor account balances
- Add support for chain reorganizations
- Implement more advanced querying capabilities
Test Snippet


