rmi-backend/app/_archive/legacy_2026_07/discovery_router.py
cryptorugmunch 628c1d2a10
Some checks failed
CI / build (push) Failing after 3s
refactor(rmi-backend,audit): mount Wave 3 + archive 136 dead-code files (P2.3)
PHASE 2.3 (AUDIT-2026-Q3.md):

Task 1 — Wire-in Wave 3 (1 router mounted, 2 deferred):
  - app.routers.unified_scanner_router mounted at /api/v2/scanner/* (2 routes:
    POST /api/v2/scanner/token/scan, POST /api/v2/scanner/wallet/scan).
    Refactored prefix from /api/v2 -> /api/v2/scanner to avoid future conflicts
    with the v1 /api/v1/scanner/ stub.
  - app.routers.unified_wallet_scanner DEFERRED (no router APIRouter attribute;
    library module consumed by unified_scanner_router via get_wallet_scanner()).
  - app.routers.admin_extensions DEFERRED (DORMANT per audit; 25 routes at
    /api/v1/admin/* would shadow /api/v1/admin/alerts_webhook).

Task 2 — Archive 136 dead-code files to app/_archive/legacy_2026_07/:
  - 73 routers in app/routers/ (reach graph showed zero reach into mount.py).
  - 63 flat app/*.py (domain modules never imported by live code).
  - 1 file RESTORED post-archive: app/routers/x402_bridge_health.py (caught by
    tests/unit/test_bridge_health.py which directly imports it; reach graph
    considered tests/ only as transitive reach — to be patched in next cycle).

Forced-LIVE (NOT archived per user directive):
  - app/ai_pipeline_v3.py  (3 importers in audit window, importers themselves DEAD)
  - app/splade_bm25.py       (LIVE via app.rag_service)
  - app/wallet_manager_v2.py (LIVE via x402_enforcement, x402_tools, sweep_all, sweep_now)
  - app/crypto_embeddings.py (NOT in audit ARCHIVE list; heavy import graph)

Verification (forward-import closure from mount.py + main.py + factory.py + lifespan.py):
  - imports = 348 app.* modules
  - reached = 194 files reachable from roots
  - archive set = audit_dead (186) - reached - forced_live (4) - test_live (1) = 136
  - Net delta: 136 files moved, 44,932 LOC reduction, 293->295 active routes (+2 from Wave 3)

pyproject.toml updates:
  - setuptools.packages.find: added exclude for app._archive*
  - ruff.extend-exclude: added "app/_archive/"
  - mypy.exclude: added "app/_archive/"

Smoke test: pytest tests/ — 817 passed, 3 pre-existing failures unchanged
(0 new failures; 0 routes lost; all 4 forced-LIVE files still importable).

Restoration: git mv app/_archive/legacy_2026_07/<name>.py <original-path>
and add the import to app/mount.py ROUTER_MODULES.

Refs: AUDIT-2026-Q3.md /home/dev/pry/rmi-final-deadcode-2026-07-06.md
2026-07-06 20:52:31 +02:00

162 lines
5.3 KiB
Python

"""
Token Discovery API Router - Multi-chain token discovery from DexScreener + GeckoTerminal.
Connects to /api/v1/discovery/*
"""
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from app.token_discovery import MONITORED_CHAINS, discover_tokens, scan_token_security
router = APIRouter(prefix="/api/v1/discovery", tags=["token-discovery"])
class DiscoveryRequest(BaseModel):
chains: list[str] | None = None
limit: int = 20
@router.post("/tokens")
async def discover_new_tokens(req: DiscoveryRequest):
"""Discover new tokens across chains (DexScreener + GeckoTerminal)."""
chains = req.chains if req.chains else list(MONITORED_CHAINS.keys())
result = await discover_tokens(chains=chains)
total = sum(len(tokens) for tokens in result.values())
return {"chains": list(result.keys()), "total_tokens": total, "tokens": result}
@router.get("/chains")
async def list_chains():
"""List monitored chains."""
return {"chains": MONITORED_CHAINS, "count": len(MONITORED_CHAINS)}
@router.get("/security/{chain}/{address}")
async def token_security(chain: str, address: str):
"""GoPlus security scan for a token."""
chain_id = MONITORED_CHAINS.get(chain, chain)
result = await scan_token_security(chain_id, address)
if not result:
raise HTTPException(status_code=404, detail="Security scan unavailable")
return {"chain": chain, "address": address, "security": result}
@router.get("/health")
async def discovery_health():
cg_status = {}
try:
from app.coingecko_connector import COINGECKO_FREE_KEY, COINGECKO_PRO_KEY
cg_status = {
"free_key": bool(COINGECKO_FREE_KEY),
"pro_key": bool(COINGECKO_PRO_KEY),
"mode": "pro" if COINGECKO_PRO_KEY else "demo",
}
except Exception:
pass
return {
"status": "ok",
"service": "token-discovery",
"chains": len(MONITORED_CHAINS),
"coingecko": cg_status,
}
# ── CoinGecko Market Data ───────────────────────────────────
@router.get("/coingecko/trending")
async def coingecko_trending():
"""Get trending coins from CoinGecko (updated every 30 min)."""
from app.coingecko_connector import get_coingecko_connector
cg = get_coingecko_connector()
trending = await cg.get_trending()
return {"trending": trending, "count": len(trending)}
@router.get("/coingecko/markets")
async def coingecko_markets(vs: str = "usd", limit: int = 50):
"""Get top coins by market cap."""
from app.coingecko_connector import get_coingecko_connector
cg = get_coingecko_connector()
markets = await cg.get_market_overview(vs_currency=vs, per_page=min(limit, 250))
return {"markets": markets, "count": len(markets)}
@router.get("/coingecko/global")
async def coingecko_global():
"""Get global crypto market metrics (total market cap, BTC dominance, etc)."""
from app.coingecko_connector import get_coingecko_connector
cg = get_coingecko_connector()
metrics = await cg.get_global_metrics()
return metrics or {"error": "unavailable"}
@router.get("/coingecko/coin/{coin_id}")
async def coingecko_coin_detail(coin_id: str):
"""Get detailed info about a specific coin."""
from app.coingecko_connector import get_coingecko_connector
cg = get_coingecko_connector()
detail = await cg.get_token_detail(coin_id)
if not detail:
raise HTTPException(status_code=404, detail=f"Coin '{coin_id}' not found")
return detail
@router.get("/coingecko/categories")
async def coingecko_categories():
"""Get coin categories with market data."""
from app.coingecko_connector import get_coingecko_connector
cg = get_coingecko_connector()
categories = await cg.get_category_market_data()
return {"categories": categories, "count": len(categories)}
@router.get("/coingecko/exchanges")
async def coingecko_exchanges(limit: int = 20):
"""Get top exchanges by trading volume."""
from app.coingecko_connector import get_coingecko_connector
cg = get_coingecko_connector()
exchanges = await cg.get_exchanges(per_page=min(limit, 100))
return {"exchanges": exchanges, "count": len(exchanges)}
@router.get("/coingecko/ohlc/{coin_id}")
async def coingecko_ohlc(coin_id: str, vs: str = "usd", days: int = 7):
"""Get OHLC candlestick data for a coin."""
from app.coingecko_connector import get_coingecko_connector
cg = get_coingecko_connector()
data = await cg.get_ohlc(coin_id, vs_currency=vs, days=days)
return {"coin_id": coin_id, "vs": vs, "days": days, "candles": data}
@router.get("/coingecko/pools/trending")
async def coingecko_trending_pools(chain: str = "solana"):
"""Get trending DEX pools (GeckoTerminal)."""
from app.coingecko_connector import get_coingecko_connector
cg = get_coingecko_connector()
pools = await cg.get_trending_pools(chain=chain)
return {"chain": chain, "pools": pools, "count": len(pools)}
@router.get("/coingecko/status")
async def coingecko_status():
"""Check CoinGecko API key status and connectivity."""
from app.coingecko_connector import get_coingecko_connector
cg = get_coingecko_connector()
ping = await cg.ping()
key_status = await cg.api_key_status()
return {
"ping": ping,
"key_status": key_status,
"connector": cg.status(),
}