Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

8 Commits

Folders and files

Repository files navigation

Bitcoin Testnet 4 Indexer

A blockchain indexer that continuously syncs Bitcoin Testnet 4 data into PostgreSQL, exposed via a REST API and an MCP server.

Architecture

┌──────────────┐      RPC Calls      ┌──────────────┐
│   QuickNode  │◄───────────────────│   Indexer    │
│   (Testnet4) │    (JSON-RPC)       │   (Python)   │
└──────────────┘                     └──────┬───────┘
                                            │
                                   ┌────────▼────────┐
                                   │   PostgreSQL    │
                                   │   (blocks,      │
                                   │    transactions, │
                                   │    outputs)      │
                                   └────────┬────────┘
                                            │
                              ┌─────────────┼─────────────┐
                              │                           │
                     ┌────────▼──────┐          ┌────────▼──────┐
                     │  REST API     │          │  MCP Server   │
                     │  (FastAPI)    │          │  (FastMCP)    │
                     │  :8000        │          │  :8001        │
                     └───────────────┘          └───────────────┘

The system runs as three Docker containers: an indexer that fetches blocks from QuickNode and writes to PostgreSQL, a FastAPI server for REST queries, and a FastMCP server for tool-based access from AI coding agents.

Design Choices

QuickNode RPC instead of a hosted node. Rather than running a full Bitcoin node, the indexer makes JSON-RPC calls to a QuickNode Testnet 4 endpoint. This avoids the infrastructure cost and sync time of maintaining a node while still getting reliable chain data.

Pydantic models for data validation. All data flowing through the system — from RPC responses to API responses — passes through Pydantic BaseModel classes (Block, Transaction, Output). This enforces type checking at the boundary: malformed RPC data is caught immediately, and API responses are guaranteed to match the declared schema.

FastMCP for AI agent access. The MCP server wraps the same CRUD operations as the REST API but exposes them as tools that coding agents (Claude, Cursor, opencode) can call directly. This lets you query the indexed blockchain data from within an AI-assisted workflow without writing HTTP requests.

Atomic block writes. Each block, its transactions, and their outputs are written in a single database transaction. If anything fails mid-write, the entire block is rolled back, preventing partial or corrupted data.

Historical sync then live polling. On startup, the indexer catches up from the last indexed height to the current chain tip. Once caught up, it polls every 5 minutes for new blocks.

Key Features

  • Continuous indexing of blocks, transactions, and outputs
  • REST API with Swagger docs at /docs
  • MCP server for AI agent integration
  • Gap detection and backfill for missed blocks
  • Retry logic for failed RPC calls (3 attempts per block)

Requirements

  • Docker and Docker Compose
  • A QuickNode Testnet 4 RPC URL (or any Bitcoin RPC endpoint)
  • (Optional) psql for direct database queries

Setup

  1. Clone the repository:
git clone https://github.com/zane1502/bitcoin-testnet4-indexer.git
cd bitcoin-testnet4-indexer
  1. Configure environment variables in .env:
POSTGRES_USER=indexer
POSTGRES_PASSWORD=your_secure_password
POSTGRES_DB=bitcoin_testnet4

DATABASE_URL=postgresql://indexer:your_secure_password@postgres:5432/bitcoin_testnet4

BITCOIN_RPC_URL=https://your-quicknode-endpoint.btc-testnet4.quiknode.pro/your-api-key/
  1. Start all services:
docker compose up -d
  1. Check status:
docker compose logs -f api

The indexer will begin syncing from block 0 to the current chain tip on first run.

API Endpoints

Method Endpoint Description
GET /blocks/last-indexed-height Latest indexed block height
GET /blocks/latest Most recently indexed block
GET /blocks/height/{height} Block by height
GET /blocks/block/{hash} Block by hash
GET /blocks/{start}-{stop} Blocks in a height range
GET /transactions/block/{height} Transactions in a block
GET /transactions/{txid} Transaction by txid
GET /outputs/block/{txid} Outputs for a transaction
GET /outputs/address/{address} Outputs sent to an address

Full interactive docs at http://localhost:8000/docs.

MCP Server

The MCP server runs on port 8001 and exposes the same query capabilities as tools:

  • mcp_get_block_by_height
  • mcp_get_block_by_hash
  • mcp_fetch_blocks_by_range
  • mcp_get_last_indexed_height
  • mcp_get_transaction_by_txid
  • mcp_get_transactions_by_block_height
  • mcp_get_outputs_by_txid
  • mcp_get_outputs_by_address

Connect from a coding agent

opencode — add to opencode.json:

{
  "mcp": {
    "bitcoin-indexer": {
      "type": "remote",
      "url": "http://localhost:8001/mcp",
      "enabled": true
    }
  }
}

Claude Desktop — add to claude_desktop_config.json:

{
  "mcpServers": {
    "bitcoin-indexer": {
      "url": "http://localhost:8001/mcp"
    }
  }
}

To expose the MCP server remotely (e.g. from another machine), use ngrok:

ngrok http 8001

Direct Database Access

Install psql and connect:

docker exec -it indexer-postgres psql -U indexer -d bitcoin_testnet4

Example queries:

-- Latest indexed block
SELECT * FROM blocks ORDER BY height DESC LIMIT 1;

-- Total indexed
SELECT COUNT(*) AS blocks, (SELECT COUNT(*) FROM transactions) AS txs, (SELECT COUNT(*) FROM outputs) AS outputs FROM blocks;

-- Transactions in a block
SELECT * FROM transactions WHERE block_height = 100;

-- Balance for an address
SELECT address, SUM(value) AS total FROM outputs WHERE address = 'your_address' GROUP BY address;

Stopping

docker compose down          # Stop containers (data persists)
docker compose down -v       # Stop and delete all data

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages