Lesson 1 of 4 Solidity Language Fundamentals

Solidity and the Development Environment

Solidity is the main language for Ethereum smart contracts. This lesson introduces the language, the tools used to write and compile it, Remix IDE and Hardhat, and the structure every Solidity file and contract follows.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the Solidity language and how its versions differ.
  • Compare Remix IDE with Hardhat.
  • Identify each part of a Solidity source file and contract.
  • Interpret a version pragma.

The Solidity language

Solidity is a statically typed, contract-oriented language whose syntax resembles JavaScript, C++ and Java. It compiles to EVM bytecode. Contracts are similar to classes: they group state and functions, support inheritance and are instantiated by deployment. Solidity is versioned as 0.x.y; breaking changes happen at minor versions, so 0.8 code differs significantly from 0.4 code found in 2018-era examples (for example, 0.7 removed now in favour of block.timestamp, and 0.8 added checked arithmetic). Vyper is a Python-like alternative language that compiles to the same EVM.

Remix IDE and Hardhat

Two tools cover most development. The following comparison shows how they differ and when each fits.

Remix IDE
RunsIn the browser at remix.ethereum.org
SetupNone
CompileSolidity Compiler panel
DeployDeploy & Run panel to Remix VM or a wallet
TestSolidity Unit Testing plugin
Choose when Learning, quick experiments, debugging single contracts
Hardhat
RunsLocally, as a Node.js project
Setupnpm install and a configuration file
Compilenpx hardhat build
DeployIgnition modules or scripts
TestSolidity tests and Mocha or node:test tests
Choose when Real projects, automated tests, repeatable deployments

A Hardhat project keeps contracts in contracts/, tests in test/, deployment modules in ignition/modules/ and its settings in hardhat.config.js or .ts. Foundry is a third popular toolkit, written in Rust, where tests are also written in Solidity. Truffle, the framework of older material, is retired.

Sort into groups

Remix or Hardhat?

Exam skills 2.1.1 · 2.3.1

Decide which tool fits each development task better.

Log in to try this activity Interactive activities are free with an account.

Anatomy of a contract

The following contract shows the elements that appear in almost every Solidity file.

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

contract SimpleStorage {
    address public immutable owner;      // set once in the constructor
    uint256 private storedValue;         // state variable in storage

    event ValueChanged(address indexed by, uint256 newValue);
    error NotOwner(address caller);

    modifier onlyOwner() {
        if (msg.sender != owner) revert NotOwner(msg.sender);
        _;
    }

    constructor(uint256 initialValue) {
        owner = msg.sender;
        storedValue = initialValue;
    }

    function set(uint256 newValue) external onlyOwner {
        storedValue = newValue;
        emit ValueChanged(msg.sender, newValue);
    }

    function get() external view returns (uint256) {
        return storedValue;
    }
}

The following list explains each part.

  • SPDX licence identifier: a machine-readable comment stating the source licence; the compiler warns if it is missing.
  • Version pragma: ^0.8.28 accepts any compiler from 0.8.28 up to, but not including, 0.9.0.
  • State variables: owner and storedValue persist between transactions.
  • Event and error declarations: ValueChanged is emitted for off-chain listeners; NotOwner is a custom error.
  • Modifier: onlyOwner runs its check, then the function body at _.
  • Constructor: runs once at deployment with the given argument.
  • Functions: set changes state; get is a free view function.
Match the pairs

Parts of a contract

Exam skills 2.1.1 · 2.1.5

Match each line from a contract with the part of the contract it is.

Log in to try this activity Interactive activities are free with an account.

Comments, NatSpec and style

Solidity supports // and /* */ comments, and NatSpec comments (/// or /** */) with tags such as @notice, @param and @return, which tools and block explorers display to users. The official style guide uses CapWords for contracts, events and errors, mixedCase for functions and variables, and UPPER_CASE for constants.

Sort into groups

Style guide names

Exam skills 2.1.1

Sort each name by the naming style the Solidity style guide uses for it.

Log in to try this activity Interactive activities are free with an account.
Worked example

Reading a version pragma

A file declares pragma solidity >=0.7.0 <0.9.0;. A project's Hardhat configuration selects compiler 0.8.34. Will it compile, and is the pragma good practice?

It compiles, because 0.8.34 is in the range. But the range also admits 0.7.x compilers, which have unchecked arithmetic and different rules, so the same source could behave differently depending on the compiler. A narrower pragma such as ^0.8.28 (or a fixed version for deployed contracts) documents the version the code was tested with and prevents accidental compilation with a very different compiler.

Worked example

Choosing the tool

A developer wants to try a 30-line contract idea in ten minutes; a team is starting a token project with continuous integration and deployments to Sepolia and mainnet. Which tool suits each?

Remix suits the quick experiment: no installation, instant compilation and a simulated chain in the browser. The team needs Hardhat (or Foundry): source control, automated tests run on every change, scripted and repeatable deployments, and secrets kept out of the code.

Check yourself

Which compiler versions does pragma solidity ^0.8.20; accept?

Show answer

Any version from 0.8.20 up to, but not including, 0.9.0. The caret allows patch updates within the same minor version, because breaking changes happen at minor versions.

Exam Tips

Remember for the exam

  • A Solidity file starts with an SPDX licence comment and a version pragma.
  • ^0.8.28 allows 0.8.28 up to but excluding 0.9.0.
  • Remix for quick, browser-based work; Hardhat or Foundry for projects with tests and deployments.
  • Contracts contain state variables, events, errors, modifiers, a constructor and functions.