Skip to content

Latest commit

ย 

History

History
287 lines (221 loc) ยท 8.41 KB

File metadata and controls

287 lines (221 loc) ยท 8.41 KB

Architecture Overview

This section provides a comprehensive overview of CycleStark's system architecture, including the smart contracts, frontend application, and blockchain infrastructure.

๐Ÿ—๏ธ System Architecture

CycleStark is built as a decentralized application (dApp) with three main components:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Frontend      โ”‚    โ”‚   Smart         โ”‚    โ”‚   Blockchain    โ”‚
โ”‚   Application   โ”‚โ—„โ”€โ”€โ–บโ”‚   Contracts     โ”‚โ—„โ”€โ”€โ–บโ”‚   Infrastructure โ”‚
โ”‚   (React)       โ”‚    โ”‚   (Cairo)       โ”‚    โ”‚   (Madara)      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐ŸŽฏ Core Components

1. Smart Contracts (Cairo)

  • Location: /contract/src/
  • Language: Cairo 2.0
  • Network: Starknet
  • Purpose: Core business logic and state management

2. Frontend Application (React)

  • Location: /frontend/src/
  • Framework: React 18 + TypeScript
  • UI Library: Mantine 7
  • Purpose: User interface and interaction layer

3. Blockchain Infrastructure (Madara)

  • Location: /madara-app-chain-template/
  • Framework: Madara (Starknet-compatible)
  • Language: Rust
  • Purpose: Local development and testing environment

๐Ÿ”„ Data Flow

User Action โ†’ Frontend โ†’ Wallet โ†’ Smart Contract โ†’ Blockchain
     โ†‘                                                      โ†“
     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ State Update โ† Events โ†โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Detailed Flow:

  1. User Interaction: User performs action in frontend
  2. Transaction Creation: Frontend creates transaction data
  3. Wallet Signing: User approves transaction in wallet
  4. Contract Execution: Smart contract processes the action
  5. State Update: Blockchain state is updated
  6. Event Emission: Contract emits events
  7. UI Update: Frontend updates based on new state

๐Ÿ›๏ธ Smart Contract Architecture

Core Contracts

CycleStark.sol (Main Contract)
โ”œโ”€โ”€ Storage
โ”‚   โ”œโ”€โ”€ stark_heroes: Map<HeroID, StarkHero>
โ”‚   โ”œโ”€โ”€ stark_collectives: Map<CollectiveID, StarkCollective>
โ”‚   โ”œโ”€โ”€ collective_cycles: Map<(CollectiveID, u32), CollectiveCycle>
โ”‚   โ””โ”€โ”€ cycle_contributions: Map<(CollectiveID, CycleID, u32), CycleContribution>
โ”œโ”€โ”€ Interfaces
โ”‚   โ”œโ”€โ”€ ICycleStark: Main contract interface
โ”‚   โ””โ”€โ”€ IHelperFunctions: Helper functions interface
โ””โ”€โ”€ Events
    โ”œโ”€โ”€ Hero: Hero registration events
    โ”œโ”€โ”€ Collective: Collective creation events
    โ”œโ”€โ”€ Cycle: Cycle management events
    โ””โ”€โ”€ Contribute: Contribution events

Data Structures

StarkHero

struct StarkHero {
    id: ContractAddress,
    escrow: u256,
    is_active: bool,
    collectives_count: u32,
}

StarkCollective

struct StarkCollective {
    id: CollectiveID,
    name: felt252,
    rule_1: felt252,
    rule_2: felt252,
    rule_3: felt252,
    hero_count: u32,
    cycle_amount: u256,
    fine: u256,
    cycles_count: u32,
    token: TokenAddress,
    start_date: u64,
    owner: HeroID,
    active_cycle: u32,
    has_started: bool,
    has_ended: bool,
    current_hero: HeroID,
    next_hero: HeroID,
    aim: felt252,
    decimals: u32,
    symbol: felt252,
}

๐ŸŽจ Frontend Architecture

Component Structure

App.tsx
โ”œโ”€โ”€ MainLayout
โ”‚   โ”œโ”€โ”€ TopBarNavigation
โ”‚   โ”‚   โ”œโ”€โ”€ ConnectWalletBtn
โ”‚   โ”‚   โ””โ”€โ”€ CustomNavLink
โ”‚   โ””โ”€โ”€ CustomAppShell
โ”œโ”€โ”€ Pages
โ”‚   โ”œโ”€โ”€ Home
โ”‚   โ”œโ”€โ”€ Collectives
โ”‚   โ”œโ”€โ”€ CreateCollective
โ”‚   โ”œโ”€โ”€ SingleCollective
โ”‚   โ”‚   โ”œโ”€โ”€ Heroes
โ”‚   โ”‚   โ”œโ”€โ”€ Cycles
โ”‚   โ”‚   โ”œโ”€โ”€ CollectiveInfo
โ”‚   โ”‚   โ””โ”€โ”€ Chat
โ”‚   โ””โ”€โ”€ MyCollectives
โ””โ”€โ”€ Providers
    โ”œโ”€โ”€ AppProvider
    โ””โ”€โ”€ CollectiveProvider

State Management

  • React Context: For global state management
  • Local State: Component-specific state
  • Contract State: Real-time blockchain state
  • Wallet State: Connection and account information

Key Libraries

  • StarknetKit: Wallet integration
  • Mantine: UI components and theming
  • React Router: Navigation
  • Firebase: Chat functionality

โ›“๏ธ Blockchain Infrastructure

Madara App Chain

Madara provides a Starknet-compatible development environment with:

  • Full RPC Compatibility: Works with all Starknet tooling
  • Customizable Parameters: Adjustable transaction fees, step limits
  • Local Development: Fast iteration and testing
  • Genesis Configuration: Pre-configured accounts and contracts

Network Configuration

# Development Network
url = "http://localhost:5050/rpc"

# Testnet Configuration
url = "https://starknet-goerli.infura.io/v3/YOUR_KEY"

# Mainnet Configuration
url = "https://starknet-mainnet.infura.io/v3/YOUR_KEY"

๐Ÿ” Security Architecture

Smart Contract Security

  1. Access Control: Owner-only functions for critical operations
  2. Input Validation: Comprehensive parameter checking
  3. Reentrancy Protection: Safe external calls
  4. State Consistency: Atomic operations
  5. Event Logging: Complete audit trail

Frontend Security

  1. Wallet Integration: Secure wallet connection
  2. Transaction Validation: Client-side validation
  3. Error Handling: Graceful error management
  4. Input Sanitization: XSS prevention

Network Security

  1. HTTPS: Secure communication
  2. RPC Security: Authenticated API endpoints
  3. Rate Limiting: DDoS protection
  4. Monitoring: Real-time security monitoring

๐Ÿ“Š Performance Considerations

Smart Contract Optimization

  • Gas Efficiency: Optimized Cairo code
  • Storage Optimization: Efficient data structures
  • Batch Operations: Multiple operations in single transaction
  • Event Optimization: Minimal event data

Frontend Performance

  • Lazy Loading: Component and route lazy loading
  • Caching: API response caching
  • Optimization: Bundle size optimization
  • CDN: Static asset delivery

Network Performance

  • Layer 2: Starknet's L2 scaling
  • Batching: Transaction batching
  • Caching: RPC response caching
  • Load Balancing: Multiple RPC endpoints

๐Ÿ”ง Development Workflow

Local Development

  1. Start Madara: Local blockchain
  2. Deploy Contracts: Deploy to local network
  3. Start Frontend: Development server
  4. Connect Wallet: Connect to local network
  5. Test: Full integration testing

Testing Strategy

  • Unit Tests: Individual component testing
  • Integration Tests: Contract interaction testing
  • E2E Tests: Full user flow testing
  • Security Tests: Vulnerability assessment

Deployment Pipeline

  1. Contract Deployment: Deploy to testnet/mainnet
  2. Frontend Build: Production build
  3. Environment Configuration: Set environment variables
  4. Deployment: Deploy to hosting platform
  5. Verification: Post-deployment verification

๐Ÿ“ˆ Scalability Considerations

Horizontal Scaling

  • Multiple RPC Endpoints: Load balancing
  • CDN: Global content delivery
  • Database Sharding: Data distribution
  • Microservices: Service decomposition

Vertical Scaling

  • Contract Optimization: Gas efficiency
  • Frontend Optimization: Performance improvements
  • Infrastructure Upgrades: Hardware improvements

๐Ÿ”ฎ Future Architecture

Planned Improvements

  1. Layer 3 Solutions: Additional scaling
  2. Cross-Chain Integration: Multi-chain support
  3. Advanced Analytics: Real-time analytics
  4. Mobile App: Native mobile application
  5. API Gateway: Public API access

Technology Roadmap

  • Cairo 2.0: Latest language features
  • Starknet 2.0: Protocol upgrades
  • Advanced UI: Enhanced user experience
  • AI Integration: Smart recommendations

๐Ÿ“š Related Documentation