5fb1d52026
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>
111 lines
2.4 KiB
Python
111 lines
2.4 KiB
Python
import pytest
|
|
|
|
from app.config import Settings
|
|
from app.api import positions
|
|
|
|
|
|
class _Scalar:
|
|
def __init__(self, value):
|
|
self._value = value
|
|
|
|
def scalar_one_or_none(self):
|
|
return self._value
|
|
|
|
def scalar_one(self):
|
|
return self._value
|
|
|
|
def scalars(self):
|
|
return self
|
|
|
|
def all(self):
|
|
return self._value
|
|
|
|
|
|
class _Request:
|
|
async def json(self):
|
|
return {
|
|
"wallet": "0xabc",
|
|
"timestamp": 123,
|
|
"signature": "0xsig",
|
|
}
|
|
|
|
|
|
class _Trade:
|
|
id = 7
|
|
wallet_address = "0xabc"
|
|
closed_at = None
|
|
hl_order_id = "paper"
|
|
leverage = 3
|
|
asset = "BTC"
|
|
exit_price = 101.5
|
|
pnl_usd = 2.25
|
|
|
|
|
|
class _Sub:
|
|
leverage = 3
|
|
hl_api_key = None
|
|
|
|
|
|
class _Db:
|
|
def __init__(self, responses):
|
|
self._responses = list(responses)
|
|
|
|
async def execute(self, _stmt):
|
|
return _Scalar(self._responses.pop(0))
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_manual_close_returns_close_result(monkeypatch):
|
|
async def fake_close_and_finalize(**_kwargs):
|
|
return None
|
|
|
|
monkeypatch.setattr(positions, "verify_signed_request", lambda **_kwargs: None)
|
|
|
|
from app.services import bot_engine
|
|
|
|
monkeypatch.setattr(bot_engine, "close_and_finalize", fake_close_and_finalize)
|
|
|
|
db = _Db([_Trade(), _Sub(), _Trade()])
|
|
|
|
result = await positions.manual_close(7, _Request(), db)
|
|
|
|
assert result.status == "ok"
|
|
assert result.trade_id == 7
|
|
assert result.exit_price == 101.5
|
|
assert result.pnl_usd == 2.25
|
|
assert result.reason == "manual"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_open_positions_requires_signed_wallet_read(monkeypatch):
|
|
calls = []
|
|
|
|
def fake_verify(**kwargs):
|
|
calls.append(kwargs)
|
|
|
|
monkeypatch.setattr(positions, "verify_signed_request", fake_verify)
|
|
db = _Db([[]])
|
|
|
|
result = await positions.get_open_positions(
|
|
wallet="0xABC",
|
|
ts=123,
|
|
sig="0xsig",
|
|
db=db,
|
|
)
|
|
|
|
assert result.wallet == "0xabc"
|
|
assert calls == [{
|
|
"action": positions.ACTION_VIEW_POSITIONS,
|
|
"wallet": "0xabc",
|
|
"timestamp_ms": 123,
|
|
"signature": "0xsig",
|
|
"body": None,
|
|
"allow_replay": True,
|
|
}]
|
|
|
|
|
|
def test_settings_default_to_production_when_environment_not_explicit():
|
|
settings = Settings(database_url="sqlite+aiosqlite:///./test.db")
|
|
|
|
assert settings.environment == "production"
|