Blockchain - Writing our first Smart Contract
Tags: blockchain
Published: 2021-12-22 06:58:52

Photo by <a href="https://unsplash.com/@peiobty?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Pierre Borthiry</a> on <a href="https://unsplash.com/s/photos/smart-contracts?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>

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](https://remix.ethereum.org/) 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](https://remix.ethereum.org/), 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7kugfawgqlf85tvu4l2f.png)

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

```solidity
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;
    }
}
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/55kzuu57mwhbut959n52.png)

Now let's understand the code and its functioning, this might feel familiar to you with JavaScript.
![Code explaination](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bx5ucmt4mpex3h87ts0g.png)

Now we can deploy this contract inside remix to do that left nav button which is as below.
![deploy button](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mi4kkmjicj89m8grtwfz.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/etude8cc0pwp992ly8gg.png)
Now you can set some initial value and click on deploy. You will notice a new contract in the below section.
![contract deployed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hbd2gb3yi7twnnp3m660.png)
One more interesting thing to notice here is the amount of ether in the account selected above.
![decreased ether](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qs1sfsin7l8rgnx5cuu0.png)
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.