Ultimate Guide to Solidity

Ultimate Guide to Solidity

Jul 4, 2022ยท

4 min read

Play this article

What is Solidity?

Solidity is an object-oriented programming language for writing smart contracts. It is a high-level language for implementing smart contracts. It is the primary language for writing smart contracts in the Ethereum Blockchain.

What is a Smart Contract?

A smart contract is a program or a piece of code that governs and controls actions within the blockchain.

To practice some Solidity basics, we are going to use the Remix IDE. Remix is an online open-source IDE that allows us to write and deploy our smart contracts.

If you are a beginner and this is your first time trying the solidity language, don't worry this guide is also for you. As long as you have programming knowledge you are good to go. Even if you don't, you are also good to go.

// an example of a simple smart contract
pragma solidity 0.8.11;

contract MyContract {
  // Set Global public variables (make getters with "public")
  uint8 public storedNumber;

  // let's create a function to change numbers by anyone. Must not be the same number
  // If it is, it will fail with the message "They are the same number"

  function changeNumber(uint8 newNumber) public {
    require(storedNumber != newNumber, "They are the same number")
       storedNumber = newNumber 
  }

  // This is how to return a public variable. This is already set with public
  // however for non public variables, this is how we create them
  function getCurrentNumber() public pure returns(uint8) {
    return storedNumber;
  }
}

This is what a smart contract looks like.

Now let's get some fundamentals.

Solidity Data Types

Data types are classifications or types which tell the computer how we want to use the data.

1. Boolean

The bool data type is used to represent situations that have binary results, such as 1 or 0, true or false. The 0 is false and the 1 is true.

The Solidity language supports both logical and comparison operators.

Logical

! Logical Negation

&& And

|| OR

Comparison == equality

!= inequality

// an example of the bool data type in solidity
pragma solidity 0.8.11;

contract MyBool {
      bool public isSet = True;
      bool public isPresent = False;
}

2. Bitwise Operators

& And

| OR

^ XOR

~ Bitwise negation

<< Left Shift

>> Right Shift

3. Arithmetic Operators

+ Addition

- Subtraction

* Multiplication

/ Division

% Modulo

Value Types in Solidity

1. Addresses

Addresses in solidity hold a 20-byte value which represents the size of an Ethereum address. An address is a unique public key that points to a wallet where assets can be stored.

pragma solidity ^0.8.1;

contract FirstProgram {
        address public walletAddress = "0x2Dc85851940eC853f63587c45751126E1877d8C0";


}

2. Enums

enums or enumerable in solidity are used to create user-defined data types. It restricts the variable to only one of the pre-defined values.

Enums allow the contract more readable, and maintainable. it also reduces the errors in the contract.

pragma solidity ^0.8.1;

contract MyFirst {

// let's create an enum
enum Switch  {
ON,
OFF
};

// create a variable of type enum
Switch switch;

//create a function to turn on the light switch
function onSwitch() public{
    switch =  Switch.ON;
}

// create a function to turn off the light switch
function offSwitch() public {
      switch = Switch.OFF;
}

// lastly we create a function that gets the value of the Switch
function switchReturn() public view returns(Switch) {
// finally let's return the value of the Switch
  return Switch;
}
}

Reference Types in Solidity

4. Arrays

An array is a collection of elements of the same data type which can be identified using their location called index.

An array can either be dynamic or fixed.

// Creating arrays in solidity
address[] addresses
uint256[] myArray

// Add to array
function add() internal {
// adding value to an array
  myArray.push(123); 
}

Example of Dynamic array and fixed array

uint256[] dynamicArrays;

uint256[5] fixedArrays;

5. Struct

Struct allows us to create more complicated data types and define our own type in form of structures.

struct people {
  string name; 
  uint256 favoriteColor;
  uint256 age;

}

6. Mapping

Mapping is similar to Dictionary or Hash tables in other languages. It accepts a key type and value type pair.

Mapping Syntax

mapping(key_type => value_type) mappingName;

struct people {
  string name; 
  uint256 favoriteColor;
  uint256 age;

}

mapping(address => people) bio;
address[] public people_bio;

Conclusion

In this article, we discussed Solidity and its data types, value types, and reference types. We also explained what smart contracts are and how they are connected to solidity.

There is going to be part 2 of this article. Follow me and subscribe to my newsletter to get notified when I publish part 2. Thank you ๐Ÿค—

Follow me on Twitter and Github

ย