## 4 Platform Design
### Trading Platform Interacting Entities  
The Provider: which represents the set of prosumers cooperating in generating energy that is injected into the power grid owned by the DSO, and transferred to the requesting consumers, on demand. The Consumer: being the entity that purchases energy by subscribing to the provider through a smart contract for a certain duration of time called a trading round, prepays for his consumption and is supplied until exhaustion of the prepaid amount, or billed according to his energy consumption in case of unsubscribing prematurely. The DSO: also referred to as a utility owner company. The abstraction of the interacting entities is illustrated using the Entity Relationship Diagram ERD in Figure 7. However, these entities are not stored in a traditional database, but rather, they are represented by the state of the smart contract in which they are defined. The state of the smart contract is modified upon a successful call of its functions and is stored in the blockchain shared ledger. Each entity is assigned to its corresponding field through an appropriate mapping. Smart contract 1 (DSO): As its name indicates, DSO smart contract is deployed and owned by the DSO. It stores the list of certified consumer nodes as well as the energy balance of the providers. Only the DSO has the credentials to add, remove or update the certified nodes list. Providers' balance is updated by calling the appropriate smart contract function upon a new aggregation of a defined amount of energy or its consumption. The provider's energy balance is abstracted using a simple structure representing a defined amount of energy called an energy token. New instances of this data structure are mapped to prosumers accordingly upon the appropriate aggregation of energy. Consumed tokens are set to spent state. The global variables for the DSO smart contract are defined in Algorithm 5. \begin{table}
\begin{tabular}{c c c} \hline \hline  & **Deploy** & **Call Contract Function** \\ \hline K-mean smart contract & 801,911 & 1,334,160 \\ Smart contract in [65] & 760,550 & 82,721 \\ \hline \hline \end{tabular}
\end{table} 
Table 1: Gas consumption. Figure 7: ERD diagram of the proposed platform. ```
1:\(ProviderSmartContractAddresses:address[]\)\(\triangleright\) /* Addresses of all providers' deployed smart contracts */
2:\(DsoAddress:address\)\(\triangleright\) /*\(DSO\) account's address*/
3:\(Certified:Mapping\) (address: bool)\(\triangleright\) /* Check if an account is certified or not. By default, accounts are not certified. Certifying an account is restricted to the DSO */
4:Structure
5:\(TimeStamp:uint256\)
6:\(Spent:Bool\)
7:EndStructure
8:\(ProviderEnergyTokens:Mapping\) (address: EnergyToken[])\(\triangleright\) Keep track of EnergyToken instances owned by each Provider account
```  
**Algorithm 5** DSO Smart Contract: Global Variables  
The energy balance of the prosumer is defined by the number of unspent token instances and is publicly accessible by the function defined in Algorithm 6. ```
1:\(\triangleright\) /* This function returns the energy balance of a given address' account. */
2:Input:\(addr:address\)
3:Output:\(uint256\)
4:\(CountOfUnspentEnergyToken:uint256\)
5:\(CountOfUnspentEnergyToken\leftarrow\)
6:for\(i\gets 0\) to \(ProviderEnergyTokens[addr].Length\)do
7:if!ProviderEnergyTokens[addr][i].spentthen
8:\(CountOfUnspentEnergyToken\gets CountOfUnspentEnergyToken+1\)
9:endif
10:endfor
11:Return:\(CountOfUnspentEnergyToken\)
```  
**Algorithm 6** DSO Smart Contract: ProviderEnergyBalance  
Updating the provider's balance per consumed or redeemed token is done exclusively through the provider's smart contract, by calling the DSO smart contract functions defined in Algorithms 7 and 8, respectively. ```
1:Comment/* This function is called to consume N tokens, it takes two inputs, an integer N and an address addr. It sets N unspent tokens from the tokens owned by addr to spent.