No history yet

Introduction to Solidity

What is Solidity?

Think of Solidity as the primary language for giving instructions to the Ethereum blockchain. Just as developers use JavaScript to build interactive websites, they use Solidity to create smart contracts.

Solidity is the main programming language for writing smart contracts for the Ethereum blockchain.

But what’s a smart contract? It’s a program that runs on the blockchain. Once it’s deployed, it executes automatically based on predefined rules, without needing a person or company to manage it. These contracts form the backbone of decentralized applications, or DApps.

Lesson image

Solidity was designed to be familiar to programmers who already know languages like C++, Python, or JavaScript. It's statically typed, which means you have to declare the type of your variables (like numbers or text) upfront. This helps catch errors early and makes the code more secure and predictable, which is crucial when dealing with valuable assets.

Solidity's Role in Ethereum

Code written in Solidity doesn't run on the blockchain directly. First, it has to be translated into a language that the Ethereum network can understand. This process is called compilation.

A special program called a compiler takes the human-readable Solidity code and turns it into low-level instructions known as bytecode. This bytecode is what gets deployed to the Ethereum blockchain and is executed by the Ethereum Virtual Machine (EVM).

You can think of the EVM as a single, global computer made up of all the computers (nodes) in the Ethereum network. It processes transactions and executes the logic inside smart contracts, ensuring everyone agrees on the outcome.

A First Look at the Syntax

Every Solidity file starts with a line that tells the compiler which version of the language to use. This is called a pragma. It's important for preventing issues that can arise when a contract is compiled with a newer, potentially incompatible compiler version.

// This contract will compile with any compiler version
// from 0.8.0 up to (but not including) 0.9.0
pragma solidity ^0.8.0;

After the pragma, you define the contract itself using the contract keyword, followed by the contract's name. Think of a contract as a container for your code, similar to a class in other programming languages. All of your variables and functions will live inside this container.

Here’s a basic contract that stores a piece of text.

pragma solidity ^0.8.20;

// Defines a contract named SimpleStorage
contract SimpleStorage {
    // A state variable to store a string
    string public message;

    // A function to change the message
    function updateMessage(string memory newMessage) public {
        message = newMessage;
    }
}

Let's break this down:

  • string public message;: This line declares a state variable called message. A state variable is data that is permanently stored on the blockchain within the contract. The string type means it holds text, and the public keyword automatically creates a function that allows anyone to read its value.

  • function updateMessage(...): This is a function that can be called to change the value of the message variable. It takes one input, newMessage, and updates the contract's state.

Now that you've seen the basics, test your knowledge.

Quiz Questions 1/5

What is the primary purpose of the Solidity language?

Quiz Questions 2/5

The process of converting human-readable Solidity code into low-level instructions (bytecode) that the EVM can understand is called: