Automated Action 85b1f2a581 Enhance Solana RPC client with better error handling and debugging
- Add detailed logging throughout the Solana client and scanner
- Improve error handling in RPC client methods
- Add debug endpoints to validate Solana connection
- Add message field to scan status responses
- Enhance health endpoint with RPC connectivity status
- Handle invalid block ranges and API rate limits
2025-05-28 23:08:35 +00:00

105 lines
2.8 KiB
Python

import logging
from datetime import datetime
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.api.v1.api import api_router
from app.core.config import settings
from app.core.scheduler import scheduler
from app.models import base # noqa
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
# Enable debug logging for the scanner and Solana client modules
logging.getLogger("app.services.scanner").setLevel(logging.DEBUG)
logging.getLogger("app.services.solana_client").setLevel(logging.DEBUG)
# Also enable debug for solana.rpc
logging.getLogger("solana.rpc").setLevel(logging.DEBUG)
logger = logging.getLogger(__name__)
app = FastAPI(
title=settings.PROJECT_NAME,
description="Solana Arbitrage Detector API",
version="0.1.0",
openapi_url="/openapi.json",
docs_url="/docs",
redoc_url="/redoc",
)
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include API router
app.include_router(api_router, prefix=settings.API_V1_STR)
# Health check endpoint
@app.get("/health", tags=["health"])
async def health_check():
"""Health check endpoint with Solana RPC connectivity status"""
from app.services.solana_client import SolanaClient
health_status = {
"status": "ok",
"timestamp": datetime.now().isoformat(),
"solana_rpc": {
"url": settings.SOLANA_RPC_URL,
"connection": "unknown"
}
}
# Test Solana RPC connection
try:
client = SolanaClient()
response = client.client.get_latest_blockhash()
if "result" in response:
health_status["solana_rpc"]["connection"] = "ok"
health_status["solana_rpc"]["latest_blockhash"] = response["result"].get("blockhash", "unknown")
else:
health_status["solana_rpc"]["connection"] = "error"
health_status["solana_rpc"]["error"] = "No result in response"
except Exception as e:
health_status["solana_rpc"]["connection"] = "error"
health_status["solana_rpc"]["error"] = str(e)
if health_status["solana_rpc"]["connection"] != "ok":
health_status["status"] = "degraded"
return health_status
@app.on_event("startup")
async def startup_event():
"""
Startup event handler
"""
logger.info("Starting up application")
# Start the scheduler
scheduler.start()
@app.on_event("shutdown")
async def shutdown_event():
"""
Shutdown event handler
"""
logger.info("Shutting down application")
# Shut down the scheduler
scheduler.shutdown()
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)