Solidity like ABC

A beginners guide to Solidity programming

Nnamdi Umeh's photo
Nnamdi Umeh
·Jun 29, 2022·

8 min read

Solidity like ABC

Solidity is the most popular programming language used in writing smart contracts for the blockchain. For a first-timer, Solidity sounds like a complex programming language only those with years of experience in programming can really understand.

I used to think like that at one point, but having worked with solidity, and building amazing products with the language, I can tell you it is not so. Hence, the decision to help budding enthusiasts demystify Solidity - An amazing language for building the future! (Haha, I am a big blockchain fan)

In this tutorial, you'll learn :

  • What Solidity is
  • Introduction to Remix
  • How to write a smart contract
  • Data types in Solidity
  • Variables, constants, and functions
  • Build, deploy and interact with a simple blockchain program

What is Solidity?

Solidity is an object-oriented, high-level language for implementing smart contracts. Smart contracts are programs which govern the behaviour of accounts within the Ethereum state.

Wooooh! You said this was going to be easy, WTH?

That explanation was copied from the official Solidity website. Solidity, in simple terms, is a programming language used for writing smart contracts. A smart contract is basically a block of codes where you can create variables, functions, and all the other cool stuff to tell the blockchain exactly what you want it to do, and how to do it.

How do I get started?

There are certain tools required to run Solidity on your local machine. Setting up these tools involves multiple steps that might seem daunting right now. To keep this tutorial as simple as ABC, we will use an excellent online IDE called Remix. Remix helps you to write, compile and deploy smart contracts to the blockchain without having to install anything. Click on open Remix and let's get started!

Setting up Remix

Screenshot 2022-06-28 at 8.20.06 PM.png

  1. Click on "New File"
  2. Name the file first_contract.sol (The .sol tells Remix that it is a Solidity file) image.png

  3. Click the 3rd icon on the side navigation - The one that says Solidity compiler. Select 0.8.7. This tells the Solidity compiler what version it should use to run your file. Ensure you have this set correctly. Doing this ensures the code in this tutorial will compile correctly in the future.

Now that we have set up Remix, let's begin coding!

Writing our first smart contract

Writing a smart contract with Solidity is just as simple as typing:

pragma solidity 0.8.7; //This tells the compiler what version to use
contract MyFirstContract {

}

This is our first smart contract! But we can do more, right? Did you notice how we named our contract? This naming style is called the Pascal case. It means that each word of the contract's name must start with a capital letter. This style is not required by the compiler, but it helps the readability of our smart contract.

Now, to write a functioning smart contract, we need to understand the basic data types in Solidity.

Data types in Solidity

Data types specifies how our data is stored on the blockchain. Solidity is a strongly typed programming language. This means we must specify the data type before using it. There are many data types in Solidity, but I will introduce you to the basic ones for now. The basic data type includes:

Integers: Integers are whole numbers. There are signed and unsigned integers. signed integers are negative whole numbers (ex. -3), while unsigned integers are positive whole numbers(ex. 3). In solidity, unsigned integers are commonly used. When we declare data as an unsigned integer, only positive numbers can be associated with that data. We declare integers in Solidity with the keywords uint8, uint16, uint32, and up to uint256. uint256 is the most common and can also be written as uint. It is also worthy of note that fractions or floats are also represented as Integers in Solidity.

Strings: Strings are data types that are enclosed in quotes (""). A string can represent a name, location, and any data that is a text. To declare a value as a string in Solidity, we use the keyword string. An example of a string is "I love Solidity".

Addresses: These data types hold wallet addresses. When you declare data as an address, only a blockchain wallet or smart contract address can be associated with it. We declare a data as a string by using the keyword address. An example of a wallet address is 0xb724a346624f35DC2F5FdbA07151F36DD11a0dac (This is not a real wallet address)

There are other data types in Solidity. These data types will be taken care of as we progress in subsequent tutorials.

Solidity variables, constants, and functions

A variable holds data that can change later in our code. In solidity, a variable can either be a private variable or a public variable. A private variable can only be accessed and updated within the smart contract, while a public variable can be accessed outside the smart contract.

pragma solidity 0.8.7;
contract MyFirstContract{
    string public name = "Nnamdi Umeh"; //can be accessed outside this contract
    uint private age = 29; //cannot be accessed outside this contract
}

A constant holds data that cannot change throughout the execution of a code. This means that the value should remain the same after it is created. Changing the value of a constant after declaration will result in an error. In solidity, a constant can also be a private or public constant as explained above. It is conventional to write constants in uppercase, and also use an underscore to separate words.

pragma solidity 0.8.7;
contract MyFirstContract{
    address constant public WALLET_ADDRESS = 0xb724a346624f35DC2F5FdbA07151F36DD11a0dac; 
    //
    address WALLET_ADDRESS = 0x2531b834f20501d79C4062a4e9619E9161C2a8dd; //This will cause an error
}

A function is a block of code that does a specific task. A function can have both variables and constants inside them. These variables and constants cannot be accessed outside their functions.

Functions in Solidity can be either private, public, internal, or external functions. This is called function visibility. It defines how a function can be called.

  • A private function is a function that can only be called within its contract.
  • A public function is a function that can be called both within its contract and outside its contract.
  • An Internal function is a private function, that cannot be called from an inheriting smart contract.
  • An External function is a function that can only be called by another smart contract. These functions cannot be called within their own contract.

We can also specify whether a function is a pure, or a view function. A pure function is one that does not read or update the contract variables, while a view function only reads state variables.

pragma solidity 0.8.7;
contract MyFirstContract{
     uint private a = 5;
     uint private b = 2;

    function sum() public view returns (uint){
        uint c = 3; // c cannot be accessed outside sum function
        return a + b + c;
    } 
}

The function above gives the sum of the variables a, b, and c. It returns a value. This means that whenever sum() is called, it is expected to give the sum of the variables a, b, and c. Note that the keyword "view" is also added, meaning that this function only reads the contract variables to perform its task, but does not update the contract's variables.

Building a Simple counter

To drive home all that we have learned so far, we are going to create a simple smart contract that counts infinitely. Awesome, right?

Create a new file and name it Counter.sol, then write the skeleton contract. Your code should look like this

//Counter.sol
pragma solidity 0.8.7;
contract Counter{

}

Now let's add a variable that keeps track of the count. Since we're performing an arithmetic operation, we are going to declare the variable as an integer, and also make it a private variable.

pragma solidity 0.8.7;
contract Counter{
    uint private count = 0; 
}

Awesome! We should write a function that updates our count whenever we call it.

pragma solidity 0.8.7;
contract Counter{
    uint public count = 0; 

    function incrementCount() public {
        count = count + 1;
    }
}

Kudos! Now we just need another function that shows the current value of count anytime we call it.

//SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.7;
contract Counter{
    uint private count = 0; 

    function incrementCount() public {
        count = count + 1;
    }

    function getCount() public view returns (uint) {
        return count;
    }
}

That was easy, right? Now that we have written our program, we need to compile it, and deploy it to the blockchain. Your screen should look like this

Screenshot 2022-06-29 at 4.49.43 AM.png

Click on Compile Counter.sol, if you followed the steps correctly, you should not have any errors. Nevertheless, if you encountered any errors, do not worry. Errors are part of the learning process. Post your issues in the comments, and I'll be glad to help you out.

To deploy our smart contract to the blockchain, click on the icon below the Solidity compiler - The one that says "Deploy & run transactions". Your screen should look like this

image.png

Remix already has a local blockchain running with 100 test ETH, so you don't have to worry about getting test ETH yourself. Let's deploy right away!

Leave all defaults and click on "Deploy". You should see your Deployed contract at the bottom of the panel. For reference, your panel should look like this

Screenshot 2022-06-29 at 3.18.55 AM.png

Now, we see our deployed contract address and two buttons - incrementCount and getCount. These buttons map the functions on our smart contract.

To interact with our smart contract, click on the "incrementCount" button. This will run the incrementCount function. You'll notice the console is updated at the bottom of the editor.

To get the value of the count, click on the "getCount" button. This will run the getCount function on our smart contract. You should get the value of the count under the button. It looks like this

Screenshot 2022-06-29 at 3.27.02 AM.png

Click on the incrementCount button again, then the getCount. You'll notice the counter increases each time we call the incrementCount function.

Congratulations on writing your first Solidity program! If you enjoyed this post, I intend on making more posts like this on Solidity. Posts that cover more about the language but in a beginner-friendly way.

I'll go from Solidity to building interactive web applications that communicate with the blockchain. I also plan on tweeting about web3 stuff. If that's your kind of thing, you can also follow me on Twitter. Thanks for reading!

 
Share this