How to write your first Solidity smart contract?

How to write your first Solidity smart contract?

ยท

5 min read

Hello folks! Maybe, as you might know, I am learning blockchain development. My current focus is Solidity smart contract development. I decided to make a simple article to show you how to write your first Solidity smart contract. I don't think you will learn a lot. But I want to show how it is simple to start. Maybe that will motivate you to continue to do some interesting things. Who knows? Let's start.

What is Remix IDE?

Starting to develop Solidity smart contracts is very simple. There is an online IDE for that called Remix IDE. You can open it in your browser and start it! You don't need to set up everything on your computer.

Maybe some serious developers use the local environment to build robust smart contracts. However, I don't know a simpler way for beginners than Remix IDE. So, don't waste your time and click on https://remix-project.org/.

Then scroll down and click on that block.

remix ide link

If you even use VSCode, then you will like Remix IDE UI. They are very similar.

remix ide home

My goal is to show you the simplicity of writing a new contract. So, let's go.

Writing smart contract code

Left-click on the contracts folder on the left and choose a new file. Name it MyContract.sol.

create new file in remix ide

Cool, now we should write code. But before we write the first Solidity smart contract, let's check some examples of open contracts which are already in the contracts folder. You could open, for example, 1_Storage.sol.

example of solidity smart contract

There aren't a lot of code lines. It's pretty simple. What could we learn from it?

  1. Each Solidity contract should have a license comment and a Solidity version at the top.
  2. Each contract is very similar to a class in other languages.
  3. The contract could have methods inside.

Let's write our contract now. First copy license and pragma version. Then add an empty contract.

```// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

contract MyContract {

}```

Looks great, but it's empty. Let's add some logic.

```contract MyContract { string name;

function setName(string memory _name) public {
    name = _name;
}

function whatIsName() public view returns(string memory) {
    return name;
}

}```

I will not jump into detail today. The general idea of MyContract is similar to 1_Storage.sol. However, it stores strings instead of integers.

Compiling and Deployment

Press Ctrl + S or CMD + S to compile the contract. Another way is to click that icon and press the button.

remix ide compiler icon

remix ide compile smart contract

If everything is good, you will see the green mark.

Testing

How could we test the contract? In the real world, developers deploy contracts on the blockchain. However, it costs money. Remix IDE provides an environment to deploy contracts and test them for free.

Go to that tab.

remix ide deployment section icon

There are many things, but you don't need to know each one to test your smart contract. Just press the Deploy button and see the magic.

remix ide deployment section

remix ide transactions

remix ide deployed contract

As you can see contract is deployed. Let's test it. Press the arrow on the left and expand the contract details. You can see our methods/functions from our contract.

remix ide interacting with solidity smart contract

Remix IDE provides UI for interacting with our contract. Write something in the input text field and press the "setName" button. You will see some transaction appears in the bottom window. It is similar to transactions when we deploy contracts.

remix ide transactions

Now press the "whatIsName" button. THE MAGIC WILL APPEAR!

remix ide interacting with solidity smart contract

Conclusion

Is it pretty simple? Absolutely! Of course, to develop some complex smart contracts as real developers do, you should learn more about blockchains, crypto, and Solidity language. However, I hope this small note in my blog will inspire you to try and go deeper.

Stay posted if you are interested in more articles about blockchains, crypto, and Solidity.

By the way, did you check my article about how to make a great Upwork profile?

ย