Experiment #101 How To Create a Blockchain Workbench App

In our Experiment 101# we have implemented an IoT  device with BlockChain . In this post learn how to create a Blockchain Worbench App.

 

  1. Add a new Azure Blockchain Workbench

An Azure Blockchain Workbench is not a unique resource that we can add to our resource group.

 

It is a whole container with certain preset and linked resources.

 

Wait for Blockchain Workbench to deploy. This process can take from 60 to 90 minutes to complete.

 

  1. Configure the Azure App Service registration

The App Service resource gives us the URL to start with the Blockchain Workbench Web Client set up.

 

 

Follow all the steps to configure the Azure Blockchain Workbench application registration.

 

 

Now, we are ready to build a new application accessing to the mentioned URL.

 

  1. Create a Solidity (.sol) Blockchain Smart Contract

Specify the Solidity version:

pragma solidity >=0.4.25 <0.6.0;

Begin the Smart Contract to fill it:

contract OrganTransportApp { }

Define the donation states:

enum StateType {Creating, Created, Ready, InTransit, Monitoring, Completed, Spoiled} 

Create the donation state property:

StateType public DonationState;

Add the participating agents:

address public DonationCenter;
address public OrganSender;
address public TransportVehicle;
address public TelemetryDevice;
address public OrganReceiver;

Add the temperature threshold properties:

int public MinTemperature;
int public MaxTemperature;

Create the real-time temperature reading property:

int public SensorTemperature;

Add a property to store donated organ information:

string public DonationInfo;

Define the Organ Transport App Smart Contract constructor:

Constructor (address sending, address vehicle, address device, address receiving, 
             string memory donation, int max, int min) public {
    DonationCenter = msg.sender;
    OrganSender = sending;
    TransportVehicle = vehicle;
    TelemetryDevice = device;
    OrganReceiver = receiving;
    MinTemperature = min;
    MaxTemperature = max;
    DonationInfo = donation;
    DonationState = StateType.Created;
    if (MinTemperature >= MaxTemperature) {
        revert();
    }
}

The donation center is who starts the process by creating the Smart Contract and adding all needed information about the donation.

Create a function to report that the organ is ready in the sending hospital:

function OrganReady() public {
    if (DonorHospital != msg.sender || DonationState != StateType.Created) {
        revert();
    }
    DonationState = StateType.Ready;
}

Create a function to report that the organ is inside the transport vehicle:

function OrganInTransit() public {
    if (TransportVehicle != msg.sender || 
        DonationState != StateType.Ready) {
        revert();
    }
    DonationState = StateType.InTransit;
}

Create a function to report that the organ has arrived at the receiving hospital:

function OrganArrived() public
{
    if (OrganReceiver != msg.sender || 
        DonationState != StateType.Monitoring) {
        revert();
    }
    DonationState = StateType.Completed;
}

Add a function to check the temperature inside the organ transport fridge:

function TemperatureCheck(int temperature) public {
    if ( TransportVehicle != msg.sender || 
        (DonationState != StateType.InTransit &&
         DonationState != StateType.Monitoring) ) {
        revert();
    } else {
        if (temperature > MaxTemperature || 
            temperature < MinTemperature) {
            DonationState = StateType.Spoiled;
        }
        SensorTemperature = temperature;
        if (DonationState == StateType.InTransit) {
            DonationState = StateType.Monitoring;
        }
    }
}
  1. Create a JSON Metadata Configuration File

Define the Smart Contract object in a JSON format:

{
    "ApplicationName": "OrganTransportApp",
    "DisplayName": "Organ Transport Application",
    "ApplicationRoles": [ {“Name": "Admin”}, {"Name": "User”} ],
    "Workflows": [ {
            "Initiators": [ "Admin" ],
            "StartState": "Creating",
            "Properties": [ ],
            "Constructor": { "Parameters": [ ] },
            "Functions": [ ],
            "States": [ ]
    } ]
}

Add metadata for all the Properties of the object:

{
    "Name": "DonationState",
    "Type": { "Name": "state" }
}, {
    "Name": "DonationCenter",
    "Type": { "Name": "user" }
}, {
    "Name": "OrganSender",
    "Type": { "Name": "user" }
}, {
    "Name": "TransportVehicle",
    "Type": { "Name": "user" }
}, {
    "Name": "TelemetryDevice",
    "Type": { "Name": "user" }
}, {
    "Name": "OrganReceiver",
    "Type": { "Name": "user" }
}, {
    "Name": "MinTemperature",
    "Type": { "Name": "int" }
}, {
    "Name": "MaxTemperature",
    "Type": { "Name": "int" }
}, {
    "Name": "SensorTemperature",
    "Type": { "Name": "int" }
}, {
    "Name": "DonationInfo",
    "Type":{ "Name": "string" }
}

Define the Constructor parameters for the Smart:

{
    "Name": "sending",
    "DisplayName": "Organ Sender",
    "Type": { "Name": "user" } 
}, {
    "Name": "vehicle",
    "DisplayName": "Transport Vehicle",
    "Type": { "Name": "user" }
}, {
    "Name": "device",
    "DisplayName": "Telemetry Device",
    "Type": { "Name": "user" } 
}, {
    "Name": "receiving",
    "DisplayName": "Organ Receiver",
    "Type": { "Name": "user" } 
}, {
    "Name": "donation",
    "DisplayName": "Donation",
    "Type": { "Name": "string" }

}, {
    "Name": "min",
    "DisplayName": "Min Temperature",
    "Type": { "Name": "int" } 
} , {
    "Name": "max",
    "DisplayName": "Max Temperature",
    "Type": { "Name": "int" } 
}

Add metadata to define the States of the Smart Contract:

{
    "Name": "Creating",
    "PercentComplete": 0,
    "Value": 0,
    "Style": "Success",
    "Transitions": []
}, {
    "Name": "Created",
    "PercentComplete": 20,
    "Value": 1,
    "Style": "Success",
    "Transitions": [ {
            "AllowedRoles": [ "Admin", "User" ],
            "AllowedInstanceRoles": [ "OrganSender" ],
            "Function": "OrganReady",
            "NextStates": [ "Ready" ]
        } ]
}, {
    "Name": "Ready",
    "PercentComplete": 40,
    "Value": 2,
    "Style": "Success",
    "Transitions": [ {
            "AllowedRoles": [ "Admin", "User" ],
            "AllowedInstanceRoles": [ "TransportVehicle" ],
            "Function": "OrganInTransit",
            "NextStates": [ "InTransit" ]
        } ]
}, {
    "Name": "InTransit",
    "PercentComplete": 60,
    "Value": 3,
    "Style": "Success",
    "Transitions": [ {
        "AllowedRoles": [ "Admin", "User" ],
        "AllowedInstanceRoles": [ "TelemetryDevice" ],
        "Function": "TemperatureCheck",
        "NextStates": [ "Monitoring", "Spoiled " ]
    }, {
    "Name": "Monitoring",
    "PercentComplete": 80,
    "Value": 4,
    "Style": "Success",
    "Transitions": [ {
        "AllowedRoles": [ "Admin", "User" ],
        "AllowedInstanceRoles": [ "OrganReceiver" ],
        "Function": "OrganArrived",
        "NextStates": [ "Completed" ]
    }, {
        "AllowedRoles": [ "Admin", "User" ],
        "AllowedInstanceRoles": [ "TelemetryDevice" ],
        "Function": "TemperatureCheck",
        "NextStates": [ "Monitoring", "Spoiled " ]
    } ]
}, {
    "Name": "Completed",
    "PercentComplete": 100,
    "Value": 5,
    "Style": "Success",
    "Transitions": []
}, {
    "Name": "Spoiled",
    "PercentComplete": 100,
    "Value": 6,
    "Style": "Failure",
    "Transitions": []
}

The process jumps from a State to another through a Function that can only be run by certain kind of role. This is called ‘Transition’.

 

  1. Build the Blockchain Workbench Application

Create a new Blockchain Workbench application by accessing to the app service URL and clicking the ‘New’ button.

 

 

Upload the JSON metadata configuration file and the Solidity Blockchain Smart Contract. Finally, click on the ‘Deploy’ button.

 

 

Once the Blockchain Workbench application is deployed, you can start adding members to it and start creating contracts and blocks.

 

Check the demo video in our Youtube channel 

 

 

Official links:

Microsoft Cloud Workshop Azure Blockchain HoL


Stay up to date!