Hands-On Blockchain Smart Contracts
By: Rajesh Shah
Tags: Gift Cards, Remix, Blockchain, Smart Contracts, Solidity

Introduction

This article assumes the reader has a basic understanding of Blockchain protocol and platforms like “Hyperledger” and “Ethereum”. The article focuses on understanding Smart Contracts and a walk through the basics of writing a simple Smart Contract. We will use a Gift Card example to understand and create a Smart Contract. Think of Smart Contract as defining a Class in programming languages like Java or Python. When we define a Class it has attributes and functions/methods. When an object is created from that Class it has specific attribute values, which is called the state of the Object. Functions are used to read or update the state of the Object. Now, let’s apply the same analogy to the idea of Smart Contracts, in order to think of them as a Class. When a Smart Contract is deployed to a Blockchain platform, it is like one unique Object is created as part of that transaction. From that point onward, any read or update to Smart Contract Object is recorded as an unique transaction on the Blockchain Platform. Each Blockchain transaction is identified by a unique cryptographic hash value and is immutable.

Simple Gift Card Scenario

Consider a fake company “GC Inc” issuing Gift Cards in the denominations of $50. When a Gift Card is issued a Smart Contract is deployed as a Block on the Blockchain Platform like “Hyperledger” or “Ethereum”. Every transaction conducted on the Gift Card is recorded as a new Blockchain transaction. Let’s assume a gift card of $50 with the following code -”ABC-PQR-XYZ” is issued to a customer on 03/31/2020. The customer spends the $50 in the following four transactions:

$20 on 04/01/2020

$10 on 04/15/2020

$5 on 04/30/2020

$15 on 05/04/2020

This will create the following chain of Blocks(Transactions) as shown in the below diagram.

Define a Smart Contract

Smart Contract for the Simple Gift Card Scenario will have the following attributes and functions:

code: attribute for the unique code associated with Gift Card

balance: attribute for the balance left on Gift Card

transactions: attribute for collection of transactions recorded on the Gift Card

new(balance,code): constructor to create a new object of the Contract. For this example values will be 50 and “ABC-PQR-XYZ”

redeem(amount,date): function to redeem Gift Card for supplied amount and date. This function will throw an error if the balance is less than the redeem amount

balance(): function to return current balance on the Gift Card

transactions(): functions to return all transactions recorded on the Gift Card.

Blockchain transactions need a digital identity for the “GC Inc” and the “Consumer” . For this example, we will not focus on digital identity part of the Blockchain framework.

Write Smart Contract using Solidity

“Solidity” and “Vyper” are two popular languages for writing Blockchain Smart Contracts. For this example, let’s use “Solidity”, a contract in Solidity will look something as below:

pragma solidity >=0.4.22 <0.7.0;

pragma experimental ABIEncoderV2; /**

* Smart Contract for Gift Card Issued By GC Inc

*

*/

contract GCIncGiftCardContract { uint256 balance;// Gift Card Balance

string code;//Gift Card Code

string[] transactions; // Transactions recorded on Git Card



/**

* constructor to issue a new Gift Card

*/

constructor(uint256 _balance,string memory _code) public {

balance = _balance;

code=_code;

}



/**

* function to redeem Gift Card with amount and record transactions

*/

function redeeem(uint256 amount,string memory description) public {

assert(balance >= amount); //throw error if the balance is less

balance=balance-amount;

transactions.push(description);





} /**

* function to return current balance on the Gift Card

*/

function getBalance() public view returns (uint256){

return balance;

}



/**

* function to return current transactions on the Gift Card

*/

function gettransactions() public view returns (string[] memory){

return transactions;

}



}

Deploy/Test Smart Contract with Remix IDE

Remix provides a web-based IDE to build, deploy and test Smart Contracts. Open this URL https://remix.ethereum.org/ in a Chrome or Firefox Browser.

Step 1: Select “Solidity” environment on the home page of Remix

Step 2: Copy the above code as a new file “GCIncGiftCardContract.sol”

Step 3: Compile the Contract — “GCIncGiftCardContract.sol”

Step 4: Deploy the Contract — “GCIncGiftCardContract.sol” with values “_balance=50” and “_code=ABC-PQR-XYZ”. Select default values for other parameters on this screen.

Step 5: Add four “redeem” transactions to the Contract — “GCIncGiftCardContract.sol” as listed in the section “Simple Gift Card Scenario”

Step 5: Check the Balance and Transactions — “GCIncGiftCardContract.sol”. The balance will be 0 and 4 transactions listed as below. If we try to add any more “reedem” transactions it should fail as the balance is zero now.

Final Thoughts

This is a very simple use case to explain the basic concepts of Blockchain Smart Contracts. This is rapidly expanding technology with a large community of developers supporting the movement. We all know the virtual currency, BitCoin, is the most popular use of this technology. We are starting to see other real-life business applications of this technology as it continues to grow. “Hyperledger” and “Ethereum” are the two most popular Blockchain platforms with extensive tooling and industry-wide support.

I think this is an exciting time to be part of this technology! Enjoy!

Disclaimer: This is a personal blog. The opinions expressed here are my own and not those of my current or any previous employers.