Introduction
Overview
Rahat Project is a comprehensive blockchain-based Cash and Voucher Assistance (CVA) system designed to provide secure, transparent, and efficient humanitarian aid distribution. The system leverages blockchain technology to ensure accountability, traceability, and efficient management of aid disbursements to beneficiaries through a network of vendors.
System Architecture
High-Level Architecture
The Rahat Project follows a microservices architecture with the following core components:
- Backend API Service (
apps/rp
) - NestJS-based REST API - Smart Contracts (
apps/contracts
) - Ethereum-based smart contracts - Blockchain Indexing (
apps/graph
) - The Graph subgraph for event indexing - Database Layer - PostgreSQL with Prisma ORM
- Message Queue - Redis-based job processing
Technology Stack
- Backend: NestJS (Node.js framework)
- Blockchain: Ethereum/Solidity with Hardhat
- Database: PostgreSQL with Prisma ORM
- Indexing: The Graph Protocol
- Message Queue: Redis with Bull
- Monorepo: Nx workspace
- Package Manager: pnpm
Core Components
1. Smart Contract Architecture
The blockchain layer consists of several interconnected smart contracts:
RahatPayrollProject.sol
- Purpose: Main project contract managing beneficiary allocations and disbursements
- Key Features:
- Beneficiary management (add/remove beneficiaries)
- Token allocation tracking
- Batch disbursement processing
- Claim processing through approved vendors
- Integration with disbursement triggers
RahatToken.sol
- Purpose: ERC20 token for aid distribution
- Features:
- Standard ERC20 functionality
- Minting capabilities for treasury
- Permit functionality for gasless approvals
- Access management integration
RahatClaim.sol
- Purpose: Handles claim creation and processing
- Features:
- OTP-based claim verification
- Multi-signature support
- Offline transaction support
- Claim lifecycle management
RahatTreasury.sol
- Purpose: Central treasury management
- Features:
- Token creation and minting
- Balance management
- Approval workflows
- Access control
Vendor.sol
- Purpose: Vendor management and redemption processing
- Features:
- Vendor registration and verification
- Token redemption workflows
- OTP verification for transactions
- Offline claim processing
RahatAccessManager.sol
- Purpose: Access control and permissions
- Features:
- Role-based access control
- Authority management
- Permission validation
2. Backend API Service
The NestJS application provides REST APIs for:
Beneficiary Management
- Registration and verification of beneficiaries
- Wallet address management
- Beneficiary type classification (ENROLLED, REFERRED)
- Offline beneficiary support
Vendor Management
- Vendor registration and verification
- Redemption processing
- Transaction history tracking
- Blockchain integration for vendor operations
Disbursement Plans
- Creation and management of payment plans
- Batch processing capabilities
- Trigger-based disbursements
- Plan status tracking
Blockchain Integration
- Smart contract interaction
- Transaction processing
- Event monitoring
- Gas optimization
Communication Services
- Notification management
- Message queuing
- Integration with external communication providers
3. The Graph Subgraph
The subgraph indexes blockchain events and provides GraphQL APIs for:
Event Indexing
- Token transfers and approvals
- Beneficiary management events
- Claim processing events
- Vendor operations
- Treasury activities
Data Entities
- BeneficiaryAdded/BeneficiaryRemoved
- TokensAllocated/TokensDisbursed
- ClaimProcessed/ClaimCreated
- Transfer/Approval events
- OfflineClaimProcessed/OtpVerified
Data Flow
1. Beneficiary Registration Flow
- Beneficiary data is registered in the backend API
- Smart contract interaction adds beneficiary to blockchain
- The Graph indexes the beneficiary addition event
- Frontend applications can query beneficiary status
2. Token Allocation Flow
- Treasury mints tokens for the project
- Tokens are allocated to beneficiaries via smart contract
- Allocation events are indexed by The Graph
- Backend API tracks allocations in database
3. Disbursement Flow
- Disbursement plan is created in backend
- Smart contract processes batch disbursement
- Tokens are transferred to beneficiary allocations
- Events are indexed for transparency
4. Redemption Flow
- Vendor requests tokens from beneficiary
- OTP verification is performed
- Smart contract processes the claim
- Tokens are transferred to vendor
- Transaction is recorded and indexed
Security Features
Smart Contract Security
- Access control through AccessManager
- Multi-signature support for critical operations
- OTP verification for sensitive transactions
- Offline transaction support with proper validation
API Security
- Role-based access control
- Input validation and sanitization
- Rate limiting and throttling
- Secure communication protocols
Blockchain Security
- Meta-transaction support (ERC2771)
- Gas optimization techniques
- Event-driven architecture for transparency
- Immutable audit trails
Offline Support
The system includes comprehensive offline support:
Offline Beneficiary Management
- Beneficiaries can be marked as offline
- Offline transactions are queued for later processing
- Synchronization mechanisms ensure data consistency
Offline Claim Processing
- Vendors can process claims offline
- OTP verification works in offline mode
- Transaction batching for efficiency
Scalability Considerations
Horizontal Scaling
- Stateless API design
- Redis-based session management
- Database connection pooling
- Load balancing support
Blockchain Scaling
- Batch processing for gas efficiency
- Event-driven architecture
- Indexed data for fast queries
- Meta-transactions for user experience
Development Workflow
Local Development
- Bootstrap:
pnpm bootstrap
sets up the entire environment - Database: PostgreSQL with Prisma migrations
- Blockchain: Hardhat local network
- Indexing: Local Graph node
- API: NestJS development server
Testing Strategy
- Unit tests for smart contracts
- Integration tests for API endpoints
- End-to-end tests for complete workflows
- Contract coverage reporting
Deployment
- Docker containerization
- Environment-specific configurations
- Database migration management
- Contract deployment automation