Blockchain - Writing our first Smart Contract

nikhildhawan

Nikhil Dhawan

Posted on December 22, 2021

Blockchain - Writing our first Smart Contract

Photo by Pierre Borthiry on Unsplash

Hi all, in this article we will start writing our first smart contract based on Ethereum in Solidity. In the last few articles, we discussed blockchain and its various terminology.

So for writing our smart contract we will be using an online code editor Remix with which we can quickly write our contract and test that out in the web browser itself without any local setup. We will also do some local setup in upcoming articles, But in this let us just try to understand important aspects of smart contract and solidity. So if you are familiar with JavaScript it would be easy for you to grasp solidity.
Once you open Remix, you will be able to see the left panel from that you can navigate to contracts and open Ballot. sol file. Sol is the file extension for the solidity contract.
Remix screenshot left Nav

To start with you can copy-paste the below code in the editor and then I will explain this line by line.

pragma solidity ^0.4.17;

contract Inbox{
    string private message;
    function Inbox(string initialmsg) public{
        message=initialmsg;
    }
    function setMsg(string newmsg) public{
        message=newmsg;
    }
    function getMsg() public view returns (string){
        return message;
    }
}
Enter fullscreen mode Exit fullscreen mode

In this we are using solidity version 0.4.17, you can verify if the remix is using the same version of this or not avoid any complication error as per the image below.
version check

Now let's understand the code and its functioning, this might feel familiar to you with JavaScript.
Code explaination

Now we can deploy this contract inside remix to do that left nav button which is as below.
deploy button
Now make sure in this tab JavaScript VM is selected, and you might see you have some 100 test ethers for this environment and make sure our file is selected in contract input.
contract deploy
Now you can set some initial value and click on deploy. You will notice a new contract in the below section.
contract deployed
One more interesting thing to notice here is the amount of ether in the account selected above.
decreased ether
Now with this, we can understand that deploying contracts over the network blockchain involves some charges. YOu can now play around with the contract you deployed by retrieving and setting new messages.
Please let me know in case of any doubts.

💖 💪 🙅 🚩
nikhildhawan
Nikhil Dhawan

Posted on December 22, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related