Let Smart Contracts take care of your Health!


Let Smart Contracts Take Care of your HEALTH

pic credit: stormventures.com

Since the launch of the 8 pages long Bitcoin WhitePaper in 2008, the world has consistently been witnessing the disruption in a plethora of different sectors by Blockchain. Quite fortunately, Blockchain has turned out to be enormously effective than the pre-existing systems in all those sectors.

One such sector that really needs complete transformation by Blockchain is Healthcare.

While this might have been an enormously troublesome task back then but enhancing the effectiveness of the HealthCare sector is undoubtedly achievable with Blockchain.

Therefore, its literally not an exaggeration when I say-

Its time to let Smart Contracts take care of our Health and Medical Insurances.

Just in case you don’t agree, Give it some time and You will.

But WHY… But How?

Now when we talk about transforming an entire sector with Blockchain and Smart Contracts, it’s quite obvious that a plethora of queries might pop up in one’s brain.

If you too are one of those with such pop-ups, let’s dive in and Understand the WHY first.

Buy Best Crypto Deals on CoinCodeCap

Why Smart Contracts and Blockchain?

Chaos in the Health Sector

Truth be told, the HealthCare sector is one of those rare sectors where we can’t afford any sort of chaos. One of the most imperative necessities for the HealthCare sector is to be well organized, transparent and reliable because we have lives at stake.

While one can’t deny the fact that medical data is of utmost importance to any patient, there is also no denial in the fact that the pre-existing system doesn’t ensure a system for proper management and security of this incredibly large data.

Moreover considering the fact that medical data of a patient is not just a single paper but a bundle of several documents, X -Rays, Images, etc, and such crucial data is often found scattered around multiple databases of multiple medical institutions.

At this point, Blockchain with its astonishing characteristics like

  • Immutability
  • Elimination of Central Point of Control
  • Strong Data Integrity
  • Network-wide public accountability etc

has proven to be the most reliable tool that can be used to transform the pre-existing Health Care.

In fact with the InterPlanetary File System(IPFS), storing medical documents, images etc will be much more effective than the way we store now.

Well, this problem with the HealthCare sector only represents the issues with the proper storage system in the industry. However, there still lies some major concern regarding the current HealthCare and medical insurance sector.

The Financial Problem

The Second problem with this sector is the current paradigm of the medical insurance world and financial solutions for the patients.

With the advancement of technology and medical science, it is quite obvious that not every patient can afford the costly medical facilities. Unfortunately, this is the case with the majority of them.

Source: https://www.cnbc.com

According to a CarePayment Research Survey, it was quite clearly found that almost 64% of all the patients in America either avoids or delays any medical care because of the high cost.

While 61% of them are the ones who can’t even think of affording medical care.

And this data remains same or even worse for other countries as well.

While the insurance and donations do help patients to get the best possible medical facility, there still lies an enormously large chain of Middlemen at multiple levels in this procedure.

Moreover, we have to trust a particular central authority or organization and a central database in all such Medical Insurance sector. Thus, there always lies a constant threat of security, financial data manipulation and loss of funds.

This is where Smart Contracts play a significant role as it simply eliminates any reason or need to trust a third party for its execution as well as maintenance or supply of funds.

These contracts not only operate independently but also keeps an immutable record of the participants withdrawing or donating funds while maintaining a control system that limits the accessibility of participants in the network from certain features.

How Smart Contracts can Transform Health Care Sector?

Now, this is indeed an imperative question.

The way smart contracts can completely transform this sector is something that needs much more than just a theoretical explanation.

Therefore let’s expand our boundaries and understand some Solidity code that I wrote to develop a Smart Contract that solves some of the issues with the Health Care sector.

Trust me, It’s not BORING!

Note: In order to understand the codes completely you would need some basic understanding of the Solidity Programming Language.
However, since I will explain the entire code, you can proceed anyway.

Enough of Theories, Let’s Code

Declaring State Variables

State variables in Solidity are those variables that are permanently stored on the Blockchain.

One of the best ways to understand state variables is to perceive them as hard disc or SSD, since they retain their value and space even when the smart contract is not in the execution state.

As it can quite clearly be seen, pidCount, withdrawCount etc are some of the state variables I have defined which will be used later.

What is an Ownable Contract?

The part that needs some proper explanation in this code snippet, lies in line number 3 and 5.

In line no. 3 we are simply importing a more reliable contract called Ownable.sol from OpenZepplin. While in line no. 5, we are simply inheriting from this contract using is keyword.


As discussed earlier, Smart Contracts aims to provide a high level of security to the Health Care security and therefore it ensures that not all the participants in the network have access to every function of the smart contract.

This means that there might be some functions that only the doctors can access while the other functions can only be accessed the donors or patients.

Thus, in order to implement this security layer, we use the Ownable.sol contract which let’s us restrict particular nodes on the network from accessing specific functionalities.

Setting up the Structs

Structs in Solidity are quite similar to Classes in other programming languages.

They are simply used to define the properties a particular entity is supposed to have.

For example, in the snippet above, we have different participants or entities in the network and mentioned the properties they will have.

Using Mappings to store data

Mappings are a way to store data data in Solidity which resembles the Hash Maps.

These are data structures that promotes the key-value pair storage system.

For example, the mapping in line no. 4, i.e., patienList, is stored as a Key-Value pair where the every different address corresponds to different Patients in the network.

What Functions does this Smart Contract provide?

All right! Now comes the most interesting part.

We will now look into the functions that this smart contract can perform.

1.Adding a New Patient in the Blockchain

This functions basically takes a few information about the Patient like name, disease, the insurance amount for the patient as well as a IPFS hash of the images or medical files that will occur once we upload the files on the Interplanetary File System. Read more about IPFS here.

Moreover, this function can only be called by the Owner of the smart contract.

Tasks performed by this function:

  • Generation of Patient’s Unique ID: As you can see in Line no.5, I have used keccak256 which is a hashing algorithm that returns a completely unique hash. This hash becomes the identity of a particular patient on the network.
  • Adding a Insurance Amount: Line no. 9 adds the initial insurance amount(msg.value) to the patient. These are the funds that can later be used for the patient’s medical expanses.
  • Increase the Patient Count: At last, we simply the increase the count of patient by 1, in line no. 12, for every patient that gets added to the Blockchain.

2. Adding A New Doctor

This function is quite similar to the setPatient() function above.

This function is also only accessible by the Owner of the smart contract.

3. Doctor’s Digital Signature

This function plays a major role in the contract as well.

No Doctor is assigned to the Patient at the time of adding a new patient to the Blockchain.

However, this is done after a particular doctor has thoroughly examined the patient’s medical records and calls this function.

Note: This function can only be called by a Doctor in the network.
Doctor’s Digital Signature

As soon as the doctor is satisfied with the documents and signs for the patient’s unique Id, that particular doctor gets assigned to the patients.

This doctor will now have access to the Patient’s funds and he/she will be the only one to withdraw from that patient’s account.

More about the withdrawal function is explained below.

3.Withdrawing Funds from a Patient

This is one of the most imperative section of the smart contract because it deals with the transaction of money in the network.

In simpler terms, the function is designed to just take the patient’s Id and the amount as input and transfer that amount to the doctor’s account.

However, considering the fact that this entire transaction of funds will take place in within the smart contract without any involvement of any third party, this function needs to ensure that there are no security threats during the execution of this function.

Therefore, in order to ensure a secure and reliable transfer of funds we do need to impose certain checkpoints within the function.

Let’s dive in and look at how this function ensures security:

The 3 Major Checkpoints in the function

Before beginning its execution, the function needs to satisfy three major conditions in order to proceed further. Moreover, if any one of these conditions fails, the entire function will stop.

The 3 conditions in line no. 5,6,7 are as follows:

  • Only Doctors: According to this contract, the only participants in the network who can withdraw the patient’s fund are the doctors. Therefore, if anyone else calls this function, the execution stops.
  • Only the Assigned Doctor: Since there might be multiple doctors within the network, it is crucial to ensure that the amount withdrawn from a particular patient’s account can only be done by the doctor assigned to that patient. Otherwise, the execution stops.
  • Available Funds are sufficient: Its also imperative to check that the amount requested for withdrawal by the Doctor is available in the patient’s account.

Once all these three conditions are satisfied, the function proceeds and perform the main task of funds transfer. As line no. 10 to 12 clearly shows, the amount gets deducted from the patient’s account and gets added to the Doctor’s account so that the medical expanses of the patients can be met.

Storing the entire transaction into Blockchain

The function doesn’t stop after the transfer. In fact, as soon as the transaction is complete the function performs a most imperative task.

It then stores this entire withdrawal history(line no. 16), which includes the Doctor who received the funds, the patient whose funds were taken, time of transaction, amount transferred etc, into the Blockchain.

Thus, this function not only ensures a safe transaction but also maintains a proper ledger of the transaction which is immutable and can’t be tampered with in the future.

Donating Funds to Patients

This function is accessible by all the participants in the network because there is no harm in donating funds to the patients who need them. Right?

Tasks performed by this function:

  • Check Donor’s available Balance: At the very beginning, this function ensures that the person donating funds to a patient already has enough of it. Well, that’s a crucial checkpoint. Isn’t it?
  • Transfer of Funds: Once the condition is satisfied, the funds are transferred from the donor to the patient.
  • Storing Donation History: Well, we cannot end this function without storing the entire donation history on the Blockchain.

This is so because

With MONEY, comes possible Security Threats .

Time to Wrap-UP

Smart Contracts literally possess the potential to transform various sectors. Just in case you want to know more about it, Go to this article Why are Smart Contracts engulfing most of the pre-existing Financial Services Industries?

Well, what I covered in this article was just a small demonstration of how Smart Contracts can modify the pre-existing HealthCare sector in ways we can’t imagine.

Moreover, I have connected this smart contract to Front-end with Web3 and created a complete Decentralized Application in Django.

Since there is no deny in the fact that there are still enormous ways we can upgrade this contract and add more features to it.

Therefore, Feel free to contribute on Github to develop this contract further.


Let Smart Contracts take care of your Health! was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.