How to write your first Smart Contract with Solidity.

How to write your first Smart Contract with Solidity.

Solidity is the Programming language that is used to code smart contracts. Before we dive into solidity, we need to know what a smart contract is. A smart contract on the blockchain is a self-executing line of codes. They are lines of code that are automatically executed on the blockchain when predetermined terms and conditions are met.

So what does a smart contract do?. An example would be perfect to explain what a smart contract does. If you have to go and purchase a car in the car stores, you'll have to go through several processes and people before buying that car. From identifying your identity to meeting the salesperson, to interacting with the financial broker, etc. To compensate for the work done by these people, you'll have to pay on top of the base price for the car.

Now with a smart contract, you can cut all these processes and streamline the process that involves all these intermediaries because of the lack of trust. A smart contract can be created between you, your bank, the dealer, so once the funds are released to the dealer, the car ownership, would be automatically transferred to you, because the transaction would be recorded in the blockchain, and anyone can share and it can be checked at any time. Some of the benefits of smart contracts are:

  • Speed and Accuracy.

  • Trust.

  • Security.

  • Saves the need for intermediaries.

So back to solidity, in this tutorial, we are going to be creating our first smart contract with solidity. Solidity is the language that is used to code smart contracts on the blockchain. It is a statically typed language, meaning that its variable types are known from the compile-time instead of the run-time. To be able to code solidity, you'll need an IDE called Remix IDEwith this IDE, you can be able to write smart contracts, compile and run smart contracts from your browser. You don't need to download any text editor or terminal to your computer.

Let's get started with creating our file in the IDE Head over to the Remix IDE

Click on the (+) sign on the right side of the screen Remix IDE 1.PNG

We can then name the smart contract we are trying to create. We can call this smart contract myfirstsmartcontract.sol

Remix2.PNG

Click ok, and then you now have a place that you can write your smart contract. Before jumping in to write your first contract, you'll need to install two plugins. These are the plugins that will help you compile and deploy your smart contract to the virtual Blockchain in your Browser.

  • Search for "Solidity Compiler" on the remix plugin search bar as shown below. remix 3.PNG

  • Click on the Activate button to install the plugin.

  • Next, you search for the deployment plugin. Search for "Deploy and run transaction" plugin and click on Activate to install the plugin.

All set. You are now ready to write your first smart contract with solidity, compile and deploy it to the virtual Blockchain in your browser. First, you go to the myfirstsmartcontract.sol file to write your first contract. You then create the structure for your smart contract this way

Myfirst smart contract.PNG pragma solidity ^0.6.0; declares the version of the solidity we are using, in this case, we are using the 0.6.0 version of the solidity language. This is written so that the smart contract is not compilable with a new version of the solidity, where things can behave differently. The next thing we do is to create our smart contract using the contract keyword followed by the name of the contract. Let us name our first contract gamescore. Let us create a smart contract that keeps score in a game. It will store the current score of the game and increase it when we want to increase it, but first, let us create a variable count. Unlike other programming languages like Javascript that declares a variable with the var keyword, in solidity, variables are declared first with their type. So if you want to create a variable that contains a number, you create it by first stating the variable type before the variable name. This is why solidity is known as a statically typed language.

So back to our smart contract. let us create a variable that holds an integer and you do that like this:

Remix 4.PNG This code snippet means

  • A variable has been created, and we named the variable "gamescore".

  • The variable type is unit, uint means unsigned integer, that is number without the negative sign such as 1, 2, 3, 4, etc. The variable value will be stored in the Blockchain. Any variable that is stored in the Blockchain is called a state variable.

Next, we create an initial value for the gamescore, lets say the initial value is 5, we initialize the value with a constructor:

remix 10.PNG

  • A constructor() function in solidity is a special kind of function, that runs whenever the smart contract is created and deployed to the blockchain. it runs just once as the smart contract is created and deployed.

Next, we create a function to fetch the current gamescore. This function can be written as Remix 5.PNG

Let's break down the code The function is declared with the function keyword, and then given the name getgamescore.

  • The Public keyword shows that this function is visible and we can call this function in the interface of the Remix IDE.

  • The view keyword tells solidity that this function is a read-only function.

  • We then specify that the function returns a uint (unsigned integer) as the value of the gamescore when it is called. This function just reads the current game score. Let's create another function that when called increases the game score. we create the function this way:

Remix 6.PNG This function just adds one to the current gamescore when it is called. Great, so our smart contract has been created, it initializes a game score, fetches the current count we initialized it to, and then increases when you want to. Our full Smart contract codes now look like this

remix 8.PNG

The next thing to do is compile the smart contract by clicking on the solidity compilers you installed at the beginning of this tiiutorial.

Remix 9.PNG

  • Click the solidity compiler icon, then set the compiler version to 0.6.12. This is to enable the compiler to compile the smart contract based on the version of the smart contract that you have stated it to run on. To also avoid compile error of lower or previous version.

  • Click on the Compile button and then your smart contract will be compiled.

remix 11.PNG

Next is to deploy the smart contract to the Virtual Blockchain. You can do this by clicking on the deploy transaction plugin Icon you installed at the beginning of this tutorial and then click on the deploy button.

remix 12.PNG Hurray!!!!!!! You have successfully created your first smart contract, compiled and deployed it to the virtual blockchain in the browser. After deploying, scroll down and you will see your deployed contract where you can then interact with the smart contract.

You can interact with your smart contracts by

  • Getting the current gamescore count by calling the function, clicking on the getgamescore button.

  • Increasing the gamescore count by calling on the incrementgamescore function by clicking on the incrementgamescore button.

remix 13.PNG

Hurray!!!!! You have just created your first Smart contract!.

This is part of my 100daysofsolidity coding challenge that is ongoing. You can follow me on Twitter to follow me with more tutorials and also on my journey to becoming a Blockchain developer. Follow me on Twitter