Files
trumpsignal-backend/app/schemas.py
T
k 5fb1d52026 Pre-launch hardening: KOL module, Telegram, scanners, WS resilience
Big-picture changes since b941223:

KOL pipeline (new) — Substack/podcast/blog RSS → AI ticker extraction →
on-chain wallet diff → talks-vs-trades divergence detection. Daily polls,
19 feeds, divergence emits Post + Telegram fan-out.

Telegram push (new) — walletless free tier + wallet-linked Pro upgrade,
in-bot preference commands (/trump /btc /funding /kol /conf /quiet),
signed-envelope API for dashboard. Disconnect-wallet keeps free
subscription.

BTC funding-rate reversal scanner (new) — hourly cron, 30d cumulative
funding threshold + mean-revert + 7d price confirm, emits via
/api/signals/ingest. BTC bottom-reversal scanner promoted to System 2.

WS broadcast rewrite — per-client send timeout + parallel fan-out
(asyncio.gather). Fixes "Binance WS no close frame" reconnect storms +
APScheduler 11-min job misses, both caused by one slow client stalling
the kline loop.

Error visibility — three silent-error sites (trumpstruth/truth_social
fetchers, funding_reversal scanner) now include exception type name so
httpx ConnectError-style empty-message errors stop logging blank lines.
Telegram bot loop now classifies ReadTimeout vs network vs unknown +
logger.exception for the unknown bucket.

Security hygiene — trumpsignal.db untracked from git (held subscriber
wallets + encrypted HL keys + 22 bot trades); .gitignore now blocks
*.db/.next/backups. CORS only allows FRONTEND_URL in production.

New ops scripts —
  - scripts/preflight.py: env/DB/Telegram/AI auth verification gate
  - scripts/backup_db.sh: cron-friendly daily DB backup (SQLite + Postgres)
  - scripts/seed_kol_wallets.py: idempotent KOL on-chain wallet seeder

15 new Alembic migrations (007-021) covering convex strategy fields,
phase-1 safety, two-system frozen exits, invalidation prices, dynamic
SYS2 leverage, staged de-risk + pyramiding, peak gain tracking, risk
mode, auto-trade + grow flags, KOL module, KOL on-chain, KOL divergence,
Telegram bindings + walletless.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-25 00:52:56 +08:00

155 lines
5.0 KiB
Python

from typing import Optional
from pydantic import BaseModel
class PriceImpact(BaseModel):
asset: str
# None = window still open (live rolling peak) or no price data yet.
# Float = sealed peak move (%) in the signal direction once window closed.
m5: Optional[float] = None
m15: Optional[float] = None
m1h: Optional[float] = None
price_at_post: float
# None = outcome window not yet reached; True/False = signal direction matched
correct_m5: Optional[bool] = None
correct_m15: Optional[bool] = None
correct_m1h: Optional[bool] = None
class TrumpPost(BaseModel):
id: int
text: str
source: str
published_at: str # ISO
sentiment: str
signal: Optional[str] = None # buy | sell | short | hold
ai_confidence: int
ai_reasoning: Optional[str] = None
prefilter_reason: Optional[str] = None # rt_only | url_only | empty | parse_error | api_error | null
analysis_version: Optional[str] = None
relevant: bool
price_impact: Optional[PriceImpact] = None
# v5 asset routing — what the bot will actually trade if signal != hold.
# target_asset = any HL perp ticker (BTC/ETH/SOL/TRUMP/...). category
# buckets the post's catalyst type. expected_move_pct = AI's own 1h-move
# estimate on target_asset. All three are null on pre-v5 posts.
target_asset: Optional[str] = None
category: Optional[str] = None
expected_move_pct: Optional[float] = None
invalidation_price: Optional[float] = None
model_config = {"from_attributes": True}
class Candle(BaseModel):
time: int
open: float
high: float
low: float
close: float
volume: float
class BotTrade(BaseModel):
id: int
asset: str
side: str
entry_price: float
exit_price: float
pnl_usd: float
hold_seconds: int
trigger_post_id: int
opened_at: str
closed_at: str
# Source tag of the originating signal (e.g. 'truth', 'breakout', 'my_strategy').
# Joined from posts.source on read; not stored on BotTrade itself.
# 'unknown' when the trigger post has been deleted or trigger_post_id is null.
trigger_source: Optional[str] = None
# True iff this was a paper trade (hl_order_id == 'paper'). Lets the UI
# tag the row so paper-mode P&L isn't mixed with real money in summaries.
is_paper: bool = False
model_config = {"from_attributes": True}
class BotPerformance(BaseModel):
period_days: int
total_trades: int
win_rate: float
net_pnl_usd: float
avg_hold_seconds: float
max_drawdown_pct: float
class SignedEnvelope(BaseModel):
"""Every write request carries this envelope.
Client builds a canonical message (see app.services.signed_request.build_message)
and signs it via EIP-191 personal_sign. Server re-builds the message from
{action, wallet, timestamp, sha256(body)} and recovers the signer.
"""
wallet: str
timestamp: int # milliseconds since epoch
signature: str
class SubscribeRequest(SignedEnvelope):
# Optional paper-mode flag. When true the bot writes simulated trades to
# the DB but never hits Hyperliquid — safe path for new users to try the
# system without risking funds. Defaults to false (live) for backwards
# compatibility with the existing signed-message format (body=None).
paper_mode: Optional[bool] = False
class SubscribeResponse(BaseModel):
status: str
wallet: str
paper_mode: bool = False
class SetApiKeyRequest(SignedEnvelope):
api_key: str # Hyperliquid API wallet private key (0x...)
class SetApiKeyResponse(BaseModel):
status: str
masked_key: str # last 6 chars only, e.g. "...a1b2c3"
verified: bool = False
class UserSettings(BaseModel):
leverage: int
position_size_usd: float
take_profit_pct: Optional[float] = None
stop_loss_pct: Optional[float] = None
min_confidence: int
daily_budget_usd: Optional[float] = None
# System-2 (bottom reversal) leverage — independent of `leverage` (Trump).
# None → platform default (SYS2_DEFAULT_LEVERAGE). The protective stop is
# auto-scaled to this so the position is never exchange-liquidated.
sys2_leverage: Optional[int] = None
# System-2 risk mode: "standard" (default) or "aggressive" (separately
# funded high-risk/high-explosiveness sleeve). None → unchanged/standard.
sys2_mode: Optional[str] = None
# ISO-8601 UTC strings; both None = always on (Subscription.active still gates it).
active_from: Optional[str] = None
active_until: Optional[str] = None
class SetSettingsRequest(SignedEnvelope):
settings: UserSettings
class UserResponse(BaseModel):
wallet_address: str
active: bool
subscribed_at: Optional[str] = None
hl_api_key_set: bool
hl_api_key_masked: Optional[str] = None
trades: list[BotTrade]
settings: UserSettings
# Convex-strategy: ISO-UTC timestamp until which the bot is manually armed.
# When null or in the past, the regular active_from/active_until schedule applies.
manual_window_until: Optional[str] = None