# A Framework for Decentralized Energy Trading Based on Blockchain Technology
## 2 Methodology
### Order  
The energy market is modeled as a model that flows into and out of trading orders. All orders have the following characteristics:  
1. Quantity: buy and sell a quantity of energy with the unit of kWh;
2. Residual amount: used when an order is only partially satisfied by previous transactions;
3. Order price: the price for selling or buying energy;
4. Order time: the time when the order was issued;
5. Expiration time: if the order is not (fully) satisfied, it is removed from the pool at this time.
6. Price Clearing Mechanism  
We implemented the price clearing mechanism by using a high-low matching (HLM) bidding mechanism like that presented in [18].  
At every time step, the order pool holds the list of orders received. Buy orders are sorted in descending order. Sell orders are sorted in ascending order, including the limit price \(\mathrm{S_{r}}\) offered by the regulator. Orders with the same limit price are sorted in ascending order with respect to the order issue time.  
At each step, various new orders are inserted, or old orders are withdrawn from the pool. As soon as the time comes, the first buy order and the first sell order from the lists are inspected to verify if they match. If they match, a transaction occurs. The order with the smallest residual amount is fully executed, whereas the order with the largest amount is only partially executed and remains at the head of the list, with its residual amount reduced by the amount of the matching order. Clearly, if both orders have the same residual amount, they are both fully executed. The price clearing process is shown in Figure 4. In this figure, C1-C8 is the bidding price for each consumer, and P1-P5 is the bidding price for each producer.  
Figure 4: Sample of price clearing mechanism.  
Message Exchange Process  
The scenario is visualized conceptually in Figure 5. It is envisaged that the machines participating in this system would each be equipped with a computer containing their digital representations, enabling them to interact with a blockchain. Here, the physical machines are replaced with physical simulations of Matlab.  
A typical trade proceeds as follows:  
1. The producer and consumer nodes send price request messages and receive the highest and lowest price in the prior round. According to this message, offers in kWh expressed in USD are prepared and published in the stream, as shown on Figure 6. The preparations require the producers to lock enough energy assets and encode the details of the exchange.
2. Each node receives the messages and sends them to the order pool.
3. The price clearing process is motivated after a certain period of time. The result will be sent to these nodes that send orders.
4. The consumer and producer start to produce or consume, according to the order.
5. Finally, the nodes in the network will calculate the reward and punishment according to their actions, and the result will be added to the blockchain permanently.  
Figure 5: Visual presentation of participating in an energy market over a blockchain.  
Figure 6: Diagram of the test platform.