# aarya695/decentralized-energy-trading-system architecture

> Architecture diagram of the GitHub repository aarya695/decentralized-energy-trading-system, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aarya695/decentralized-energy-trading-system
- Repository: https://github.com/aarya695/decentralized-energy-trading-system

## Overview

#### Purpose and Entry Points
Smart Energy Platform is an in-memory simulator for peer-to-peer household solar-energy trading. A FastAPI backend advances a simulated grid every three seconds and exposes REST/WebSocket state; a Flutter client presents system, grid-flow, household, and billing views.

Backend startup is `backend/main.py` (`uvicorn main:app` from `backend/`). Flutter startup and navigation are rooted at `frontend/lib/main.dart`.

#### Backend Orchestration and State
`backend/main.py` owns the FastAPI application, simulation loop, and real-time broadcast lifecycle. `backend/state.py` is the singleton, process-local source of truth for the 12 simulated houses, market data, transactions, tick/time counters, and billing state. There is no database, persistence layer, or distributed coordination: reset/restart discards state.

Core domain/Pydantic structures defining houses, orders, trades, and bills reside in `backend/models.py`.

#### Per-Tick Energy Simulation
Each tick represents one simulated hour. The engine in `backend/simulation.py` calculates per-house solar generation and time/weather-sensitive consumption, then derives net surplus or deficit. Surplus becomes sellable supply; deficits become buy demand.

The system’s energy-source priority is a material control boundary: self-produced solar is consumed first, then locally matched P2P energy, with the government grid used only for unmet demand.

#### P2P Market and Price Formation
`backend/marketplace.py` converts positions into orders and matches lowest-priced sellers against highest-priced buyers when bids are compatible. Matched quantity is bounded by both parties’ remaining supply/demand; the transaction price is derived from their prices. Transactions update household balances and downstream billing inputs.

`backend/pricing.py` computes the market’s dynamic reference pricing from aggregate supply versus demand, bounded to ₹2–₹12/kWh. It influences offers and reflects scarcity/abundance, while unmatched deficits retain the fixed ₹8/kWh grid fallback in `backend/simulation.py`.

#### Billing and Settlement
`backend/billing.py` accumulates P2P purchase cost, grid cost, and P2P sale earnings. At 720 ticks (30 simulated days), it produces per-house bills: costs minus P2P earnings, allowing a negative amount to represent net earnings. Bill retrieval and payment are simulation-state operations, not integrations with a real payment processor.

#### API and Real-Time Boundary
`backend/router.py` exposes REST reads for houses, market/dashboard aggregates, transactions, personal views, and bills, plus manual tick/reset and bill-payment commands. `backend/main.py` exposes health/application lifecycle and distributes full-state JSON snapshots to connected WebSocket clients after each tick. FastAPI provides interactive documentation at `/docs`.

The API boundary is consumed by `frontend/lib/services/api_service.dart`, which combines HTTP fetching with WebSocket-driven refreshes.

#### Flutter Presentation Layer
`frontend/lib/models/models.dart` maps backend payloads into Dart-side models. `frontend/lib/main.dart` composes app-level navigation and screens.

Primary views are:
- System-wide market metrics: `frontend/lib/screens/dashboard_screen.dart`
- Animated house/grid energy-flow visualization: `frontend/lib/screens/grid_screen.dart`
- Selected household status and activity: `frontend/lib/screens/my_dashboard_screen.dart`
- Current and historical billing presentation: `frontend/lib/screens/my_bill_screen.dart`

#### Runtime and Infrastructure
Backend runtime is Python 3.9+ with FastAPI/Uvicorn dependencies declared in `backend/requirements.txt`. Frontend is Flutter/Dart, targeting at least web and Windows project scaffolding, with package dependencies in `frontend/pubspec.yaml`. External infrastructure is intentionally minimal: no database, broker, smart-meter integration, payment gateway, or external energy-market service is represented.

Main entry points: [`frontend/lib/models/models.dart`](https://github.com/aarya695/decentralized-energy-trading-system/blob/main/frontend/lib/models/models.dart) (Client payload models), [`backend/state.py`](https://github.com/aarya695/decentralized-energy-trading-system/blob/main/backend/state.py) (Process-local state), [`frontend/lib/main.dart`](https://github.com/aarya695/decentralized-energy-trading-system/blob/main/frontend/lib/main.dart) (App navigation), [`backend/simulation.py`](https://github.com/aarya695/decentralized-energy-trading-system/blob/main/backend/simulation.py) (Energy simulation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_backend["Python backend"]
  node_backend_main{{"FastAPI runtime<br/>[main.py]"}}
  node_api_router["REST commands and reads<br/>FastAPI router<br/>[router.py]"]
  node_ws_clients["WebSocket clients<br/>real-time channel<br/>[main.py]"]
  node_sim_state[("Process-local state<br/>singleton state<br/>[state.py]")]
  node_domain_models["Energy domain models<br/>Pydantic models<br/>[models.py]"]
  node_simulation["Energy simulation<br/>tick engine<br/>[simulation.py]"]
  node_energy_priority["Energy allocation priority<br/>control policy<br/>[simulation.py]"]
  node_pricing["Dynamic market pricing<br/>price engine<br/>[pricing.py]"]
  node_marketplace["P2P order matching<br/>market engine<br/>[marketplace.py]"]
  node_billing["Billing and settlement<br/>billing engine<br/>[billing.py]"]
end

subgraph group_frontend["Flutter client"]
  node_flutter_main{{"App navigation<br/>Flutter entry point<br/>[main.dart]"}}
  node_api_service["API and socket client<br/>Flutter service<br/>[api_service.dart]"]
  node_dart_models["Client payload models<br/>Dart models<br/>[models.dart]"]
  node_dashboard["Market dashboard<br/>Flutter screen"]
  node_grid_view["Grid flow view<br/>Flutter screen<br/>[grid_screen.dart]"]
  node_household_view["Household dashboard<br/>Flutter screen"]
  node_bill_view["Billing view<br/>Flutter screen"]
end

node_backend_main -->|"runs each tick"| node_simulation
node_simulation -->|"derives positions"| node_energy_priority
node_energy_priority -->|"aggregate P2P supply/demand"| node_pricing
node_pricing -->|"reference prices"| node_marketplace
node_marketplace -->|"trade costs and earnings"| node_billing
node_simulation -->|"grid fallback costs"| node_billing
node_simulation -->|"updates household state"| node_sim_state
node_marketplace -->|"records orders and trades"| node_sim_state
node_billing -->|"stores billing state"| node_sim_state
node_domain_models -->|"state structures"| node_sim_state
node_api_router -->|"reads and commands"| node_sim_state
node_backend_main -->|"mounts REST API"| node_api_router
node_backend_main -->|"broadcasts snapshots"| node_ws_clients
node_flutter_main -->|"navigates"| node_dashboard
node_flutter_main -->|"navigates"| node_grid_view
node_flutter_main -->|"navigates"| node_household_view
node_flutter_main -->|"navigates"| node_bill_view
node_api_service -->|"HTTP REST"| node_api_router
node_api_service -->|"WebSocket refreshes"| node_ws_clients
node_api_service -->|"maps payloads"| node_dart_models
node_dart_models -->|"view data"| node_dashboard
node_dart_models -->|"view data"| node_grid_view
node_dart_models -->|"view data"| node_household_view
node_dart_models -->|"view data"| node_bill_view

click node_backend_main "https://github.com/aarya695/decentralized-energy-trading-system/blob/main/backend/main.py"
click node_api_router "https://github.com/aarya695/decentralized-energy-trading-system/blob/main/backend/router.py"
click node_ws_clients "https://github.com/aarya695/decentralized-energy-trading-system/blob/main/backend/main.py"
click node_sim_state "https://github.com/aarya695/decentralized-energy-trading-system/blob/main/backend/state.py"
click node_domain_models "https://github.com/aarya695/decentralized-energy-trading-system/blob/main/backend/models.py"
click node_simulation "https://github.com/aarya695/decentralized-energy-trading-system/blob/main/backend/simulation.py"
click node_energy_priority "https://github.com/aarya695/decentralized-energy-trading-system/blob/main/backend/simulation.py"
click node_pricing "https://github.com/aarya695/decentralized-energy-trading-system/blob/main/backend/pricing.py"
click node_marketplace "https://github.com/aarya695/decentralized-energy-trading-system/blob/main/backend/marketplace.py"
click node_billing "https://github.com/aarya695/decentralized-energy-trading-system/blob/main/backend/billing.py"
click node_flutter_main "https://github.com/aarya695/decentralized-energy-trading-system/blob/main/frontend/lib/main.dart"
click node_api_service "https://github.com/aarya695/decentralized-energy-trading-system/blob/main/frontend/lib/services/api_service.dart"
click node_dart_models "https://github.com/aarya695/decentralized-energy-trading-system/blob/main/frontend/lib/models/models.dart"
click node_dashboard "https://github.com/aarya695/decentralized-energy-trading-system/blob/main/frontend/lib/screens/dashboard_screen.dart"
click node_grid_view "https://github.com/aarya695/decentralized-energy-trading-system/blob/main/frontend/lib/screens/grid_screen.dart"
click node_household_view "https://github.com/aarya695/decentralized-energy-trading-system/blob/main/frontend/lib/screens/my_dashboard_screen.dart"
click node_bill_view "https://github.com/aarya695/decentralized-energy-trading-system/blob/main/frontend/lib/screens/my_bill_screen.dart"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_backend_main,node_api_router,node_ws_clients,node_sim_state,node_domain_models,node_simulation,node_energy_priority,node_pricing,node_marketplace,node_billing toneBlue
class node_flutter_main,node_api_service,node_dart_models,node_dashboard,node_grid_view,node_household_view,node_bill_view toneAmber
```

## Components

### Python backend

- [FastAPI runtime](https://github.com/aarya695/decentralized-energy-trading-system/blob/main/backend/main.py): FastAPI app + tick loop · Advances simulation every three seconds and broadcasts snapshots. · `backend/main.py`
- [REST commands and reads](https://github.com/aarya695/decentralized-energy-trading-system/blob/main/backend/router.py): FastAPI router · Serves state views, ticks, resets, bills, and payments. · `backend/router.py`
- [WebSocket clients](https://github.com/aarya695/decentralized-energy-trading-system/blob/main/backend/main.py): real-time channel · Receives full-state snapshots after ticks. · `backend/main.py`
- [Process-local state](https://github.com/aarya695/decentralized-energy-trading-system/blob/main/backend/state.py): singleton state · In-memory source of truth; reset or restart discards it. · `backend/state.py`
- [Energy domain models](https://github.com/aarya695/decentralized-energy-trading-system/blob/main/backend/models.py): Pydantic models · Defines houses, orders, trades, and bills. · `backend/models.py`
- [Energy simulation](https://github.com/aarya695/decentralized-energy-trading-system/blob/main/backend/simulation.py): tick engine · Calculates generation, consumption, and net household positions. · `backend/simulation.py`
- [Energy allocation priority](https://github.com/aarya695/decentralized-energy-trading-system/blob/main/backend/simulation.py): control policy · Solar first, then P2P, then ₹8/kWh grid fallback. · `backend/simulation.py`
- [Dynamic market pricing](https://github.com/aarya695/decentralized-energy-trading-system/blob/main/backend/pricing.py): price engine · Uses aggregate supply and demand within ₹2–₹12/kWh. · `backend/pricing.py`
- [P2P order matching](https://github.com/aarya695/decentralized-energy-trading-system/blob/main/backend/marketplace.py): market engine · Matches compatible seller offers and buyer bids. · `backend/marketplace.py`
- [Billing and settlement](https://github.com/aarya695/decentralized-energy-trading-system/blob/main/backend/billing.py): billing engine · Settles bills every 720 ticks; bills may be negative. · `backend/billing.py`

### Flutter client

- [App navigation](https://github.com/aarya695/decentralized-energy-trading-system/blob/main/frontend/lib/main.dart): Flutter entry point · Composes application navigation and screens. · `frontend/lib/main.dart`
- [API and socket client](https://github.com/aarya695/decentralized-energy-trading-system/blob/main/frontend/lib/services/api_service.dart): Flutter service · Combines HTTP requests with WebSocket-driven refreshes. · `frontend/lib/services/api_service.dart`
- [Client payload models](https://github.com/aarya695/decentralized-energy-trading-system/blob/main/frontend/lib/models/models.dart): Dart models · Maps backend payloads into presentation models. · `frontend/lib/models/models.dart`
- [Market dashboard](https://github.com/aarya695/decentralized-energy-trading-system/blob/main/frontend/lib/screens/dashboard_screen.dart): Flutter screen · Shows system-wide market metrics. · `frontend/lib/screens/dashboard_screen.dart`
- [Grid flow view](https://github.com/aarya695/decentralized-energy-trading-system/blob/main/frontend/lib/screens/grid_screen.dart): Flutter screen · Visualizes household and grid energy flows. · `frontend/lib/screens/grid_screen.dart`
- [Household dashboard](https://github.com/aarya695/decentralized-energy-trading-system/blob/main/frontend/lib/screens/my_dashboard_screen.dart): Flutter screen · Shows selected household status and activity. · `frontend/lib/screens/my_dashboard_screen.dart`
- [Billing view](https://github.com/aarya695/decentralized-energy-trading-system/blob/main/frontend/lib/screens/my_bill_screen.dart): Flutter screen · Shows current and historical bills. · `frontend/lib/screens/my_bill_screen.dart`

## Connections

- FastAPI runtime → Energy simulation: runs each tick
- Energy simulation → Energy allocation priority: derives positions
- Energy allocation priority → Dynamic market pricing: aggregate P2P supply/demand
- Dynamic market pricing → P2P order matching: reference prices
- P2P order matching → Billing and settlement: trade costs and earnings
- Energy simulation → Billing and settlement: grid fallback costs
- Energy simulation → Process-local state: updates household state
- P2P order matching → Process-local state: records orders and trades
- Billing and settlement → Process-local state: stores billing state
- Energy domain models → Process-local state: state structures
- REST commands and reads → Process-local state: reads and commands
- FastAPI runtime → REST commands and reads: mounts REST API
- FastAPI runtime → WebSocket clients: broadcasts snapshots
- App navigation → Market dashboard: navigates
- App navigation → Grid flow view: navigates
- App navigation → Household dashboard: navigates
- App navigation → Billing view: navigates
- API and socket client → REST commands and reads: HTTP REST
- API and socket client → WebSocket clients: WebSocket refreshes
- API and socket client → Client payload models: maps payloads
- Client payload models → Market dashboard: view data
- Client payload models → Grid flow view: view data
- Client payload models → Household dashboard: view data
- Client payload models → Billing view: view data
