# metroxtt/fairbet architecture

> Architecture diagram of the GitHub repository metroxtt/fairbet, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/metroxtt/fairbet
- Repository: https://github.com/metroxtt/fairbet

## Overview

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: [`src/templates`](https://github.com/metroxtt/fairbet/tree/main/src/templates) (Templates), [`src/config/urls.py`](https://github.com/metroxtt/fairbet/blob/main/src/config/urls.py) (URL router), [`src/betting`](https://github.com/metroxtt/fairbet/tree/main/src/betting) (Betting), [`docker-compose.yml`](https://github.com/metroxtt/fairbet/blob/main/docker-compose.yml) (Docker Compose)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_presentation["Presentation"]
  node_templates["Templates<br/>server UI"]
  node_static_assets["Static assets<br/>css/js"]
end

subgraph group_domain["Domain apps"]
  node_config_urls["URL router<br/>django routing<br/>[urls.py]"]
  node_users_app["Users<br/>identity"]
  node_wallet_app[("Wallet<br/>ledger")]
  node_events_app["Events<br/>markets"]
  node_betting_app["Betting<br/>wagering"]
  node_users_api["User API<br/>views+serializers<br/>[views.py]"]
  node_wallet_api["Wallet API<br/>views+serializers<br/>[views.py]"]
  node_events_api["Events API<br/>views+serializers<br/>[views.py]"]
  node_betting_api["Betting API<br/>views+serializers<br/>[views.py]"]
end

subgraph group_async["Async runtime"]
  node_config_asgi["ASGI app<br/>channels entry<br/>[asgi.py]"]
  node_config_celery["Celery app<br/>task runner<br/>[celery.py]"]
  node_events_ws["Live odds<br/>websocket consumer<br/>[consumers.py]"]
  node_events_tasks["Events jobs<br/>background tasks<br/>[tasks.py]"]
  node_betting_tasks["Betting jobs<br/>background tasks<br/>[tasks.py]"]
end

subgraph group_data["Data stores"]
  node_db[("PostgreSQL<br/>primary store")]
  node_redis["Redis<br/>cache+channel layer"]
end

subgraph group_ops["Ops and bootstrap"]
  node_docker["Docker Compose<br/>deployment<br/>[docker-compose.yml]"]
  node_seed_events["Seed events<br/>management command<br/>[seed_events.py]"]
end

node_config_urls -->|"routes"| node_users_api
node_config_urls -->|"routes"| node_wallet_api
node_config_urls -->|"routes"| node_events_api
node_config_urls -->|"routes"| node_betting_api
node_config_asgi -->|"WS"| node_events_ws
node_config_celery -->|"runs"| node_events_tasks
node_config_celery -->|"runs"| node_betting_tasks
node_users_api -->|"renders"| node_templates
node_wallet_api -->|"renders"| node_templates
node_events_api -->|"renders"| node_templates
node_betting_api -->|"renders"| node_templates
node_templates -->|"uses"| node_static_assets
node_users_api -->|"CRUD"| node_db
node_wallet_api -->|"ledger"| node_db
node_events_api -->|"catalog"| node_db
node_betting_api -->|"bets"| node_db
node_events_ws -->|"pubsub"| node_redis
node_events_tasks -->|"update"| node_db
node_betting_tasks -->|"settle"| node_db
node_docker -->|"provisions"| node_db
node_docker -->|"provisions"| node_redis
node_docker -->|"serves"| node_config_asgi
node_seed_events -->|"bootstraps"| node_events_app
node_betting_app -->|"checks"| node_users_app
node_betting_app -->|"uses funds"| node_wallet_app
node_betting_app -->|"prices"| node_events_app
node_events_app -->|"pushes"| node_redis

click node_config_urls "https://github.com/metroxtt/fairbet/blob/main/src/config/urls.py"
click node_config_asgi "https://github.com/metroxtt/fairbet/blob/main/src/config/asgi.py"
click node_config_celery "https://github.com/metroxtt/fairbet/blob/main/src/config/celery.py"
click node_templates "https://github.com/metroxtt/fairbet/tree/main/src/templates"
click node_static_assets "https://github.com/metroxtt/fairbet/tree/main/src/static"
click node_users_app "https://github.com/metroxtt/fairbet/tree/main/src/users"
click node_wallet_app "https://github.com/metroxtt/fairbet/tree/main/src/wallet"
click node_events_app "https://github.com/metroxtt/fairbet/tree/main/src/events"
click node_betting_app "https://github.com/metroxtt/fairbet/tree/main/src/betting"
click node_users_api "https://github.com/metroxtt/fairbet/blob/main/src/users/views.py"
click node_wallet_api "https://github.com/metroxtt/fairbet/blob/main/src/wallet/views.py"
click node_events_api "https://github.com/metroxtt/fairbet/blob/main/src/events/views.py"
click node_betting_api "https://github.com/metroxtt/fairbet/blob/main/src/betting/views.py"
click node_events_ws "https://github.com/metroxtt/fairbet/blob/main/src/events/consumers.py"
click node_events_tasks "https://github.com/metroxtt/fairbet/blob/main/src/events/tasks.py"
click node_betting_tasks "https://github.com/metroxtt/fairbet/blob/main/src/betting/tasks.py"
click node_docker "https://github.com/metroxtt/fairbet/blob/main/docker-compose.yml"
click node_seed_events "https://github.com/metroxtt/fairbet/blob/main/src/events/management/commands/seed_events.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_templates,node_static_assets toneBlue
class node_config_urls,node_users_app,node_wallet_app,node_events_app,node_betting_app,node_users_api,node_wallet_api,node_events_api,node_betting_api toneAmber
class node_config_asgi,node_config_celery,node_events_ws,node_events_tasks,node_betting_tasks toneMint
class node_db,node_redis toneRose
class node_docker,node_seed_events toneIndigo
```

## Components

### Presentation

Server-rendered pages, shared UI, and browser assets

- [Templates](https://github.com/metroxtt/fairbet/tree/main/src/templates): server UI · Shared layout and per-domain HTML pages · `src/templates`
- [Static assets](https://github.com/metroxtt/fairbet/tree/main/src/static): css/js · Browser-side styling and small interactive scripts · `src/static`

### Domain apps

Core Django business subsystems

- [URL router](https://github.com/metroxtt/fairbet/blob/main/src/config/urls.py): django routing · Top-level router composing all app boundaries · `src/config/urls.py`
- [Users](https://github.com/metroxtt/fairbet/tree/main/src/users): identity · Registration, login, KYC, profile, exclusions, and limits · `src/users`
- [Wallet](https://github.com/metroxtt/fairbet/tree/main/src/wallet): ledger · Balances, deposits, withdrawals, and audit history · `src/wallet`
- [Events](https://github.com/metroxtt/fairbet/tree/main/src/events): markets · Event catalog, markets, pricing, and live odds updates · `src/events`
- [Betting](https://github.com/metroxtt/fairbet/tree/main/src/betting): wagering · Bet placement, combinations, settlement, and cash-out workflows · `src/betting`
- [User API](https://github.com/metroxtt/fairbet/blob/main/src/users/views.py): views+serializers · HTTP endpoints and JSON serialization for account flows · `src/users/views.py`
- [Wallet API](https://github.com/metroxtt/fairbet/blob/main/src/wallet/views.py): views+serializers · HTTP endpoints for balance and ledger interactions · `src/wallet/views.py`
- [Events API](https://github.com/metroxtt/fairbet/blob/main/src/events/views.py): views+serializers · HTTP endpoints for events, markets, and live data · `src/events/views.py`
- [Betting API](https://github.com/metroxtt/fairbet/blob/main/src/betting/views.py): views+serializers · HTTP endpoints for bet lifecycle and history · `src/betting/views.py`

### Async runtime

WebSocket and background processing paths

- [ASGI app](https://github.com/metroxtt/fairbet/blob/main/src/config/asgi.py): channels entry · ASGI entrypoint for HTTP and WebSocket traffic · `src/config/asgi.py`
- [Celery app](https://github.com/metroxtt/fairbet/blob/main/src/config/celery.py): task runner · Celery bootstrap for deferred jobs · `src/config/celery.py`
- [Live odds](https://github.com/metroxtt/fairbet/blob/main/src/events/consumers.py): websocket consumer · Channels consumers broadcasting event and odds changes · `src/events/consumers.py`
- [Events jobs](https://github.com/metroxtt/fairbet/blob/main/src/events/tasks.py): background tasks · Deferred event-side work and updates · `src/events/tasks.py`
- [Betting jobs](https://github.com/metroxtt/fairbet/blob/main/src/betting/tasks.py): background tasks · Deferred settlement and workflow processing · `src/betting/tasks.py`

### Data stores

Primary persistence and messaging infrastructure

- **PostgreSQL**: primary store · System of record for users, wallet, events, and bets
- **Redis**: cache+channel layer · Used for caching and WebSocket messaging

### Ops and bootstrap

Deployment and management entry points

- [Docker Compose](https://github.com/metroxtt/fairbet/blob/main/docker-compose.yml): deployment · Containerized runtime wiring for web, database, and Redis · `docker-compose.yml`
- [Seed events](https://github.com/metroxtt/fairbet/blob/main/src/events/management/commands/seed_events.py): management command · Bootstrap command for event data · `src/events/management/commands/seed_events.py`

## Connections

- 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
