Our Blog

How to code the blockchain - a beginners’ guide

How to code the blockchain - a beginners’ guide

By now, most of you probably have a good idea of what the blockchain is. Furthermore, if you are living in Malta, the chances are that you have heard the country referred to as the “blockchain island” as the government has positioned it as a hub for all things crypto and blockchain related. But whilst many of us have a sort of basic grasp on what it is, not many know how to build applications on it.

For those that are not in the know, but want to be in the know, here is the Finerton guide to coding the blockchain.

 

How does the blockchain work?

Every 10 minutes, blockchain transactions are grouped together in what is called a ‘block’. This block is then linked back to the block before it, creating a continuous blockchain. When we ‘mine’ a block, a block is confirmed and this process involves computers in a network, working to solve a mathematical problem.

As soon as a block is mined and added to the network, this is updated across the entire network and becomes impossible to edit, amend or delete in any way.

 

Smart Contracts

Smart contracts allow us to replace the middleman with an autonomous process or system which is facilitated through programming. Bitcoin was a bit hard for people to code autonomous systems on, so instead, Ethereum was born. Instead of providing another form of decentralised cryptocurrency. Ethereum allowed the creation of a network of computers which can all host code in the form of smart contracts.

A smart contract is a digital contract that can have conditions programmed into it. These conditions include requirements, standards, prerequisites, and even types of data. Each step of the contract needs the previous step to be satisfied before the subsequent step or action can be executed.

Here is an example of how to code a simple smart contract that can store and retrieve test scores of high school students.

Using solidity, the first line of code tells the compiler which version of solidity is being used. After this has been established, the contract test scores can be established. In order to store both the students name and their scores, an array needs to be created to store the names, and an associative array for grades.

Then, constructor must be created in order to send the contract a list of student names.

When using solidity, the constructor will only be called once and student names will be an argument that are stored in the student list array. Now, in order to continue another function must be written to assign students with a grade as well as one to determine whether the student is valid or not.

The “giveGradeToStudent” function takes on two different arguments; the name of the student and of course, the grade. The require function then has to check if the “ValidStudent” function is true or false- if false, the execution is cancelled.

Lastly, a function needs to be created to fetch the score of each student. The “getGradeForStudent” function considers the student name as an argument and then returns the score from the associative array.

If you are trying out this tutorial yourself, feel free to deploy this on a personal blockchain created with Grenache. Once you have installed and ran Ganache, on a new terminal you can deploy your smart contract. Don’t forget however that deploying on the blockchain will cost you “gas” and you need to specify how much you are willing to allocate to those who are happy to use their computational power for you. You can do this by using a gas calculator and you will only have to pay when the contract is deployed on the public Ethereum blockchain.

See? This whole blockchain thing, is not as complicated as you might think. With blockchain skills in big demand in the tech world, getting to grips with coding for blockchain and smart contracts might not be such a bad idea!

 

Credits:

Bloggers: Finerton News Team
Images: Unsplash.com

Last modified on: February 25, 2019

6 comments

Post A Comment