Skip to content

feat: live TVL and user metrics with 24h sparkline and /api/stats#12

Open
ijeoma270 wants to merge 1 commit into
SmartDropLabs:mainfrom
ijeoma270:feat/live-tvl-user-metrics
Open

feat: live TVL and user metrics with 24h sparkline and /api/stats#12
ijeoma270 wants to merge 1 commit into
SmartDropLabs:mainfrom
ijeoma270:feat/live-tvl-user-metrics

Conversation

@ijeoma270

Copy link
Copy Markdown

Closes #7

What changed

New files

File Purpose
src/lib/stats.ts fetchStats() — data layer; queries factory contract (stubbed with TODO for when it's deployed), falls back to realistic demo data; exports StatsData type
src/app/api/stats/route.ts GET /api/stats with 60-second Next.js revalidation — all clients share one cached response
src/hooks/useStats.ts useStats() react-query hook; polls every 60 s; tries /api/stats first, falls back to direct fetchStats() call so it works in static export too
src/components/Sparkline/Sparkline.tsx Dependency-free SVG sparkline with area fill and latest-point dot

Modified files

  • src/app/page.tsx — replaces hardcoded 30,738 / $302M / 213 with live data from useStats(); adds Chakra skeleton loading state; Demo badge (with tooltip explaining contracts not yet deployed) on the TVL card; 24h SVG sparkline inline with TVL value; third panel swapped to "Last Updated" with relative-time label (just now, 2m ago, …)
  • next.config.tsoutput: "export" now conditional on NEXT_EXPORT=true; unset = server mode, API routes active (Vercel / next start)
  • .github/workflows/deploy-github-pages.yml — adds NEXT_EXPORT: "true" to the build step so GitHub Pages CI still produces the static out/ directory

Architecture

Browser / polling every 60 s
       │
       ▼
useStats (react-query, staleTime 60 s)
       │
       ├─ tries GET /api/stats  (server deployment: Vercel / next start)
       │         │
       │         └─ fetchStats()  ← stubbed Soroban RPC + demo fallback
       │              └─ revalidate: 60 (Next.js cache)
       │
       └─ falls back to fetchStats() directly (static GitHub Pages export)

Wiring real data

When the factory contract is deployed, replace the TODO block in src/lib/stats.ts with:

// 1. Enumerate pools
const poolIds = await invokeView(rpc, factoryId, "get_pools", [])
// 2. Aggregate locked amounts per pool
// 3. USD conversion via Stellar DEX order book
// 4. Count unique stakers
// Then return source: "live"

The Demo badge and its tooltip disappear automatically once source: "live" is returned.

How to test

npm run dev          # server mode — /api/stats active, metrics update every 60s
NEXT_EXPORT=true npm run build  # static export — builds out/ without API routes
npm run build        # server build — /api/stats shows Revalidate: 1m in output
npx tsc --noEmit     # passes clean

Closes SmartDropLabs#7

- src/lib/stats.ts — fetchStats() queries the factory contract via Soroban
  RPC (stubbed with clear TODO) and falls back to plausible demo data until
  contracts are deployed; also exports StatsData type and formatUsd helper

- src/app/api/stats/route.ts — GET /api/stats with Next.js 60-second
  revalidation so all clients share a cached response without hammering RPC

- src/hooks/useStats.ts — react-query hook (refetchInterval: 60 s) that
  calls /api/stats when running in server mode; falls back to fetchStats()
  directly when the route is unavailable (static GitHub Pages export)

- src/components/Sparkline/Sparkline.tsx — dependency-free SVG sparkline
  component with optional fill area and latest-point dot indicator

- src/app/page.tsx — replaces hardcoded "30,738 / $302M / 213" with live
  data from useStats(); adds skeleton loading state, "Demo" badge (with
  tooltip) while contracts are undeployed, sparkline on the TVL card, and a
  "Last Updated" relative-time panel

- next.config.ts — output: "export" now conditional on NEXT_EXPORT=true so
  the API route is active in server/Vercel deployments while GitHub Pages CI
  continues to produce the static out/ directory

- deploy-github-pages.yml — sets NEXT_EXPORT=true in the build step
@prodbycorne

Copy link
Copy Markdown
Contributor

Let me review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Live Total Value Locked and User Metrics

2 participants