Architecture Overview

Architecture Design Overview

System Components

BetBit has four main components that work together to handle the full wager lifecycle:

  User (Browser)
       |
       v
  +-----------+     +-------------+     +------------+
  | Frontend  |---->| Backend API |---->| Database   |
  |           |     |             |     |            |
  +-----------+     +-------------+     +------------+
       |                  |
       v                  v
  +-----------+     +-------------+
  | Pimlico   |     | The Graph   |
  | Bundler + |     | Subgraph    |
  | Paymaster |     | (indexing)  |
  +-----------+     +-------------+
       |                  |
       v                  v
  +-------------------------------+
  |    Avalanche C-Chain          |
  |  WagerEscrow  |  USDC        |
  |  AccessControl (UUPS proxy)  |
  +-------------------------------+

Smart Contracts (On-Chain)

Three contracts deployed to Avalanche:

WagerEscrow: The core protocol. Holds deposited USDC in escrow, manages the wager state machine (11 states from PENDING through SETTLED/REFUNDED), evaluates the outcome truth table, computes fee deductions, and handles withdrawals. This contract is immutable. Once deployed, its logic cannot change.

WagerAccessControl: Governance layer deployed behind a UUPS proxy. Manages protocol fees, pause states, mediator roles, and dispute resolution escalation. Upgradeable so the team can adjust parameters without redeploying the escrow.

USDC: The settlement currency. On testnet, a MockUSDC ERC-20 is used. On mainnet, the real Avalanche USDC contract would be referenced.

Backend API (Off-Chain)

A lightweight API server handling:

  • User accounts: Signup, authentication, wallet linking

  • Wager metadata: Descriptions, match details, mediator contact info (too expensive to store on-chain)

  • Shareable links: Signed invite URLs that let the second party join a wager

  • Mediator tokens: Time-limited tokens for dispute resolution

  • Subgraph queries: Fetches indexed wager events for listing and history

The backend never touches funds. All financial operations go through the smart contract. The backend is purely a metadata and coordination layer.

Frontend (User Interface)

A modern web application with:

  • Passkey authentication: WebAuthn registration and login, no seed phrases

  • Smart wallet management: Account creation and transaction signing via ERC-4337

  • Contract interaction: Efficient ABI encoding for all escrow operations

  • Real-time state: Direct on-chain reads via eth_call for deposit/outcome status

  • State-driven UI: Each of the 11 wager states maps to a specific screen with contextual actions

The Graph Subgraph (Indexing)

Indexes all WagerEscrow events (WagerCreated, Deposited, OutcomeSubmitted, Settled, etc.) into a queryable GraphQL API. Used primarily for the dashboard's wager list and historical transaction data. Not used for real-time state. That comes from direct RPC calls.


Core Workflows

Wager Creation

Deposit Flow (Second Party)

Outcome Submission & Settlement

Withdrawal


Technical Structure

Monorepo Layout

The project is organized as a monorepo with five packages:

Database

Six core tables store off-chain data: user accounts (wallet addresses, credentials, profiles), wager metadata (descriptions, deadlines, metadata hashes), shareable invite links, mediator approval tokens, outcome descriptions, and a notification audit log.

All on-chain data (balances, wager state, deposits, outcomes) is read directly from the smart contract or subgraph. The database only stores what can't live on-chain cost-effectively.

Security

Contract level: ReentrancyGuard on all fund operations, SafeERC20 for token transfers, pull-pattern withdrawals, immutable constructor parameters, fee hardcaps, granular pause states.

Application level: Security headers, CORS restricted to the frontend origin, rate limiting on all endpoints (stricter on auth), parameterized queries, hashed passwords, JWT authentication.

Infrastructure level: Firewall restricting inbound traffic, brute force protection, reverse proxy rate limiting, blocked exploit paths, SSL with automatic certificate renewal, automatic security updates.

Deployment

The production environment runs behind a reverse proxy that routes API traffic to the backend and all other requests to the frontend. The database runs locally on the same host. A process manager keeps both services running with automatic restarts, and SSL certificates auto-renew.

Last updated