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
| Type | Description | Example |
|---|---|---|
uint256 | Unsigned integer (0 to 2²⁵⁶-1) | uint256 balance = 100; |
address | 20-byte Ethereum address | address owner = msg.sender; |
bool | Boolean | bool isActive = true; |
string | UTF-8 string | string name = "ZeroToBlock"; |
bytes32 | Fixed-size byte array | bytes32 hash = keccak256(...); |
mapping | Key-value store | mapping(address => uint) balances; |
Access Modifiers
public: Callable by anyoneprivate: Only callable within the contractinternal: Callable within contract and derived contractsexternal: 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 tokensapprove(): Allow another address to spend your tokensbalanceOf(): 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 ownershiptokenURI(): 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:
- Checks-Effects-Interactions: Always check conditions, update state, then interact with external contracts
- Reentrancy Guards: Prevent recursive calls that drain funds
- Integer Overflow: Use Solidity 0.8+ (built-in overflow checks)
- Access Control: Implement proper role-based permissions
- Auditing: Always get professional audits before mainnet deployment
Solidity Development Tools
| Tool | Purpose |
|---|---|
| Hardhat | Development, testing, deployment framework |
| Foundry | Fast Rust-based testing (forge, cast, anvil) |
| Remix IDE | Browser-based Solidity editor |
| OpenZeppelin | Audited, reusable contract libraries |
| Ethers.js | JavaScript 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.