You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Crypto Data Aggregator is a Next.js application that aggregates cryptocurrency market data from
multiple sources. It uses the App Router for routing and server-side rendering, with Edge Runtime
for API routes.
Key Design Principles
Edge-First - API routes run on Edge Runtime for low latency
Cache-Heavy - Multi-layer caching reduces API calls
Progressive Enhancement - Works without JavaScript, enhanced with it
Zero API Keys Required - Free tier APIs only (CoinGecko, DeFiLlama)
System Architecture
flowchart TB
subgraph Client["Browser"]
UI[React Components]
SWR[SWR Cache]
LS[LocalStorage]
end
subgraph NextJS["Next.js App"]
subgraph Pages["App Router"]
SSR[Server Components]
CSR[Client Components]
end
subgraph API["API Routes (Edge)"]
Market["/api/market/*"]
DeFi["/api/defi"]
Sentiment["/api/sentiment"]
Portfolio["/api/portfolio"]
end
subgraph Lib["Library Layer"]
MarketData[market-data.ts]
Cache[cache.ts]
Alerts[alerts.ts]
PortfolioLib[portfolio.ts]
end
end
subgraph External["External APIs"]
CoinGecko[(CoinGecko API)]
DeFiLlama[(DeFiLlama API)]
AlternativeMe[(Alternative.me)]
end
UI --> SWR
SWR --> API
UI --> LS
SSR --> Lib
API --> Lib
Lib --> Cache
MarketData --> CoinGecko
MarketData --> DeFiLlama
MarketData --> AlternativeMe
Loading
Data Flow
Market Data Flow
sequenceDiagram
participant U as User
participant C as Component
participant S as SWR
participant A as API Route
participant L as Lib/market-data
participant M as Memory Cache
participant E as External API
U->>C: View Page
C->>S: useSWR('/api/market/coins')
alt Cache Hit (SWR)
S-->>C: Return cached data
else Cache Miss
S->>A: GET /api/market/coins
A->>L: getTopCoins()
alt Memory Cache Hit
L->>M: getCached('top-coins')
M-->>L: Cached data
else Memory Cache Miss
L->>E: fetch(coingecko.com/...)
E-->>L: Market data
L->>M: setCache('top-coins', data)
end
L-->>A: TokenPrice[]
A-->>S: JSON Response
S-->>C: Update UI
end
C-->>U: Render coins table
Loading
Price Alert Flow
sequenceDiagram
participant U as User
participant C as AlertModal
participant P as AlertsProvider
participant L as LocalStorage
participant A as API
participant W as WebSocket
U->>C: Create Alert
C->>P: addAlert(alert)
P->>L: Save to localStorage
loop Every 30s
P->>A: Check prices
A-->>P: Current prices
P->>P: Evaluate conditions
alt Alert Triggered
P->>U: Show notification
P->>L: Mark as triggered
end
end
Note over W: Alternative: WebSocket
W-->>P: Real-time price update
P->>P: Evaluate immediately
Loading
Portfolio Calculation Flow
flowchart LR
subgraph Input
H[Holdings<br/>coinId, amount, avgPrice]
end
subgraph Fetch
P[Current Prices<br/>getTopCoins]
end
subgraph Calculate
V[Value = amount × price]
PL[P&L = value - cost]
A[Allocation = value / total]
end
subgraph Output
R[PortfolioValue<br/>totalValue, holdings[]]
end
H --> V
P --> V
V --> PL
V --> A
PL --> R
A --> R
flowchart LR
subgraph Request
R[HTTP Request]
P[Parse Params]
V[Validate]
end
subgraph Process
C[Check Cache]
F[Fetch Data]
T[Transform]
end
subgraph Response
H[Add Headers]
J[JSON Response]
end
R --> P --> V
V --> C
C -->|Hit| H
C -->|Miss| F --> T --> H
H --> J