System Architecture
Smart Contract Architecture
Component Overview
Core Contracts
CeloReferEnhanced.sol
This is the main contract that orchestrates the entire referral system: Key Responsibilities:- User registration and referral tree management
- Dynamic reward calculation and distribution
- Quest and season management
- Partner authorization and fee collection
- Badge tier progression
- User mappings (referral codes, referrer relationships, stats)
- Quest and season data
- Partner authorization records
- Platform configuration (fees, tiers)
ReputationNFT.sol
This contract manages soulbound reputation NFTs: Key Responsibilities:- Minting unique, non-transferable NFTs for users
- Generating dynamic metadata based on user stats
- Ensuring NFTs cannot be traded or transferred
- Token ownership mappings
- Metadata templates
- User stat snapshots
SDK Layer
The TypeScript SDK provides a clean interface for interacting with the contracts: Key Modules:- User management (registration, info retrieval)
- Referral operations (action recording, reward claiming)
- Badge and reputation systems
- Quest and season participation
- NFT operations
- Partner integration tools
Data Flow
User Registration
- User calls registerUser()with optional referral code
- Contract validates referral code (if provided)
- New user entry created with unique referral code
- Referral relationships established in tree
- Event emitted for off-chain tracking
Action Recording
- Authorized partner calls recordAction()for user
- Contract validates partner authorization
- Action value processed through fee system
- Rewards calculated based on badge tier
- Rewards distributed to referrers
- User stats updated for badge progression
- Events emitted for analytics
Reward Claiming
- User calls claimRewards()
- Contract calculates available rewards
- Rewards transferred to user wallet
- Claimed amounts updated in storage
- Event emitted for tracking
Quest Participation
- User performs actions that count toward quests
- Progress tracked automatically by contract
- User calls claimQuestReward()when completed
- Reward transferred to user
- Completion recorded
Season Competition
- Platform owner creates season with parameters
- User actions during season count toward leaderboard
- Leaderboard updated in real-time
- Season ends automatically
- Rewards distributed to top performers
Security Model
Access Control
- Owner: Platform administrator with special privileges
- Partners: Authorized dApps that can record actions
- Users: Anyone can register and participate
Key Security Features
- ReentrancyGuard on all state-changing functions
- Input validation and require statements
- Time-based restrictions (season start/end)
- Rate limiting where appropriate
- Emergency pause mechanisms
Upgradeability
- Contracts designed for upgradeability patterns
- Storage layout considerations for future versions
- Proxy pattern support
Technology Stack
Blockchain Layer
- Platform: Celo (EVM-compatible)
- Consensus: Proof of Stake
- Token Standards: ERC-20 (cUSD), ERC-721 (NFT)
Smart Contract Layer
- Language: Solidity ^0.8.20
- Framework: OpenZeppelin contracts
- Development Tools: Hardhat, Foundry
- Testing: Mocha, Chai
SDK Layer
- Language: TypeScript
- Framework: viem for blockchain interactions
- Build Tools: Vite, Rollup
- Testing: Jest, TypeScript
Infrastructure
- Node Providers: Celo nodes (Alchemy, Infura)
- Indexing: The Graph (planned)
- Monitoring: Tenderly, Datadog
Scalability Considerations
Gas Optimization
- Efficient data structures to minimize storage costs
- Batch operations where possible
- Event-driven architecture for off-chain processing
Performance
- Optimized read functions for frontend display
- Caching strategies for frequently accessed data
- Pagination for large datasets
Future Improvements
- Layer 2 integration for reduced costs
- Subgraph implementation for efficient querying
- Cross-chain support for broader reach
Integration Patterns
For dApp Developers
- Install SDK and dependencies
- Initialize SDK with wallet client
- Register users automatically on first action
- Call recordAction()for valuable user actions
- Display user stats, rewards, and progress
For Platform Operators
- Deploy contracts with appropriate parameters
- Set platform fee percentages
- Authorize partner contracts
- Create quests and seasons
- Monitor system performance and analytics
For Users
- Register with referral code or as genesis user
- Share referral link with friends
- Perform actions in partner dApps
- Claim rewards periodically
- Participate in quests and seasons
- Mint reputation NFTs