Architecture

PolyMarks follows a modular, feature-based architecture for scalability and maintainability.

Project Structure

polymarks/
β”œβ”€β”€ app/                    # Next.js App Router pages
β”‚   β”œβ”€β”€ layout.tsx         # Root layout
β”‚   β”œβ”€β”€ page.tsx           # Homepage (3D landing)
β”‚   β”œβ”€β”€ markets/           # Markets dashboard
β”‚   β”œβ”€β”€ whales/            # Whale tracker
β”‚   β”œβ”€β”€ outliers/          # Smart money signals
β”‚   β”œβ”€β”€ market/[slug]/     # Individual market pages
β”‚   └── api/               # API routes (proxies)
β”‚       β”œβ”€β”€ markets/       # Polymarket markets proxy
β”‚       └── trades/        # Polymarket trades proxy
β”œβ”€β”€ core/                   # Shared utilities
β”‚   β”œβ”€β”€ api/               # API clients
β”‚   β”œβ”€β”€ config/            # Environment config
β”‚   └── types/             # TypeScript types
β”œβ”€β”€ features/               # Feature modules
β”‚   β”œβ”€β”€ markets/           # Market listing & filtering
β”‚   β”œβ”€β”€ whales/            # Whale tracking
β”‚   β”œβ”€β”€ outliers/          # Smart money detection
β”‚   β”œβ”€β”€ referral/          # Polymarket referral URLs
β”‚   └── market-whale-sentiment/  # Market sentiment
β”œβ”€β”€ components/            # Shared UI components
└── hooks/                 # Custom React hooks

Design Principles

1. Feature Modules

Each feature is self-contained with:

  • Types

  • Business logic

  • Components

  • README documentation

Example: features/whales/

whales/
β”œβ”€β”€ README.md              # Feature documentation
β”œβ”€β”€ components/
β”‚   β”œβ”€β”€ WhaleTradeCard.tsx
β”‚   └── index.ts
β”œβ”€β”€ lib/                   # Business logic (if needed)
└── index.ts               # Public API

2. Core Module

Shared utilities used across features:

  • API clients (Polymarket, Helius)

  • TypeScript types

  • Environment configuration

3. API Routes as Proxies

Next.js API routes proxy external APIs to:

  • Avoid CORS issues

  • Hide API keys (server-side only)

  • Add caching/rate limiting

Example: /api/markets β†’ gamma-api.polymarket.com/markets

4. Client vs Server Components

  • Server Components: Data fetching, initial render

  • Client Components: Interactivity, real-time updates

Data Flow

User Request
    ↓
Next.js Page (Server Component)
    ↓
API Route (/api/markets)
    ↓
External API (Polymarket)
    ↓
Transform Data (core/api clients)
    ↓
Feature Module (features/markets)
    ↓
UI Component
    ↓
User

State Management

  • Server State: React Server Components + fetch with caching

  • Client State: React useState + useEffect

  • Real-Time: Polling with setInterval

No external state library needed (keeping it simple).

Performance Optimizations

  1. Next.js Caching:

    • next: { revalidate: 60 } for markets (1 min cache)

    • next: { revalidate: 30 } for trades (30 sec cache)

  2. Incremental Static Regeneration (ISR):

    • Markets dashboard pre-rendered

    • Revalidates in background

  3. API Rate Limiting:

    • Removed market-specific whale fetching from grid

    • Only fetch on detail pages

    • Prevents 429/500 errors

  4. Lazy Loading:

    • Three.js components lazy loaded

    • Heavy dependencies code-split

Last updated