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

179 lines
6.9 KiB
Python

#!/usr/bin/env python3
"""#20 - Cross-Chain Portfolio Rebalance Bot. Users set automated rebalancing rules.
Executes via x402, monitors via DataBus. "Keep 50% USDC on Solana, 30% ETH on Arbitrum." """
import os
from datetime import UTC, datetime
import httpx
from fastapi import APIRouter, BackgroundTasks, HTTPException
from pydantic import BaseModel
router = APIRouter(prefix="/api/v1/rebalance", tags=["portfolio-rebalance"])
DATABUS = os.environ.get("DATABUS_URL", "http://localhost:8000/api/v1/databus")
BACKEND = os.environ.get("BACKEND_URL", "http://localhost:8000")
# In-memory store for rebalancing rules (Redis/DB in prod)
_rebalance_rules: dict[str, dict] = {}
class RebalanceRule(BaseModel):
user_id: str
name: str
allocations: list[dict] # [{"chain": "solana", "token": "USDC", "target_pct": 50}, ...]
rebalance_threshold_pct: float = 5.0 # Rebalance if any allocation off by >5%
max_slippage_pct: float = 1.0
enabled: bool = True
@router.post("/rules")
async def create_rebalance_rule(rule: RebalanceRule):
"""Create an automated portfolio rebalancing rule."""
rule_id = f"{rule.user_id}:{rule.name}"
_rebalance_rules[rule_id] = {
"id": rule_id,
"user_id": rule.user_id,
"name": rule.name,
"allocations": rule.allocations,
"rebalance_threshold_pct": rule.rebalance_threshold_pct,
"max_slippage_pct": rule.max_slippage_pct,
"enabled": rule.enabled,
"created_at": datetime.now(UTC).isoformat(),
"last_rebalanced": None,
}
return {"id": rule_id, "status": "created", "allocations": rule.allocations}
@router.get("/rules/{user_id}")
async def list_rules(user_id: str):
"""List all rebalancing rules for a user."""
user_rules = [r for rid, r in _rebalance_rules.items() if r["user_id"] == user_id]
return {"rules": user_rules, "count": len(user_rules)}
@router.delete("/rules/{rule_id}")
async def delete_rule(rule_id: str):
"""Delete a rebalancing rule."""
_rebalance_rules.pop(rule_id, None)
return {"id": rule_id, "status": "deleted"}
@router.get("/simulate/{user_id}")
async def simulate_rebalance(user_id: str):
"""Simulate rebalancing - check what would change without executing."""
user_rules = [r for rid, r in _rebalance_rules.items() if r["user_id"] == user_id and r["enabled"]]
if not user_rules:
return {"simulations": [], "note": "No enabled rules found"}
simulations = []
for rule in user_rules[:5]:
current: list[dict] = []
# Fetch current balances for each allocation
async with httpx.AsyncClient(timeout=15) as client:
for alloc in rule["allocations"]:
try:
resp = await client.get(
f"{DATABUS}/{alloc['chain']}/balance/{user_id}", params={"token": alloc.get("token", "native")}
)
if resp.status_code == 200:
data = resp.json().get("data", {})
current.append(
{
"chain": alloc["chain"],
"token": alloc.get("token", "native"),
"balance_usd": data.get("value_usd", 0) or 0,
"current_pct": 0,
"target_pct": alloc["target_pct"],
"difference_pct": 0,
}
)
except Exception:
current.append(
{
"chain": alloc["chain"],
"token": alloc.get("token", "native"),
"balance_usd": 0,
"target_pct": alloc["target_pct"],
"error": "unavailable",
}
)
# Calculate percentages and differences
total = sum(a["balance_usd"] for a in current) or 1
needs_rebalance = False
for a in current:
a["current_pct"] = round((a["balance_usd"] / total) * 100, 1)
a["difference_pct"] = round(a["current_pct"] - a["target_pct"], 1)
if abs(a["difference_pct"]) > rule["rebalance_threshold_pct"]:
needs_rebalance = True
simulations.append(
{
"rule": rule["name"],
"needs_rebalance": needs_rebalance,
"total_value_usd": round(total, 2),
"current_allocations": current,
"rebalance_threshold_pct": rule["rebalance_threshold_pct"],
}
)
return {"simulations": simulations}
@router.post("/execute/{rule_id}")
async def execute_rebalance(rule_id: str, background_tasks: BackgroundTasks):
"""Execute a rebalancing operation (simulation only - no real execution without x402)."""
rule = _rebalance_rules.get(rule_id)
if not rule:
raise HTTPException(404, "Rule not found")
if not rule["enabled"]:
raise HTTPException(400, "Rule is disabled")
# Simulation mode - return what WOULD be executed
async with httpx.AsyncClient(timeout=15) as client:
trades_needed: list[dict] = []
current_values: list[dict] = []
for alloc in rule["allocations"]:
try:
resp = await client.get(f"{DATABUS}/{alloc['chain']}/balance/{rule['user_id']}")
if resp.status_code == 200:
data = resp.json().get("data", {})
current_values.append(
{
"chain": alloc["chain"],
"token": alloc.get("token", "native"),
"value_usd": data.get("value_usd", 0) or 0,
"target_pct": alloc["target_pct"],
}
)
except Exception:
pass
total = sum(a["value_usd"] for a in current_values) or 1
for a in current_values:
current_pct = (a["value_usd"] / total) * 100
diff_pct = current_pct - a["target_pct"]
diff_usd = total * (diff_pct / 100)
if abs(diff_pct) > rule["rebalance_threshold_pct"]:
trades_needed.append(
{
"chain": a["chain"],
"token": a["token"],
"action": "SELL" if diff_pct > 0 else "BUY",
"amount_usd": round(abs(diff_usd), 2),
"current_pct": round(current_pct, 1),
"target_pct": a["target_pct"],
}
)
rule["last_rebalanced"] = datetime.now(UTC).isoformat()
return {
"rule_id": rule_id,
"status": "simulated",
"total_value_usd": round(total, 2),
"trades_needed": trades_needed,
"note": "Simulation only. Real execution requires x402 payment authorization.",
}