Blockchain - Writing our first Smart Contract
Nikhil Dhawan
Posted on December 22, 2021
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.
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;
}
}
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.
Now let's understand the code and its functioning, this might feel familiar to you with JavaScript.
Now we can deploy this contract inside remix to do that left nav button which is as below.
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.
Now you can set some initial value and click on deploy. You will notice a new contract in the below section.
One more interesting thing to notice here is the amount of ether in the account selected above.
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.
Posted on December 22, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.