> For the complete documentation index, see [llms.txt](https://mohamed-amins-personal-organizat.gitbook.io/smart-grid-ledger/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mohamed-amins-personal-organizat.gitbook.io/smart-grid-ledger/chaincode-specifications.md).

# Chaincode Specifications

## Introduction

The **SmartGridLedger** utilizes specialized chaincodes to manage the distinct operations of the energy distribution system. Each chaincode is tailored to the unique needs of the network's participants, ensuring secure, efficient, and transparent transactions. The specifications below serve as an initial design, which can be refined as the system requirements evolve.

## Chaincode for EV Charging Stations (Channel C1)

### Chaincode Name: `EVChargingChaincode`

#### Overview

This chaincode handles transactions related to the operations of EV charging stations, including session management, billing, and energy usage tracking.

#### Asset Definitions

* **ChargingSession**: `{ sessionId, stationId, vehicleId, startTime, endTime, energyConsumed, price, status }`
* **StationInfo**: `{ stationId, location, totalChargers, chargerType }`

#### Transaction Definitions

* **StartChargingSession**: Initiates a new charging session for a vehicle.
* **EndChargingSession**: Concludes an active charging session and calculates the final cost.
* **GetChargingSessionInfo**: Retrieves details of a specific charging session.
* **GetStationInfo**: Provides information about a specific charging station.

#### Endorsement Policy

Transactions must be endorsed by the peer nodes of the EV station organization to which the charging station belongs.

## Chaincode for Energy Storage Systems (Channel C2)

### Chaincode Name: `EnergyStorageChaincode`

#### Overview

This chaincode manages the ledger for energy storage systems, focusing on recording energy transactions, state-of-charge, and maintenance events.

#### Asset Definitions

* **StorageUnit**: `{ unitId, ownerId, capacity, chargeLevel, status, maintenanceSchedule }`
* **EnergyTransaction**: `{ transactionId, unitId, type, quantity, timestamp, price }`

#### Transaction Definitions

* **RecordEnergyStorage**: Logs energy charging or discharging to/from a storage unit.
* **UpdateMaintenanceSchedule**: Updates the maintenance schedule for a storage unit.
* **QueryEnergyTransactions**: Retrieves a list of energy transactions for a specified period or unit.
* **GetStorageUnitStatus**: Provides the current status and charge level of a storage unit.

#### Endorsement Policy

All transactions related to energy storage must be endorsed by the organization that owns the storage system.

## Chaincode for Power Generation Systems (Channel C3)

### Chaincode Name: `PowerGenerationChaincode`

#### Overview

This chaincode is responsible for recording and managing data related to power generation, including production metrics, feed-in tariffs, and distribution.

#### Asset Definitions

* **GenerationRecord**: `{ recordId, generatorId, amountGenerated, timestamp, feedInRate }`
* **GeneratorProfile**: `{ generatorId, type, capacity, location, status }`

#### Transaction Definitions

* **LogPowerGeneration**: Records the amount of power generated by a source.
* **UpdateGeneratorStatus**: Modifies the operational status of a generation source.
* **GetGenerationHistory**: Retrieves historical generation data for analysis and reporting.
* **CalculateFeedInTariff**: Computes the feed-in tariff based on the generated amount and current rates.

#### Endorsement Policy

Transactions must be endorsed by the peer nodes of the organization that operates the generation system.
