rmi-backend/app/_archive/legacy_2026_07/chain_feeder.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

101 lines
3.3 KiB
Python

"""
Chain Data Feeder - Pre-loads wallet clustering engine with real on-chain data.
Uses Helius (primary) with QuickNode fallback. Rate-limited + cached.
"""
import logging
from datetime import UTC, datetime
from app.chain_cache import get_chain_cache
from app.chain_client import get_chain_client
from app.wallet_clustering import Transaction, get_clustering_engine
logger = logging.getLogger(__name__)
async def feed_wallet_transactions(wallet: str, limit: int = 50) -> int:
"""Fetch real transactions for a wallet and load into clustering engine.
Returns number of transactions loaded. Cached for 5 min."""
cache = get_chain_cache()
cached = await cache.get("feed_wallet", wallet, limit)
if cached is not None:
return cached
client = get_chain_client()
engine = get_clustering_engine()
# Get recent signatures
sigs = await cache.get("signatures", wallet, limit)
if sigs is None:
sigs = await client.get_signatures(wallet, limit=min(limit, 50))
await cache.set("signatures", sigs, wallet, limit, ttl=120)
if not sigs:
await cache.set("feed_wallet", 0, wallet, limit)
return 0
# Get parsed transactions (batch)
tx_hashes = [s.get("signature") for s in sigs[:25] if s.get("signature")]
txs = await cache.get("transactions_batch", *tx_hashes[:5])
if txs is None:
txs = await client.get_transactions(tx_hashes[:25])
await cache.set("transactions_batch", txs, *tx_hashes[:5], ttl=120)
count = 0
for sig_data in sigs:
sig = sig_data.get("signature")
if not sig:
continue
ts = sig_data.get("blockTime")
dt = datetime.fromtimestamp(ts, tz=UTC) if ts else datetime.now(UTC)
# Simple transaction from signature data
tx = Transaction(
signature=sig,
timestamp=dt,
from_address=wallet,
to_address=sig_data.get("to", "unknown"),
amount=float(sig_data.get("lamport", 0)) / 1e9 if sig_data.get("lamport") else 0,
token="SOL",
program="system",
)
engine.add_transaction(tx)
count += 1
await cache.set("feed_wallet", count, wallet, limit)
logger.info(f"Fed {count} transactions for {wallet[:12]}...")
return count
async def get_holders_for_token(token_address: str, limit: int = 20) -> list[str]:
"""Get top token holders. Uses cached Helius token-accounts lookup."""
cache = get_chain_cache()
cached = await cache.get("holders", token_address, limit)
if cached is not None:
return cached
client = get_chain_client()
accounts = await client.get_token_accounts(token_address)
holders = []
for acct in accounts[:limit]:
info = acct.get("account", {}).get("data", {}).get("parsed", {}).get("info", {})
owner = info.get("owner")
if owner:
holders.append(owner)
await cache.set("holders", holders, token_address, limit, ttl=300)
return holders
async def get_wallet_balance(wallet: str) -> float:
"""Get SOL balance with caching."""
cache = get_chain_cache()
cached = await cache.get("balance", wallet)
if cached is not None:
return cached
client = get_chain_client()
balance = await client.get_balance(wallet)
await cache.set("balance", balance, wallet, ttl=300)
return balance