Welcome to Solidity!

Hi there! Welcome to this guide! First, congratulations on taking the step that led you to this resource. It means that you are ready to take the next set of actions that will give you an insight into building on the Blockchain and just enough knowledge to get you started with writing code on the Blockchain!

In this guide, we will cover a few concepts in which you may already have some background knowledge.

Contents:

  1. What is a Blockchain?
  2. How is web2 different from web3?
  3. Why web3, and what are its use cases?
  4. Programming in web3
  5. Solidity - Syntax Overview
  6. "Hello, World!" in Solidity.

What is a Blockchain?

An insightful way to explain Blockchains is to first reiterate how the centralized internet works. The centralized internet is organized so that companies that own platforms can open their services to users and, in return, store the data of such users on their servers (databases). A typical example is Social Media companies that give users access to their platform and allow users "own space" on their servers to store their information using associated tables in a database from which they can easily retrieve the data.

A Blockchain is a decentralized distributed ledger technology where user information is not stored in centralized databases. The Blockchain is designed so that many actors can come together to participate in a decentralized network. The Blockchain is a collection of computers called nodes, with each computer running a piece of software that is not a copy of the same software running on other computers in the network. Instead, it is the actual replica (not a clone) of the software across the network. In this network, the Data on a computer in the network 'compA' is the same Data on 'compB'.

In a Blockchain network, the user data is stored using hashes and recorded on the distributed ledger. The user can interact with the Blockchain from one computer in the node, and the node will broadcast the information that the user wants to save on the Blockchain, and then all computers will take note of the Data that the user just saved on the network and keep a record of it on the ledger.

Various blockchains exist, with famous examples being Bitcoin and Ethereum. In this article, we will focus on the Ethereum blockchain. The Blockchain ledger is arranged, so that information saved on the Blockchain is chained sequentially. The point where a user saves Data on the Blockchain and other computers in the network and then records it is called a Block. Each Block is identified by a unique hash and assigned a Block number. Now, pay attention to the following illustration.

When a user saves data containing their name, e.g. "name=john doe", to the Blockchain at a point when the block number was 10001, the user can not edit the data. The Data is chained to the following block number: 10002 and so on, where n+1. Blockchain is commonly referred to as web3 due to several improvements it has over the traditional internet relayed over HTTPS, and this is one of the critical areas where web2 is different from web3.

How is web2 different from web3?

Before explaining how web2 differs from web3, it is essential to mention that web3 is not a new kind of internet. Although it uses its own set of protocols for interaction, the web3 can be accessed via web browsers over HTTPS. The difference between web2 and web3 is primary in the structural and interactional make-up.

One component in the interactional make-up of Blockchains is the use of Addresses. Given that the Blockchain is a Ledger, user interactions with Data on chain happen via the use of Addresses. Address is of 2 types: Wallet address (also called External Owned Address EOA) and Contract Addresses (Smart Contract code deployed on the Blockchain). A user is also identified by their EOA on chain.

The table below uses a comparison method to outline the differences between web2 and web3.

Web2Web3
Users must join a platform that sometimes requires them to create an account.A user can interact with any platform using their wallet address.
The data that a user post on the Blockchain is owned by the user.
A user can be kicked out of a platform.The user cannot be kicked out of using any platform on the Blockchain
Stored Data can be edited and deleted.Stored Data cannot be edited nor deleted.
It can be closed and it is mostly private to platform users only.It is open and public.
Data is not transparent.Data is transparent.
Sometimes relies on the use of intermediaries.Does not need any intermediaries.
Data is stored on a central server.Data is stored on a decentralized server.

Web2.png Web2

Web3.png Web3

Why web3, and what are its use cases?

Given the listed differences between web2 and web3, one can identify many use cases in that web3 is most suited to guarantee implementations that will serve a real-world need. Such examples include Identity verification, Finance, Insurance and Ownership, to mention a few.

An identity use case involves a user entering their record on the Blockchain at a particular block number. Given that information stored on a chain cannot be edited, user identity can be verifiable in such an instance.

To build applications that can meet such use on Blockchain involves writing code that can save data on Blockchains.

Programming in web3.

Programming in web3 involves writing computer logic and pushing it on blockchains. The code cannot be edited when this piece of computer code lives on the Blockchain. The code is written as a business logic where all interactions with it respond with programmed outcomes. This code that executes the business instruction is called a Smart Contract.

In this article, you will write and deploy your first Smart Contract. You must understand the environment where Smart Contracts run and the language in which to write the code.

In Ethereum, the network, as we described earlier, has a piece of software (Operating System, if you like) called the Ethereum Virtual Machine (EVM) running in each computer (node) in the network. The EVM can only read Bytecode. The Solidity Programming Langauge is available to developers to program Smart Contracts, which are then compiled to Bytecode and deployed to the Blockchain.

Smart Contract code can be written in Solidity, Vyper and the newly released Huff programming language. Solidity is easy to learn. We are going to write our first program in Solidity. In this example, we will write and deploy the code in Remix. Remix is an interactive online tool that makes it easy to write, deploy, and interact with Smart Contract.

Solidity Syntax

Solidity is a statically typed programming language much like C++; this means that every data type in Solidity must be declared as it is initialized.

"Hello, World!" in Solidity.

For our first Smart Contract, kindly go to remix and paste the following code.

//SPDX-Licenses-Identifier: MIT
pragma solidity ^0.8.16;

contract HelloWorld {
    address public owner;
    string greeting;

     constructor(){
        owner = msg.sender;
     }

     function setGreeting() public {
        greeting = "Hello, World!";
     }

     function getGreeting() public view returns(string memory) {
        return greeting;
     }
}

This Smart Contract is written in Solidity. For developers familiar with JavaScript, you will see a similarity in the function statements. This Smart Contract implements a function that writes "Hello, World!" to the Blockchain and another that returns it. We will walk through the code and explain each line.

//SPDX-Licenses-Identifier: MIT

The Solidity Compiler encourages using machine readable SPDX license identifiers to ensure trust in Smart Contracts. This line indicates the type of licenses with which this Smart Contract is published.

pragma solidity ^0.8.16;

The pragma statement indicates the version of Solidity that the Smart Contract should be compiled in.

Contract HelloWorld {
}

The Smart Contract Logic is entirely written in curly braces initialized with the Contract keyword followed by the name of the particular Smart COntract, in this instance: HelloWorld

address public owner;

Variables in Solidity are statically typed. This line is a declaration of a variable named owner. It is of a data type address and set to a visibility type of public. Solidity functions can be specified in 4 different visibility forms: Public, External, Internal and Private. A function set as public means that anyone interacting with the Contract can call the particular function.

string greeting;

This declaration of a greeting variable of data type string. It will be used to initialize the greeting in the setGreeting function.

constructor(){
  owner = msg.sender;
}

When a Smart Contract is deployed on a Blockchain, the constructor keyword is used to initialize variables that are used in the implementation. The constructor is only run when the code is first deployed. The constructor in this example is initializing the owner variable and assigning it a particular value, msg.sender msg.sender where msg is an abbreviation for message. This keyword denotes the wallet address that pushed the Contract onto the Blockchain.

function setGreeting() public {
  greeting = "Hello, World!";
}

This is the function statement where we set the "Hello, World" greeting. When the Contract is first deployed, it will be set at a Block number on chain.

function getGreeting() public view returns(string memory) {
  return greeting;
}

This is the call to return the greeting saved on chain. There are new unique keywords here, view and returns. The view keyword indicates functions that return data and do not make further changes to the Blockchain. The returns keyword indicates the data type being returned in the function call, indicating the location it is stored, which in this case is memory.

To understand how this Smart Contract will work on an actual blockchain, we will simulate its interaction by compiling it and deploying it in Remix.

Web3-DnR.png

To compile the Contract, kindly click on the Solidity Compiler tab and select the compiler version from the dropdown. Click on the Compile HelloWorld button. You should see the green tick appear on the tab upon a successful compilation.

Web3-SCompiler.png

The Contract is compiled into Bytecode. You can click on the Bytecode icon to copy the Bytecode and paste it into a notepad file to view opcodes.

After the successful compilation, the next step is to deploy the Smart Contract. Click on the Deploy and Run Transactions tab. Understanding the various UI components before clicking the deploy button is essential.

ENVIRONMENT: This is the Blockchain where the Smart Contract code will be deployed. It is default set to a "London Fork" Remix Virtual Machine.

ACCOUNT: These are a set of 10 Wallet Addresses. The addresses hold test Ether. You can use any of the addresses when interacting with the Smart Contract.

GAS LIMIT: Gas is the unit of measure to ascertain the amount of computation your interaction with the Blockchain will cost. The default limit is set as 3 Million units.

VALUE: This field is used when you want to pass units of ether to spend when interacting with a Smart Contract.

Ensure that the HelloWorld Contract is highlighted in the CONTRACT dropdown. Click Deploy This will deploy your Smart Contract. You can view your deployed Contract in the Deployed Contracts section. Click on HelloWorld. This will open a dropdown where you can see all the functions written in your Smart Contract getGreeting,setGreeting, including the Owner variable.

Web3-Functions.png

NOTE: When a variable is initialized, and its accessibility is set to public, that variable can be returned as a view function on chain.

Click the various function buttons to interact with the Contract on chain. Begin with checking the owner and then the getGreeting. The owner will return the address highlighted in the ACCOUNT section when the Smart Contract was deployed. That is the address that owns the Smart Contract.

The getGreeting will return0. This is because we have notsetGreetingon the Blockchain; we have only deployed the Contract! Now, go ahead and clicksetGreeting. This will store "Hello, World!" on the Blockchain. Click ongetGreeting`, and it will return the text: "Hello, World!"

You have successfully deployed your first Smart Contract written in Solidity on the Blockchain. This piece of logic is a hello world program, and it can not be changed! Well, you can adjust the setGreeting function and parse in an argument such that you can return any greeting as you please.

function setGreeting(string memory _greeting) public {
  string memory s = "Hello ";
  greeting = string.concat(s, _greeting);
}

This is an update to the setGreeting function. This update has the concat keyword, used to concatenate strings. This was introduced in solidity version 0.8.12 The string s is held in memory before it is pushed on chain.

Conclusion.

This is your: "Welcome to Solidity" tutorial. This article has laid out for you a baseline that you can use to get a feel for what to expect when you start writing more Solidity code. Here are recommended references that will significantly aid your quest to learn Solidity.

Resources.

Ethereum.org Solidity Docs.