6. Structure of the Smart Contract

The Smart Contract is deployed via Mist browser to our private Ethereum blockchain. It represents a trading platform, which can be used by the five actors. They can perform the following activities:

  • Charge their prepaid account with ether

  • Offer energy for a specified ether price

  • Buy a specific amount of energy for the offered ether price

Each of these activities are realized by a function of the Smart Contract.

For the realization of these functions, the Smart Contract has...

  • its own ether wallet to store the total prepaid balance of all actors.

  • five account variables to store the prepaid balance of each actor. The logic for the separation is implemented in the charging and buying function.

  • five price variables to store the offers of each actor.

The solidity source code of the Smart Contract can be found here.

Logic of the Smart Contract

The following sequence diagram shows the basic procedure that gets carried out when a complete transaction gets executed.

Sequencediagram of the smart contract procedure

6.1 Sample Scenario

  1. "Load" charges its prepaid account with 40 Ether.

  2. "CHP" offers each kwh for 3 Ether.

  3. "Load" buys 10 kWh from "CHP" for 30 Ether.

6.1.1 "Load" charges its prepaid account with 40 Ether.

To charge the prepaid account, "Load" executes the PayMe function of the Smart Contract with the chosen amount of Ether. The Ether will be transferred from the "Load" wallet to the Smart Contract wallet. The Smart Contract recognizes who sent the Ether and increases the corresponding account balance variable.

Logic of the smart contract

Steps:

Step 1
Step 2
Step 3
Step 4

Choose function and amount of ether

Send Ether from "Load" wallet to contract wallet
Writing the transaction into the blockchain

Wallet of the contract (top right) and prepaid balance of "load"

6.1.2 "CHP" offers each kWh for 3 Ether.

To publish an offer, "CHP" executes the bid function with the corresponding "CHP kWh" value. The smart contract recognizes who executes the function. Therefore each actor can only change its own offers.

Logic of the Smart Contract

Steps:

Step 1
Step 2

CHP changes its price per kWh

The new offer is saved in the Smart Contract

6.1.3 "Load" buys 10 kWh from "CHP" for 30 Ether

"Load" checks which actor has the cheapest price per kwh. Afterwards chooses how much kWh should be bought from which actor and executes the "Energy Tokens" function. The Smart Contract will check, if "Load" has enough prepaid balance to perform this transaction. If so, the Smart Contract will send the corresponding amount of ether from its wallet to the wallet of all involved actors. The prepaid balance of "Load" will be reduced.

Logic of the Smart Contract

Steps:

Step 1
Step 2
Step 3
Step 4

Load looks for the best price offer
Load decides to buy 10 kWh per 3 Ether from Chp
Chp received 30 Ether from the Smart Contract wallet
Wallet of Smart Contract and prepaid balance of "Load" decreased by 30 Ether