How To Write Simple Solidity Smart Contracts For The Ethereum Blockchain Using Remix IDE, For Complete Beginners- Part One

George Gbenle
6 min readJul 18, 2021

This article was first published on codedli.com

Note: Throughout this post, I referred to “the blockchain”. Anywhere you see this in the post, I’m referring to the Ethereum Blockchain.

The Remix IDE has become one of the most helpful tools for quick troubleshooting and debugging smart contracts. It offers an interactive interface and provides immediate feedback about your smart contract. This is an over-simplistic use case of Remix. It has more cool stuff on offer than you can imagine.

In today’s post, I’ll attempt to kill two birds with one stone as well as also try to keep the post length relatively short.

Bird One: Smart Contracts

We’ll write a simple smart contract. We’ll dismantle the smart contract into different parts to understand the general structure of a typical smart contract. This way, you wouldn’t need to memorize anything.

Bird Two: Remix IDE

We’ll use the Remix IDE while killing bird one above. This way, we can appreciate some of the cool features Remix has to offer that you normally don’t get to enjoy with your code editors.

Let’s get to business!

Step 1:

Head over to https://remix.ethereum.org/ . You should see something like the image below with the exception of few personal files I created.

You could start by creating a new file inside the contract directory. We’ll name our contract file Name.sol. Every solidity file has to be appended with .sol . This way, your code editor would understand it’s dealing with a solidity file.

Next, copy and paste the code below into the Name.sol contract file you’ve just created. Don’t worry, we’ll break everything down later in this post.

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

contract Name {
string public name;

constructor ( string memory initialName) {
name = initialName;
}

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

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

The above code is a simple smart contract that when deployed to the blockchain does nothing other than letting the user set a new name and get the name stored in memory. Your file should now look like the image below.

The compiler icon, highlighted in red circle to the left of the window should be ticked in green. This means that your smart contract could be compiled without any error. Make sure your code is as shown above. In the next section, we’ll explore the features of Remix and touch on how to navigate through these errors.

Now, let’s break our smart contract down into pieces and try to understand each component.

Line 1: SPDX-License-Identifier

// SPDX-License-Identifier: MIT

This is not the most important part of your code. However, when you try to remove this line, Remix immediately throws you a warning (You could see this warning by placing your cursor on the compiler icon as shown in the image above). You need to specify your contract’s SPDX-License-Identifier. In this case, we’re using MIT.

Note: Your contract would still be compiled and deployed to the blockchain with or without it.

Line 2: Solidity Version

pragma solidity >=0.4.22 <0.9.0;

In this line of code, we’re specifying the version of solidity we’re using. I have decided to use a range of between versions 0.4.22 to 0.9.0. You could decide to use a specific version, say 0.5.0, or any other version if you so wish. If you do decide to use a specific version, remember to change the version of your compiler to soothe your solidity version as well (To be addressed in the next section). Otherwise, you’ll receive some output errors.

Line 4: Contract Name

contract Name {}

This is the contract itself that houses all your contract functions. You could give it any name you desire as long as you follow naming conventions. The contract name should demonstrate what your contract is about. In this case, we’re naming our contract Name because it allows users to set new names and get the stored names. Think of your contract as your regular JavaScript class.

Line 5: Variable declaration

string public name;

If you’re familiar with other contemporary programming languages, you’ll notice the marked difference between this variable declaration and that of those other languages. Solidity is a strongly typed programming language. We specified the data type string and marked its accessibility public for anyone to be able to access it. And of course, the name of the variable is name.

Line 7–9: Constructor

Recall when I said you should think of your contract as a regular JavaScript class. Now, here you have it, the constructor that instantiates the class upon the creation of a new instance. Whenever an instance of your smart contract is created, this is the function that gets called.

constructor ( string memory initialName) {
name = initialName;
}

However, there are some marked differences between your regular class constructor and your contract’s constructor (as well as your functions, as we’ll see later).

This constructor takes on a parameter of initialName, but in the typical Solidity style, we had to specify the data type string and the data storage location of either storage or memory. In this case, we’ve chosen memory.

Lines 11–17 : Smart Contract Functions

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

Remember your class method? Yeah, this is somewhat the equivalence of your class method. By now, you should be familiar with what string, memory, and public are. The setName smart contract function is taking newName as a parameter and setting it to the name stored in memory. There are other data types (as you can imagine) and function types in Solidity. Please see below other types of function declaration in Solidity:

Types of Solidity Function Declaration

Below are the most common types of solidity function declaration types that you’ll run into in the wild. There are other less common types, but considering the scope of this post (Beginner friendly), these ones should do. You could easily infer what these function types mean just by the names.

  1. Public
  2. Private
  3. View
  4. Pure
  5. Payable

The second function, getName, just retrieves whatever name is stored in memory. It takes no parameter, it’s also public (as you’ve guessed, it can be called by anyone) and view means it cannot update the name variable of the smart contract.

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

One other interesting thing to point out is the returns keyword in the code. This is different from the return keyword used in the next line. Because this function is a read-only function, we needed to specify the type of data it should return. This is another major difference between Solidity and the other contemporary programming languages out there.

There you have it, a simple smart contract. But you’ve not seen this contract in action on the blockchain. We need to deploy it and interact with it on the blockchain. Remix offers an exciting user interface to do all these.

This post is getting too long for my style. Just so you and I don’t get bored with it, I’ll break it down into two parts. In the next part, we’ll deploy our Name smart contract to the blockchain. In the process, we’ll see the cool features of Remix and interact with our smart contract directly.

After that, we’ll notice a simple defect in the contract. We’ll then refactor our smart contract and deploy another instance of it to the blockchain.

Conclusion

That’s it for part one. In this post, we wrote a simple Name Smart Contract and dismantled it in order to understand its different components using the Remix online IDE.

In the next part, we’ll deploy our smart contract to the blockchain and interact with it. We’ll then refactor our code and deploy another instance of the smart contract to the blockchain.

Feel free to start a discussion with me on Twitter at https://twitter.com/GeorgeShammar

Peace!!!

--

--

George Gbenle

An ex-banker turned Software Engineer. Loves writing and attempts to solve everyday problems with simple software solutions. https://twitter.com/GeorgeShammar