# jeevandhar-mehar/crypto-fund architecture

> Architecture diagram of the GitHub repository jeevandhar-mehar/crypto-fund, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jeevandhar-mehar/crypto-fund
- Repository: https://github.com/jeevandhar-mehar/crypto-fund

## Overview

```markdown
### Decentralized Fund Raiser

#### Overview
The Decentralized Fund Raiser project is a blockchain-backed platform that lets users create, back and manage fundraising campaigns in a fully decentralized, transparent, and immutable way. It combines smart contracts for secure funds management, a relational database for indexing campaign metadata, a Python-based API for business logic, and a Streamlit-powered frontend for an intuitive user experience.

#### Table of Contents
1. [Features](#features)  
2. [Workflow](#workflow)  
   2.1 [Smart Contract Development](#21-smart-contract-development)  
   2.2 [Smart Contract Deployment](#22-smart-contract-deployment)  
   2.3 [Database Initialization](#23-database-initialization)  
   2.4 [API Server Startup](#24-api-server-startup)  
   2.5 [Frontend (Streamlit) Launch](#25-frontend-streamlit-launch)  
3. [Setup Guide](#setup-guide)  
   3.1 [Prerequisites](#31-prerequisites)  
   3.2 [Installation](#32-installation)  
   3.3 [Environment Setup](#33-environment-setup)  
   3.4 [Running the Project](#34-running-the-project)  
4. [Significance](#significance)  

#### Features
- Decentralized fundraising campaigns on Ethereum-compatible blockchains  
- Transparent, immutable transactions powered by smart contracts  
- Python/SQLAlchemy-based API for campaign CRUD operations  
- Streamlit multipage frontend for dashboard, explore, campaign creation & details  
- Migration scripts for versioned database schema management  

#### Workflow

##### 2.1 Smart Contract Development
• contracts/ – directory containing Solidity contracts  
• Write campaigns.sol with methods to create campaigns, contribute, withdraw, etc.

##### 2.2 Smart Contract Deployment
• Use Hardhat or Truffle to compile & deploy to local or public testnet  
• Scripts live in scripts/ (if you add custom deployment scripts)  
• Deployed addresses are recorded in your `.env` or config for the API/frontend  

##### 2.3 Database Initialization
• `init_db.py` & `db_init.py` create and seed your relational DB  
• Migrations managed via Alembic (`alembic.ini`, migrations/… )  
• Models defined in `models.py` reflect campaign metadata & user contributions  

##### 2.4 API Server Startup
• `api/app.py` defines Flask/FastAPI endpoints for:  
  - Campaign listing & details  
  - Creating a campaign record (after on-chain deployment)  
  - Querying contribution history  
• Utility functions in `api/utils.py` handle DB sessions & web3 interactions  
• Launch via `run_api.py`  

##### 2.5 Frontend (Streamlit) Launch
• `app.py` + `components.py` orchestrate Streamlit pages under `pages/`  
  - `dashboard.py`, `explore.py`, `create_campaign.py`, `campaign_details.py`  
• Interacts with the API to read/write campaign data and with Web3 for on-chain calls  
• Config in `.streamlit/config.toml`  

#### Setup Guide

##### 3.1 Prerequisites
- Python 3.8+  
- Node.js (if using Hardhat/Truffle)  
- Yarn or npm  
- Ganache or another local Ethereum node (for dev)  
- MetaMask (for frontend blockchain interaction)  

##### 3.2 Installation
1. Clone the repo  
   ```bash
   git clone https://github.com/your-repo/DecentralizedFundRaiser.git
   cd DecentralizedFundRaiser
   ```
2. Install Python dependencies  
   ```bash
   pip install -r requirements.txt
   ```
3. Install Node.js dependencies (for contracts)  
   ```bash
   cd contracts && npm install && cd ..
   ```

##### 3.3 Environment Setup
Create a `.env` in project root:
```ini
INFURA_API_KEY=<your-infura-api-key>
PRIVATE_KEY=<your-wallet-private-key>
NETWORK=<network-name>           # e.g., rinkeby, localhost
DATABASE_URL=postgresql://user:pw@localhost:5432/fundraiser
API_HOST=127.0.0.1
API_PORT=8000
```

##### 3.4 Running the Project
1. **Compile & Deploy Contracts**  
   ```bash
   cd contracts
   npx hardhat compile
   npx hardhat run scripts/deploy.js --network $NETWORK
   cd ..
   ```
2. **Initialize Database & Run Migrations**  
   ```bash
   python init_db.py
   alembic upgrade head
   ```
3. **Start API Server**  
   ```bash
   python run_api.py
   ```
4. **Launch Frontend**  
   ```bash
   streamlit run app.py
   ```
5. Open your browser at `http://localhost:8501`

#### Significance
By combining smart contracts, a robust API layer, and a user-friendly frontend, the Decentralized Fund Raiser ensures that all transactions are immutably recorded on-chain while providing performant querying and management off-chain. This design fosters trust, transparency, and accessibility for both campaign creators and contributors.
```

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Smart Contract Layer
    subgraph "Smart Contract Layer"
        SCDev["contracts/"]:::service
        SCDev -->|Deploy contracts| Blockchain
    end

    %% Database Layer
    subgraph "Database Layer"
        DBInit["init_db.py & db_init.py"]:::service
        Migrations["alembic.ini & migrations/"]:::service
        Models["models.py"]:::service
        DBInit -->|Initialize DB| Postgres
        Migrations -->|Migrate Schema| Postgres
        Models -->|Define Schema| Postgres
    end

    %% API Layer
    subgraph "API Layer"
        APIApp["api/app.py"]:::api
        Utils["api/utils.py"]:::api
        RunAPI["run_api.py"]:::service
        APIApp -->|Uses utils for DB/Web3| Utils
        APIApp -->|Reads/Writes| Postgres
        APIApp -->|Interacts| Blockchain
        RunAPI -->|Starts| APIApp
    end

    %% Frontend Layer
    subgraph "Frontend (Streamlit)"
        AppMain["app.py"]:::frontend
        Components["components.py"]:::frontend
        subgraph "Pages"
            Dash["pages/dashboard.py"]:::frontend
            Explore["pages/explore.py"]:::frontend
            Create["pages/create_campaign.py"]:::frontend
            Details["pages/campaign_details.py"]:::frontend
        end
        Config[".streamlit/config.toml"]:::config
        AppMain -->|Imports| Components
        AppMain -->|Loads pages| Dash
        AppMain -->|Loads pages| Explore
        AppMain -->|Loads pages| Create
        AppMain -->|Loads pages| Details
        AppMain -->|Reads config| Config
        AppMain -->|Calls| APIApp
        AppMain -->|On-chain calls| Blockchain
    end

    %% External Services
    Blockchain[(Ethereum / Ganache)]:::blockchain
    Postgres[(PostgreSQL)]:::db

    %% Click Events
    click SCDev "https://github.com/jeevandhar-mehar/crypto-fund/tree/main/contracts/"
    click DBInit "https://github.com/jeevandhar-mehar/crypto-fund/blob/main/init_db.py"
    click DBInit "https://github.com/jeevandhar-mehar/crypto-fund/blob/main/db_init.py"
    click Migrations "https://github.com/jeevandhar-mehar/crypto-fund/blob/main/alembic.ini"
    click Migrations "https://github.com/jeevandhar-mehar/crypto-fund/tree/main/migrations/"
    click Models "https://github.com/jeevandhar-mehar/crypto-fund/blob/main/models.py"
    click APIApp "https://github.com/jeevandhar-mehar/crypto-fund/blob/main/api/app.py"
    click Utils "https://github.com/jeevandhar-mehar/crypto-fund/blob/main/api/utils.py"
    click RunAPI "https://github.com/jeevandhar-mehar/crypto-fund/blob/main/run_api.py"
    click AppMain "https://github.com/jeevandhar-mehar/crypto-fund/blob/main/app.py"
    click Components "https://github.com/jeevandhar-mehar/crypto-fund/blob/main/components.py"
    click Dash "https://github.com/jeevandhar-mehar/crypto-fund/blob/main/pages/dashboard.py"
    click Explore "https://github.com/jeevandhar-mehar/crypto-fund/blob/main/pages/explore.py"
    click Create "https://github.com/jeevandhar-mehar/crypto-fund/blob/main/pages/create_campaign.py"
    click Details "https://github.com/jeevandhar-mehar/crypto-fund/blob/main/pages/campaign_details.py"
    click Config "https://github.com/jeevandhar-mehar/crypto-fund/blob/main/.streamlit/config.toml"

    %% Styles
    classDef service fill:#D6EAF8,stroke:#1B4F72,stroke-width:1px
    classDef api fill:#D5F5E3,stroke:#145A32,stroke-width:1px
    classDef frontend fill:#FCF3CF,stroke:#7D6608,stroke-width:1px
    classDef db fill:#FADBD8,stroke:#922B21,stroke-width:1px
    classDef blockchain fill:#E5E8E8,stroke:#566573,stroke-width:1px
    classDef config fill:#F5EEF8,stroke:#6C3483,stroke-width:1px
```
