Solidity For Dummies.

Ifeoluwaolubo
Coinmonks
8 min readOct 7, 2023

--

In my previous article, I talked about how I journeyed into the new tech space of web3 and gave some details from my findings on the blockchain technology, a little background on Bitcoin itself, the Ethereum network, transactions etc. You can find the article here ⬇️:

I noted that the main difference between Bitcoin and Ethereum is that Bitcoin was only used for the transfer of monetary values from peer to peer while Ethereum broadens the scope for the different applications we can apply the blockchain technology to build.

The main thing that makes this possible is the idea of Smart Contracts which are just a piece of code that lives on the Ethereum blockchain. The smart contract is what instructs the blockchain on how to behave with respect to the application or project it has been written for. Think of it as an account thats being controlled by code. There are many smart contract languages out there but the most popular by far and most widely used (and the one I used) is called Solidity. This article intends to introduce you to the most basic underlying concepts for understanding and writing solidity codes. Let dive right into it 🚀.

Although I try to make the article quite simple and easy to understand, having the basic knowledge of at least one programming language would be very beneficial.

Solidity vs JavaScript…

A lot of us are familiar with JavaScript but if you’re not, that’s fine as well. JavaScript is a language for the web especially for building the frontend of applications. I consider Solidity to be very dis-similar to JavaScript (based on some underlying implementations) and still very similar to JavaScript (based on syntax), except for the fact that solidity is a statically typed language.

Remix Editor

The quickest way to get to writing solidity codes is by using an online editor called remix which makes it very easy to write and quickly test out solidity codes. Go to https://remix.ethereum.org/ to get started, you should arrive at an editor that looks like this

Remix comes with a test Ethereum network which we can easily use to deploy and test out our written smart contract codes. It also has some accounts that has been pre-funded with 100 ethers each which we can use while testing. For this article, all our codes would be written in the 3_Ballot.sol file that’s located in the contracts folder. Clear out what is in the file and write the 2 lines you see in the code below.

The .sol in the file extension is the extension for solidity files. Inside this file, we added the // SPDX-License-Identifier: MIT, to ignore whatever warning the editor might be giving.

On the left menu select the S shaped icon and make sure to select the same compiler version as you have set in your code. In this case, it is 0.8.21. Also make sure that you select the auto compile check box.

Under the S shaped icon we have the debug and run transactions icon. This is where we’ll come to deploy and test our codes. You can verify that you have multiple accounts in the accounts dropdown and they each have 100 ethers in them. Also make sure you have a Remix VM selected, in our case its Shanghai.

The Solidity Compiler

Our solidity code is not being executed directly on the blockchain. Instead something called the solidity compiler compiles the source code and spits out a Bytecode and an ABI (Application Binary Interface). The bytecode is what is then deployed on the blockchain, while the ABI is what we use to communicate or interact with what has been deployed on the blockchain.

Solidity compiles to a bytecode and an ABI

When building full featured Web3 projects, we would often need a frontend (eg JavaScript code) that interacts with the blockchain and the only way this said interaction can occur is by using our ABI. Without it, we can’t possibly communicate with what was deployed on the blockchain.

JavaScript can’t understand the bytecode

Writing Solidity Codes

Now that we’ve gotten some administrative stuff out of the way, we’ve finally reached the point where we can actually start writing some solidity code. Consider the code below.

Solidity Contract declaration

The code on line 3 tells the solidity compiler that we are about to write solidity code and the version it should use for compiling. The code on line 5 is where the fun starts, this is where we declare our smart contract, giving it a name of MailBox. We are attempting to build a sort of email application, but on the blockchain of course 😁. Our contract would be able to create a mail and set a default message to it, we can chose to set a new mail and even retrieve what the current mail is. Nothing crazy for now, just enough to get our hands dirty while understanding some basic fundamentals of solidity.

Some basic types.

In the code above, we are creating 4 variables that would be used in our email application. The first variable is a string type and this holds the actual message of the mail. The public keywords means that the variable should be exposed and seen outside of our contract. We’ll see more details on this soon.

Note that the senderAddress variable has no public keyword attached to it. This is intensional and is used for demonstration purposes.

For now, we are only using the basic variable types similar to other programming languages like string, uint (or number type in JavaScript), boolean (true or false) and address type which is one we’re probably new to cause it’s specifically used in solidity. The address type holds addresses to different accounts. Examples of what the address type looks like are

0x5B38Da6a701c568545dCfcB03FcB875f56beddC4
0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db

The uint type has other variations which stores different sizes of numbers (uint8, uint16, uint32, uint256 etc).

The default uint type refers to uint256.
The 4 variables are set to their default types by default, which is
“” for string
false for bool
0 for uint
0x0000000000000000000000000000000000000000 for address

Now let’s look at the full code for our email contract.

Full Email Code

On line 11, we are creating a constructor which basically creates and set the initial states of our MailBox contract. It takes in initialMail variable as argument which we then set to the public mail variable. We are setting the isInitialMail to true cause this is the first message set upon contract creation, increasing the mail count by 1 and setting the senderAddress variable to the address of the user initiating the contract creation.

Note that the msg variable is a global variable and would be discussed more in later articles.

The setMail function takes a string newMail as argument, sets it to the mail variable, sets initialMail to false (since this is no longer the initial mail) and increases the mailCount by 1. It is set to public so we can access it from the outside world, and by outside world I mean outside the contract itself.

Initially, the senderAddress was not set to public and this means that we have no way of accessing it outside the contract. Hence, we created the getAddress function which gets the value stored in the senderAddress variable and returns it to us. Let’s break the getAddress function down a little bit

We already know that the public keyword exposes the function, the view keyword means that the function’s only purpose is to get the value of some variable that was stored and not to initiate a transaction on the blockchain (more on this in later article as well) and the returns keyword tells us that we want to return some value of a specific type in this case an address type which is why we have the (address).

Alright, now that we are done with our MailBox contract, let’s test it out. Click on the debug and run transactions button from earlier and beside your deploy button, set intialMail to “Hello World” (in quotes) then click on the deploy button.

Notice how the amount of ethers you have reduces, this is because of the gas used in the contract deployment. More of this in later article as well. 😁

Deployed Contract

Click on the deployed contracts at the bottom and start interacting with your contract live on a test Ethereum network (Congratulations btw 🥳).

Notice that the isInitialMail, mail and mailCount variables are all exposed and we can see the values set on each of them, except the senderAddress variable. Once again, this is because we did not make it public and the only way we can get it’s value is by clicking on the getAddress button which runs the getAddress function to return the value of the senderAddress variable.

Now let’s try to set the mail to a new value and see how it affects our contract.

set a new mail

After setting the mail to “Hello, Contract”, notice how the value of the mail changes, the mail count increases to 2 and the isInitialMail is now false since this is no longer the initial mail.

That’s it guys, in this article, we have gone through the basics of solidity while creating a simple MailBox Contract, deployed it to a test Ethereum network on remix while also familiarizing ourselves with the new remix editor. In the next article, we’ll dive even deeper into solidity exploring some new data types and other concepts like gas price, transactions, etc.

I hope you liked this article and find it useful. Do give a like, comment and follow for more on the Web3 technology.

Find the next step below 👇🏻👇🏻

--

--

Ifeoluwaolubo
Coinmonks

A software developer with love for the blockchain