Ethereum
18 min

Learn Solidity & Smart Contracts: Complete Ethereum Developer Guide

Published
· Updated

What Is Solidity?

Solidity is a statically-typed, contract-oriented programming language designed for writing smart contracts on the Ethereum Virtual Machine (EVM). Created by Gavin Wood in 2014, it's influenced by JavaScript, Python, and C++.

Why Learn Solidity?

  • Market demand: Solidity developers are among the highest-paid in tech
  • DeFi: Decentralized Finance protocols managing billions are written in Solidity
  • NFTs: Marketplaces like OpenSea rely on Solidity smart contracts
  • DAOs: Decentralized governance systems use Solidity
  • Innovation: Build applications that were impossible before blockchain

Understanding Smart Contracts

A smart contract is a self-executing program stored on the blockchain. Once deployed, its code cannot be changed (immutable), and anyone can interact with it without needing permission.

Real-World Analogy

Think of a vending machine: you put in money, select an item, and the machine automatically dispenses it. No operator needed. A smart contract works the same way — but for any programmable agreement.

The Ethereum Virtual Machine (EVM)

The EVM is the runtime environment for smart contracts. Key concepts:

Gas

Every operation on the EVM costs gas — a unit measuring computational effort:

  • Simple transfer: ~21,000 gas
  • Token transfer (ERC-20): ~65,000 gas
  • Complex DeFi swap: ~150,000+ gas

Gas price fluctuates with network demand. Understanding gas optimization is critical for professional Solidity development.

State & Storage

The EVM maintains a global state. Each contract has:

  • Storage: Persistent data (expensive to write)
  • Memory: Temporary data (cheap, cleared after function execution)
  • Stack: EVM's working memory (free, limited to 1024 items)

Solidity Basics

Contract Structure

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

contract SimpleStorage {
    // State variable (stored on blockchain)
    uint256 public storedValue;

    // Event (emitted for off-chain listeners)
    event ValueChanged(uint256 newValue);

    // Function to store a value
    function store(uint256 _value) public {
        storedValue = _value;
        emit ValueChanged(_value);
    }

    // View function (reads state, no gas cost when called externally)
    function retrieve() public view returns (uint256) {
        return storedValue;
    }
}

Data Types

TypeDescriptionExample
uint256Unsigned integer (0 to 2²⁵⁶-1)uint256 balance = 100;
address20-byte Ethereum addressaddress owner = msg.sender;
boolBooleanbool isActive = true;
stringUTF-8 stringstring name = "ZeroToBlock";
bytes32Fixed-size byte arraybytes32 hash = keccak256(...);
mappingKey-value storemapping(address => uint) balances;

Access Modifiers

  • public: Callable by anyone
  • private: Only callable within the contract
  • internal: Callable within contract and derived contracts
  • external: Only callable from outside the contract

Function Modifiers

Custom modifiers enforce conditions:

modifier onlyOwner() {
    require(msg.sender == owner, "Not the owner");
    _;
}

function withdraw() public onlyOwner {
    // Only owner can call this
}

Common Token Standards

ERC-20 (Fungible Tokens)

The standard for tokens like USDC, LINK, and UNI:

  • transfer(): Send tokens
  • approve(): Allow another address to spend your tokens
  • balanceOf(): Check token balance

ERC-721 (Non-Fungible Tokens / NFTs)

Each token is unique with a distinct ID:

  • ownerOf(): Who owns token #42?
  • transferFrom(): Transfer NFT ownership
  • tokenURI(): Metadata for the NFT

ERC-1155 (Multi-Token)

Combines fungible and non-fungible tokens in one contract. Used heavily in gaming.

Security Best Practices

Smart contract bugs can cost millions. Critical patterns:

  1. Checks-Effects-Interactions: Always check conditions, update state, then interact with external contracts
  2. Reentrancy Guards: Prevent recursive calls that drain funds
  3. Integer Overflow: Use Solidity 0.8+ (built-in overflow checks)
  4. Access Control: Implement proper role-based permissions
  5. Auditing: Always get professional audits before mainnet deployment

Solidity Development Tools

ToolPurpose
HardhatDevelopment, testing, deployment framework
FoundryFast Rust-based testing (forge, cast, anvil)
Remix IDEBrowser-based Solidity editor
OpenZeppelinAudited, reusable contract libraries
Ethers.jsJavaScript library for Ethereum interaction

Your Learning Path

Before diving into Solidity, you should understand how blockchain fundamentally works. ZeroToBlock's Bitcoin Proof of Work course for developers teaches hashing, mining, consensus, and transactions through interactive simulations — the perfect foundation before writing smart contracts. See also: learn Bitcoin and blockchain interactively and the full curriculum.

Ready to Learn By Doing?

Stop reading. Start building. Our interactive simulations teach you blockchain mechanics through hands-on experience.

Explore Courses