Skip to content

Repository files navigation

P2P Energy Trading System

A decentralized, trustless marketplace for peer-to-peer energy trading on the Ethereum blockchain.

Problem Statement

Traditional centralized energy grids are inefficient for handling distributed energy resources (DERs) like solar panels. Prosumers (producers + consumers) often lack a transparent, direct mechanism to sell excess energy to their neighbors, relying instead on centralized utility companies with unfavorable rates and delayed settlements.

This project solves this by creating a blockchain-based trading platform where energy acts as a digital asset. It enables automated, transparent, and immediate settlement between energy sellers (Prosumers) and buyers (Consumers) without intermediaries.

Key Features

  • Role-Based Access Control: Strict segregation between Prosumers (authorized sellers) and Consumers (buyers) via a UserRegistry smart contract.
  • Tokenized Energy (NRG): ERC-20 compliant EnergyToken representing energy units, minted exclusively by an authorized Smart Meter Oracle to ensure physical-digital parity.
  • Trustless Market: A Market contract that handles offer creation, token escrow, and atomic swaps (ETH for NRG), eliminating counterparty risk.
  • Automated Settlement: Immediate transfer of ownership and funds upon purchase execution.
  • Security First: Leveraging OpenZeppelin's battle-tested standard libraries for token and ownership management.

Tech Stack

  • Smart Contracts: Solidity (v0.8.20)
  • Development Framework: Hardhat
  • Standards: ERC-20 (OpenZeppelin)
  • Testing & Scripting: TypeScript, Mocha, Chai, Ethers.js v6
  • Deployment: Hardhat Ignition

Project Structure

├── contracts/
│   ├── EnergyToken.sol   # ERC-20 Token implementation (NRG)
│   ├── Market.sol        # Marketplace logic (Offers, Buying)
│   └── UserRegistry.sol  # User role management
├── ignition/             # Deployment modules
├── test/                 # Automated tests
├── hardhat.config.ts     # Configuration (Networks, Plugins)
└── package.json          # Dependencies

Installation

  1. Clone the repository

    git clone <repository-url>
    cd p2p-energy-trade
  2. Install dependencies

    npm install
  3. Compile contracts

    npx hardhat compile

Usage

Running Tests

Execute the comprehensive test suite (Solidity + Mocha/Chai):

npx hardhat test

Deployment (Local)

Deploy the contracts to a local Hardhat Network node:

# Start a local node
npx hardhat node

# Deploy modules (in a separate terminal)
npx hardhat ignition deploy ignition/modules/Counter.ts --network localhost

(Note: Update the Ignition module path to match your specific deployment script name if different from the default)

Deployment (Sepolia Testnet)

  1. Set your SEPOLIA_PRIVATE_KEY using the Hardhat Keystore or environment variables:
    npx hardhat keystore set SEPOLIA_PRIVATE_KEY
  2. Deploy to Sepolia:
    npx hardhat ignition deploy ignition/modules/Counter.ts --network sepolia

Contributing

  1. Fork the repository.
  2. Create a feature branch (git checkout -b feature/amazing-feature).
  3. Commit your changes (git commit -m 'Add some amazing feature').
  4. Push to the branch (git push origin feature/amazing-feature).
  5. Open a Pull Request.

License

Distributed under the ISC License. See package.json for details.

About

A decentralized, trustless marketplace for peer-to-peer energy trading on the Ethereum blockchain.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages