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.
npx hardhat buildA 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.
Remix or Hardhat?
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.28accepts any compiler from 0.8.28 up to, but not including, 0.9.0. - State variables:
ownerandstoredValuepersist between transactions. - Event and error declarations:
ValueChangedis emitted for off-chain listeners;NotOwneris a custom error. - Modifier:
onlyOwnerruns its check, then the function body at_. - Constructor: runs once at deployment with the given argument.
- Functions:
setchanges state;getis a freeviewfunction.
Parts of a contract
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.
Style guide names
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.
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.
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.