This project simulates a decentralized P2P energy trading platform using Ethereum smart contracts, enabling prosumers to trade surplus energy directly with consumers, regulated by a DSO (Distribution System Operator). It is designed for simulation on a local blockchain using Ganache and Web3.py.
P2P-energy-trading/
├── contracts/ # Solidity smart contracts
│ └── EnergyTrading.sol
│
├── build/ # Compiled ABI & bytecode
│ └── EnergyTrading/
│ ├── abi.pkl
│ └── bytecode.pkl
│
├── deploy_contract.py # Compiles and deploys the smart contract
├── dso.py # DSO role logic
├── prosumer.py # Prosumer role logic
├── consumer.py # Consumer role logic
│
├── config.json # Runtime configuration (addresses, paths)
└── requirements.txt # Python dependencies
| Role | Script | Responsibilities |
|---|---|---|
| 🛠️ DSO | dso.py |
Registers nodes, matches trades, verifies transactions |
| 🔋 Prosumer | prosumer.py |
Adds funds, injects energy, checks balance |
| 🔌 Consumer | consumer.py |
Adds funds, purchase energy, check account balance |
All roles interact with the smart contract via Web3.py and log smart contract events.
- Python 3.8+
- Ganache (local Ethereum blockchain)
- Solidity compiler via
py-solc-x
git clone https://github.com/Rahil-07/P2P-energy-trading.git
cd P2P-energy-tradingpip install -r requirements.txt-
Start Ganache GUI or run ganache-cli
-
Note down the RPC URL and account addresses
Update config.json with your Ganache addresses and paths:
{
"contract_path": "./contracts/",
"build_path": "./build/",
"dso_address": "0x...",
"prosumer_address": "0x...",
"consumer_address": "0x...",
"contract_address": ""
}python deploy_contract.py EnergyTrading.solAfter successful deployment, the contract address will be written to config.json.
You can now run the respective scripts for each stakeholder:
python dso.py-
Register new nodes
-
Match a prosumer with a consumer
-
Execute energy trades
-
Verify transactions
-
View threshold values
python prosumer.py-
Add funds to wallet
-
Inject energy into grid
-
View energy and account balances
python consumer.py-
Add funds to wallet
-
View owned energy and balance
Written in Solidity 0.6.0, the contract supports:
-
Role-based access control
-
Ownership and trading of energy units
-
Trade matching and verification
-
Transparent event logging
Contract ABI and bytecode are stored in build/EnergyTrading/.
Each interaction emits on-chain events such as:
- OwnershipEvent
- MatchEvent
- EnergyTraded
- BalanceAdded
- EnergyInjected
- TradingLog
- VerificationEvent
- EnergyLossCalculated
These are printed to console for simulation clarity.
This project is licensed under the MIT License.