Part 1 - Context and building the simulator
===========================================

In this project, we are going to implement a virtual power plant on 
Ethereum (using Solidity) and a Python client that will interact 
with this system.

In this context, our virtual power plant aims to manage a rather simplied
energy market between actors of a community.

Context
^^^^^^^
The goal is to implement a simple system to manage the energy 
in an small community. We will consider that this community 
is composed of participant that have different **equipements**. Those 
equipments can either be a **solar panel** (producing energy) or a **water 
heater** (consuming energy). We will simplify a bit the situation by considering
that each equipment is a participant. In other words, each participant can 
either produce or sell energy.

Each participant can place **orders** when they will need to buy energy from the community 
and they can sell energy when they produce. An order (either a selling or buying order) 
is composed of two informations : a quantity and a price per unit.

Orders will then be matched, in a **market**, with each others so participants can buy the energy 
they need or sell the energy they produced.

In this project we won't actually implement a real system (that will produce and consume energy 
over the course of one whole day) but rather a **simulator** that will trigger the different actions
in the system as they would have occured in a real world system. Instead of continously monitoring 
the energy production and demand, the simulator will execute the system at a given frequency (e.g 
once per hour) Such simulators are other easier to implement than real world system and can be used
to validate a prototype for later refinment. The simulator should output its data (energy produced, 
energy consumed, wallet of participants, ...) as a CSV file that you can later use with tools like 
Excel.

Our python simulator will interact with a **smart contract** that will manage the energy market. 

Phase 1 - Design 
^^^^^^^^^^^^^^^^

In this part of the project, we will design our system without coding first.

Implementing a software, even a small one, can be a daunting task. It can be 
really helpful to think in advance what we want to create before trying to 
implement it. That's what we are going to do here by designing first our system. 

The important tasks of this part is to identify the things we will need to implement (classes,
functions, interactions between the classes, ...). For our project we need to also 
think in advance about what will be implemented on the blockchain part (through smart contract)
and what will be implemented on the python part.

Specifications
--------------

From the explanations above, try to :

- Identify the differents entities interacting in the system (those entities will be later 
  implemented as classes or as a smart contract)
- Identify the different actions that can occur in the system (those actions will be later 
  implemented as functions)
- Identify what will occur on the python part (users part) and what will occur on the smart 
  contract part

.. admonition:: Question

    What important pieces of informations should be kept by the system about 
    an "order"? In other words, what would define an "order"?

.. admonition:: Question

    In our project, what would be managed by the blockchain? What would not
    be managed by the blockchain? (think of issues like "enforcing 
    market rules", "estimating production and demand"...)

Our specification should take the form of a design document detailing everything that needs 
to be implemented. You can imagine that you could give this document to a software engineering 
team. This team should be able to implement what you imagined based on the design document.

.. admonition:: Task

   This document should include a diagram representing the virtual power plants, its different actors, 
   the blockchain database nodes and the information flow between all those actors. This 
   diagram should illustrate how the system could work (you can mention the function calls 
   to the blockchain).

.. admonition:: Question

   In our Labs, we used "Ganache" which is a development tool. How our development environment
   differ from a "production" environment? You can include a diagram to illustrate the difference 
   between our "ganache environment" and a "real world" blockchain network that could host our 
   application.

Phase 2 - Implementing the Python simulator
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The goal here is to implement a simple simulator to illustrate how your contract work. 
This simulator will consist in a script that will represent a usage 
scenario of your market system. Different actors with different accounts 
can place selling and buying orders and see their wallet evolve when the "order matching" 
function will work. 

In this part we won't actually implement the smart contract, we already have a lot to do!

Here are python classes and parameters for the equipment:
- :download:`PV </_static/PV.py>`
- :download:`WaterHeater </_static/WaterHeater.py>`
- :download:`Data </_static/ProjectData.csv>`
- :download:`Parameters </_static/ProjectParameters.JSON>`

Part 2 - Building the smart contracts
=====================================

This is the second part of the project where we will 
actually implement the smart contract that will manage
all orders between our virtual power plant participants.

Phase 1 - Designing the smart contract
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Your contract should include a function that will match selling and buying orders. 
This function could be called periodically by a python script. Think in advance how
this function could work (the "algorithmic part") and try to describe it in a few lines. 
Thinking about the behavior of a function without writing the actual code can help a lot
for the implementation. 

This market system will be implemented as a smart contract hosted on a blockchain.
Define the key functions your contract will need. You may need to think about adding
some "helpers" function that could help a software interact with your system to have 
insights about it like "how many orders have been emitted?" or "how many orders have 
not been satisfied?". Think about the final python script that will interact with your 
smart contracts. What will it needs to be able to simulate our scenario and print all 
interesting informations?

Considering all those functions, what data should the contract manage? In other words,
what should you include as variables in the contract? 


Phase 2 - Implementing the smart contract
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Compilation and deployment
--------------------------
Write first the core structure of your smart contracts : 

-   The variables used by the contract
-   The structure (struct https://solidity-by-example.org/structs/) representing the orders 
-   The different functions your contract will have

You can then try to implement the different functions.

.. tip:: 
    In solidity, a contract can store addresses (https://docs.soliditylang.org/en/latest/types.html#address) 
    of a given account. In particular, a function can knows what address called it 
    by using the function "msg.sender"

Find the base :download:`here </_static/project.sol>`
