# hemendrasharma04/supplychaindapp architecture

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

- Interactive diagram: https://gitdiagram.com/hemendrasharma04/supplychaindapp
- Repository: https://github.com/hemendrasharma04/supplychaindapp

## Overview

##### Project Overview
- This project is a Smart Contract based Supply Chain Management system focused on ensuring data integrity and preventing tampering.
- It integrates blockchain (Ethereum/Polygon Mumbai testnet and Solidity contracts developed with Hardhat), IoT (Arduino with DHT11 sensor and Node Red flows), a FastAPI-powered backend, and a React/Ethers.js frontend.
- The system is designed to interact between on-chain smart contracts and off-chain IoT and backend services.

##### Main Components
- Frontend (Client)
- Built with React, located in the "client" directory.
- Involves interaction with the blockchain via Ethers.js.
- Backend
- Developed with FastAPI (see files like main.py, contract.py, and router modules).
- Manages API endpoints to interact with blockchain/smart contracts and possibly process data coming from the IoT setup.
- Smart Contracts / Blockchain Layer
- Solidity contracts and Hardhat configuration (located in "contracts", "hardhat.config.js").
- Deployed on Polygon Mumbai testnet.
- IoT
- Arduino code ("arduino_code.c") for sensor reading.
- Node Red flow (Node_Red.json) for reading from the COM port and forwarding data via an API call.
- Other Supporting Components
- Docker setup (Dockerfile files in backend and client, docker-compose.yml) to simplify deployment.
- Testing and auxiliary scripts for development ("test" directory, "scripts" directory).

##### Architectural Relationships and Interactions
- The IoT component collects sensor data using Arduino and sends this data through the Node Red setup. Node Red, using its JavaScript logic, calls an API hosted in the backend.
- The Backend (FastAPI) acts as an intermediary that receives IoT data and communicates with the blockchain. It uses the contract details in contract.py to execute functions on the deployed smart contract.
- The Frontend (React app) interacts directly with the blockchain via Ethers.js, as well as making API calls to the backend to fetch or display data (products, status, workers, etc.).
- The Smart Contracts on Ethereum/Polygon ensure tamper-proof recording of supply chain events.
- Docker simplifies the deployment by containerizing each service (client, backend, and possibly IoT-related services).

##### Architectural Patterns & Design Principles
- Separation of Concerns: Each component (frontend, backend, IoT, smart contract) is designed to handle its own responsibilities.
- Modular Architecture: The project is divided into discrete modules/directories—evidenced by subdirectories like router inside backend and components inside client—making maintenance and scalability easier.
- Use of Containerization: Dockerfiles in both the client and backend, along with a docker-compose file, emphasize a microservice or containerized approach.
- Event-driven communication: The IoT part leverages Node Red’s event-driven programming for data handling and subsequent API calls.

- Frontend (React/Ethers.js)
- Backend (FastAPI API endpoints, smart contract interaction logic)
- Blockchain/Smart Contracts (Solidity contracts with Hardhat)
- IoT Components (Arduino sensor code and Node Red flow)
- Annotations for configuration/deployment tools (Docker, docker-compose) and note any intermediary services (like Alchemy) that enable blockchain interactions.

- For Frontend, include sub-components like API consumers, display components (e.g., DisplayData, DisplayProducts).
- For Blockchain, show the relationship between deployed contracts, Hardhat configuration, and testnet (Polygon Mumbai).
- For IoT, display the Arduino sensor setup, the role of Node Red (import flow JSON), and its API call to the backend.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend"
        FE["React/Ethers.js Frontend"]:::frontend
    end

    %% Backend Layer
    subgraph "Backend"
        BE["FastAPI Backend"]:::backend
        BE_MAIN["main.py"]:::backend
        BE_CONTRACT["contract.py"]:::backend
        BE_ROUTER["Router Module"]:::backend
        BE --> BE_MAIN
        BE --> BE_CONTRACT
        BE --> BE_ROUTER
    end

    %% Blockchain / Smart Contracts
    subgraph "Blockchain/Smart Contracts"
        SC["Solidity Contracts"]:::blockchain
        HC["Hardhat Config"]:::blockchain
        SC --> HC
    end

    %% IoT Layer
    subgraph "IoT"
        ARDUINO["Arduino Sensor Code"]:::iot
        NODE_RED["Node Red Flow"]:::iot
        ARDUINO --> NODE_RED
    end

    %% Deployment & Auxiliary Components
    subgraph "Deployment & Auxiliary"
        DF_CLIENT["Dockerfile (Client)"]:::deployment
        DF_BACKEND["Dockerfile (Backend)"]:::deployment
        DC["docker-compose.yml"]:::deployment
        TESTS["Test Scripts"]:::deployment
        SCRIPTS["Auxiliary Scripts"]:::deployment
    end

    %% Data Flow Relationships
    FE -->|"APIcalls"| BE
    FE -->|"Blockchaininteractions"| SC
    NODE_RED -->|"DataAPI"| BE
    BE -->|"SmartContractcalls"| SC

    %% Click Events
    click FE "https://github.com/hemendrasharma04/supplychaindapp/tree/hemendra/client"
    click BE_MAIN "https://github.com/hemendrasharma04/supplychaindapp/blob/hemendra/backend/main.py"
    click BE_CONTRACT "https://github.com/hemendrasharma04/supplychaindapp/blob/hemendra/backend/contract.py"
    click BE_ROUTER "https://github.com/hemendrasharma04/supplychaindapp/tree/hemendra/backend/router/"
    click SC "https://github.com/hemendrasharma04/supplychaindapp/tree/hemendra/contracts"
    click HC "https://github.com/hemendrasharma04/supplychaindapp/blob/hemendra/hardhat.config.js"
    click ARDUINO "https://github.com/hemendrasharma04/supplychaindapp/blob/hemendra/arduino_code.c"
    click NODE_RED "https://github.com/hemendrasharma04/supplychaindapp/blob/hemendra/Node_Red.json"
    click DF_CLIENT "https://github.com/hemendrasharma04/supplychaindapp/tree/hemendra/client/Dockerfile"
    click DF_BACKEND "https://github.com/hemendrasharma04/supplychaindapp/tree/hemendra/backend/Dockerfile"
    click DC "https://github.com/hemendrasharma04/supplychaindapp/blob/hemendra/docker-compose.yml"
    click TESTS "https://github.com/hemendrasharma04/supplychaindapp/tree/hemendra/test"
    click SCRIPTS "https://github.com/hemendrasharma04/supplychaindapp/tree/hemendra/scripts"

    %% Styles
    classDef frontend fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef backend fill:#A9DFBF,stroke:#196F3D,stroke-width:2px;
    classDef blockchain fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef iot fill:#D2B4DE,stroke:#6C3483,stroke-width:2px;
    classDef deployment fill:#F5B7B1,stroke:#922B21,stroke-width:2px;
```
