right-longArchitecture

System Architecture

┌─────────────────┐
│   Frontend      │
│   (Next.js)     │
└────────┬────────┘

         ├─── Dashboard Page
         ├─── Chat Page
         ├─── Trading Page
         ├─── Backrooms Page
         └─── Agent Page

┌────────▼────────┐
│   API Routes    │
│   (Next.js)     │
└────────┬────────┘

         ├─── Trading API
         ├─── Dashboard API
         ├─── Chat API
         └─── Backrooms API

┌────────▼────────┐
│   External APIs │
└────────┬────────┘

         ├─── DexScreener
         ├─── Polymarket
         ├─── Hyperliquid
         └─── Anthropic Claude

┌────────▼────────┐
│   MongoDB       │
│   (Atlas)       │
└─────────────────┘

Data Flow

Trading Bot Flow

  1. Background worker triggers every 30 seconds

  2. Fetches market data from all sources

  3. Gets AI decision from Claude

  4. Executes trade via Trading API

  5. Updates MongoDB state

  6. Clients sync state every 1 second

Dashboard Data Flow

  1. Client requests data from Dashboard API

  2. API checks cache (10 seconds TTL)

  3. If cache miss, fetches from external APIs

  4. Returns cached or fresh data

  5. Client updates UI

  6. Auto-refreshes at configured intervals

Chat Flow

  1. User sends message

  2. Chat API receives message

  3. Fetches current market data

  4. Sends context to Claude

  5. Receives AI response

  6. Returns response to user

Caching Strategy

Server-Side Caching

  • In-Memory Cache: 10 seconds TTL for dashboard data

  • Vercel Edge Cache: CDN caching with stale-while-revalidate

  • Cache Headers: Optimized for performance

Client-Side Caching

  • React State: Local component state

  • Real-Time Sync: Periodic database sync

  • Optimistic Updates: Immediate UI updates

Database Schema

Trading State Collection

Reflections Collection


Last updated