**Introduction to Smart Contract Languages (Solidity)

This lesson introduces Solidity, the primary language for writing smart contracts on the Ethereum blockchain and other EVM-compatible networks. You'll learn the fundamental syntax, data types, and basic structure of a Solidity smart contract, preparing you to build decentralized applications.

Learning Objectives

  • Understand the basic syntax and structure of Solidity code.
  • Identify and differentiate between common Solidity data types.
  • Learn how to declare variables and functions in Solidity.
  • Comprehend the purpose and use of smart contracts.

Lesson Content

What is Solidity?

Solidity is a high-level, object-oriented programming language designed for writing smart contracts. These smart contracts are self-executing agreements written in code that run on the blockchain. When deployed, the code becomes immutable, ensuring that its behavior is transparent and verifiable. Think of it as the language used to tell the blockchain what to do. Solidity is the most popular language, but others exist (like Vyper), but this course will focus on Solidity. It's similar in syntax to JavaScript and C++, which may help in your transition to learning the language.

Quick Check: What is the purpose of the `pragma` directive in Solidity?

Basic Solidity Syntax

Solidity code is structured like other programming languages. Here are some basic elements:

  • Pragma: Defines the compiler version. For example: pragma solidity ^0.8.0; indicates the code is compatible with Solidity versions 0.8.0 and above.

  • Contract: The fundamental building block of a Solidity program. It's similar to a class in other languages. Example: contract SimpleStorage { ... }

  • Variables: Used to store data within the contract. You need to specify a data type. Examples: uint public storedData; (unsigned integer), bool public isInitialized; (boolean), string public myString; (string).

  • Functions: Blocks of code that perform specific tasks. They can take arguments and return values. Examples: function set(uint x) public { storedData = x; }, function get() public view returns (uint) { return storedData; }

  • Comments: Used to explain the code. Single-line comments start with //, and multi-line comments are enclosed in /* ... */.

Quick Check: Which of the following is NOT a valid Solidity data type?

Data Types

Solidity supports various data types:

  • Value Types:

    • uint: Unsigned integer (e.g., uint8, uint256 - 8-bit to 256-bit integers).
    • int: Signed integer.
    • bool: Boolean (true or false).
    • address: Ethereum address (e.g., 0x...).
    • string: Text strings.
    • bytes: Sequences of bytes (e.g., bytes32).
  • Reference Types:

    • array: Ordered collection of elements of the same type.
    • struct: User-defined data structure.
    • mapping: Key-value store (like a dictionary).

Quick Check: What is the purpose of the `public` keyword in a Solidity function or variable declaration?

A Simple Smart Contract Example

Let's create a basic contract that stores a number:

pragma solidity ^0.8.0;

contract SimpleStorage {
    uint public storedData; // Declare a state variable of type uint

    // Function to set the value
    function set(uint x) public {
        storedData = x;
    }

    // Function to get the value
    function get() public view returns (uint) {
        return storedData;
    }
}
  • pragma solidity ^0.8.0;: Specifies the compiler version.
  • contract SimpleStorage: Defines the contract named SimpleStorage.
  • uint public storedData;: Declares a state variable storedData (public means accessible from outside the contract).
  • function set(uint x) public: A function that takes an unsigned integer x as input and sets the storedData.
  • function get() public view returns (uint): A function that retrieves the value of storedData. view means it doesn't modify the state of the blockchain.

Quick Check: What does the `view` keyword do in a Solidity function?

Progress
0%