merge: chore/cleanup-remove-bloat-and-secrets into main

This commit is contained in:
Crypto Rug Munch 2026-07-02 01:24:22 +07:00
commit bde2f3a97d
1173 changed files with 437609 additions and 0 deletions

View file

@ -0,0 +1,78 @@
"""Alerts domain — public API + health check registration."""
from __future__ import annotations
from app.core import health as health_mod
from app.core.health import DomainHealth
def _read_env_file() -> dict[str, str]:
"""Read env from /proc/self/environ (process env, not the mutable os.environ)."""
env: dict[str, str] = {}
try:
with open("/proc/self/environ", "rb") as f:
for chunk in f.read().split(b"\x00"):
if b"=" in chunk:
k, _, v = chunk.partition(b"=")
env[k.decode("utf-8", "replace")] = v.decode("utf-8", "replace")
except Exception:
pass
return env
async def _health_check() -> DomainHealth:
"""Alerts health: Redis reachable via process env (not mutable os.environ)."""
import redis as redis_lib
env = _read_env_file()
host = env.get("REDIS_HOST", "rmi-redis")
port = int(env.get("REDIS_PORT", "6379"))
password = env.get("REDIS_PASSWORD", "") or None
try:
client = redis_lib.Redis(
host=host,
port=port,
password=password,
db=int(env.get("REDIS_DB", "0")),
decode_responses=True,
socket_connect_timeout=2,
socket_timeout=2,
)
client.ping()
return DomainHealth(
name="alerts",
healthy=True,
details={"redis": "ok", "host": host, "port": port},
)
except Exception as e:
return DomainHealth(
name="alerts",
healthy=False,
details={"host": host, "port": port},
error=str(e),
)
health_mod.register_health_check("alerts", _health_check)
# Public API
from app.domain.alerts.broadcaster import AlertBroadcaster
from app.domain.alerts.models import (
AlertEvent,
AlertSubscription,
AlertType,
CreateAlertRequest,
)
from app.domain.alerts.repository import AlertRepository
from app.domain.alerts.service import AlertService
__all__ = [
"AlertBroadcaster",
"AlertEvent",
"AlertRepository",
"AlertService",
"AlertSubscription",
"AlertType",
"CreateAlertRequest",
]

View file

@ -0,0 +1,27 @@
"""Broadcaster — pushes fired alert events to WebSocket subscribers.
Uses core/websocket's broadcast helper. No FastAPI imports.
"""
from __future__ import annotations
from app.core.logging import get_logger
from app.core.websocket import broadcast_alert
from app.domain.alerts.models import AlertEvent
log = get_logger(__name__)
class AlertBroadcaster:
"""Thin wrapper that knows how to push an AlertEvent to live clients."""
async def broadcast_event(self, event: AlertEvent) -> None:
"""Broadcast a single event to all connected WebSocket subscribers."""
try:
await broadcast_alert(event.model_dump(mode="json"))
except Exception as e:
# Broadcasting failure must not break the firing path.
log.warning(
"alert_broadcast_failed",
error=str(e),
alert_id=event.subscription_id,
)

View file

@ -0,0 +1,71 @@
"""Pydantic v2 models for the alerts domain.
No FastAPI imports. Pure data shapes.
"""
from __future__ import annotations
from datetime import datetime
from enum import StrEnum
from typing import Any
from pydantic import BaseModel, ConfigDict, Field, field_validator
class AlertType(StrEnum):
"""Valid alert trigger types. Matches the legacy alert_types enum."""
LIQUIDITY_REMOVE = "liquidity_remove"
MINT = "mint"
BLACKLIST = "blacklist"
HONEYPOT = "honeypot"
RUG_PULL = "rug_pull"
WHALE_MOVEMENT = "whale_movement"
DEPLOYER_SELL = "deployer_sell"
@classmethod
def values(cls) -> list[str]:
return [m.value for m in cls]
class AlertSubscription(BaseModel):
"""A token alert subscription record (what we store in Redis)."""
model_config = ConfigDict(str_strip_whitespace=True)
id: str = Field(..., description="Unique subscription id, e.g. alert:1700000000")
token_address: str = Field(..., min_length=1, max_length=256)
alert_types: list[AlertType] = Field(default_factory=lambda: [AlertType.LIQUIDITY_REMOVE, AlertType.MINT, AlertType.BLACKLIST])
webhook_url: str | None = Field(default=None, max_length=2048)
created_at: datetime = Field(default_factory=datetime.utcnow)
active: bool = True
owner_id: str | None = Field(default=None, description="User id of subscription owner, None = anonymous")
@field_validator("alert_types")
@classmethod
def _at_least_one_type(cls, v: list[AlertType]) -> list[AlertType]:
if not v:
raise ValueError("at least one alert_type required")
return v
class CreateAlertRequest(BaseModel):
"""What the API accepts to create a subscription."""
model_config = ConfigDict(str_strip_whitespace=True)
token_address: str = Field(..., min_length=1, max_length=256)
alert_types: list[AlertType] | None = None
webhook_url: str | None = Field(default=None, max_length=2048)
class AlertEvent(BaseModel):
"""A fired alert, ready to broadcast to subscribers + WebSocket clients."""
model_config = ConfigDict(use_enum_values=True)
subscription_id: str
token_address: str
event_type: AlertType
payload: dict[str, Any] = Field(default_factory=dict)
fired_at: datetime = Field(default_factory=datetime.utcnow)
severity: str = Field(default="info", description="info | warning | critical")

View file

@ -0,0 +1,60 @@
"""Repository — async Redis storage for alert subscriptions.
Storage layout (matches legacy for cutover):
Hash key: "rmi:alerts"
Field: alert id (e.g. "alert:1700000000")
Value: JSON-encoded AlertSubscription
This is a thin async wrapper. No business logic. Pure data access.
"""
from __future__ import annotations
from app.core.logging import get_logger
from app.core.redis import get_redis_async
from app.domain.alerts.models import AlertSubscription
log = get_logger(__name__)
HASH_KEY = "rmi:alerts"
class AlertRepository:
"""Async Redis-backed subscription storage."""
def __init__(self) -> None:
self._key = HASH_KEY
async def list_all(self) -> list[AlertSubscription]:
r = get_redis_async()
raw: dict[str, str] = await r.hgetall(self._key) or {}
out: list[AlertSubscription] = []
for value in raw.values():
try:
out.append(AlertSubscription.model_validate_json(value))
except Exception as e:
log.warning("alert_repo_corrupt_record", error=str(e))
return out
async def get(self, alert_id: str) -> AlertSubscription | None:
r = get_redis_async()
raw: str | None = await r.hget(self._key, alert_id)
if raw is None:
return None
try:
return AlertSubscription.model_validate_json(raw)
except Exception as e:
log.warning("alert_repo_corrupt_record", alert_id=alert_id, error=str(e))
return None
async def create(self, sub: AlertSubscription) -> None:
r = get_redis_async()
await r.hset(self._key, sub.id, sub.model_dump_json())
async def delete(self, alert_id: str) -> bool:
r = get_redis_async()
removed: int = await r.hdel(self._key, alert_id)
return removed > 0
async def list_by_token(self, token_address: str) -> list[AlertSubscription]:
all_subs = await self.list_all()
return [s for s in all_subs if s.token_address == token_address]

View file

@ -0,0 +1,133 @@
"""Service — business logic for alert subscriptions and event firing.
This is the only thing the api/ layer should call. It composes
the repository (storage) and the broadcaster (WebSocket push).
No FastAPI. No HTTP. Pure Python.
"""
from __future__ import annotations
from datetime import datetime
from app.core.logging import get_logger
from app.domain.alerts.broadcaster import AlertBroadcaster
from app.domain.alerts.models import (
AlertEvent,
AlertSubscription,
AlertType,
CreateAlertRequest,
)
from app.domain.alerts.repository import AlertRepository
log = get_logger(__name__)
class AlertService:
"""Orchestrates subscription CRUD and event firing."""
def __init__(
self,
repo: AlertRepository | None = None,
broadcaster: AlertBroadcaster | None = None,
) -> None:
self._repo = repo or AlertRepository()
self._broadcaster = broadcaster or AlertBroadcaster()
async def create_subscription(
self,
req: CreateAlertRequest,
owner_id: str | None = None,
) -> AlertSubscription:
"""Create a new subscription, persist it, return the saved record."""
# Use provided types, or the model default if not given.
types = req.alert_types or [
AlertType.LIQUIDITY_REMOVE,
AlertType.MINT,
AlertType.BLACKLIST,
]
sub = AlertSubscription(
id=f"alert:{int(datetime.utcnow().timestamp())}",
token_address=req.token_address,
alert_types=types,
webhook_url=req.webhook_url,
owner_id=owner_id,
)
await self._repo.create(sub)
log.info(
"alert_subscription_created",
alert_id=sub.id,
token=sub.token_address,
types=[t.value for t in sub.alert_types],
owner=owner_id,
)
return sub
async def list_subscriptions(self, owner_id: str | None = None) -> list[AlertSubscription]:
"""List subscriptions. If owner_id given, filter to that user."""
all_subs = await self._repo.list_all()
if owner_id is None:
return all_subs
return [s for s in all_subs if s.owner_id == owner_id]
async def cancel_subscription(self, alert_id: str, owner_id: str | None = None) -> bool:
"""Cancel (delete) a subscription. Returns True if removed."""
sub = await self._repo.get(alert_id)
if sub is None:
return False
if owner_id is not None and sub.owner_id != owner_id:
log.warning(
"alert_cancel_unauthorized",
alert_id=alert_id,
owner=owner_id,
actual_owner=sub.owner_id,
)
return False
return await self._repo.delete(alert_id)
async def fire_event(
self,
token_address: str,
event_type: AlertType,
payload: dict | None = None,
) -> list[AlertEvent]:
"""Fire an event for a token. Returns the events that were broadcast.
Looks up all active subscriptions matching this token + event_type,
creates an AlertEvent for each, and broadcasts via the broadcaster.
"""
subs = await self._repo.list_by_token(token_address)
matching = [
s for s in subs
if s.active and (event_type.value in [t.value for t in s.alert_types])
]
if not matching:
return []
events: list[AlertEvent] = []
for sub in matching:
ev = AlertEvent(
subscription_id=sub.id,
token_address=token_address,
event_type=event_type,
payload=payload or {},
severity=_severity_for(event_type),
)
events.append(ev)
await self._broadcaster.broadcast_event(ev)
log.info(
"alert_events_fired",
token=token_address,
event_type=event_type.value,
count=len(events),
)
return events
def _severity_for(event_type: AlertType) -> str:
"""Map event type → severity (used by clients to choose alert UI)."""
critical = {AlertType.RUG_PULL, AlertType.HONEYPOT, AlertType.BLACKLIST}
warning = {AlertType.LIQUIDITY_REMOVE, AlertType.WHALE_MOVEMENT, AlertType.DEPLOYER_SELL}
if event_type in critical:
return "critical"
if event_type in warning:
return "warning"
return "info"