Implement Solana RPC proxy with automatic failover and caching - Add multi-provider support for 5 free Solana RPC endpoints (Alchemy, PublicNode, Helius, QuickNode, Solana Public) - Implement automatic failover with 30-minute backoff for failed providers - Add disk-based response caching with 100GB LRU eviction - Create SQLite error logging with UUID tracking - Support both HTTP JSON-RPC and WebSocket connections - Include provider-specific authentication handling - Add response normalization for consistent output - Write end-to-end tests for core functionality The proxy provides a unified endpoint that automatically routes requests to available providers, caches responses to reduce load, and logs all errors with retrievable UUIDs for debugging.
74 lines
1.9 KiB
Python
74 lines
1.9 KiB
Python
import json
|
|
import logging
|
|
from aiohttp import web, ClientSession
|
|
from router import Router
|
|
|
|
|
|
async def handle_rpc_request(request: web.Request) -> web.Response:
|
|
router: Router = request.app['router']
|
|
logger = logging.getLogger(__name__)
|
|
|
|
try:
|
|
body = await request.json()
|
|
|
|
if not isinstance(body, dict):
|
|
return web.json_response({
|
|
"jsonrpc": "2.0",
|
|
"id": body.get("id", 1) if isinstance(body, dict) else 1,
|
|
"error": {
|
|
"code": -32600,
|
|
"message": "Invalid Request"
|
|
}
|
|
}, status=400)
|
|
|
|
method = body.get("method")
|
|
params = body.get("params", [])
|
|
request_id = body.get("id", 1)
|
|
|
|
if not method:
|
|
return web.json_response({
|
|
"jsonrpc": "2.0",
|
|
"id": request_id,
|
|
"error": {
|
|
"code": -32600,
|
|
"message": "Missing method"
|
|
}
|
|
}, status=400)
|
|
|
|
logger.info(f"Handling RPC request: {method}")
|
|
|
|
response = await router.route_request(method, params)
|
|
response["id"] = request_id
|
|
|
|
return web.json_response(response)
|
|
|
|
except json.JSONDecodeError:
|
|
return web.json_response({
|
|
"jsonrpc": "2.0",
|
|
"id": 1,
|
|
"error": {
|
|
"code": -32700,
|
|
"message": "Parse error"
|
|
}
|
|
}, status=400)
|
|
|
|
except Exception as e:
|
|
logger.error(f"Unexpected error: {e}")
|
|
return web.json_response({
|
|
"jsonrpc": "2.0",
|
|
"id": 1,
|
|
"error": {
|
|
"code": -32603,
|
|
"message": "Internal error"
|
|
}
|
|
}, status=500)
|
|
|
|
|
|
|
|
|
|
|
|
def setup_routes(app: web.Application) -> None:
|
|
app.router.add_post('/', handle_rpc_request)
|
|
|
|
|