To write your first smart contract in Solidity, open the Remix IDE in your browser, create a file ending in .sol, add a license comment, a pragma line and a contract with a state variable and functions, then compile and deploy it to a built-in test chain. A short greeting contract is enough to show every basic part, and you can run it in a few minutes without installing anything.
This guide walks through a first solidity smart contract line by line, then shows how to test it locally with Hardhat.
Solidity is a statically typed, contract-oriented language that compiles to EVM bytecode. It is developed at the Solidity project and its compiler is called solc. Current releases are in the 0.8.x series. Since 0.8.0 arithmetic is checked, so integer overflow reverts instead of wrapping silently.
Open the Remix IDE at remix.ethereum.org. In the file explorer create Greeter.sol and paste the following code:
Go through the file from top to bottom.
// SPDX-License-Identifier: MIT declares the license. The compiler warns if it is missing.pragma solidity ^0.8.24; says the file compiles with version 0.8.24 or newer within 0.8.x. It protects you from building with an incompatible compiler.contract Greeter { ... } defines the contract, similar to a class.string public greeting; is a state variable stored on chain. The public keyword makes the compiler generate a getter function named greeting().address public immutable owner; is set once in the constructor and then stored in the bytecode, which is cheaper to read than normal storage.event and emit write a log entry that off-chain apps can listen for.error NotOwner(); is a custom error. It is cheaper than a revert string and can be caught by tools.constructor runs once at deployment. msg.sender is the account that sent the current call.setGreeting changes state. calldata marks a read-only argument taken straight from the transaction data, and external means it is called from outside the contract.greet is view, so it only reads state and can be called without a transaction. string.concat joins strings and is available since 0.8.12."Hello" with the quotes, and click Deploy.greet and you should see Hello! in the output.setGreeting and send the transaction. Calling greet now returns the new text.setGreeting again. The transaction reverts with the NotOwner error, which proves the access rule works.Calls that read data are free and instant. Calls that change state create a transaction and use gas, which you can see in the terminal pane at the bottom.
Remix is good for learning, but real projects use tests that run on every change. Create a project and install the toolbox:
Put Greeter.sol in the contracts folder and create test/Greeter.js:
Run the tests with:
Tool configuration changes between Hardhat major versions, so follow the project's current getting-started guide if the commands differ. Foundry is a popular alternative where tests are written in Solidity itself.
memory, storage and calldata. Strings and arrays need an explicit data location in function parameters.view when it writes state, which the compiler rejects.Use a recent 0.8.x release and pin it in the pragma or the build configuration. Avoid versions below 0.8.0 for new work, because they lack built-in overflow checks.
It makes the compiler generate a read-only getter function with the variable's name. It does not make the variable writable by others.
No. Remix VM and Hardhat Network are local test chains with free accounts. For a public network you only need free test ETH from a faucet.
A view function can read state but not change it. A pure function neither reads nor changes state and depends only on its arguments.
A first Solidity contract needs a license comment, a pragma line, a contract block, state variables, a constructor and a few functions. You can compile and deploy it in Remix within minutes, using a local test chain, then move to Hardhat tests for repeatable checks. Learn the role of public, immutable, events, custom errors and the view modifier, and always test access control alongside the happy path.