How okeson453/crashwave works
Purpose and Runtime Entry
A TypeScript/Node.js 20+ service automates observation and controlled participation in BC.Game Crash rounds, with Telegram operator control, deterministic safeguards, settlement, and analytics. Process composition and startup are centered on src/index.ts, src/app/composition.ts, and src/app/spec-upgrade-wiring.ts. Configuration is loaded and validated before runtime assembly through src/config/loader.ts and src/config/schema.ts.
Command, Lifecycle, and Safety Control
Telegram is the primary operator boundary: authenticated commands enter through src/telegram/gateway.ts and src/telegram/router.ts, while platform-level tenant routing is in src/platform/telegram-router.ts. The core orchestrator coordinates domain services over an event bus in src/core/orchestrator.ts, src/core/event-bus/bus.ts, and src/core/event-bus/events.ts. Session state is explicit and guarded by src/core/state-machine/machine.ts; emergency stops, single-instance locking, recovery, and session supervision prevent unsafe concurrent or automatic continuation (src/core/emergency-stop.ts, src/core/instance-lock.ts, src/core/recovery-manager.ts).
Game Observation and Browser Boundary
The system observes game rounds through an adapter abstraction, separating game data acquisition from execution policy: src/game/adapter.ts, src/game/observer.ts, and src/game/adapters/aggregator.ts. Browser automation is isolated behind session, profile, and health management in src/browser/manager.ts, src/browser/session.ts, and src/browser/health.ts. Protocol/WebSocket parsing forms a second game-input path in src/protocol/session-handshake.ts and src/protocol/ws-payload-schemas.ts. Proxy, TLS, and HTTP/2 behavior are similarly encapsulated under src/network/proxy-manager.ts and src/network/tls/ja4-fingerprint.ts.
Decision and Betting Execution
Round data feeds prediction/entry decisions and a deterministic risk gate. The prediction subsystem builds features and evaluates signals through src/prediction/feature-engine.ts, src/prediction/prediction-engine.ts, and src/prediction/entry-decision-service.ts; it also contains historical datasets, backtesting, and model registry components. Authorized entries pass through coordination, idempotency, execution safeguards, placement, confirmation, and cash-out control: src/betting/betting-coordinator.ts, src/betting/idempotency.ts, src/betting/executor.ts, src/betting/live-executor.ts, and src/betting/cashout.ts. Risk state and policy enforcement are bounded in src/betting/risk-engine.ts and src/risk/velocity-controller.ts.
Financial Truth, Settlement, and Persistence
PostgreSQL/TimescaleDB is the durable system of record, Redis supports runtime coordination/cache concerns, and repositories isolate storage access (src/persistence/client.ts, src/persistence/redis-client.ts, src/persistence/transaction.ts). Schema evolution establishes relational, time-series, tenant/RLS, audit, outbox, billing, and double-entry settlement controls under migrations/001_init_relational.sql, migrations/008_tenancy.sql, and migrations/017_double_entry_settlement.sql. Operational balances and daily limits are tracked in src/ledger/balance-tracker.ts and src/ledger/daily-entries.ts. Authoritative post-round settlement, drift detection, and boot reconciliation are handled by src/settlement/authoritative-settlement-engine.ts, src/settlement/drift-guard.ts, and src/settlement/boot-reconcile.ts.
Multi-Tenant Platform and Payments
The platform layer adds tenant context, quotas, container lifecycle, secrets, subscriptions, and payment integrations: src/platform/control-plane.ts, src/platform/tenant-manager.ts, and src/platform/tenant-context.ts. Stripe subscription webhooks are processed by src/platform/billing/stripe-webhook.ts; Paystack virtual-account/payment handling is in src/platform/payments/paystack-client.ts and src/platform/payments/webhook-handler.ts. Background workers perform daily billing and settlement reconciliation in src/background-workers/daily-billing-worker.ts and src/background-workers/settlement-reconciler.ts.
Analytics, Notifications, and Operations
Persisted round/bet outcomes feed descriptive metrics, anomaly detection, recommendations, and reports through src/analytics/engine.ts, src/analytics/learning/anomaly.ts, and src/analytics/reports/daily.ts. Notifications route to Telegram via src/notifications/notification-router.ts and src/notifications/telegram.ts. Health, logs, traces, metrics, and live alerts are provided by src/observability/health/monitor.ts, src/observability/metrics/registry.ts, and src/observability/alerts/live-alerts.ts. Docker deployments provision the application with PostgreSQL/Redis plus Prometheus and Grafana assets under docker/docker-compose.yml, docker/prometheus/prometheus.yml, and docker/grafana/.
Main entry points: orchestrator.ts Round orchestrator · betting-coordinator.ts Bet execution · adapter.ts Game observation · client.ts PostgreSQL / TimescaleDB
Components 22
Runtime & Control
- Startup composition · Builds and wires the service after configuration validation.
src/app/composition.ts - Config validation
src/config/loader.ts - Telegram gateway · Authenticated operator commands enter here.
src/telegram/gateway.ts - Tenant routing
src/platform/telegram-router.ts - Round orchestrator · Coordinates lifecycle and domain services through events.
src/core/orchestrator.ts - Event bus
src/core/event-bus/bus.ts - Session safety · Guards lifecycle, emergency stop, locking, and recovery.
src/core/state-machine/machine.ts
Game & Decision
- Game observation · Abstracts round acquisition from execution policy.
src/game/adapter.ts - Protocol input
src/protocol/session-handshake.ts - Browser automation · Manages browser sessions, profiles, and health.
src/browser/manager.ts - Prediction & entry · Builds features, evaluates signals, and selects entries.
src/prediction/entry-decision-service.ts - Risk gate · Enforces deterministic risk state and velocity controls.
src/betting/risk-engine.ts - Bet execution · Coordinates idempotent placement, confirmation, and cash-out.
src/betting/betting-coordinator.ts
Settlement & Platform
- PostgreSQL / TimescaleDB · Durable relational, audit, outbox, and settlement data.
src/persistence/client.ts - Redis
src/persistence/redis-client.ts - Operational ledger · Tracks balances and daily limits.
src/ledger/balance-tracker.ts - Authoritative settlement · Settles rounds and detects reconciliation drift.
src/settlement/authoritative-settlement-engine.ts - Tenant control plane · Manages tenant context, quotas, containers, and secrets.
src/platform/control-plane.ts - Billing & payments · Processes Stripe subscriptions and Paystack payments.
src/platform/billing/stripe-webhook.ts
Operations
- Analytics & reports · Produces metrics, anomaly detection, recommendations, and reports.
src/analytics/engine.ts - Telegram notifications
src/notifications/notification-router.ts - Observability · Provides health monitoring, metrics, and live alerts.
src/observability/health/monitor.ts
Connections 25
- Config validation → Startup composition: validates before assembly
- Startup composition → Round orchestrator: wires
- Telegram gateway → Tenant routing: commands
- Tenant routing → Round orchestrator: tenant-scoped control
- Round orchestrator → Event bus: publishes / consumes
- Round orchestrator → Session safety: lifecycle guard
- Round orchestrator → Game observation: observes rounds
- Protocol input → Game observation: alternate round input
- Game observation → Prediction & entry: round data
- Prediction & entry → Risk gate: entry proposal
- Risk gate → Bet execution: authorized entry
- Bet execution → Browser automation: place and cash out
- Bet execution → PostgreSQL / TimescaleDB: execution records
- Bet execution → Operational ledger: balance effects
- Game observation → Authoritative settlement: round outcome
- Authoritative settlement → Operational ledger: authoritative entries
- Authoritative settlement → PostgreSQL / TimescaleDB: settlement truth
- Round orchestrator → Redis: runtime coordination
- Tenant control plane → Tenant routing: tenant context
- Billing & payments → Tenant control plane: subscription and payment status
- Billing & payments → PostgreSQL / TimescaleDB: billing records
- PostgreSQL / TimescaleDB → Analytics & reports: round and bet outcomes
- Analytics & reports → Telegram notifications: reports and recommendations
- Round orchestrator → Telegram notifications: operator alerts
- Round orchestrator → Observability: health, logs, metrics