AI + Blockchain Medical Inventory Management System for Tamil Nadu Healthcare Supply Chain
- Problem Statement
- Solution
- System Architecture
- Tech Stack
- Repository Structure
- Getting Started
- Dataset Plan
- Smart Contracts
- ML Service
- API Reference
- Mobile App
- Demo Flow
Tamil Nadu's healthcare supply chain serving 77 million people suffers from five critical failures:
| Problem | Impact |
|---|---|
| No demand forecasting | Drug shortages during malaria & dengue seasons |
| Mutable inventory records | Stock manipulation goes undetected |
| No drug verification | Counterfeit medicines reach patients |
| Poor expiry management | ~18% of batches wasted |
| No decision support | Reactive, manual procurement decisions |
MedChain TN combines blockchain immutability with AI intelligence to fix every layer of the supply chain.
Blockchain → tamper-proof batch records + ownership transfers
AI → demand forecasting + anomaly detection + expiry prediction
Mobile App → unified interface for manufacturers, distributors, patients
┌─────────────────────────────────────────────┐
│ React Native Expo App │
│ ┌──────────┬──────────────┬─────────────┐ │
│ │ Patient │ Distributor │Manufacturer │ │
│ │ 4 Tabs │ 4 Tabs │ 4 Tabs │ │
│ └────┬─────┴──────┬───────┴──────┬──────┘ │
└───────┼────────────┼──────────────┼──────────┘
└────────────┼──────────────┘
│ HTTPS / REST + JWT
┌────────────▼─────────────┐
│ Node.js API Gateway │
│ JWT + RBAC Middleware │
└──┬───────────┬───────────┘
│ │
┌──────▼───┐ ┌────▼─────────┐
│PostgreSQL│ │ Flask ML API │
│ + Redis │ │ 3 Modules │
└──────────┘ └──────────────┘
│
┌──────▼──────────────────┐
│ ethers.js │
│ ↕ │
│ Ganache Local Chain │
│ 4 Solidity Contracts │
└─────────────────────────┘
| Layer | Technology |
|---|---|
| Mobile App | React Native + Expo SDK 51 |
| Backend | Node.js + Express |
| ML Service | Python + Flask |
| Database | PostgreSQL + Redis |
| Blockchain | Solidity + Hardhat + Ganache |
| ML Models | scikit-learn, XGBoost, Prophet, Keras |
| Maps | react-native-maps |
| Charts | victory-native |
| Auth | JWT + expo-secure-store |
/medchain-tn
/mobile ← Expo React Native app
/app
/_layout.tsx
/auth/
/(patient)/
/(distributor)/
/(manufacturer)/
/components
/shared/
/patient/
/distributor/
/manufacturer/
/hooks/
/services/
/backend ← Node.js API Gateway
/src
/routes/
/controllers/
/middleware/
/blockchain/
/ml/
/db/
app.js
/ml-service ← Python Flask ML API
/data
/raw/
/processed/
/synthetic/
/models/
/notebooks/
/src
/features/
/training/
/evaluation/
/api/
app.py
/contracts ← Solidity Smart Contracts
/contracts/
MedAccessControl.sol
MedBatch.sol
MedTransfer.sol
MedAudit.sol
/scripts/
/test/
hardhat.config.js
/data ← Dataset generation
/synthetic/
constants.py
generate_usage.py
generate_movements.py
generate_expiry.py
generate_regional.py
run_all.py
/docs/
docker-compose.yml
README.md
node >= 18
python >= 3.10
docker + docker-compose
ganache (desktop or CLI)
expo-cligit clone https://github.com/your-org/medchain-tn.git
cd medchain-tncp .env.example .envFill in your .env:
# Ganache
GANACHE_RPC_URL=http://127.0.0.1:7545
GANACHE_CHAIN_ID=1337
GANACHE_MNEMONIC="your twelve word mnemonic from ganache ui"
GANACHE_PRIVATE_KEY_ADMIN=0x...
GANACHE_PRIVATE_KEY_MANUFACTURER=0x...
GANACHE_PRIVATE_KEY_DISTRIBUTOR=0x...
# PostgreSQL
POSTGRES_URL=postgresql://medchain:password@localhost:5432/medchain_tn
# Redis
REDIS_URL=redis://localhost:6379
# JWT
JWT_SECRET=your_jwt_secret_here
JWT_EXPIRES_IN=24h
# ML Service
ML_SERVICE_URL=http://localhost:5000docker-compose up -dThis starts: Ganache · PostgreSQL · Redis · Node Backend · Flask ML Service
cd contracts
npm install
npx hardhat run scripts/deploy.js --network ganache
npx hardhat run scripts/seed.js --network ganachecd data/synthetic
pip install -r requirements.txt
python run_all.py
cd ../../ml-service
pip install -r requirements.txt
python src/training/train_all.pycd mobile
npm install
npx expo startScan the QR code with Expo Go on your device.
Three datasets covering Tamil Nadu's pharmaceutical supply chain across 10 districts, 12 drugs, 3 facility types, 24 months.
| Dataset | Rows | Purpose |
|---|---|---|
| Drug Usage History | ~3,500 | Demand Forecasting |
| Stock Movement Log | ~5,000 | Anomaly Detection |
| Batch Expiry & Wastage | ~2,000 | Expiry Prediction |
Chennai · Coimbatore · Madurai · Trichy · Salem
Tirunelveli · Vellore · Erode · Thoothukudi · Kanchipuram
Paracetamol · Amoxicillin · Metformin · Ibuprofen · Omeprazole
Amlodipine · Artemether · Ciprofloxacin · Insulin · Salbutamol
Ondansetron · Chloroquine
Dengue / Vector-borne Oct–Jan → Paracetamol, Ciprofloxacin
Malaria Jun–Sep → Artemether, Chloroquine
Respiratory Dec–Feb → Salbutamol, Amoxicillin
Gastroenteritis May–Jun → Ondansetron, Omeprazole
Diabetes Oct–Nov → Metformin, Insulin
| Contract | Responsibility |
|---|---|
MedAccessControl.sol |
Role management (Admin, Manufacturer, Distributor, Patient) |
MedBatch.sol |
Drug batch registration + QR hash storage |
MedTransfer.sol |
Ownership transfer between supply chain participants |
MedAudit.sol |
Expiry tracking + immutable audit trail |
Development: Ganache local (port 7545, chainId 1337)
Testing: Polygon Amoy Testnet
Production: Polygon PoS Mainnet
// MedBatch.sol
registerBatch(drugName, region, quantity, expiryDate, qrHash)
getBatch(batchId)
verifyBatch(batchId) → isValid, status
// MedTransfer.sol
initiateTransfer(batchId, to, quantity, toRegion)
acceptTransfer(transferId)
getTransferHistory(batchId)
// MedAudit.sol
getAuditTrail(batchId)
getExpiringBatches(withinDays)cd contracts
npx hardhat test --network ganacheModule 1 — Demand Forecasting
Models: Random Forest · XGBoost · Facebook Prophet
Endpoint: POST /predict/demand
Output: predicted_qty_30d, predicted_qty_60d, predicted_qty_90d,
confidence_interval, reorder_recommendation
Module 2 — Anomaly Detection
Models: Isolation Forest · Autoencoder (Keras) · Z-Score
Endpoint: POST /anomaly/detect
Output: is_anomaly, anomaly_type, confidence_score,
flagged_features, recommended_action
Module 3 — Expiry Waste Prediction
Models: Gradient Boosting · Logistic Regression · Survival Analysis
Endpoint: POST /predict/expiry
Output: expiry_risk, probability_of_waste,
redistribution_recommendation
POST /auth/login
POST /auth/logout
GET /auth/me
GET /drugs
GET /drugs/:id
POST /drugs
POST /batch/register
GET /batch/:batchId
GET /batch/verify/:qrHash
POST /transfer/initiate
POST /transfer/accept
POST /transfer/reject
GET /audit/:batchId
GET /predict/demand
POST /anomaly/detect
POST /predict/expiry
GET /alerts
PATCH /alerts/:id/resolve
Home ← drug search + availability + stock status
Scan QR ← verify drug authenticity via blockchain
Prescriptions ← track active + past prescriptions
Notifications ← low stock alerts for prescribed medicines
Inventory ← stock levels, batch breakdown, incoming shipments
Transfers ← initiate + accept + track batch transfers
Forecast ← AI demand predictions 30/60/90 days
Chain History ← full blockchain transaction log
Add Batch ← register drug batch on blockchain + generate QR
Supply Chain ← map view of all batches + regional distribution
Distributors ← activity, stock levels, demand vs supply gaps
Alerts ← anomaly feed + expiry warnings + resolve actions
End-to-end demo in 5 steps:
Step 1 — Manufacturer registers a Artemether batch
→ batch written to Ganache chain
→ QR code generated
Step 2 — Manufacturer transfers batch to Madurai distributor
→ transfer pending on-chain
Step 3 — Distributor accepts transfer
→ ownership updated on-chain
→ inventory dashboard reflects new stock
Step 4 — Patient scans QR code
→ app queries blockchain
→ authenticity confirmed: manufacturer, expiry, chain of custody shown
Step 5 — Anomaly alert fires
→ ML service flags unusual stock drop on Insulin batch
→ manufacturer sees alert with confidence score 0.91
→ alert escalated for investigation
✓ JWT required on all non-public endpoints
✓ RBAC middleware checks role before every write operation
✓ Private keys never exposed to mobile app
✓ All blockchain writes go through Node backend server wallet
✓ Patient role is read-only — zero write access to chain
✓ Anomaly alerts auto-escalate at confidence score > 0.85
✓ Ganache --deterministic flag keeps wallet addresses stable across restarts
Built for Tamil Nadu. Designed for every healthcare supply chain.