fix: pre-launch hardening — HYPE price feed, KOL wallet cleanup, Telegram Trump alert, rate limiting, brittle test

Batch of the pre-launch audit campaign (BUG-01…14 plus three new features):

Pricing / TP-SL protection
- Add app/services/hl_price_feed.py: supplemental HL allMids poller for
  HL-native assets (HYPE, PURR) not listed on Binance. Pumps price_store +
  tp_sl_monitor.on_price_tick so bot trades on these assets keep full
  stop-loss / take-profit / trailing protection instead of max-hold only.
- Wire feed into main.py lifespan (startup task + graceful shutdown cancel).

Telegram
- Add format_trump_mention + PATH B in _dispatch: crypto-relevant Trump
  posts with no directional signal (relevant=True, signal=hold) now alert
  the public channel only (no per-subscriber noise).
- Rate limiter (slowapi) on the API; assorted bot/digest fixes.

KOL on-chain
- seed_kol_wallets.py: KOL_FEEDS coverage cross-check; reversibly deactivate
  orphaned wallets (handle not in KOL_FEEDS → can never produce divergence)
  so the scanner stops burning cycles on them.

Tests / misc
- Fix brittle test_macro_ahr999_uses_same_formula_as_scanner: mock now uses
  realistic ms timestamps so the in-progress-day drop fires, matching the
  fetcher's bar count (was 0.3179 vs 0.3178 off-by-one).
- Refresh stale notify_signal comment in truth_social.py.

Frontend reduce-action type fix lives in the sibling repo.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
k
2026-05-29 11:57:19 +08:00
parent 6471e44aac
commit d6c802ef26
40 changed files with 1833 additions and 209 deletions
+39 -8
View File
@@ -11,11 +11,38 @@ from app.ws.manager import manager
logger = logging.getLogger(__name__)
ASSET_MAP = {
"btcusdt": "BTC",
"ethusdt": "ETH",
# symbol (lowercase Binance ticker) → asset (HL perp name, uppercase).
#
# WHY THIS MATTERS: tp_sl_monitor.on_price_tick(asset, price) is the sole
# mechanism that fires stop-loss, take-profit, and trailing-stop for live
# trades. If an asset is missing here, every trade on that asset silently
# loses all TP/SL protection — only the asyncio max-hold timer remains.
#
# Trump's AI picks target_asset from the full HL perp universe. The assets
# below cover the realistic set (BTC/ETH always, SOL/TRUMP/BNB/DOGE/LINK/AAVE
# for direct-named or thematic posts). HL-native assets (HYPE, PURR) are NOT
# on Binance; those require a separate HL price feed (see BUG-08 note in CLAUDE.md).
# Until that feed is added, HYPE trades fall back to max-hold only.
ASSET_MAP: dict[str, str] = {
"btcusdt": "BTC",
"ethusdt": "ETH",
"solusdt": "SOL",
"trumpusdt": "TRUMP",
"bnbusdt": "BNB",
"dogeusdt": "DOGE",
"linkusdt": "LINK",
"aaveusdt": "AAVE",
}
# Build the combined-stream WS URL from ASSET_MAP so the two are always in sync.
# Format: wss://...?streams=sym1@kline_1m/sym2@kline_1m/...
_BINANCE_WS_BASE = "wss://data-stream.binance.vision/stream"
BINANCE_WS_URL = (
_BINANCE_WS_BASE
+ "?streams="
+ "/".join(f"{sym}@kline_1m" for sym in ASSET_MAP)
)
async def _process_message(raw: str):
try:
@@ -98,16 +125,20 @@ async def run_binance_ws():
* max_queue — drop frames if downstream can't keep up rather than back
the WS receive buffer up indefinitely
"""
# Pre-fill with historical data
await fetch_historical("BTC", "btcusdt", limit=500)
await fetch_historical("ETH", "ethusdt", limit=500)
# Pre-fill historical candles for every tracked asset so price_store has
# a warm baseline before the first live tick arrives.
await asyncio.gather(*(
fetch_historical(asset, symbol, limit=500)
for symbol, asset in ASSET_MAP.items()
), return_exceptions=True)
backoff = 1
while True:
try:
logger.info("Connecting to Binance WebSocket: %s", settings.binance_ws_url)
logger.info("Connecting to Binance WebSocket (%d assets): %s",
len(ASSET_MAP), BINANCE_WS_URL)
async with websockets.connect(
settings.binance_ws_url,
BINANCE_WS_URL,
ping_interval=20,
ping_timeout=10,
close_timeout=5,