How metroxtt/fairbet works
1. Overall shape: a modular Django monolith
- The repo is a single Django project (src/config) split into four domain apps: users, wallet, events, and betting.
- It mixes server-rendered pages, JSON/DRF endpoints, and real-time WebSocket features, but all business logic lives in the same backend codebase.
2. Main runtime and infrastructure
- The app runs on Django 5 with Django REST Framework, Channels, Celery, PostgreSQL, and Redis.
- Deployment is containerized with Docker Compose: one web container (Daphne/ASGI), one PostgreSQL instance, and one Redis instance used for cache and channel layer/WebSocket messaging.
3. Presentation layer
- The frontend is mostly server-rendered HTML under src/templates, with shared layout/components and per-domain pages for home, events, wallet, betting, and user/account flows.
- There is a small amount of client-side JavaScript and CSS, mainly for auth and dynamic interactions.
- This is not a SPA; the browser primarily talks to Django views and only uses WebSockets where live odds are needed.
4. users app: identity, permissions, and responsible gaming
- This app owns registration/login, KYC simulation, profile pages, self-exclusion, and deposit limits.
- It also appears to enforce role separation via permissions.py, matching the project’s “staff cannot bet” policy.
- Templates for login/register/KYC/deposit limits/self-exclusion are part of this boundary, so user lifecycle is a first-class subsystem rather than an afterthought.
5. wallet app: virtual funds and accounting
- This is the financial core of the simulator: deposits, withdrawals, balances, and ledger history.
- The README and ADRs indicate a double-entry accounting model with immutable/auditable ledger behavior and hash chaining.
- Because betting affects funds, this app is a critical boundary: other subsystems should not mutate balances directly; they should create/consume wallet entries.
6. events app: sports events, markets, and live odds
- This app owns the catalog of sporting events, market configuration, margin/odds logic, and live updates.
- It includes consumers.py, routing.py, and signals.py, which strongly suggests WebSocket broadcast paths for odds changes and event state updates.
- There is also a seed_events management command, indicating events can be bootstrapped separately from normal user traffic.
7. betting app: bet placement, combinations, and settlement
- This subsystem handles single and combined bets, bet lifecycle, settlement/liquidation, and cash-out-related logic.
- The presence of tasks, migrations for suspicious activity, and state-machine ADRs suggests betting is modeled as a controlled workflow rather than a simple record insert.
- Betting depends on both users (eligibility, limits, exclusions) and wallet (fund reservation/settlement), making it a coordination layer across domain boundaries.
8. Asynchronous and real-time flows
- Channels + Redis are used to push live odds/event updates from the backend to connected clients over WebSockets.
- Celery handles background work such as settlement, recotization/repricing, or other deferred operations that should not block the request cycle.
- This creates two non-HTTP paths in the system: one for real-time updates and one for background processing.
9. Data model and persistence
- PostgreSQL is the system of record for all domains: users, wallet ledger, events/markets, and bets.
- Migrations are present per app, showing that each domain owns its own schema evolution.
- The ADRs indicate careful decisions around concurrency, idempotency, state transitions, precision, and auditability, which are especially important for wallet/betting correctness.
10. API and view boundaries
- Each app has views.py, serializers.py, and urls.py, which implies a mix of DRF-backed endpoints and conventional Django views.
- The likely pattern is: templates for human-facing pages, serializers/views for JSON interactions, and WebSocket consumers for live data.
- config/urls.py is the top-level router that composes these app boundaries into one site.
11. Operational/admin surface
- There is a distinct operator/admin experience: custom admin pages, event management screens, and dashboard views under templates/users/.
- The admin.py files across apps and the management command for seeding events show the project supports back-office workflows in addition to end-user flows.
- This admin side is important architecturally because operators can manage events and verify ledger activity without using the same user-facing paths.
12. Cross-cutting engineering concerns
- The docs/adr/ folder is a major architectural artifact: it documents why the system uses TextChoices, 24h cooldowns, double-entry accounting, select_for_update, idempotency, bet state machines, and WebSocket recotization.
- Those decisions imply the architecture emphasizes correctness, auditability, and concurrency safety over simplicity.
- The project is explicitly educational and simulates betting with virtual currency, so the wallet and betting subsystems are business-critical but not connected to external payment providers.
13. Useful diagram boundaries
- A clear diagram should show: browser/UI layer → Django app layer → domain subsystems (users, wallet, events, betting) → PostgreSQL, Redis, and Celery.
- Also show a separate WebSocket path from browser to events consumers via ASGI/Redis, and a background job path from Celery back into the database.
- The key dependency directions are: betting depends on users/wallet/events; wallet is the source of truth for funds; events is the source of live pricing; users controls eligibility and responsible gaming rules.
Main entry points: templates Templates · urls.py URL router · betting Betting · docker-compose.yml Docker Compose
Components 20
Presentation
Server-rendered pages, shared UI, and browser assets
- Templates · Shared layout and per-domain HTML pages
src/templates - Static assets · Browser-side styling and small interactive scripts
src/static
Domain apps
Core Django business subsystems
- URL router · Top-level router composing all app boundaries
src/config/urls.py - Users · Registration, login, KYC, profile, exclusions, and limits
src/users - Wallet · Balances, deposits, withdrawals, and audit history
src/wallet - Events · Event catalog, markets, pricing, and live odds updates
src/events - Betting · Bet placement, combinations, settlement, and cash-out workflows
src/betting - User API · HTTP endpoints and JSON serialization for account flows
src/users/views.py - Wallet API · HTTP endpoints for balance and ledger interactions
src/wallet/views.py - Events API · HTTP endpoints for events, markets, and live data
src/events/views.py - Betting API · HTTP endpoints for bet lifecycle and history
src/betting/views.py
Async runtime
WebSocket and background processing paths
- ASGI app · ASGI entrypoint for HTTP and WebSocket traffic
src/config/asgi.py - Celery app · Celery bootstrap for deferred jobs
src/config/celery.py - Live odds · Channels consumers broadcasting event and odds changes
src/events/consumers.py - Events jobs · Deferred event-side work and updates
src/events/tasks.py - Betting jobs · Deferred settlement and workflow processing
src/betting/tasks.py
Data stores
Primary persistence and messaging infrastructure
- PostgreSQL · System of record for users, wallet, events, and bets
- Redis · Used for caching and WebSocket messaging
Ops and bootstrap
Deployment and management entry points
- Docker Compose · Containerized runtime wiring for web, database, and Redis
docker-compose.yml - Seed events · Bootstrap command for event data
src/events/management/commands/seed_events.py
Connections 27
- URL router → User API: routes · Composes user-facing and API endpoints
- URL router → Wallet API: routes · Composes wallet endpoints
- URL router → Events API: routes · Composes event endpoints
- URL router → Betting API: routes · Composes betting endpoints
- ASGI app → Live odds: WS · Exposes Channels consumers for live updates
- Celery app → Events jobs: runs · Registers deferred event work
- Celery app → Betting jobs: runs · Registers deferred betting work
- User API → Templates: renders · Serves account, KYC, and exclusion pages
- Wallet API → Templates: renders · Serves wallet views and history pages
- Events API → Templates: renders · Serves event listing and detail pages
- Betting API → Templates: renders · Serves bet slip and betting history pages
- Templates → Static assets: uses · Shared UI depends on CSS and small client-side scripts
- User API → PostgreSQL: CRUD · Stores identity, profile, and responsible-gaming state
- Wallet API → PostgreSQL: ledger · Persists balances and immutable ledger history
- Events API → PostgreSQL: catalog · Stores events, markets, and odds state
- Betting API → PostgreSQL: bets · Persists bets, combinations, and settlement state
- Live odds → Redis: pubsub · Broadcasts live odds via channel layer
- Events jobs → PostgreSQL: update · Applies deferred event-side changes
- Betting jobs → PostgreSQL: settle · Applies deferred betting workflow updates
- Docker Compose → PostgreSQL: provisions · Runs PostgreSQL as a containerized service
- Docker Compose → Redis: provisions · Runs Redis as a containerized service
- Docker Compose → ASGI app: serves · Web container runs the Django/ASGI app
- Seed events → Events: bootstraps · Loads initial event catalog data
- Betting → Users: checks · Depends on eligibility, exclusions, and limits
- Betting → Wallet: uses funds · Relies on wallet reservation and settlement
- Betting → Events: prices · Consumes events and odds for bet placement
- Events → Redis: pushes · Uses Redis-backed live update path