Writing your first Smart Contract

Akshat Jain
4 min readSep 10, 2019

This is part2 of the continuous series of post about smart contracts , please go and have a read part1 in case you missed it.

Write our own smart contract(unsplash)

In my last post I promised to come back again with regarding to smart contracts and help you write your first one.

Now we know what smart contracts are and how they might work in theory and what problems they can help us solve and and what issues they are facing.

It comes without saying but this post is going to be technicalities behind creating a smart contracts. I’ll mainly be focusing on writing smart contracts using Solidity. Solidity is fairly simple and first choice for writing smart contracts because we want a platform which should be easy to use , debug and test. So let’s begin with defining some key terminologies that is often used in conjunction with smart contracts and what are they .

Key Terminologies

EVM - Ethereum Virtual Machine is a level of abstraction (256 bit stack register) , and is responsible for executing code (smart contracts) in Ethereum network .

Ethereum - open-source global platform for decentralised apps(dApps)

Solidity - Most used programming language for writing smart contracts .

Remix - Online browser based IDE for writing smart contracts in Solidity and deploy them.

Transaction - message that is sent from one account to another.

Gas - each transaction is charged certain gas , which is pretty much the unit of payment to make the execution of smart contracts by EVM . Gas is usually calculated as (gasPrice * gas) before making the transaction.

Since now you know the building blocks of smart contracts we can begin writing our first Simple smart contract.

So the first line states the version of Solidity used , the way its expressed here is to make sure it works with any version from 0.4.24 till 0.5.9 . Why would you want that ? Its because if you remember the blockchains immutable nature and changes cannot be made so to make sure all the version are compatible.

Then we continue to define the Smart Contract called HelloSmartContract first letter should always be capital. Here we have created enum which are user defined type in solidity to describe different state type of the smart contract and declared the variables intent to be used. There are few different types of variable in solidity mainly :

  • number (uint , uint8 … uint256 , int , int8 ..)
  • string (bytes , bytes , bytes8 .. bytes256 , string)
  • address(msg.sender )
  • boolean(true/false)
  • structs
  • enum
  • array
  • mappings

msg.sender contains the address of the user that executes the function. We use memory keyword before any variable if we are creating temporary instance variable. Since we want this smart contract to have different instances of values in every case that in runs thats why we will define the variables in different function call to be temporary so that they set the global variables value as temporary.

First we create sendRequest function to help us send request and verify if the if the requestor is the same as the one who initialised the contract(see the constructor function) msg.sender is the address (usually in form 0x…(forty numbers following 0x)) . In case if it isn’t then revert is called(it can help throw error message or return the gas used if any) . The request message is set as the string request set as functions parameter , similarly the sendRespond function is called by setting Responders address .

Summary

HelloSmartContract application presents a workflow between a user sending a request and a user responding to the request. The state transition diagram will show the interactions between the states.

https://github.com/AkshatJen/blockchain/tree/master/blockchain-workbench/application-and-smart-contract-samples/hello-blockchain

Please go to this link for the full source code : https://github.com/AkshatJen/blockchain/blob/master/blockchain-workbench/application-and-smart-contract-samples/hello-blockchain/HelloBlockchain.sol

I hope you learned something about smart contracts and got a starting point to continue your adventure in blockchain programming.

Remix IDE

To go one step further and run your smart contracts go to Remix , you can find the docs on how to setup , debug and run it. Remix does already comes with ready to go smart contract(ballot.sol) so do play around with it.

Now in case if you’re still not overwhelmed by my post then i do have another source of anxiety to add up (xD), please go here and knock yourself out with all the smart contracts source code examples.

In next part I will discuss another languages available for building smart contracts , also highlight the future that lies ahead.

--

--