Learn Solidity 01: Writing your first Smart Contract

Photo by Zoltan Tasi on Unsplash

I have been fascinated by blockchain technology for so many reasons, which I will not discuss in this article. However, it’s not a new topic that cryptocurrency is the future of money, and NFTs are doing incredibly impressive for digital artists.

So what is my aim with this article? I got interested in the blockchain space, and after doing a lot of research, I couldn’t find enough learning guides like you would, for, say, Data Science which is pretty standard. It would help if you understood that the reason for this is that there is a shortage of Solidity programmers(about 200,000* developers in the world) compared to other popular programming languages. Therefore, there aren’t enough tutorials out there. Hence, I will be writing a series of articles on learning the language, and you can learn from this to become a blockchain developer.

This is the first article of the series, and it will be on writing your first smart contract with Solidity. There are many concepts that you might not be familiar with yet, and that’s perfectly okay. I will do my best to break down each concept into bits as we move along the series, and you can do more research from there.

To get started, here are a few questions that you should be asking; What is a Smart Contract? What is Solidity? How does the blockchain work?

Follow along as I provide answers to these questions below.

What is a Smart Contract?

A “Smart Contract” is simply a program that runs on the Ethereum blockchain. It is a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereum blockchain. Think of a smart contract as an account on the Ethereum network, which can not be controlled by the user and runs as a program. However, a user can interact with a smart contract by submitting transactions that execute a function defined on the smart contract.

The way smart contracts ensure that transactions occur in real life is much more complex, but here is a simple explanation to understand how things work.

Photo by Cedrik Wesche on Unsplash

Imagine that this article is for sale, and I am the seller, you the reader is the buyer, and you are buying this article by using an affiliate link you saw on the internet. We can deploy a smart contract for this transaction, and here’s how the exchange might occur.

You, the reader, creates a transaction that sends the price of this article to the seller in exchange for a copy of this article. Like you would usually in a normal bank transaction, you will provide my address as an input to the smart contract’s address. The smart contract will then carry out this transaction by verifying that you have enough money in your wallet and that I also have a copy of this article ready for sale. Then, it deducts the money from your wallet, sends it to my wallet, tells me to send you a copy of the article, and finally sends the affiliate commission to the owner of the affiliate link after deducting that from my wallet.

The above is how smart contracts can facilitate transactions between users who do not trust each other, and it is used in the real world to carry out much more complex transactions.

This leads us to the following question: What is Solidity?

What is Solidity?

We already established the fact that Smart Contracts are software programs. For Ethereum, you can write smart contracts using a statically-typed language called Solidity, which is the most widely used smart contract programming language at the moment. Solidity is a full-featured language for writing general-purpose smart contracts, and it looks like JavaScript.

How does the blockchain work?

To answer the last question, I think it’s an important concept to know. I will share a good video by Anders Brownworth, it explains the concept: How does the blockchain work?

Watch here; an elementary visual introduction to the concepts behind how the blockchain works.

Now that you have a good understanding of the concepts above let’s see how you can write your first smart contract.

Step 1 — Setup Remix IDE

1.1 — The first step is setting up the Remix IDE. Remix IDE is a browser-based development environment for Smart Contracts. The IDE provides several plugins and compilers for different solidity versions.

To launch the Remix IDE, visit http://remix.ethereum.org; you will see the page below, which might be different depending on when you are reading this article.

Remix Home Page

Notice that in the image above, you can see “Featured Plugins” because the Remix IDE is built with a pluggable architecture, which means that all the operations are performed using plugins.

1.2 — The next thing to do is to enable the Solidity compiler plugins, as shown below.

Enable Plugins

1c — After enabling the plugin, click on the “Solidity compiler” button, and select the compiler configuration shown in the image below.

Compiler Configuration

It is noteworthy to mention that compiler versions in Solidity are tricky to work with, and the advice is that you should try not to work with the most recent version. Interestingly, you will find some legacy projects still running on older solidity versions, and this is okay. In summary, try to stick with the version you see in a particular tutorial if you are a beginner.

In the image above, you will see that we are working with compiler version 0.8.1, which might not be the recent version. Also, you can set the compiler version in the Solidity source file by using the “pragma” line at the beginning of the file. The Pragma line is just a way of telling the compiler the version you are working with.

Now that everything is all set let’s write our first smart contract in the next step.

Step 2 — Write your first Smart Contract

Like any other programming language, the first step is creating a project file for the language. Here, you will create a file titled, “MyFirstSmartContract.sol”. The “sol” file extension stands for Solidity, just like “py” is for Python.

See the images below to create this file in the Remix IDE.

2.1 — Create Workspace

Create workspace

2.2 — Create the file; MyFirstSmartContract.sol

Create a file

2.3 — Write the code

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.1;
contract MyFirstContract {
string public myString = “Hello World”;

Part 1

`// SPDX-License-Identifier: GPL-3.0` — Is the Software Package Data Exchange® (SPDX®) identification used to specify the license under which the Solidity file will be distributed. It is recommended that it is included in your code, and the compiler will throw a warning message if it is missing from your file.

Part 2

pragma solidity ^0.8.1, the ^0.8.1 means version between 0.8.1 and 0.9.0, inclusive of 0.8.1 but not 0.9.0. In other words >=0.8.1 and <0.9.0.

The pragma keyword instructs the compiler to enable or check specific features. Finally, the version pragma is a mechanism that informs the compiler about the Solidity file’s compiler version.

Part 3

contract MyFirstContract is the beginning of the code, just like the Class keyword in the Python programming language. The “myString” is a public storage variable to hold “Hello World.” You will learn more about this as we proceed through the series.

Part 4

Notice that the compiler displayed a green icon, and this signifies that the compilation is successful because you enabled auto-compile in the previous section.

Congratulations 🎉🎉

So there you have it; Congratulations, you just wrote your first smart contract. In the next article, we will discuss how you can deploy this smart contract to an actual blockchain.

* — Estimated figure

Up next …

If you don’t want to miss the next article, consider signing up for my newsletter, and you will receive a notification in your email when the article is ready. Remember, these article series aim to break down complex concepts in blockchain development, especially the Ethereum network.

Get to Know me

I write about exciting topics in Data Science, Software Development, and Blockchain Tech.

Follow me on my social media for more updates; Twitter and LinkedIn. Also, don’t hesitate to write me an email for more information.

Learn Solidity 01: Writing your first Smart Contract was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.