Solidity Basics

Welcome to your first steps into the world of Solidity! Today, you'll be introduced to the foundational building blocks of smart contract development, learning the syntax, data types, and basic structures that underpin all Web3 applications. Get ready to write your first simple smart contracts and understand how they interact.

Learning Objectives

  • Identify the basic syntax rules of Solidity.
  • Understand and utilize fundamental data types in Solidity.
  • Declare and use variables in a Solidity contract.
  • Implement basic control flow structures (if/else statements) in Solidity.

Lesson Content

Introduction to Solidity

Solidity is a high-level, object-oriented programming language for writing smart contracts. These contracts are self-executing agreements that run on the Ethereum Virtual Machine (EVM). Solidity code is compiled into bytecode, which is then deployed to the Ethereum blockchain. This ensures that the code's logic is immutable and transparent.

Key features of Solidity include:
* Object-oriented: Supports classes, inheritance, and polymorphism.
* Statically typed: Requires you to declare the type of a variable.
* Gas-aware: Gas is the cost of executing a smart contract on Ethereum, so efficient code is crucial.
* Security-focused: Designed to be secure and protect against common vulnerabilities.

Quick Check: Which keyword is used to specify the compiler version in a Solidity file?

Solidity Syntax and Structure

Solidity code is structured into contracts. A contract is similar to a class in other programming languages. Every Solidity file starts with a pragma statement, which specifies the compiler version to use. The basic structure looks like this:

pragma solidity ^0.8.0; // Specifies the compiler version

contract MyContract {
    // State variables, functions, and events will be defined here
}
  • pragma solidity ^0.8.0; - This line tells the compiler to use version 0.8.0 or a more recent version compatible with it (e.g., 0.8.1, 0.8.2). Using the correct compiler version is crucial for compatibility.
  • contract MyContract { ... } - Defines the contract named MyContract.

Quick Check: What data type is typically used to represent an Ethereum address?

Data Types

Solidity offers several data types:

  • Value Types: These types store the actual value. Examples include:

    • uint (unsigned integer): uint8, uint16, uint256 (common size), representing whole numbers.
    • int (signed integer): int8, int16, int256.
    • bool (boolean): true or false.
    • address: Represents an Ethereum address (e.g., a wallet address).
    • bytes: bytes1, bytes32 (common size) for storing byte sequences.
    • string: For text data.
    • fixed and ufixed: Fixed-point numbers (less commonly used).
  • Reference Types: Store a reference to the data. Examples include:

    • arrays: Ordered collections of elements (e.g., uint[] for an array of unsigned integers).
    • structs: Custom data structures.
    • mappings: Key-value stores (similar to dictionaries or hash maps in other languages).

Example:

pragma solidity ^0.8.0;

contract DataTypesExample {
    uint256 public myUint; // State variable (public means it's readable)
    bool public myBool = true; // State variable, initialized to true
    address public owner = 0x5B38Da6a7A709c6fE1F11D09dE3fE2d4a6D7B744; // Example address
    string public myString = "Hello, Solidity!";
}

Quick Check: What is the scope of a state variable in a Solidity contract?

Variables and State Variables

Variables are used to store data. In Solidity, there are two primary kinds:

  • State Variables: Declared outside of functions. They are permanently stored on the blockchain as part of the contract's state. They are analogous to class variables in object-oriented programming.
  • Local Variables: Declared inside functions. They exist only during the execution of the function.

Example:

pragma solidity ^0.8.0;

contract VariablesExample {
    uint256 public myNumber; // State variable

    function setNumber(uint256 _newNumber) public {
        uint256 localVariable = _newNumber + 10; // Local variable
        myNumber = localVariable; // Modifying the state variable
    }
}

Quick Check: Which of the following is a reference type in Solidity?

Basic Control Flow (if/else)

Control flow statements allow you to control the execution order of your code. The if/else statement allows you to execute different blocks of code based on a condition.

pragma solidity ^0.8.0;

contract IfElseExample {
    uint256 public number = 10;

    function checkNumber(uint256 _input) public {
        if (_input > number) {
            // Code to execute if _input is greater than number
            number = _input; // Example action
        } else if (_input < number) {
            // Code to execute if _input is less than number
            number = _input * 2;
        } else {
            // Code to execute if _input is equal to number
        }
    }
}

Explanation:
* The if condition checks if _input is greater than number.
* If the if condition is true, the code inside the first {} block is executed.
* If the if condition is false, the else if condition is checked.
* If the else if condition is true, its code block is executed.
* If all conditions are false, the code inside the else block (if present) is executed.

Quick Check: What keyword is used to define a custom data structure in Solidity?

Progress
0%