# shubham02022002/foretell architecture

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

- Interactive diagram: https://gitdiagram.com/shubham02022002/foretell
- Repository: https://github.com/shubham02022002/foretell

## Overview

Foretell is a virtual-credit binary prediction-market exchange. Traders discover markets, inspect probability/book activity, place or cancel orders, manage positions and wallet history, and follow trader standings; administrators create, close, resolve, or void markets. The README documents matching, escrow, settlement, candles, social features, notifications, and secured expiry handling. The map preserves these product branches, but several route implementations were not sampled: their links are shown only where the README or excerpt supports them. PostgreSQL access is represented as the shared persistence layer.

Main entry points: [`lib/prisma.ts`](https://github.com/shubham02022002/foretell/blob/main/lib/prisma.ts) (PostgreSQL via Prisma), [`lib/exchange/market.ts`](https://github.com/shubham02022002/foretell/blob/main/lib/exchange/market.ts) (Market lifecycle), [`app/markets/[slug]/page.tsx`](https://github.com/shubham02022002/foretell/blob/main/app/markets/%5Bslug%5D/page.tsx) (Market detail), [`lib/exchange/orders.ts`](https://github.com/shubham02022002/foretell/blob/main/lib/exchange/orders.ts) (Order engine)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_experience["Trader experience"]
  node_market_browse["Market discovery<br/>[page.tsx]"]
  node_market_detail["Market detail<br/>[page.tsx]"]
  node_book_ui["Book and tape<br/>[order-book.tsx]"]
  node_chart_ui["Price charts"]
  node_discussion_ui["Market discussion"]
  node_portfolio_ui["Portfolio<br/>[page.tsx]"]
  node_watchlist_ui["Watchlist<br/>[page.tsx]"]
  node_home_ui["Market home<br/>[page.tsx]"]
end

subgraph group_market["Market lifecycle"]
  node_market_api["Market routes<br/>[route.ts]"]
  node_market_lifecycle["Market lifecycle<br/>[market.ts]"]
  node_cron_close["Expiry closing<br/>[route.ts]"]
end

subgraph group_exchange["Trading engine"]
  node_orders_api["Order routes<br/>[route.ts]"]
  node_order_engine["Order engine<br/>[orders.ts]"]
  node_matching["Binary matching<br/>[matching.ts]"]
  node_settlement["Settlement math<br/>[settlement.ts]"]
  node_position_api["Positions API<br/>[route.ts]"]
  node_wallet_api["Wallet and ledger<br/>[route.ts]"]
end

subgraph group_account["Accounts and community"]
  node_auth_ui["Sign-in and registration<br/>[auth-shell.tsx]"]
  node_auth_service["Authentication<br/>[auth.ts]"]
  node_registration_api["Account registration<br/>[route.ts]"]
  node_social_api["Social and notifications<br/>[route.ts]"]
  node_leaderboard_ui["Leaderboard<br/>[page.tsx]"]
  node_leaderboard_service["Trader standings<br/>[leaderboard.ts]"]
  node_trader_profile["Trader profiles<br/>[route.ts]"]
end

subgraph group_admin["Administration"]
  node_admin_ui["Admin console<br/>[page.tsx]"]
  node_admin_routes["Market administration<br/>[route.ts]"]
end

node_trader(("Trader"))
node_database[("PostgreSQL via Prisma<br/>[prisma.ts]")]

node_trader -->|"browses"| node_market_browse
node_market_browse -.->|"queries markets"| node_market_api
node_market_api -->|"gets market stats"| node_market_lifecycle
node_market_api -->|"reads markets"| node_database
node_market_browse -.->|"opens market"| node_market_detail
node_market_detail -->|"renders tape"| node_book_ui
node_market_detail -->|"renders discussion"| node_discussion_ui
node_market_detail -.->|"renders chart"| node_chart_ui
node_market_detail -->|"submits orders"| node_orders_api
node_orders_api -.->|"executes orders"| node_order_engine
node_order_engine -->|"matches orders"| node_matching
node_order_engine -.->|"persists fills"| node_database
node_market_lifecycle -->|"aggregates book"| node_matching
node_market_lifecycle -->|"cancels orders"| node_order_engine
node_market_lifecycle -->|"calculates payouts"| node_settlement
node_market_lifecycle -.->|"reads and updates"| node_database
node_trader -->|"reviews holdings"| node_portfolio_ui
node_portfolio_ui -.->|"loads positions"| node_position_api
node_position_api -->|"reads positions"| node_database
node_trader -->|"checks balance"| node_wallet_api
node_wallet_api -->|"reads wallet"| node_database
node_trader -->|"tracks markets"| node_watchlist_ui
node_trader -->|"creates account"| node_auth_ui
node_auth_ui -.->|"registers"| node_registration_api
node_registration_api -->|"creates user"| node_database
node_auth_service -->|"authenticates users"| node_database
node_trader -->|"checks standings"| node_leaderboard_ui
node_leaderboard_ui -.->|"displays rankings"| node_leaderboard_service
node_leaderboard_service -->|"queries trade data"| node_database
node_trader -->|"views profile"| node_trader_profile
node_trader_profile -->|"gets standings"| node_leaderboard_service
node_trader_profile -->|"reads profile positions"| node_database
node_trader -.->|"comments"| node_social_api
node_admin_ui -->|"manages markets"| node_admin_routes
node_admin_routes -->|"creates markets"| node_market_lifecycle
node_admin_routes -->|"writes markets"| node_database
node_cron_close -.->|"closes expired"| node_market_lifecycle

click node_market_browse "https://github.com/shubham02022002/foretell/blob/main/app/markets/page.tsx"
click node_market_detail "https://github.com/shubham02022002/foretell/blob/main/app/markets/%5Bslug%5D/page.tsx"
click node_book_ui "https://github.com/shubham02022002/foretell/blob/main/components/order-book.tsx"
click node_chart_ui "https://github.com/shubham02022002/foretell/blob/main/components/probability-chart.tsx"
click node_discussion_ui "https://github.com/shubham02022002/foretell/blob/main/components/market-discussion.tsx"
click node_portfolio_ui "https://github.com/shubham02022002/foretell/blob/main/app/portfolio/page.tsx"
click node_watchlist_ui "https://github.com/shubham02022002/foretell/blob/main/app/watchlist/page.tsx"
click node_home_ui "https://github.com/shubham02022002/foretell/blob/main/app/page.tsx"
click node_market_api "https://github.com/shubham02022002/foretell/blob/main/app/api/markets/route.ts"
click node_market_lifecycle "https://github.com/shubham02022002/foretell/blob/main/lib/exchange/market.ts"
click node_cron_close "https://github.com/shubham02022002/foretell/blob/main/app/api/cron/close-expired/route.ts"
click node_orders_api "https://github.com/shubham02022002/foretell/blob/main/app/api/markets/%5Bslug%5D/orders/route.ts"
click node_order_engine "https://github.com/shubham02022002/foretell/blob/main/lib/exchange/orders.ts"
click node_matching "https://github.com/shubham02022002/foretell/blob/main/lib/exchange/matching.ts"
click node_settlement "https://github.com/shubham02022002/foretell/blob/main/lib/exchange/settlement.ts"
click node_position_api "https://github.com/shubham02022002/foretell/blob/main/app/api/positions/me/route.ts"
click node_wallet_api "https://github.com/shubham02022002/foretell/blob/main/app/api/wallet/route.ts"
click node_auth_ui "https://github.com/shubham02022002/foretell/blob/main/components/auth-shell.tsx"
click node_auth_service "https://github.com/shubham02022002/foretell/blob/main/auth.ts"
click node_registration_api "https://github.com/shubham02022002/foretell/blob/main/app/api/auth/register/route.ts"
click node_social_api "https://github.com/shubham02022002/foretell/blob/main/app/api/markets/%5Bslug%5D/comments/route.ts"
click node_leaderboard_ui "https://github.com/shubham02022002/foretell/blob/main/app/leaderboard/page.tsx"
click node_leaderboard_service "https://github.com/shubham02022002/foretell/blob/main/lib/leaderboard.ts"
click node_trader_profile "https://github.com/shubham02022002/foretell/blob/main/app/api/traders/%5Bid%5D/route.ts"
click node_admin_ui "https://github.com/shubham02022002/foretell/blob/main/app/admin/page.tsx"
click node_admin_routes "https://github.com/shubham02022002/foretell/blob/main/app/api/admin/markets/route.ts"
click node_database "https://github.com/shubham02022002/foretell/blob/main/lib/prisma.ts"

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_market_browse,node_market_detail,node_book_ui,node_chart_ui,node_discussion_ui,node_portfolio_ui,node_watchlist_ui,node_home_ui toneBlue
class node_market_api,node_market_lifecycle,node_cron_close,node_database toneAmber
class node_orders_api,node_order_engine,node_matching,node_settlement,node_position_api,node_wallet_api toneMint
class node_auth_ui,node_auth_service,node_registration_api,node_social_api,node_leaderboard_ui,node_leaderboard_service,node_trader_profile toneRose
class node_admin_ui,node_admin_routes,node_trader toneIndigo
```

## Components

### Trader experience

- [Market discovery](https://github.com/shubham02022002/foretell/blob/main/app/markets/page.tsx): `app/markets/page.tsx`
- [Market detail](https://github.com/shubham02022002/foretell/blob/main/app/markets/%5Bslug%5D/page.tsx): `app/markets/[slug]/page.tsx`
- [Book and tape](https://github.com/shubham02022002/foretell/blob/main/components/order-book.tsx): `components/order-book.tsx`
- [Price charts](https://github.com/shubham02022002/foretell/blob/main/components/probability-chart.tsx): `components/probability-chart.tsx`
- [Market discussion](https://github.com/shubham02022002/foretell/blob/main/components/market-discussion.tsx): `components/market-discussion.tsx`
- [Portfolio](https://github.com/shubham02022002/foretell/blob/main/app/portfolio/page.tsx): `app/portfolio/page.tsx`
- [Watchlist](https://github.com/shubham02022002/foretell/blob/main/app/watchlist/page.tsx): `app/watchlist/page.tsx`
- [Market home](https://github.com/shubham02022002/foretell/blob/main/app/page.tsx): `app/page.tsx`

### Market lifecycle

- [Market routes](https://github.com/shubham02022002/foretell/blob/main/app/api/markets/route.ts): `app/api/markets/route.ts`
- [Market lifecycle](https://github.com/shubham02022002/foretell/blob/main/lib/exchange/market.ts): `lib/exchange/market.ts`
- [Expiry closing](https://github.com/shubham02022002/foretell/blob/main/app/api/cron/close-expired/route.ts): `app/api/cron/close-expired/route.ts`

### Trading engine

- [Order routes](https://github.com/shubham02022002/foretell/blob/main/app/api/markets/%5Bslug%5D/orders/route.ts): `app/api/markets/[slug]/orders/route.ts`
- [Order engine](https://github.com/shubham02022002/foretell/blob/main/lib/exchange/orders.ts): `lib/exchange/orders.ts`
- [Binary matching](https://github.com/shubham02022002/foretell/blob/main/lib/exchange/matching.ts): `lib/exchange/matching.ts`
- [Settlement math](https://github.com/shubham02022002/foretell/blob/main/lib/exchange/settlement.ts): `lib/exchange/settlement.ts`
- [Positions API](https://github.com/shubham02022002/foretell/blob/main/app/api/positions/me/route.ts): `app/api/positions/me/route.ts`
- [Wallet and ledger](https://github.com/shubham02022002/foretell/blob/main/app/api/wallet/route.ts): `app/api/wallet/route.ts`

### Accounts and community

- [Sign-in and registration](https://github.com/shubham02022002/foretell/blob/main/components/auth-shell.tsx): `components/auth-shell.tsx`
- [Authentication](https://github.com/shubham02022002/foretell/blob/main/auth.ts): `auth.ts`
- [Account registration](https://github.com/shubham02022002/foretell/blob/main/app/api/auth/register/route.ts): `app/api/auth/register/route.ts`
- [Social and notifications](https://github.com/shubham02022002/foretell/blob/main/app/api/markets/%5Bslug%5D/comments/route.ts): `app/api/markets/[slug]/comments/route.ts`
- [Leaderboard](https://github.com/shubham02022002/foretell/blob/main/app/leaderboard/page.tsx): `app/leaderboard/page.tsx`
- [Trader standings](https://github.com/shubham02022002/foretell/blob/main/lib/leaderboard.ts): `lib/leaderboard.ts`
- [Trader profiles](https://github.com/shubham02022002/foretell/blob/main/app/api/traders/%5Bid%5D/route.ts): `app/api/traders/[id]/route.ts`

### Administration

- [Admin console](https://github.com/shubham02022002/foretell/blob/main/app/admin/page.tsx): `app/admin/page.tsx`
- [Market administration](https://github.com/shubham02022002/foretell/blob/main/app/api/admin/markets/route.ts): `app/api/admin/markets/route.ts`

### Other components

- **Trader**
- [PostgreSQL via Prisma](https://github.com/shubham02022002/foretell/blob/main/lib/prisma.ts): `lib/prisma.ts`

## Connections

- Trader → Market discovery: browses
- Market discovery → Market routes: queries markets
- Market routes → Market lifecycle: gets market stats (evidence: `app/api/markets/route.ts`)
- Market routes → PostgreSQL via Prisma: reads markets (evidence: `app/api/markets/route.ts`)
- Market discovery → Market detail: opens market
- Market detail → Book and tape: renders tape (evidence: `app/markets/[slug]/page.tsx`)
- Market detail → Market discussion: renders discussion (evidence: `app/markets/[slug]/page.tsx`)
- Market detail → Price charts: renders chart (evidence: `app/markets/[slug]/page.tsx`)
- Market detail → Order routes: submits orders (evidence: `app/markets/[slug]/page.tsx`)
- Order routes → Order engine: executes orders
- Order engine → Binary matching: matches orders (evidence: `lib/exchange/market.ts`)
- Order engine → PostgreSQL via Prisma: persists fills
- Market lifecycle → Binary matching: aggregates book (evidence: `lib/exchange/market.ts`)
- Market lifecycle → Order engine: cancels orders (evidence: `lib/exchange/market.ts`)
- Market lifecycle → Settlement math: calculates payouts (evidence: `lib/exchange/market.ts`)
- Market lifecycle → PostgreSQL via Prisma: reads and updates
- Trader → Portfolio: reviews holdings
- Portfolio → Positions API: loads positions
- Positions API → PostgreSQL via Prisma: reads positions (evidence: `app/api/positions/me/route.ts`)
- Trader → Wallet and ledger: checks balance
- Wallet and ledger → PostgreSQL via Prisma: reads wallet (evidence: `app/api/wallet/route.ts`)
- Trader → Watchlist: tracks markets
- Trader → Sign-in and registration: creates account
- Sign-in and registration → Account registration: registers
- Account registration → PostgreSQL via Prisma: creates user (evidence: `app/api/auth/register/route.ts`)
- Authentication → PostgreSQL via Prisma: authenticates users (evidence: `auth.ts`)
- Trader → Leaderboard: checks standings
- Leaderboard → Trader standings: displays rankings
- Trader standings → PostgreSQL via Prisma: queries trade data (evidence: `lib/leaderboard.ts`)
- Trader → Trader profiles: views profile
- Trader profiles → Trader standings: gets standings (evidence: `app/api/traders/[id]/route.ts`)
- Trader profiles → PostgreSQL via Prisma: reads profile positions (evidence: `app/api/traders/[id]/route.ts`)
- Trader → Social and notifications: comments
- Admin console → Market administration: manages markets (evidence: `app/admin/page.tsx`)
- Market administration → Market lifecycle: creates markets (evidence: `app/api/admin/markets/route.ts`)
- Market administration → PostgreSQL via Prisma: writes markets (evidence: `app/api/admin/markets/route.ts`)
- Expiry closing → Market lifecycle: closes expired
