#!/usr/bin/env python3 """ AITBC Blockchain Explorer A simple web interface to explore the blockchain """ import asyncio import httpx import json from datetime import datetime from typing import Dict, List, Optional, Any from fastapi import FastAPI, Request from fastapi.responses import HTMLResponse from fastapi.staticfiles import StaticFiles import uvicorn app = FastAPI(title="AITBC Blockchain Explorer", version="1.0.0") # Configuration BLOCKCHAIN_RPC_URL = "http://localhost:8082" # Local blockchain node EXTERNAL_RPC_URL = "http://aitbc.keisanki.net:8082" # External access # HTML Template HTML_TEMPLATE = """ AITBC Blockchain Explorer

AITBC Blockchain Explorer

Network: ait-devnet

Current Height

-

Latest Block

-

Node Status

-

Latest Blocks

Height Hash Timestamp Transactions Actions
Loading blocks...
""" async def get_chain_head() -> Dict[str, Any]: """Get the current chain head""" try: async with httpx.AsyncClient() as client: response = await client.get(f"{BLOCKCHAIN_RPC_URL}/rpc/head") if response.status_code == 200: return response.json() except Exception as e: print(f"Error getting chain head: {e}") return {} async def get_block(height: int) -> Dict[str, Any]: """Get a specific block by height""" try: async with httpx.AsyncClient() as client: response = await client.get(f"{BLOCKCHAIN_RPC_URL}/rpc/blocks/{height}") if response.status_code == 200: return response.json() except Exception as e: print(f"Error getting block {height}: {e}") return {} @app.get("/", response_class=HTMLResponse) async def root(): """Serve the explorer UI""" return HTML_TEMPLATE.format(node_url=BLOCKCHAIN_RPC_URL) @app.get("/api/chain/head") async def api_chain_head(): """API endpoint for chain head""" return await get_chain_head() @app.get("/api/blocks/{height}") async def api_block(height: int): """API endpoint for block data""" return await get_block(height) @app.get("/health") async def health(): """Health check endpoint""" head = await get_chain_head() return { "status": "ok" if head else "error", "node_url": BLOCKCHAIN_RPC_URL, "chain_height": head.get("height", 0), } if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=3000)