Files
plm-lottery/apps/lottery/rules.py
T
davideandClaude Sonnet 5.5 eded058dbb A3: simulated ledger with idempotency (WEB-22, WEB-23, ADR-59, ARC-75..77, DAT-80)
One Ledger interface (deposit, bet, confirm, reject, exclude, payout) with a
simulated implementation chosen by LEDGER_BACKEND, refused on mainnet. Every
operation is a single write-locked transaction with its audit row; the same
request key is the same operation, a key reused for something else is an
error. The server validates round, deadline, the round's own quota, limits and
funds. The balance is derived from the ledger. Tests include six processes
racing on one SQLite file; the idempotency tests were checked to fail when
the replay is removed.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 13:23:29 +01:00

31 lines
1.1 KiB
Python

"""Rules of the game that do not depend on how money moves (shared by the simulated ledger and,
later, the chain engine)."""
from datetime import datetime, timedelta
from django.utils import timezone
from .models import Round
# B-52: a round never grows past what one payout can spend ("it can always be paid out").
MAX_PARTICIPANTS_PER_ROUND = 400
# ADR-14: 70% to the winner, 30% to the platform, the rounding remainder to the platform.
WINNER_PERCENT = 70
def winner_share(pool_sats: int) -> int:
return pool_sats * WINNER_PERCENT // 100
def round_deadline(round_: Round) -> datetime:
"""The round's own deadline: opening plus *its* duration, never the live config (ADR-23, B-61)."""
return round_.opened_at + timedelta(seconds=round_.duration_seconds)
def round_accepts_bets(round_: Round, now: datetime | None = None) -> bool:
"""The authoritative "yellow light": a bet is refused from the deadline on, even if the
scheduler has not flipped the status yet (ADR-23)."""
now = now or timezone.now()
return round_.status == Round.Status.OPEN and now < round_deadline(round_)