Solidity Cheat Sheet
Essential Solidity syntax for Ethereum smart contracts, covering state variables, functions, modifiers, and events.
Hello World Contract
A minimal contract with a public state variable.
// SPDX-License-Identifier: MITpragma solidity ^0.8.20;contract HelloWorld { string public greeting = "Hello, World!"; function setGreeting(string memory _greeting) public { greeting = _greeting; }}
Contract Structure
State variables, a mapping, and a constructor.
contract Token { string public name; uint256 public totalSupply; address public owner; mapping(address => uint256) public balanceOf; constructor(string memory _name, uint256 _supply) { name = _name; totalSupply = _supply; owner = msg.sender; balanceOf[msg.sender] = _supply; }}
Functions & Modifiers
Visibility, mutability, and reusable checks.
- public- Callable externally and internally; auto-generates a getter for state vars
- private- Callable only within the defining contract
- view- Reads state but doesn't modify it
- pure- Doesn't read or modify contract state
- payable- Marks a function as able to receive Ether
- modifier onlyOwner() { require(msg.sender == owner, "not owner"); _; }- Reusable precondition wrapper
Data Types
Common Solidity value and reference types.
- uint256- Unsigned 256-bit integer (the default uint)
- address- 20-byte Ethereum account/contract address
- mapping(address => uint256)- Hash-map-like key/value storage
- bytes32- Fixed-size 32-byte value
- bool- true/false
- string memory- Dynamically-sized UTF-8 string, memory-allocated
Events
Emitting logs for off-chain listeners.
event Transfer(address indexed from, address indexed to, uint256 value);function transfer(address to, uint256 amount) public returns (bool) { require(balanceOf[msg.sender] >= amount, "insufficient balance"); balanceOf[msg.sender] -= amount; balanceOf[to] += amount; emit Transfer(msg.sender, to, amount); return true;}
Mappings & Structs
Store keyed data with mappings and custom struct types.
struct Account { uint256 balance; bool active;}mapping(address => Account) public accounts;mapping(address => mapping(address => uint256)) public allowance;function deposit() external payable { Account storage a = accounts[msg.sender]; a.balance += msg.value; a.active = true;}
require, revert & Custom Errors
Validate inputs and revert with gas-efficient errors.
error InsufficientBalance(uint256 available, uint256 required);function withdraw(uint256 amount) external { require(amount > 0, "amount must be positive"); uint256 bal = balances[msg.sender]; if (bal < amount) { revert InsufficientBalance(bal, amount); } balances[msg.sender] = bal - amount; (bool ok, ) = msg.sender.call{value: amount}(""); require(ok, "transfer failed");}
Inheritance & Interfaces
Compose contracts and enforce external ABIs.
interface IERC20 { function transfer(address to, uint256 amount) external returns (bool);}abstract contract Ownable { address public owner = msg.sender; modifier onlyOwner() { require(msg.sender == owner, "not owner"); _; }}contract Vault is Ownable { function rescue(IERC20 token, uint256 amt) external onlyOwner { token.transfer(owner, amt); }}
Global Variables
Special variables and units available in every function.
- msg.sender- address that called the current function
- msg.value- wei sent with the call (payable functions)
- block.timestamp- Unix time of the current block
- block.number- current block height
- tx.origin- original EOA that started the tx chain (avoid for auth)
- address(this).balance- contract's own ether balance in wei
- 1 ether / 1 gwei- ether unit denominations = 1e18 / 1e9 wei
- keccak256(abi.encode(...))- hash of ABI-encoded data
Reentrancy Guard
Checks-effects-interactions and a mutex modifier.
bool private locked;modifier nonReentrant() { require(!locked, "reentrant call"); locked = true; _; locked = false;}function claim() external nonReentrant { uint256 amount = rewards[msg.sender]; rewards[msg.sender] = 0; // effects first (bool ok, ) = msg.sender.call{value: amount}(""); // interaction last require(ok);}
Always use the checks-effects-interactions pattern — update state before making external calls — to prevent reentrancy attacks.