Write your first Smart Contract using Solidity

Ajith M
Coinmonks
4 min readMar 8, 2022

--

Before writing our first contract, let me give a quick intro about Smart Contracts and Solidity.

What is a Smart Contract?

A “smart contract” is simply a program that runs on the Ethereum blockchain. It’s a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereum blockchain.

What is Solidity?

Solidity is an object-oriented programming language for writing smart contracts. If you’re experienced with Python or any curly- bracket language, you can find a language with familiar syntax.

Let's write some Code!!

I will be using Remix IDE for developing the contracts. If you are a beginner I advise you to use the same.

We are going to write a Hello World program in Solidity.

First, under the contracts folder, create a new file called ‘HelloWorld.sol’. Make sure you saved the file with ‘.sol’ at the end of the file name. It looks like this after creating the file.

Let's Open the file, and start writing the code.

On top of the file, the solidity version should be mentioned, I am using v0.8.0. To avoid conflicts, use the same version as mine.

To create a contract we use the keyword ‘contract’. The contract keyword is followed by the name of the contract. All the contract code such as variables, functions, modifiers, etc. is enclosed inside the curly brackets. This syntax looks similar to a class in JavaScript.

We are going to declare a state variable inside the contract named ‘value’. You can name it as you like.

As you can see, it says

Let's break down this line of code. ‘string’ is the type of variable, ‘public/private is the access modifier, ‘value’ is the variable name. So, the syntax for declaring a variable is:

Now, We need to assign a string to the variable when the contract is created. Like many other programming languages, Solidity also has a ‘constructor’ properly. A constructor code is executed once a contract is created and is used to initialize the contract state. Similar to a class, a contract can have only one constructor.

We have to pass in a string to the constructor to initialize the ‘value’ variable, here we are passing a string named initialValue.

What is the ‘memory’ keyword in between the data type ‘string’ and the variable name ‘initialValue’? The keyword ‘memory’, is used to hold temporary values. It is erased between function calls.

So, the syntax for passing array, struct, or mapping types in a function is:(String is basically an array of characters)

We are assigning the ‘initialValue’ to ‘value’ using the sign ‘=’.

Finally, we need to create a function to change the value after the contract has been created. Let's name the function ‘changeValue’.

To declare a function we use the keyword ‘function’.

Hope you understood the passing of the data to the function.

What is the ‘public’ keyword doing in a function declaration?

We have to either declare a function ‘public’ or ‘private’. The ‘public’ indicates that the function can be called from outside the contract, and the functions that are declared ‘private’ can not be called from outside the contract.

You have now successfully written your first Smart Contract!! 🤩🤩

Now let's compile the solidity code. Select the respective compiler version and hit compile. Usually, in Remix IDE the auto compile is enabled. So, the code automatically gets compiled when you save the contract.

Now we have the compiled code, to test the contract we have to deploy it. Make sure that ‘JavaScript VM’ is selected in the environment.

Before deploying the contract we have to send a value to the constructor which we created. So, next to deploy, type Hello World and deploy the contract. You can see the deployed contracts below.

Remix IDE allows us to interact with the smart contract functions using buttons. This is a cool feature that Remix provides.

Now click on the ‘value’ button: It should display the data we send while creating the contract, i.e Hello World.

You can update the value by using the ‘changeValue’ button along with the data and then use the ‘value’ button to check if the value has changed.

Yes, it has!! Our Smart Contract works fine.

Thanks for reading this article ❤

To learn more about Blockchain and Smart Contracts, visit ontheether.com.

Comment on the topic of Blockchain you would like to learn next.

Clap 👏 If this article helps you.

Connect with me on LinkedIn and GitHub.

--

--