Solidity Tutorial 1 — The Patient Smart Contract

Tejas Nikumbh
4 min readFeb 7, 2018

--

If you have not checked out the previous tutorial, I suggest you go through it first. It’s a nice simple start to the series!

This tutorial is about the general skeleton of a solidity program, explained using the example of a smart contract. We will explain in depth about the various components that we ignored in the intro, such as pragma, comments and other semantics.

We will also explore briefly core components, such as functions, state variables, function modifiers and so on. If it looks like too much, don’t worry, it’s not. We will explore this in much more detail in later tutorials.

The Patient Smart Contract

Here is the code snippet we will dissect. For the purpose of this tutorial we will define a smart contract for a Patient Record, for storing his/her medical record.

pragma solidity ^0.4.19;

/** @title PatientRecord */
contract PatientRecord {

// ** Part 1 ** Enums **
enum Gender { Male, Female }

// ** Part 2 ** Structs **
struct Patient {
bytes32 name;
uint age;
Gender gender;
}

// ** Part 3 ** State Variables **
uint id; //Id for the patient record
Patient private patient; // The patient we are referring to
address public recordOwner; //The address of the owner
// ** Part 4 ** Events ** // These can be triggered for a JS script in front end
event PatientNameAccessed(address sender);

// ** Part 5 ** Modifiers **
// Like a protocol that a function should follow
modifier onlyOwner() {
require(msg.sender == recordOwner);
_;
}

// ** Part 6 ** Functions **
function PatientRecord() public {
recordOwner = msg.sender;
}

function getPatientName() public returns (bytes32) {
PatientNameAccessed(msg.sender);// Triggering the event
return patient.name;
}

// Note that the function has the onlyOwner Modifier
function setPatientName(bytes32 name) public onlyOwner {
patient.name = name;
}

}

Before we start: Pragma and Comments

Before we begin, I want to introduce two important concepts, regarding pragma marks and comments. The latter one is pretty simple and ports from other languages.

Regarding the pragma (Line 1), it indicates to the compiler that the solidity version that we are using is 0.4.19. If the compiler has a version older than this, the program will not compile. Also, if the version is of the format 0.5.x, the program will not compile. The ^ symbol makes sure of that. This is done because any solidity version release beyond 0.5.x or more is not backwards compatible by design of the language.

Comments are simple, // or /**/for a single line comment, and /* *— — */ for multiline comments. Also, comments are compatible with the doxygen format, which makes it easy to produce documentation if your code is well commented.

Components of the Smart Contract

Enums and Structs

Let’s start with the simplest first. Part 1 describes an enum used for declaring a gender of the patient. Declaring an enum is pretty straightforward as can be seen. Part 2 describes a struct. This is common in other languages like c++ and structs serve a similar purpose here as well. It’s a simple construct used to define a patient.

State Variables

Part 3 describes the state variables. A state variable simply records the state of a particular record for a patient. We have 3 state variables here, a unique id, the patient for the record and the state variable for the address of the smart contract which owns this patient record.

Whenever a smart contract is created, its constructor is called, which is the first function in our Part 6. When called, this function assigns the creating smart contract’s address to recordOwner, a state variable which we can use later.

Events

Part 4 describes events. These are essentially callbacks that you use in your solidity code to inform the front end JS code about something that has happened in your code. We simply define an interface for an event here, and this event can be listened to by the JS code.

We have an event PatientNameAccessed here.This event is triggered in the function getPatientName. This event passes the address of the caller to the event. The JS code can listen to this event and retrieve the address from which the patient name was accessed. We can use this property to show alerts on the front end if the address is not of someone who can access patient name and modify security accordingly.

Modifiers

Part 5 describes modifiers. Think of modifiers as protocols that a particular function follows, when declared accordingly. Our setPatientName function follows the onlyOwner modifier, which implies that it has to pass the test set by the onlyOwner modifier first. In this case, it ensures that the address that is modifying the patient name is that of the recordOwner only. Here is where the recordOwner state variable comes to use.

Functions

Part 6 describes functions, which are simply constructors and getter and setters here. For now we only use public functions, and the semantics of function declaration are left as an exercise. You could also try making getter and setter for other patient properties!

That’s it! You’ve learnt about the structure of a contract. I’m thinking about building out projects straight away, or will perhaps will go a bit more into solidity in further details. What do you want? Comment below! Don’t forget to follow! It motivates me to do better. Here’s the next tutorial on building your own cryptocurrency token!

Stay updated

Join us on Telegram : https://t.me/joinchat/G4Ls4hLyjTLLNke_3QGygQ

Join the ebook email-list! : https://goo.gl/FtRCuX

Ebook

Liked it? Want to explore more? If you’re just getting started, my ebook — “Blockchain programming for absolute beginners” is a perfect start. Have a look at it’s contents here!

--

--