🏠 Home
Beginner
01 — Introduction to Solidity 02 — Setting Up Your Environment 03 — Your First Smart Contract 04 — Data Types & Variables 05 — Functions & Visibility 06 — Control Flow 07 — Arrays & Mappings 08 — Structs & Enums
Intermediate
09 — Events & Logging 10 — Modifiers 11 — Inheritance 12 — Interfaces & Abstract Contracts 13 — Error Handling 14 — Ether & Wei 15 — Payable Functions 16 — msg.sender & msg.value 17 — Storage vs Memory vs Stack
Advanced
18 — Gas Optimization 19 — ERC-20 Tokens 20 — ERC-721 NFT Standard 21 — Contract Security 22 — Reentrancy Attacks 23 — Oracles & Chainlink 24 — Upgradeable Contracts 25 — Deploying to Mainnet
SolidityMaster / Lesson 03
Lesson 03 of 25

Your First Smart Contract

Write, compile, and deploy a SimpleStorage contract step-by-step.

Beginner

The SimpleStorage Contract

Let's build a contract that stores and retrieves a number. This covers the core lifecycle: write → compile → deploy → interact.
Solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract SimpleStorage {
    // State variable — stored permanently on the blockchain
    uint256 private storedNumber;

    // Write to storage (costs gas)
    function store(uint256 _number) public {
        storedNumber = _number;
    }

    // Read from storage (free — view function)
    function retrieve() public view returns (uint256) {
        return storedNumber;
    }
}

Understanding the Code

Let's break down each part:
  • // SPDX-License-Identifier: MIT — Required license comment. Use MIT for open-source.
  • pragma solidity ^0.8.0; — Accept compiler versions 0.8.0 and above (but not 0.9.0).
  • uint256 private storedNumber; — A state variable that persists on the blockchain.
  • public view returns (uint256)view means the function reads but doesn't write state.

Deploying in Remix

  1. Paste the contract into a new file in Remix
  2. Go to the Solidity Compiler tab and click Compile
  3. Go to Deploy & Run Transactions
  4. Select JavaScript VM (London) as the environment
  5. Click Deploy
  6. Expand the deployed contract and call store(42)
  7. Call retrieve() — you should see 42 returned

Adding a Constructor

Constructors run once at deployment time. Use them to set initial state.
Solidity
contract SimpleStorage {
    uint256 private storedNumber;
    address public owner;

    // Constructor runs once at deployment
    constructor(uint256 _initialValue) {
        storedNumber = _initialValue;
        owner = msg.sender; // the deploying address
    }

    function store(uint256 _number) public {
        storedNumber = _number;
    }

    function retrieve() public view returns (uint256) {
        return storedNumber;
    }
}