Solidity Basics & first smart contract deployed

#100DaysOfWeb3 Week 0 | In this blog post, I cover the topics I've learned this week, mainly Solidity basics such as types of variables, value types, etc.

Solidity Basics & first smart contract deployed

Hey, fren! gm.

Recently I decided to embark on my Web3 journey and started the #100DaysOfWeb3 challenge, which meant that for the next 100 days, I'd be exploring the world of Web3 and documenting my thoughts and learnings here on my blog.

Let's begin! 🛠


#100DaysOfWeb3 Week 0

Like all programmers, I also like to start the count with 0. 😉

So, it's been a week since I've been dabbling into the world of Web3 and Blockchain, and since my approach is code-focused, this week I took some time to explore the primary language of Web3: Solidity.

Solidity is an object-oriented, high-level programming language we use to write smart contracts on the Ethereum blockchain. Well, that's quite a mouthful, isn't it?

Let's backtrack for a minute and try to understand what those programming buzzwords I threw at you actually mean.

What does object-oriented programming (OOP for short) mean? OOP is a method of programming that involves the use of 'objects'. An object is a piece of code that allows you to create similar pieces of code again, and again, and again, so that you don’t have to write them over and over again.

These objects follow a blueprint called a Class. Here's a visual example:

The car is a blueprint and different types of cars are created following that blueprint. BTW, what's your favorite car, fren?

What is a high-level programming language? An HLL programming language is one that lets you do more stuff with less code and has a higher level of abstraction. No need to get into the nitty-gritty of HLL for now (pun intended or what? 😄).


Now on to Solidity.

Solidity is the primary language that runs on the Ethereum Virtual Machine and is used for writing smart contracts on the Ethereum blockchain.

A smart contract is a piece of code stored inside the blockchain that enables two or more parties to exchange value with each other in a conflict-free manner while avoiding the need for a third party like a bank.

Smart contracts are just like contracts in the real world with their own set of rules, terms, and conditions written in code.

Smart contracts can be used for voting, crowdfunding, ownership registrations (like property ownership), etc.

Now, we can write smart contracts using multiple programming languages like Vyper, Yul, and Rust but the most popular choice seems to be Solidity.

Here's a simple smart contract written in Solidity:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.8;

contract MyContract{

    uint myFavoriteNumber = 10;

    function set(uint x) external {
        myFavoriteNumber = x;
    }

    function get() external view returns(uint){
        return myFavoriteNumber;
    }
}

Basics of Solidity

To make sense of the smart contract code above, one must be familiar with some basics of Solidity.

In Solidity, we start off by defining the solidity compiler version we'll be using. We set the compiler version by writing pragma solidity ^0.8.8; . This tells Solidity that we want to use version 0.8.8 or above ('above' is notified by the ^ symbol).

💡
As of August 20, 2022, the latest version of Solidity is 0.8.16

We also make sure to indicate that the following code comes under the MIT copyright license so that the compiler doesn't yell at us or show any warnings. We do this by marking it as a comment using // symbol.

💡
// This is a single-line comment.

/* This is a
multi-line
comment. */

In Solidity, we can create a new smart contract using the contract keyword.

contract myContract {

}

Inside the { }, we define the set of instructions & rules for the smart contract.

To store data in a smart contract, there are 3 types of variables in Solidity.

  1. State Variables: those variables that store data on the blockchain. State variables are declared inside the contract and outside of any function.
  2. Local Variables: those variables whose values are available only within a function they are defined in and cannot be accessed from outside that function.
  3. Global Variables: these are a special type of variables that stores information about the blockchain, transactions, and the account that has called the function.

Value Types in Solidity

Integers (int/uint):  uint are unsigned integers (non-negative integers) while int are combinations of negative and non-negative integers.

uint public myFavoriteNumber = 10;

There are various sizes of integers in Solidity. The integers size ranges from 8 to 256 (in steps of 8): uint8, uint16, ... uint256, and int8, int16, ... int256.

💡
Default size of uintuint256
uint256 = 0 to 2**256 - 1
uint32 = 0 to 2**32-1
💡
Default size of intint256
int256 = -2**255 to 2**255 - 1
uint32 = -2**31 to 2**31-1

Boolean (bool): A variable with bool value type can hold any of the two values: true or false. The default value of a boolean is false.

bool public isLoggedIn: true;

Solidity supports all boolean operators such as !, &&, == etc, and they only take up 1 byte of storage.

Address: The address value type represents an Ethereum address and holds a 20-byte value. The default value of an address variable is 0x0000000000000000000000000000000000000000.

address public addr = 0x7C1E34171f7ca01B7F3610e2766a6e29C673E7D6;

There are other value types in Solidity, but I've not experienced them just yet in week 0. For the curious minds, here's the official Solidity documentation on value types. I'll either update this blog post when I learn about the remaining value types or create a separate post.


Moving on...

When we created different types of variables in the examples above, we used a special keyword called public. Now, what is this public thingy?

Turns out this thing is called Visibility modifier in Solidity. Visibility modifiers define the visibility of state variables or functions.

State Variables have access to three types of visibility modifiers: public, internal, or private, while Functions have access to an additional modifier: external.

  • Public means that both the state variable and the function can be accessed by the contract and by other smart contracts. When it is not specified, all functions are made public by default, whereas all state variables are internal by default.
  • Private means that the variable and function are only accessible with the contract they are defined in. It is the most restrictive visibility.
    uint256 private x;
  • Internal means that the variable and function can be accessed from within the same contract as well as its derived contracts. As mentioned before, state variables have internal visibility by default.
    uint256 internal x;
  • External visibility means a function can be called from other contracts and transactions, but it cannot be called from within the same contract or any of its derived contracts. External visibility is not available for state variables.
contract MyContract {
  function anExternalFunction() external view returns (uint256) {
    return 0;
  }
}

What's this view keyword, you may ask? For now, let's just say that the function anExternalFunction() is read-only. More on view and pure functions next week.


Conclusion

Week-0 has been a wild ride, from standing at the edge of the Web3 rabbit hole to diving into it head-first, I have consumed a lot of blog posts, Twitter threads, and tutorials.

I also deployed my first ERC721 smart contract on the Rinkeby test net. Here's the NFT I deployed in the first week of Road to Web3.

https://testnets.opensea.io/assets/rinkeby/0xf6be416312b1711178b25a057f610277990614bd/1

And I believe it's gonna be much more fun as I dig deeper through the rabbit hole.


Hey, fren, that's it for now! Thanks for reading till the end. If you found this post helpful or if I made a mistake somewhere, please let me know by commenting below. I'm a newbie so any corrections are greatly appreciated.

Also, please consider subscribing so that you get a notification on your mail whenever I post new stuff. Subscribing is free, and it will give me a ton of motivation to keep going in difficult times.

#WAGMI ✌🏻