## 4 Platform Design
### Trading Platform Interacting Entities
```
1:\(Deposits:Mapping\) (address:uint256) \(\triangleright\) Keep track of each consumer deposit balance
2:\(Consumption:Mapping\) (address:uint256) \(\triangleright\) Keep track of each consumer total consumption
3:\(EnergyRequested:Mapping\) (address:uint256) \(\triangleright\) Access each consumer requested energy
4:\(SubscriptionTime:uint256\) \(\triangleright\) Defined time span to accept new subscriptions after contract deployment
5:\(DsoSmartContract:address\) \(\triangleright\) DSO smart contract address
6:\(Subscribers:address[]\) \(\triangleright\) Keep track of subscribers' accounts
7:\(DeploymentTime:uint256\) \(\triangleright\) This variable is set to contract deployment time, it is set inside the smart contract constructor called when contract is deployed
8:\(EnergyTarif:uint256\)
```  
**Algorithm 9** Provider Smart Contract: Global Variables  
Algorithm 11 defines the function consumeds call to upload their energy consumption data through their certified nodes. Algorithm 12 defines the function called by consumers to subscribe to the trading round. Algorithm 13 defines the function called by consumers to unsubscribe prematurely and exit the trading cycle. ```
1:\(Time:uint256\)
2:\(TotalEnergyRequested:uint256\)
3:\(ProviderEnergyBlance:uint256\)
4:\(time\leftarrow\)\(block.timestamp\)
5:\(TotalEnergyRequested\leftarrow\)\(TotalEnergyRequested+\frac{msg.value}{10^{18}}\)
6:\(EnergyRequested[msg.sender]\leftarrow\)\(msg.value\)
7:\(ProviderEnergyBlance\leftarrow\)\(CallDSOContract1(addressDsoContract,addressProvider)\)
8:\(\triangleright\) /* Call DSO smart contract ProviderEnergyBalance function, to get Provider's energy balance */
9:\(deposits[msg.sender]\leftarrow\)\(deposits[msg.sender]-consumptionbalance\)
10:if\(TotalEnergyRequested>\)\(ProviderEnergyBlance\vee\)\(TimeSubscriptionTime\leq\)\(Time-Deployment\vee\)\(!CallDsoContract2(addressDsoContract,msg.sender)\)then
11:\(Revert()\)
12:for\(i\gets 0\) to Subscribers.Length do
13: emit\(SendEnergy(Subcribers[i])\)
14:endfor
15: emit\(StartTradingRound()\)
16:else
17:\(Deposits[msg.sender]\leftarrow\)\(Deposits[msg.sender]+msg.value\)
18:\(subsribers.push(msg.sender)\)
19:\(CallDsoContract3(addressDsoContract,addressProvider,\frac{msg.value}{(10^{18})})\)\(\triangleright\) /* Call DSO smart contract UpdateProviderBalancel function to update the provider's energy balance according to the subscribers' energy demand that needs to be supplied to them */
20:endif
```  
**Algorithm 12** Provider's Smart Contract: Subscribe  
```
1:\(BillAmount:uint256\)
2:\(TotalEnergyRequested:uint256\)
3:\(BillAmount\leftarrow\)\(consumption[msg.sender]*EnergyTarif\)
4:\(Provider.Trans\)\(fer(BillAmount)\)
5:\(msg.sender.Trans\)\(fer(deposits[msg.sender]-billamount)\)
6:\(Deposits[msg.sender]\gets 0\)
7:\(EnergyRequested[msg.sender]\gets\)\(msg.value\)
8:\(\triangleright\) // Call DSO smart contract UpdateProviderBalance2 function to re-update the provider's energy balance accordingly after consumer unsubscribing:
9:\(CallDSOContract1(addressDsoContract,addressProvider,deposits[msg.sender]-billamount/10^{18})\)
10:endif\(BalanceExhausted(msg.sender)\)
```  
**Algorithm 13** Provider Smart Contract: Unsubscribe