# sachincoder1/ecommerce-dapp architecture

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

- Interactive diagram: https://gitdiagram.com/sachincoder1/ecommerce-dapp
- Repository: https://github.com/sachincoder1/ecommerce-dapp

## Overview

##### Project Type and Purpose
- This is a full-stack decentralized e-commerce DApp.
- Frontend: Next.js + Tailwind CSS + Material-Tailwind, Web3 integration via ethers.js.
- Blockchain “backend”: Solidity smart contract, Hardhat for development/deployment, testing with Chai, deployed to Polygon (Alchemy RPC), IPFS for image/metadata storage.

##### Main Components
a. Frontend Application
- Next.js pages and React components under `frontend/pages`, `frontend/components`, `frontend/subcomponents`.
- State/context in `frontend/context/MainContext.js`.
- Web3 interface via ethers.js in wallet connector (`WalletConnect.jsx`).
- Calls out to blockchain through an RPC provider (Alchemy) and interacts with IPFS for metadata.
b. Smart Contract Layer
- Solidity contract(s) in `blockchain/contracts`.
- Deployment scripts in `blockchain/scripts/deploy.js` using Hardhat.
- Hardhat config (`hardhat.config.js`) defines networks (local, polygon).
- Unit tests (`blockchain/test/Ecommerce.test.js`) validate contract behavior.
c. Storage & External Services
- IPFS for decentralized image/metadata storage (upload in admin flow).
- Alchemy (Polygon Mumbai) as JSON-RPC provider.
- Hardhat local network for development/testing.

##### Relationships & Data Flow
- User/Admin in browser → Next.js UI → ethers.js calls → Polygon RPC (Alchemy) → Smart Contract → on-chain state.
- Admin UI also uploads to IPFS → returns CID → stored in smart contract via a transaction.
- Frontend queries smart contract for product list, order history; fetches metadata from IPFS using CID.
- Hardhat scripts deploy contracts to target network; tests run against local Hardhat network.

##### Architectural Patterns & Design Principles
- Separation of concerns: UI vs. blockchain logic vs. storage layer.
- DApp pattern: “UI → Web3 Provider → Smart Contract.”
- Context-driven state management in React.
- Config-driven deployment (Hardhat + Next.js configs).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        direction TB
        Pages["frontend/pages"]:::frontend
        Components["frontend/components"]:::frontend
        Subcomponents["frontend/subcomponents"]:::frontend
        Layout["MainLayout.jsx"]:::frontend
        Styles["globals.css & tailwind.config.js"]:::frontend
        Context["MainContext.js"]:::frontend
        WalletConnect["WalletConnect.jsx"]:::frontend
        NextConfig["next.config.js"]:::frontend
        Constants["frontend/constants"]:::frontend
        APIHello["api/hello.js"]:::frontend
    end

    %% Blockchain Layer
    subgraph "Blockchain Layer"
        direction TB
        Contracts["blockchain/contracts"]:::blockchain
        HardhatConfig["hardhat.config.js"]:::blockchain
        DeployScript["deploy.js"]:::blockchain
        Tests["Ecommerce.test.js"]:::blockchain
        ContractABI["Ecommerce.json"]:::blockchain
    end

    %% Storage/Network Layer
    subgraph "Storage/Network Layer"
        direction TB
        IPFS["IPFS"]:::storage
        OnChain["On-Chain Storage"]:::onchain
        AlchemyCloud["Alchemy/Polygon RPC"]:::network
    end

    %% Data Flows
    Pages -->|"renders pages"| Components
    Components --> Subcomponents
    Subcomponents --> Layout
    Layout --> Styles
    Layout --> Context
    Context --> WalletConnect
    WalletConnect -->|JSON-RPC| AlchemyCloud
    WalletConnect --> Constants
    Pages --> APIHello

    Contracts -->|compiled & deployed| HardhatConfig
    HardhatConfig --> DeployScript
    DeployScript --> OnChain
    Tests --> Contracts
    Tests --> HardhatConfig

    OnChain -->|state queries| WalletConnect
    OnChain -->|writes CID| IPFS
    IPFS -->|returns CID| WalletConnect
    WalletConnect -->|fetch metadata| IPFS
    AlchemyCloud -->|RPC calls| Contracts

    %% Click Events
    click Pages "https://github.com/sachincoder1/ecommerce-dapp/tree/main/frontend/pages"
    click Components "https://github.com/sachincoder1/ecommerce-dapp/tree/main/frontend/components"
    click Subcomponents "https://github.com/sachincoder1/ecommerce-dapp/tree/main/frontend/subcomponents"
    click Layout "https://github.com/sachincoder1/ecommerce-dapp/blob/main/frontend/layouts/MainLayout.jsx"
    click Styles "https://github.com/sachincoder1/ecommerce-dapp/blob/main/frontend/styles/globals.css"
    click Styles "https://github.com/sachincoder1/ecommerce-dapp/blob/main/frontend/tailwind.config.js"
    click Context "https://github.com/sachincoder1/ecommerce-dapp/blob/main/frontend/context/MainContext.js"
    click WalletConnect "https://github.com/sachincoder1/ecommerce-dapp/blob/main/frontend/components/wallet/WalletConnect.jsx"
    click NextConfig "https://github.com/sachincoder1/ecommerce-dapp/blob/main/frontend/next.config.js"
    click Constants "https://github.com/sachincoder1/ecommerce-dapp/tree/main/frontend/constants"
    click APIHello "https://github.com/sachincoder1/ecommerce-dapp/blob/main/frontend/pages/api/hello.js"
    click Contracts "https://github.com/sachincoder1/ecommerce-dapp/tree/main/blockchain/contracts"
    click HardhatConfig "https://github.com/sachincoder1/ecommerce-dapp/blob/main/blockchain/hardhat.config.js"
    click DeployScript "https://github.com/sachincoder1/ecommerce-dapp/blob/main/blockchain/scripts/deploy.js"
    click Tests "https://github.com/sachincoder1/ecommerce-dapp/blob/main/blockchain/test/Ecommerce.test.js"
    click ContractABI "https://github.com/sachincoder1/ecommerce-dapp/blob/main/frontend/constants/Ecommerce.json"

    %% Styles
    classDef frontend fill:#E0F7FA,stroke:#006064,color:#004D40
    classDef blockchain fill:#E8F5E9,stroke:#1B5E20,color:#2E7D32
    classDef storage fill:#ECEFF1,stroke:#607D8B,color:#455A64
    classDef onchain fill:#FFF3E0,stroke:#EF6C00,color:#E65100
    classDef network fill:#EDE7F6,stroke:#5E35B1,color:#4527A0
```
