# mehtaansh/blockchainvoting architecture

> Architecture diagram of the GitHub repository mehtaansh/blockchainvoting, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-08-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mehtaansh/blockchainvoting
- Repository: https://github.com/mehtaansh/blockchainvoting

## Overview

##### Project type and purpose
- This is a full-stack, blockchain-enabled E-voting system.
- Frontend: Next.js + Semantic UI React pages/components.
- Backend: Express.js + MongoDB + Mongoose models + controllers + routes.
- Blockchain: Solidity smart contracts deployed to an Ethereum network (e.g., Rinkeby) and accessed via Web3/MetaMask.
- File storage: IPFS for images.
- External services: Email (via SMTP credentials in ENV).

a. Client Browser
- Houses the Next.js React UI and the MetaMask extension.
b. Frontend Server (Next.js)
- Renders pages under /pages and uses components under /components.
- next.config.js configures it.
c. Backend API Server (Express)
- Defined in server.js, controllers/, routes/, models/.
- Exposes REST endpoints for candidate, company, voter (routes/candidate.js, etc.).
d. Database (MongoDB)
- Configured in config/database.js
- Stores user records, company data, voter data.
e. Blockchain Network (Ethereum)
- Smart contracts (Election.sol, ElectionFact.sol → compiled artifacts in Ethereum/Build).
- Deployment and interaction scripts: Ethereum/compile.js, deploy.js, election.js, election_factory.js, web3.js.
f. IPFS Network
- ipfs.js handles file uploads to IPFS.
g. Email Service
- External SMTP (EMAIL, PASSWORD from .env) used by controllers to notify candidates/voters.

##### Relationships and data flows
- Client → Next.js Frontend: HTTP(S) request for UI pages.
- Frontend → Express API: AJAX/Fetch calls to endpoints defined in routes.js.
- Frontend → Ethereum Network: Web3 calls through MetaMask for voting transactions (vote(), getResults(), etc.).
- Express API → MongoDB: Mongoose models perform CRUD on company, voter, candidate collections.
- Express API → IPFS: Upload candidate images or other assets.
- Express API → Email Service: Send registration and result notifications.
- Express API (optional) → Ethereum Network: Administrative functions (election creation/deployment via deployment scripts).

##### Architectural patterns and principles
- MVC-style backend: models/ → controllers/ → routes/ → server.js.
- Separation of concerns:
- Presentation (Next.js)
- API/Business Logic (Express)
- Data Layer (MongoDB)
- Blockchain Layer (Solidity contracts + Web3)
- Storage Layer (IPFS)
- External Integrations (SMTP email)
- Hybrid deployment: Next.js and Express run in a single Node process (server.js loads both).
- Smart contract code is decoupled in its own directory (Ethereum/).

## Diagram (Mermaid)

```mermaid
graph TD
    subgraph "Client Browser"
        Browser["Browser UI"]:::frontend
        MetaMask["MetaMask Extension"]:::frontend
    end

    subgraph "Frontend Server (Next.js)"
        NX["Next.js Server"]:::frontend
        Pages["pages"]:::frontend
        Components["components"]:::frontend
        NextConfig["next.config.js"]:::frontend
    end

    subgraph "Backend/API Server (Express.js)"
        Express["Express API Server"]:::backend
        Controllers["controllers"]:::backend
        RoutesDir["routes"]:::backend
        RoutesFile["routes.js"]:::backend
    end

    DB["MongoDB"]:::database

    subgraph "Mongoose Models"
        ModelCompany["company.js"]:::database
        ModelVoter["voter.js"]:::database
    end

    subgraph "REST API Routes"
        RouteCandidate["candidate.js"]:::backend
        RouteCompany["company.js"]:::backend
        RouteVoter["voter.js"]:::backend
    end

    subgraph "Solidity Contracts"
        ContractsDir["Ethereum/Contract"]:::blockchain
        ElectionJSON["Ethereum/Build/Election.json"]:::blockchain
        ElectionFactJSON["Ethereum/Build/ElectionFact.json"]:::blockchain
    end

    subgraph "Deployment Scripts"
        Compile["Ethereum/compile.js"]:::blockchain
        Deploy["Ethereum/deploy.js"]:::blockchain
        ElectionScript["Ethereum/election.js"]:::blockchain
        FactoryScript["Ethereum/election_factory.js"]:::blockchain
        Web3Script["Ethereum/web3.js"]:::blockchain
    end

    IPFS["ipfs.js"]:::ipfs
    Email["SMTP Email Service"]:::email
    EthereumCloud["Ethereum Network"]:::blockchain

    %% Data flows
    Browser -->|"HTTP GET"| NX
    NX -->|"Fetch/axios → /api/*"| Express
    Browser -->|"\u2014\u2014\u2014 Web3.eth.sendTransaction"| EthereumCloud
    Express -->|"CRUD (Mongoose)"| DB
    Express -->|"uses"| ModelCompany
    Express -->|"uses"| ModelVoter
    Express -->|"routes →"| RouteCandidate
    Express -->|"routes →"| RouteCompany
    Express -->|"routes →"| RouteVoter
    Express -.->|"IPFS upload"| IPFS
    Express -.->|"SMTP: nodemailer"| Email
    Compile -->|"deploy scripts"| EthereumCloud
    Deploy -->|"deployment"| EthereumCloud
    ElectionScript -->|"interact"| EthereumCloud
    FactoryScript -->|"interact"| EthereumCloud
    Web3Script -->|"Web3 config"| EthereumCloud

    %% Click events
    click Pages "https://github.com/mehtaansh/blockchainvoting/tree/master/pages/"
    click Components "https://github.com/mehtaansh/blockchainvoting/tree/master/components/"
    click NextConfig "https://github.com/mehtaansh/blockchainvoting/blob/master/next.config.js"
    click Express "https://github.com/mehtaansh/blockchainvoting/blob/master/server.js"
    click Controllers "https://github.com/mehtaansh/blockchainvoting/tree/master/controllers/"
    click RoutesDir "https://github.com/mehtaansh/blockchainvoting/tree/master/routes/"
    click RoutesFile "https://github.com/mehtaansh/blockchainvoting/blob/master/routes.js"
    click DB "https://github.com/mehtaansh/blockchainvoting/blob/master/config/database.js"
    click ModelCompany "https://github.com/mehtaansh/blockchainvoting/blob/master/models/company.js"
    click ModelVoter "https://github.com/mehtaansh/blockchainvoting/blob/master/models/voter.js"
    click RouteCandidate "https://github.com/mehtaansh/blockchainvoting/blob/master/routes/candidate.js"
    click RouteCompany "https://github.com/mehtaansh/blockchainvoting/blob/master/routes/company.js"
    click RouteVoter "https://github.com/mehtaansh/blockchainvoting/blob/master/routes/voter.js"
    click ContractsDir "https://github.com/mehtaansh/blockchainvoting/tree/master/Ethereum/Contract/"
    click ElectionJSON "https://github.com/mehtaansh/blockchainvoting/blob/master/Ethereum/Build/Election.json"
    click ElectionFactJSON "https://github.com/mehtaansh/blockchainvoting/blob/master/Ethereum/Build/ElectionFact.json"
    click Compile "https://github.com/mehtaansh/blockchainvoting/blob/master/Ethereum/compile.js"
    click Deploy "https://github.com/mehtaansh/blockchainvoting/blob/master/Ethereum/deploy.js"
    click ElectionScript "https://github.com/mehtaansh/blockchainvoting/blob/master/Ethereum/election.js"
    click FactoryScript "https://github.com/mehtaansh/blockchainvoting/blob/master/Ethereum/election_factory.js"
    click Web3Script "https://github.com/mehtaansh/blockchainvoting/blob/master/Ethereum/web3.js"
    click IPFS "https://github.com/mehtaansh/blockchainvoting/blob/master/ipfs.js"

    %% Styles
    classDef frontend fill:#1E90FF,stroke:#333,stroke-width:1px,color:#fff
    classDef backend fill:#28a745,stroke:#333,stroke-width:1px,color:#fff
    classDef database fill:#999,stroke:#333,stroke-width:1px,color:#fff
    classDef blockchain fill:#FFA500,stroke:#333,stroke-width:1px,color:#fff
    classDef ipfs fill:#800080,stroke:#333,stroke-width:1px,color:#fff
    classDef email fill:#FFD700,stroke:#333,stroke-width:1px,color:#000
```
