This blog post is an article from a series of articles on the book published in December 2019 “Blockchain with SAP” (Rheinwerk publishing, Bonn, ISBN 978-3-8362-6914-8).

This blog post concerns the sixth chapter of the book, which describes the configuration and development of blockchain applications with Hyperledger Fabric and the SAP Web IDE.

As part of the “Blockchain with SAP” book, we are developing comparable example applications for Hyperledger Fabric and MultiChain in parallel. In the previous blog post, we developed a basic application for the storage of participant data in a Hyperledger telephone book. This time, a more complex application is being developed…

The simulation of a decentralized energy market

In this blog post, we aim to tackle the implementation of a decentralized energy market. The idea is that several participants take part in a marketplace, are able to offer proposals, and make purchases. Each participant has a fictional account with 50,000 virtual coins which they can use to make purchases on the energy market. The coins are a cryptocurrency and are not administered by the blockchain. They are just simulated and linked to the users’ profiles.

The frontend

The application’s interface for the decentralized energy market looks like this:

(c) Rheinwerk Verlag,The final web frontend of the decentralized energy market with three logical areas. (Source: “Blockchain with SAP,” p. 269, SAP PRESS, Rheinwerk publishing)

The public marketplace can be seen with a product name set as “Solarstrom” in the left part under point 1 which is listed as being for sale.

The top right part under point 2 lists the personal offers and, under point 3, the sold offers of the currently selected participant.

The participant on behalf of whom actions are currently to be carried out can be selected from the top right dropdown menu. Their current account balance is displayed to the left of this.

Setting a product

The currently selected participant can set his own offers for the market via the “Create offer” button. The offer then appears in the marketplace and can be purchased:

 

(c) Rheinwerk Verlag, Newly set offer in the marketplace (Source: “Blockchain with SAP,” p. 283, SAP PRESS, Rheinwerk publishing)

The participants can be changed via the top right dropdown menu, and “Charlie Sanchez” buys the new offer:

(c) Rheinwerk Verlag, An offer has been purchased, (Source: “Blockchain with SAP,” p. 286, SAP PRESS, Rheinwerk publishing)

The costs of the purchase are deducted from the account balance – the participant “Charlie Sanchez” only has 49100 coins remaining. This already demonstrates the possibilities of the decentralized energy market. 

Application architecture

The energy market application uses a tripartite architecture with the MVC design pattern. It has…

  1. a Hyperledger Fabric blockchain which acts as a data model,
  2. a Node.js server component which acts as middleware, and
  3. a SAPUI5 website which is played as the frontend.

The frontend transmits calls to the middleware REST API which acts as controller, and likewise communicates with the Hyperledger Fabric blockchain via REST calls.

(c) Rheinwerk Verlag Software architecture of the decentralized energy market (Source: “Blockchain with SAP,” p. 270, SAP PRESS, Rheinwerk publishing)

Data modelling in the blockchain

Smart contracts for Hyperledger Fabric are called Chaincode. Unlike other blockchains, Hyperledger Fabric does not have any finished data structures. In fact it is up to us to model an appropriate description of the data objects to be followed in the chaincode and to extend it by corresponding functions. The following listing mirrors the structure of the codes without showing the details of the implementation:

//EnergyMarket Chaincode implementation

type EnergyMarket struct {

}

/

******************** Product Offering registration function *********

***********/

func (t *EnergyMarket) registerProductOffering(stub shim.ChaincodeStu

bInterface, args []string) peer.Response {

... }

/******************** Purchase Product function ********************/

func (t *EnergyMarket) purchaseProduct(stub shim.ChaincodeStubInterfa

ce, args []string) peer.Response {

...}

//

******************** get registred products function*****************

***/

func (t *EnergyMarket) getProductOfferings(stub shim.ChaincodeStubInt

erface, args []string) peer.Response {

...}

 

The chaincode for the EnergyMarket therefore manages with just three functions:

  1. registerProductOffering for setting a new product,
  2. purchaseProduct to process the sale of a product, and
  3. getProductOfferings, which provides an overview of the products for the marketplace.

That’s it. In addition to these three functions for the marketplace, a definition of the data structure is also required for the offers. A ProductOffering is a kind of electricity offering which can be set by the participants on the marketplace. This allows the most important components to be defined in the chaincode for the blockchain, and we can turn to the next architecture components: the node.JS middleware.

The node.JS middleware server component

The middleware is implemented as node.JS server which provides a REST API which is consumed by SAPUI5 frontend and which forwards requests to the blockchain. In the central index.js file of the middleware project, the URLs, also known as routes, are linked with the call of corresponding functions in JavaScript.

For example, at the start of the platform, it must be determined whether offers are already available which should be displayed in the marketplace table. This occurs by the frontend transmitting the call

/getAvailableProductOfferings

 

to the middleware while loading the page. This call is mapped to the function

requestHandlers.getAvailableProductOfferings

 

This function looks like this:

…

function getAvailableProductOfferings(req, cb) {

const methodName = "getProductOfferings";

const filter = {"selector":{"docType":"productOfferings",

"status": 1}};

_blockchainGet(methodName, filter, cb);

}

…

 

It defines the chaincode function “getProductOfferings” to be called as well as a search filter with the selector “docType”:“productOfferings” and the status value of 1 which describes the available products. We therefore search for all product offers which are still available.

The last call forwards on the request to another private function _blockchainGet, which deals with communicating with the blockchain, transmitting the relevant calls to them and returning the result.

This outlines the basic communication with the blockchain. All further functions follow the same points: The frontend calls up the middleware which in turn calls the blockchain.

More information about middleware

In addition to communication, middleware also has other tasks, such as the management of simulated participants and their account balances. This occurs via a user.json file in which a JSON list is created with the participant data. This is read in when the middleware is started, and the data is continuously updated during operation. When a product is purchased by a user, the corresponding account balances are adjusted, that is the seller is credited the sale price and the purchaser has it deducted. At the latest here it becomes clear that the account balances in coins are not a real cryptocurrency.

The SAPUI5 frontend

The display of the user interface is developed with SAPUI5 in the SAP web IDE and in the operation of the middleware it is loaded with the data for display. For example, when loading the marketplace, the following data is requested from the frontend via the middleware:

  • Available products for the market (area 1)
  • Query of the created products by the currently selected participant (area 2)
  • Query of the purchased products by the currently selected participant (area 3)

These correspond to the three outlined areas of the decentralized energy market:

(c) Rheinwerk Verlag The web frontend of the decentralized energy market with three logical areas. (Source: “Blockchain with SAP,” p. 269, SAP PRESS, Rheinwerk publishing)

The SAP Cloud platform therefore offers support for simplified setup and starting of several services in the context of an overall application and thus considerably reduces the administrative outlay – a genuine blessing for developers.

More articles of this series can be found here:

Wollen Sie mehr zur SAP Cloud Platform und der Funktionsweise der Blockchains erfahren? In unserem Buch „Blockchain mit SAP“ erläutern wir Ihnen ausführlich die Funktionsweise und demonstrieren Ihnen anhand zahlreicher Beispiele die Vorzüge dieser innovativen Technologie. Eine ausführliche Leseprobe zum Buch stellt der Verlag online bereit.

Would you like to find out more about the possibilities of Hyperledger Fabric for your company? Our book “Blockchain with SAP” gives a detailed explanation of the advantages of Hyperledger Fabric blockchain and how to set it up with nodes in the SAP Cloud Platform. Are you already familiar with our Hypertrust platform which enables decentralized apps under various blockchain frameworks? A detailed extract from the book is available online from the publisher.

 

Recommended articles

Future Value Chain

Hype meets Reality: New Business Models for 3D Printing

The 3D printing technology is gaining importance in more and more applications compared to conventional production methods. For the chemical industry …

read more
Data & Analytics

Robotic Process Automation III: Generating Data-driven Value

This blog sheds a light on the major challenges for turning data into value by simultaneously pointing out the key role …

read more
Digital Core

RISE with SAP: How to Successfully Move to SAP S/4HANA Cloud

Camelot ITLab has been a certified RISE partner from the very beginning. Find out what is behind RISE with SAP and …

read more

Reimagine your Value Chain with us

Contact us