Solidity Fundamentals

Lesson 1 of 7

Introduction to Smart Contracts & Solidity

A smart contract is a program that lives on a blockchain, deployed once, then executed automatically and identically by every node in the network, no company or server can be shut down or quietly change its behavior. Solidity is the language most Ethereum smart contracts are written in.

Your first contract

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract Greeter {
    string public greeting = "Hello, Kodstigen!";

    function setGreeting(string memory newGreeting) public {
        greeting = newGreeting;
    }
}
  • pragma solidity ^0.8.20; pins which compiler version this contract expects, the ^ allows any compatible 0.8.x version, protecting against breaking changes in future major versions.
  • contract Greeter { ... } is the closest thing Solidity has to a class, it bundles state and functions together, like a Java or C# class deployed permanently to the blockchain.
  • string public greeting is a state variable, permanently stored on the blockchain itself, public automatically generates a free getter function for it.

The EVM

Solidity compiles to bytecode that runs on the EVM (Ethereum Virtual Machine), conceptually similar to how Java compiles to bytecode for the JVM, except every EVM node in the world executes the same bytecode and must reach the exact same result, that's what makes it trustworthy without a central authority.

WARNING

Once deployed, a smart contract's code cannot be changed. There's no patching a bug in production the way you'd redeploy a web server, this is exactly why security and careful testing matter so much more here than in most other kinds of programming.

📝 Smart Contracts Basics Quiz

Passing score: 70%
  1. 1.What does the EVM do?

  2. 2.A deployed smart contract's code can be edited later the same way you would redeploy a web server.

  3. 3.The pragma line at the top of a Solidity file pins which compiler ____ the contract expects.