RoundConfig gains round_duration_seconds, round_cooldown_seconds, min_amount_sats, fee_rate_sat_vb and rbf_timeout_seconds (plus a hardcoded default for the pre-existing bet_amount_sats) as column defaults on the model itself — get_round_config no longer seeds from Settings at all. Every call site that read these from settings (scheduler, bets, withdrawals, rounds service/route, RBF bumper) now reads the DB-backed RoundConfig instead. app/config.py now holds only true env-driven infra/secrets (database URL, Electrum connection, master key, JWT, admin token) — no business parameter has an env var anymore, matching an explicit decision to drop the "seed from settings" indirection entirely rather than keep a env fallback nobody should rely on. Migration backfills the existing round_config row via server_default (matching the old settings defaults) then drops the default, so future rows go through the ORM/model defaults instead of a stale constant. Tests updated: should_bump's timeout_seconds is now required (no settings fallback); test_scheduler.py seeds a RoundConfig row directly instead of monkeypatching settings; test_withdrawals.py and test_rounds_service.py use local constants mirroring the model defaults instead of reading them off settings. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
41 lines
1.8 KiB
Python
41 lines
1.8 KiB
Python
from datetime import datetime, timedelta, timezone
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.db.models import Round
|
|
from app.rounds.config import get_round_config
|
|
|
|
_ACTIVE_STATUSES = ("open", "closing", "drawing", "paying_out")
|
|
|
|
|
|
async def get_active_round(session: AsyncSession) -> Round | None:
|
|
"""The round currently in progress (in any non-closed state), if any. Rounds
|
|
never overlap: a new round only opens once the previous one is fully closed
|
|
(payout confirmed, or no participants to pay out)."""
|
|
return await session.scalar(select(Round).where(Round.status.in_(_ACTIVE_STATUSES)).order_by(Round.id.desc()))
|
|
|
|
|
|
async def open_new_round_if_needed(session: AsyncSession) -> Round | None:
|
|
"""Returns the active round if one exists (whatever its status). Otherwise
|
|
opens a fresh one, unless the last closed round's cooldown (ROUND_COOLDOWN_SECONDS)
|
|
hasn't elapsed yet — in which case returns None. Callers that need to attach a
|
|
bet must additionally check the returned round's status == "open" — a round in
|
|
closing/drawing/paying_out isn't accepting new bets, but a new round can't open
|
|
until it's done."""
|
|
active = await get_active_round(session)
|
|
if active is not None:
|
|
return active
|
|
|
|
last_closed = await session.scalar(select(Round).where(Round.status == "closed").order_by(Round.id.desc()))
|
|
if last_closed is not None and last_closed.closed_at is not None:
|
|
closed_at = last_closed.closed_at.replace(tzinfo=timezone.utc)
|
|
cooldown_seconds = (await get_round_config(session)).round_cooldown_seconds
|
|
if datetime.now(timezone.utc) < closed_at + timedelta(seconds=cooldown_seconds):
|
|
return None
|
|
|
|
round_ = Round(status="open")
|
|
session.add(round_)
|
|
await session.flush()
|
|
return round_
|