How anirban4ru/meditrace works
Purpose and Entry Points
MediTrace is a Next.js 14 pharmaceutical supply-chain ledger that combines on-chain batch provenance, cold-chain telemetry, inspection, and compliance reporting. The primary UI entry is app/page.tsx; batch-specific tracking is served by app/track/[batchId]/page.tsx. Global rendering, styles, and error boundaries originate in app/layout.tsx, app/globals.css, and app/error.tsx.
Application Shell and Role Experiences
The App Router frontend composes authentication, theme, and ledger state around specialized operational views. Manufacturer, pharmacy/inspection, role-administration, and telemetry workflows are represented by components/views/manufacturer-dashboard.tsx, components/views/pharmacy-terminal.tsx, components/views/role-allocation-dashboard.tsx, and components/views/telemetry-console.tsx. Shared state boundaries include components/auth-context.tsx and components/ledger-context.tsx.
Blockchain Ledger Boundary
Immutable supply-chain operations and access roles are defined in the Solidity contract at contracts/MedicineTracker.sol, configured for Hardhat in hardhat.config.ts. Frontend integration depends on the contract ABI and ledger-facing client logic in lib/abi.ts and lib/engine.ts. The intended network is Arbitrum Sepolia; wallet signatures authorize role changes, batch creation/handovers, and irreversible spoilage state.
Batch Lifecycle and Tracking Flow
A manufacturer creates or advances a batch through the ledger; subsequent carrier/pharmacy custody and status changes become contract-backed provenance. The application’s batch detail route, app/track/[batchId]/page.tsx, reads and presents this lifecycle, with route/chain visualization in components/route-map.tsx and explorer-oriented access in lib/explorer.ts. Simulation/reference data is centralized rather than embedded solely in views via lib/engine.ts and lib/reference-library.ts.
Telemetry, Webhook, and Alert Ingestion
Cold-chain conditions feed the telemetry console and may result in an on-chain spoiled batch. Alchemy sends blockchain event notifications to app/api/webhooks/alchemy/route.ts; this handler is the trust boundary for HMAC-verified external payloads before persistence/alert propagation. The webhook simulator at scripts/alchemy-webhook-simulator.js supports local event-flow validation. User-facing alert aggregation is implemented in components/alerts-inbox.tsx.
Supabase Data and Authorization
Supabase provides PostgreSQL-backed application state and RBAC-adjacent data outside the immutable ledger. Client connection code is in lib/supabase-client.ts; database shape and enforced row-access policy are defined in supabase/schema.sql and supabase/rls-policies.sql. Authentication context (components/auth-context.tsx) controls frontend identity/role state, while the smart contract remains the authority for on-chain permissions.
Inspection and Compliance Outputs
Pharmacy verification includes client-side computer-vision processing through lib/cv-pipeline.ts and the browser worker public/cv.worker.js, surfaced through components/views/pharmacy-terminal.tsx. Audit evidence combines ledger history and telemetry into downloadable compliance material through components/audit-log.tsx and lib/pdf-report.ts. Batch/operational exports are supported by lib/csv-export.ts.
Supporting Server Interfaces
The server exposes a news endpoint at app/api/news/route.ts and webhook ingestion at app/api/webhooks/alchemy/route.ts; these are the repository’s explicit backend HTTP boundaries. Deployment is a Next.js serverless/Edge-oriented runtime, backed by Supabase, Arbitrum Sepolia, and Alchemy, with client-side OpenCV for physical-package checks.
Main entry points: engine.ts Ledger engine · layout.tsx App shell · audit-log.tsx Audit evidence · page.tsx Batch tracking
Components 23
Next.js frontend
- App shell · Global rendering, styling, and error boundary.
app/layout.tsx - Operations home
app/page.tsx - Batch tracking · Presents a batch lifecycle and provenance.
app/track/[batchId]/page.tsx - Identity context · Holds frontend identity and role state.
components/auth-context.tsx - Ledger state
components/ledger-context.tsx - Role operations · Manufacturer, pharmacy, role administration, and telemetry workflows.
components/views/manufacturer-dashboard.tsx - Provenance map
components/route-map.tsx
Ledger integration
- Ledger engine · Centralizes ledger-facing operations and simulation data.
lib/engine.ts - Contract ABI
lib/abi.ts - MedicineTracker · Immutable provenance, custody, roles, and spoilage authority.
contracts/MedicineTracker.sol - Explorer access
lib/explorer.ts
Supabase
- Supabase client
lib/supabase-client.ts - Database schema
supabase/schema.sql - Row access policies
supabase/rls-policies.sql
Event ingestion
- Alchemy webhook · HMAC-verifies external blockchain event payloads.
app/api/webhooks/alchemy/route.ts - Webhook simulator
scripts/alchemy-webhook-simulator.js - Alerts inbox
components/alerts-inbox.tsx
Inspection & reporting
- Pharmacy terminal · Surfaces package verification for pharmacy workflows.
components/views/pharmacy-terminal.tsx - Package CV pipeline
lib/cv-pipeline.ts - OpenCV worker
public/cv.worker.js - Audit evidence · Combines ledger history and telemetry evidence.
components/audit-log.tsx - Compliance PDF
lib/pdf-report.ts - Operational CSV
lib/csv-export.ts
Connections 27
- App shell → Operations home: renders
- App shell → Batch tracking: renders
- App shell → Identity context: provides
- App shell → Ledger state: provides
- Operations home → Role operations: hosts
- Identity context → Role operations: identity and role state
- Ledger state → Role operations: ledger state
- Role operations → Ledger engine: creates and advances batches
- Batch tracking → Ledger engine: reads lifecycle
- Batch tracking → Provenance map: visualizes route
- Batch tracking → Explorer access: links transactions
- Ledger engine → Contract ABI: uses
- Ledger engine → MedicineTracker: reads and writes · Wallet signatures authorize state transitions.
- Identity context → Supabase client: uses identity session
- Supabase client → Database schema: persists application state
- Row access policies → Database schema: enforces row access
- Webhook simulator → Alchemy webhook: sends test events
- Alchemy webhook → Supabase client: persists event state
- Alchemy webhook → Alerts inbox: propagates alerts
- Alerts inbox → Ledger engine: surfaces spoilage status
- Pharmacy terminal → Package CV pipeline: runs inspection
- Package CV pipeline → OpenCV worker: offloads browser CV
- Pharmacy terminal → Ledger engine: verifies batch
- Audit evidence → Ledger engine: collects ledger history
- Audit evidence → Supabase client: collects telemetry evidence
- Audit evidence → Compliance PDF: generates compliance report
- Audit evidence → Operational CSV: exports operations