Introduction to Smart Contracts

This lesson introduces you to smart contracts and the Solidity programming language, the foundation of Web3 development. You'll learn the basic syntax, data types, and control structures necessary to build simple decentralized applications (dApps), culminating in writing and deploying your first 'Hello, World!' contract.

Learning Objectives

  • Define what a smart contract is and its role in Web3.
  • Understand the basic syntax and structure of a Solidity smart contract.
  • Identify and use common Solidity data types (e.g., uint, bool, string).
  • Write, compile, and deploy a basic 'Hello, World!' smart contract using Remix IDE.

Lesson Content

What is a Smart Contract?

A smart contract is a self-executing agreement written in code and stored on a blockchain. It automatically enforces the terms of the contract when predetermined conditions are met, eliminating the need for intermediaries. Think of it like a vending machine: you insert money (conditions met), and you receive your snack (outcome). Smart contracts are immutable, meaning once deployed, they cannot be changed, ensuring transparency and trust.

Quick Check: What is a smart contract?

Introduction to Solidity

Solidity is the primary programming language for writing smart contracts on the Ethereum blockchain. It's a statically-typed, object-oriented language inspired by JavaScript, C++, and Python. Key features include:

  • Variables: Stores data. Examples: uint age = 30;, bool isVerified = true;, string message = "Hello, world!";
  • Functions: Execute code to perform specific actions.
  • Data Types: Define the type of data a variable can hold. Common data types include:
    • uint: Unsigned integer (e.g., uint256 for a 256-bit integer)
    • bool: Boolean (true or false)
    • string: Text
    • address: Ethereum address
    • bytes: Sequence of bytes
  • Control Structures: Control the flow of execution.
    • if/else statements
    • for loops
    • while loops

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

Basic Solidity Syntax

Let's break down a simple contract:

pragma solidity ^0.8.0; // Specifies the Solidity compiler version

contract HelloWorld {
    string public message;

    constructor() {
        message = "Hello, World!";
    }
}
  • pragma solidity ^0.8.0;: Tells the compiler which version of Solidity to use. The ^ means compatible with versions greater than or equal to 0.8.0, but less than 0.9.0.
  • contract HelloWorld { ... }: Defines a smart contract named HelloWorld.
  • string public message;: Declares a public variable message of type string. The public keyword makes the variable readable from outside the contract.
  • constructor() { message = "Hello, World!"; }: The constructor is a special function that runs only once when the contract is deployed. It initializes the message variable.

Quick Check: What does the `pragma solidity ^0.8.0;` line do?

Writing Your 'Hello, World!' Contract in Remix

  1. Go to Remix IDE: Open the Remix IDE in your browser: https://remix.ethereum.org/.
  2. Create a New File: In the File Explorer (left panel), create a new file named HelloWorld.sol.
  3. Paste the Code: Copy and paste the example HelloWorld contract code above into HelloWorld.sol.
  4. Compile the Contract: Go to the Solidity Compiler tab (left panel, looks like a compiler icon). Select the compiler version (matching the pragma directive) and click the 'Compile HelloWorld.sol' button.
  5. Deploy the Contract: Go to the Deploy & Run Transactions tab (left panel, looks like a blockchain). Choose 'Injected Provider - MetaMask' to connect your MetaMask wallet. Select the HelloWorld contract from the dropdown and click the 'Deploy' button. Confirm the transaction in your MetaMask wallet.
  6. Interact with the Contract: Once deployed, you'll see your deployed contract under 'Deployed Contracts'. Click the dropdown arrow to view its functions and variables. Click the 'message' function to see "Hello, World!" displayed in the Remix console.

Quick Check: What is the purpose of the `constructor()` function in a smart contract?

Progress
0%