Markets Architecture
Overview
Babylon has three distinct market types, each with dedicated components and APIs:
1. Prediction Markets (Binary YES/NO)
Purpose: Users bet on binary outcomes of questions
Components:
MarketsPanel.tsx- Feed/trending sidebar widgetPredictionTradingModal.tsx- Buy YES/NO sharesPredictionPositionsList.tsx- View active positions
Pricing: Constant Product AMM
yesPrice = yesShares / (yesShares + noShares)
noPrice = noShares / (yesShares + noShares)APIs:
GET /api/feed/widgets/markets- Top prediction markets with price changesGET /api/markets/predictions- All prediction marketsPOST /api/markets/predictions/{id}/buy- Buy sharesPOST /api/markets/predictions/{id}/sell- Sell shares
Features:
- AMM pricing with dynamic odds
- Price change tracking (24h)
- Top movers section (markets with biggest probability shifts)
- Fallback “Most Active” section (by volume when no movers)
2. Perpetual Futures (Leveraged Trading)
Purpose: Long/short trading on company stocks with leverage
Components:
TopMoversPanel.tsx- Top gainers/losers widgetMarketOverviewPanel.tsx- Market statisticsMarketsWidgetSidebar.tsx- Combines both for /markets pagePerpTradingModal.tsx- Open long/short positionsPerpPositionsList.tsx- View open positions
Features:
- 1-100x leverage
- Funding rates (paid every 8h)
- Automatic liquidations
- Real-time PnL tracking
APIs:
GET /api/markets/perps- All perpetual marketsPOST /api/markets/perps/open- Open positionPOST /api/markets/perps/{id}/close- Close position
3. Trading Pools (NPC-Managed Funds)
Purpose: Users invest in pools managed by NPC traders
Components:
PoolsList.tsx- Browse available poolsUserPoolPositions.tsx- View your investmentsPoolsErrorBoundary.tsx- Error handling
Features:
- NPC-managed trading strategies
- Performance-based fees
- Reputation system
- Multiple tier levels (S, A, B, C)
APIs:
GET /api/pools- All poolsPOST /api/pools/{id}/deposit- Invest in poolPOST /api/pools/{id}/withdraw- Withdraw from poolGET /api/pools/deposits/{userId}- User’s positions
4. P&L Sharing (Social Features)
Purpose: Share trading performance on social media
Components:
PortfolioPnLCard.tsx- Overall portfolio displayCategoryPnLCard.tsx- Per-category (perps/predictions/pools)PortfolioPnLShareModal.tsx- Share portfolioCategoryPnLShareModal.tsx- Share category performancePortfolioPnLShareCard.tsx- Shareable image cardCategoryPnLShareCard.tsx- Category shareable card
Features:
- Download 1200x630 PNG cards
- Share to Twitter/X
- Share to Farcaster
- Copy share links
- Reputation tracking
5. Market Bias Indicator
Purpose: Show active market manipulations/biases
Components:
MarketBiasIndicator.tsx- Display active biases
Features:
- Show sentiment adjustments
- Price impact display
- Decay rates
- Expiration timers
API:
GET /api/markets/bias/active- Active market biases
Architecture Principles
Why This Works Well
- Clear Separation: Each market type has dedicated components
- No Code Duplication: Perps, predictions, and pools are distinct
- Proper Abstraction: Trading modals follow similar patterns but aren’t duplicated
- Consistent APIs: RESTful endpoints for each market type
- Modular Widgets: Can be composed into different sidebars
Component Composition
Feed/Trending Pages:
<WidgetSidebar>
<LatestNewsPanel />
<TrendingPanel />
<MarketsPanel /> {/* Prediction markets */}
</WidgetSidebar>Markets Page:
<MarketsWidgetSidebar>
<MarketOverviewPanel />
<TopMoversPanel /> {/* Perp futures */}
</MarketsWidgetSidebar>Data Flow
- Widget Components fetch from APIs
- Modal Components handle user actions (buy/sell/deposit/withdraw)
- Position Lists display user’s active positions
- Share Components generate social media cards
Future Considerations
- Consider adding price history tracking for predictions (currently only perps)
- Could add pool performance charts
- May want to consolidate P&L calculation logic
- Consider adding WebSocket for real-time price updates
Files Removed
PoolDetailModal.tsx- Empty/unused file (removed)
Debug Code Cleaned
- Removed 6 console.log statements from
MarketsPanel.tsx - Kept appropriate console.error in
MarketBiasIndicator.tsx
Last updated on