Skip to content

Jatin123lodhi/trading-app-next

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

41 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Trading App

A Next.js trading application.

Setup

  1. Start Docker locally and run the database:
docker-compose up -d
  1. Install dependencies:
npm i
  1. Run the development server:
npm run dev

The server will start at http://localhost:3000

πŸš€ Day 1: Full-Stack Trading App Foundation - Authentication & Database Setup

Set up the complete backend infrastructure for a Next.js trading application with production-ready patterns:

βœ… Completed Features

  • Project Initialization: Bootstrapped Next.js 14+ app with TypeScript, MongoDB integration, and Docker containerization
  • Authentication System: Implemented secure user registration and login API routes with JWT-based authentication
  • MongoDB Integration: Configured local MongoDB instance using Docker Compose with replica set support
  • Connection Optimization: Implemented MongoDB connection caching to prevent connection exhaustion in development mode
  • API Testing: Validated all authentication endpoints using Postman with successful user registration, login, and token verification flows

πŸ› οΈ Tech Stack

  • Frontend: Next.js 14+ (App Router)
  • Backend: Next.js API Routes
  • Database: MongoDB (Dockerized)
  • Authentication: JWT
  • Validation: Zod schemas
  • Tools: Docker, Postman

πŸ“ Key Learnings

  • Proper MongoDB connection pooling in serverless environments
  • Securing API routes with middleware authentication
  • Docker Compose for consistent local development environments
  • RESTful API design patterns for financial applications

🎯 Day 2: Market Creation & Role-Based Access Control

βœ… Completed Features

Market Creation System: Built a secure admin-only API endpoint for creating prediction markets with comprehensive validation layers.

The challenge? Handling multiple validation checks to ensure data integrity:

  • Admin Authorization: Role-based middleware restricting market creation to admins only
  • Input Validation: Zod schemas validating market data (title, description, category, end date)
  • Uniqueness Checks: Ensured unique market titles to prevent duplicates
  • Query System: Added pagination and filtering by category and status

πŸ”₯ Technical Challenge

Market creation required orchestrating multiple validation layers in sequence: JWT verification β†’ Role authorization β†’ Schema validation β†’ Database uniqueness checks. Getting the order right was crucial for both security and performance.

πŸ“š Key Learnings

  • Role-based access control patterns in Next.js API routes
  • Mongoose schema design for marketplace applications
  • RESTful query parameter handling for filtering and pagination

Day 2 complete - Markets are live! πŸŽͺ

πŸ’° Day 3: Order Placement System with Wallet Integration

βœ… Completed Features

Order Placement with Transactions: Implemented a complex order placement system that integrates user wallets with market trades using MongoDB transactions to ensure data consistency.

Wallet Management: Built user wallet system supporting multiple currencies (INR, USD) with balance tracking and locking mechanisms.

Key implementations:

  • Wallet Creation: User-specific wallets with currency validation and duplicate prevention
  • Balance Locking: Implemented lockedBalance field to reserve funds for pending orders
  • Transaction-Based Orders: Used MongoDB sessions for atomic operations across multiple collections
  • Multi-Step Validation: Market existence β†’ Market status β†’ Wallet verification β†’ Balance check
  • Order Lifecycle: Created status system (locked, won, lost, cancelled) for tracking order states

πŸ”₯ Technical Challenge

Order placement was the most complex endpoint yet - required MongoDB transactions to maintain ACID properties across wallet and order collections. The flow: start session β†’ validate market β†’ check balance β†’ lock funds β†’ create order β†’ commit/abort transaction.

If any validation fails, the entire transaction rolls back automatically, preventing inconsistent states like locked funds without an order.

πŸ“š Key Learnings

  • MongoDB transactions for multi-document ACID operations
  • Balance locking patterns for financial applications
  • Session management with proper cleanup (try-catch-finally)
  • Atomic operations using $inc for concurrent updates
  • Composite indexes for user-currency wallet lookups

Day 3 complete - Money in motion! πŸ’Έ

πŸ† Day 4: Market Settlement & Order Management

βœ… Completed Features

Market Settlement Engine: Admin endpoint to settle markets, determine winners, and distribute payouts using pool-based calculations with MongoDB transactions.

Order Lifecycle: Implemented order cancellation with balance restoration and individual order retrieval with ownership validation.

Wallet Deposits: Added balance top-up functionality with transaction tracking.

Key implementations:

  • Order Cancellation: Atomic wallet unlock on order cancellation
  • Settlement Algorithm: Pool-based payout distribution across winners
  • Bulk Operations: Mass order/wallet updates using bulkWrite
  • Dynamic Routes: GET /api/markets/[id] and /api/orders/[id] endpoints

πŸ”₯ Technical Challenge

Orchestrating atomic updates across Markets, Orders, and Wallets in a single transaction - calculate pools, update statuses, distribute winnings, all with rollback safety.

πŸ“š Key Learnings

  • Pool-based payout algorithms
  • MongoDB bulk operations with sessions
  • Financial state management patterns

Day 4 complete - Markets settled, winners paid! πŸ’°

🎨 Day 5: Frontend Integration - Minimal UI

βœ… Completed Features

UI Implementation: Connected backend APIs with a clean, minimal interface using shadcn/ui components and Tailwind CSS.

Built pages:

  • Login/Register: Form-based authentication with JWT token storage
  • Dashboard: User profile, wallet balances, and markets list with click-through navigation
  • Market Page: Individual market details with interactive trading panel

Trading Panel Component:

  • Wallet selection dropdown
  • Amount input with YES/NO outcome buttons
  • Real-time order placement via /api/orders
  • Toast notifications for feedback

πŸ”₯ Technical Challenge

Implementing Next.js hybrid rendering - Server Components for data fetching, Client Components for interactivity (forms, trading). Handled JWT auth state with localStorage while leveraging server-side rendering benefits.

πŸ“š Key Learnings

  • Next.js Server/Client Component patterns
  • JWT token management in browser storage
  • Component composition in App Router
  • shadcn/ui integration

πŸ“Έ Screenshots

(Screenshots will be added)

image image

Day 5 complete - Backend meets Frontend! 🎨

πŸ”§ Day 6: Fixes & Improvements

βœ… Completed Features

Settlement Engine Fix: Fixed critical transaction bugs - wallets now properly update on market settlement with correct payout calculations.

Wallet Management UI: Added wallet creation and balance top-up interface directly in dashboard.

Smart UX Enhancements:

  • Order list auto-refreshes after placement
  • Bet buttons disabled until wallet selected
  • Validation errors display as readable toast messages

πŸ”₯ Technical Challenge

Settlement was silently failing - wallet updates weren't part of the transaction. Added missing session parameters and fixed the payout math (winners get bet + share of losing pool).

πŸ“š Key Learnings

  • MongoDB transaction session propagation
  • React refresh patterns with prop triggers
  • Zod error formatting for better UX

Day 6 complete - Production-ready! 🎯

πŸ” Day 7: Middleware & Route Protection

βœ… Completed Features

Authentication Middleware: Implemented Next.js middleware for automatic route protection and authentication state management with Edge Runtime compatibility.

Cookie-Based Auth: Migrated from localStorage-only to httpOnly cookies for enhanced security against XSS attacks.

Logout System: Built server-side logout endpoint to properly clear httpOnly cookies.

Key implementations:

  • Route Protection: Middleware automatically redirects unauthenticated users from protected routes (/dashboard, /create-market, /market/*) to login page
  • Auth Route Guards: Logged-in users automatically redirected from /login and /register to dashboard
  • Redirect Preservation: Saves intended destination in query params (?redirect=/dashboard) to redirect users after login
  • Edge Runtime: Used jose library instead of jsonwebtoken for Edge Runtime compatibility
  • Security Hardening:
    • httpOnly cookies prevent JavaScript access
    • Conditional secure flag based on environment (HTTPS in production)
    • sameSite: strict for CSRF protection

πŸ”₯ Technical Challenge

Next.js middleware runs in Edge Runtime, which doesn't support Node.js-specific libraries like jsonwebtoken. Had to switch to jose library and convert JWT verification to work with Web Crypto APIs. Also discovered that httpOnly cookies require server-side deletion, necessitating a logout API endpoint.

πŸ“š Key Learnings

  • Edge Runtime vs Node.js runtime differences
  • httpOnly cookie security patterns
  • Next.js middleware execution model and config matchers
  • JWT verification with jose library (Web Crypto API)
  • Redirect flow patterns with query parameters
  • Open redirect vulnerability prevention

πŸ”’ Security Improvements

  • Tokens stored in httpOnly cookies (not accessible via JavaScript)
  • Environment-aware secure flag for HTTPS
  • Validation of redirect URLs to prevent open redirect attacks
  • Automatic token verification on every protected route

Day 7 complete - Secure by default! πŸ”

✨ Day 8: Testing & Micro Interactions

βœ… Completed Features

Test Coverage: Added unit tests for components to ensure reliability and catch regressions early.

Micro Interactions: Polished UI with smooth, delightful interactions:

  • Hover effects on login button
  • Shimmer skeleton loaders for card components
  • Icon loading states for async actions
  • Animated heading transitions

πŸ“š Key Learnings

  • Component testing with Jest and React Testing Library
  • CSS animations for better perceived performance
  • Skeleton loaders improve UX during data fetching
  • Small interactions create big impacts on user experience

Day 8 complete - Details matter! ✨

πŸŒƒ Day 9: 3D Landing Page with Three.js

βœ… Completed Features

Cyberpunk City Background: Integrated Three.js (via CDN) to create an immersive 3D animated cityscape on landing pages.

Interactive 3D Scene:

  • Mouse/touch-responsive camera rotation
  • Animated buildings with particle effects and dynamic lighting
  • Conditional rendering (landing, login, register pages only)
  • Glassmorphism UI overlay with backdrop blur

πŸ”₯ Technical Challenge

Loading Three.js from CDN with Next.js Script component while managing React strict mode double-mounting. Implemented proper cleanup with useRef to prevent memory leaks and duplicate scene initialization.

πŸ“š Key Learnings

  • Three.js integration in Next.js App Router
  • CDN-based library loading for reduced bundle size
  • Animation frame management and scene cleanup
  • Combining 3D backgrounds with modern UI patterns
image

Day 9 complete - Visual impact unlocked! πŸŒƒ

πŸ“Š Day 10: Real-Time Market Updates with Smart Polling

βœ… Completed Features

Real-Time Data Sync: Implemented automatic market updates to display live bet amounts and probability percentages as users place orders across the platform.

Smart Polling Strategy: Dashboard automatically refreshes market data every 5 seconds without disrupting the user experience.

πŸ”₯ Technical Challenge

SSE vs Polling Decision: Initially explored Server-Sent Events (SSE) for real-time updates, but discovered Vercel's serverless architecture has critical limitations:

Vercel Constraints:

  • Function timeout: 10s (Hobby) / 60s (Pro) / 5min (Enterprise max)
  • Each SSE connection blocks a serverless execution slot
  • Connections forcibly closed after timeout
  • Not designed for persistent connections

Why SSE Doesn't Scale on Vercel:

❌ 10k users with SSE = 10k blocked serverless executions
❌ Connections die after 60 seconds max
❌ Cost: Extremely expensive (pay per execution time)

Day 10 complete - Real-time data without the real-time server! πŸ“Š

πŸ”„ Day 11: TanStack Query Migration - Professional Data Management

βœ… Completed Features

TanStack Query Integration: Migrated entire dashboard from manual fetch/useState patterns to TanStack Query for declarative, cache-first data management.

Query & Mutation Implementation:

  • Markets Query: Automatic polling (5s intervals) with smart caching
  • Wallets Query: Cached fetching with conditional execution
  • Create Wallet Mutation: Optimistic updates with automatic cache invalidation
  • Add Balance Mutation: Transactional updates with rollback safety

State Management Elimination:

  • Removed 100+ lines of manual state management boilerplate
  • Eliminated all manual useState for loading states
  • Replaced manual useEffect polling with declarative refetchInterval
  • Automatic error handling via mutation callbacks

πŸ”₯ Technical Challenge

Dashboard had grown complex with manual fetch logic, loading states, polling intervals, and manual refetch calls scattered everywhere. The challenge: migrate to TanStack Query without breaking existing functionality while maintaining type safety.

Before (Manual Pattern):

const [markets, setMarkets] = useState([]);
const [loading, setLoading] = useState(true);
useEffect(() => { fetchMarkets() }, [user]);
useEffect(() => { setInterval(() => fetchMarkets(), 5000) }, []);
// Manual cleanup, manual refetch after mutations...

After (TanStack Query):

const { data: markets = [], isLoading } = useQuery({
  queryKey: ['markets'],
  queryFn: fetchMarkets,
  refetchInterval: 5000,
  enabled: !!user
});
// Automatic cleanup, automatic caching, automatic refetching!

🎯 Impact Summary

Metric Before After Improvement
Lines of Code ~180 ~80 πŸ”₯ 55% reduction
Manual useState 8 hooks 3 hooks ⚑ 62% reduction
useEffect hooks 4 hooks 0 hooks ✨ 100% elimination
Loading states Manual Automatic πŸ€– Fully automated
Cache management None Built-in πŸš€ Free performance

Day 11 complete - Data fetching, elevated! πŸ”„

🎨 Day 12: UI Polish & UX Refinements

βœ… Completed Features

Landing Page Redesign: Overhauled the landing page with enhanced visual hierarchy and modern design patterns for better first impressions.

Profile Dropdown: Added user profile dropdown component with quick access to account actions and navigation.

Live Price Animations: Implemented micro-interactions showing real-time price updates with smooth transitions and visual feedback.

Market Details Improvements: Enhanced the market page with better layout, clearer information architecture, and improved trading interface.

Route Fixes: Resolved navigation issues in market routing for smoother page transitions.

Flash of Red Elimination: Fixed the jarring red flash that appeared on initial page load by optimizing CSS loading order and component initialization.

πŸ”₯ Technical Challenge

Eliminating the red flash required tracing CSS cascade issues and component mount timing. The flash occurred due to Tailwind styles loading after initial paint, causing layout shifts with default browser styling showing first.

πŸ“š Key Learnings

  • CSS-in-JS loading optimization strategies
  • Dropdown component patterns with proper focus management
  • Animation frame timing for smooth price transitions
  • Layout shift prevention techniques

Day 12 complete - Polished to perfection! ✨

πŸš€ Day 13: Guest Access & Authentication

βœ… Features Added

  • Guest Login: Users can explore without registration
  • Enhanced Auth Flow: Improved login/register UX

πŸ”₯ Challenge

Implementing secure guest access while maintaining proper authentication state management.


Day 13 complete - Welcome guests aboard! πŸŽ‰

🎨 Day 14: Dashboard Redesign & Portfolio

βœ… Features Added

  • Dashboard Overhaul: New color scheme, spacing, and visual hierarchy
  • Trending Carousel: Interactive carousel for trending markets
  • Portfolio Enhancement: Better layout and data visualization
  • Design System: Unified card design with consistent styling

🎯 Key Improvements

  • Background: White β†’ Soft gray for easier viewing
  • Stats Cards: Gray β†’ Color-coded gradients for visual hierarchy
  • Trending: Static grid β†’ Interactive carousel
  • Layout: Variable heights β†’ Fixed, consistent spacing

Day 14 complete - Dashboard transformed! ✨

πŸ“Š Day 15: Real Volume Analytics

βœ… Features Added

  • Volume Trends Graph: Replaced mock data with real trading volume using MongoDB aggregation
  • Analytics API: /api/analytics/weekly-volume endpoint aggregates last 7 days of order data
  • Smart Fallbacks: Shows real data when available, mock data otherwise

πŸ”₯ Technical Challenge

MongoDB aggregation pipeline to group orders by date, sum daily volumes, and fill missing days with zeros for consistent 7-day visualization.

Impact: Chart now shows actual daily trading volume (β‚Ή3,500, β‚Ή18,200, β‚Ή0...) instead of fake patterns, providing real business insights. image


Day 15 complete - Real data, real insights! πŸ“Š

πŸŒ“ Day 16: Dark & Light Theme

βœ… Features Added

  • Theme Toggle: Dark and light mode support with system preference detection
  • UI Fixes: Resolved minor styling inconsistencies across components
  • UX Improvements: Enhanced visual consistency and theme-aware components

Day 16 complete - Theme switching unlocked! πŸŒ“

πŸ“± Day 17: Landing Page & Mobile Responsiveness

βœ… Features Added

  • Landing Page Enhancements: Improved layout, typography, and visual hierarchy for better first impressions
  • Mobile Optimization: Responsive design patterns ensuring seamless experience across all device sizes
  • Touch Interactions: Enhanced mobile-friendly interactions and navigation

πŸ”₯ Technical Challenge

Adapting animated Framer Motion components and complex gradient backgrounds to work smoothly on smaller screens while maintaining visual impact and performance.


Day 17 complete - Mobile-first, always! πŸ“± Building in public - One day at a time πŸ’ͺ

Releases

No releases published

Packages

 
 
 

Contributors