AI Generate Solidity docs instantly

Solidity Cheat Sheet

Quick reference guide with copy-paste ready code snippets

Try DocuWriter Free

Getting Started

4 snippets

Smart contract basics

Hello World Contract

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

contract HelloWorld {
    string public message = "Hello, World!";
}

Pragma Version

pragma solidity ^0.8.0;  // >= 0.8.0 < 0.9.0
pragma solidity 0.8.20;  // Exact version
pragma solidity >=0.8.0 <0.9.0;  // Range

SPDX License

// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: GPL-3.0
// SPDX-License-Identifier: UNLICENSED

Comments

// Single line comment

/*
 * Multi-line
 * comment
 */

/// NatSpec documentation

Data Types

6 snippets

Solidity type system

Integers

uint256 public count;  // 0 to 2^256-1
int256 public balance;  // -2^255 to 2^255-1
uint8 age;  // 0 to 255
int8 temp;  // -128 to 127

Address

address public owner;
address payable public recipient;

owner = msg.sender;
recipient = payable(msg.sender);

Boolean

bool public isActive = true;
bool public isPaused = false;

Bytes

bytes32 public hash;
bytes public data;

hash = keccak256(abi.encodePacked("text"));

String

string public name = "MyToken";
string public symbol = "MTK";

Enums

enum Status { Pending, Active, Completed }
Status public currentStatus = Status.Pending;

State Variables & Visibility

4 snippets

Contract storage

State Variables

contract MyContract {
    uint256 public count;  // Storage
    address private owner;  // Storage
    mapping(address => uint) balances;  // Storage
}

Visibility

uint public x;     // Accessible externally
uint private y;    // Only this contract
uint internal z;   // This + derived contracts
uint external w;   // Only for functions, not state vars

Constants

uint public constant MAX_SUPPLY = 1000000;
address public constant BURN_ADDRESS = address(0);

Immutable

address public immutable owner;

constructor() {
    owner = msg.sender;  // Set once in constructor
}

Tired of looking up syntax?

DocuWriter.ai generates documentation and explains code using AI.

Try Free

Functions

6 snippets

Function declarations and modifiers

Function Basics

function add(uint a, uint b) public pure returns (uint) {
    return a + b;
}

Function Visibility

function publicFunc() public { }
function privateFunc() private { }
function internalFunc() internal { }
function externalFunc() external { }

View & Pure

function getBalance() public view returns (uint) {
    return balance;  // Reads state
}

function calculate(uint x) public pure returns (uint) {
    return x * 2;  // No state access
}

Payable Functions

function deposit() public payable {
    balance[msg.sender] += msg.value;
}

Multiple Returns

function getUser() public view returns (string memory, uint) {
    return (name, age);
}

(string memory n, uint a) = getUser();

Function Modifiers

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

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

Control Flow

6 snippets

Conditionals and loops

If Statement

if (balance >= amount) {
    balance -= amount;
} else {
    revert("Insufficient balance");
}

Require

require(msg.sender == owner, "Not authorized");
require(amount > 0, "Amount must be positive");

Assert

assert(balance >= 0);  // Internal error check

Revert

if (paused) {
    revert("Contract is paused");
}

For Loop

for (uint i = 0; i < 10; i++) {
    total += i;
}

While Loop

uint i = 0;
while (i < 10) {
    sum += i;
    i++;
}

Mappings & Arrays

5 snippets

Complex data structures

Mapping

mapping(address => uint) public balances;

balances[msg.sender] = 100;
uint bal = balances[msg.sender];

Nested Mapping

mapping(address => mapping(address => uint)) public allowances;

allowances[owner][spender] = amount;

Dynamic Array

uint[] public numbers;

numbers.push(10);
numbers.push(20);
uint first = numbers[0];
uint len = numbers.length;

Fixed Array

uint[5] public fixedArray;
address[3] public admins;

Array Operations

numbers.push(30);     // Add to end
numbers.pop();        // Remove last
delete numbers[0];    // Set to 0, doesn't reduce length

Events & Errors

4 snippets

Logging and error handling

Define Event

event Transfer(address indexed from, address indexed to, uint amount);

Emit Event

emit Transfer(msg.sender, recipient, amount);

Indexed Parameters

event Deposit(address indexed user, uint indexed amount, uint timestamp);

Custom Errors

error InsufficientBalance(uint requested, uint available);

if (balance < amount) {
    revert InsufficientBalance(amount, balance);
}

Advanced Patterns

4 snippets

Inheritance, interfaces, and more

Constructor

constructor(string memory _name) {
    name = _name;
    owner = msg.sender;
}

Inheritance

contract Parent {
    uint public x;
}

contract Child is Parent {
    function setX(uint _x) public {
        x = _x;
    }
}

Interface

interface IERC20 {
    function transfer(address to, uint amount) external returns (bool);
    function balanceOf(address account) external view returns (uint);
}

Fallback & Receive

receive() external payable {
    // Handle plain ETH transfers
}

fallback() external payable {
    // Handle unknown function calls
}

More Cheat Sheets

FAQ

Frequently asked questions

What is a Solidity cheat sheet?

A Solidity cheat sheet is a quick reference guide containing the most commonly used syntax, functions, and patterns in Solidity. It helps developers quickly look up syntax without searching through documentation.

How do I learn Solidity quickly?

Start with the basics: variables, control flow, and functions. Use this cheat sheet as a reference while practicing. For faster learning, try DocuWriter.ai to automatically explain code and generate documentation as you learn.

What are the most important Solidity concepts?

Key Solidity concepts include variables and data types, control flow (if/else, loops), functions, error handling, and working with data structures like arrays and objects/dictionaries.

How can I document my Solidity code?

Use inline comments for complex logic, docstrings for functions and classes, and README files for projects. DocuWriter.ai can automatically generate professional documentation from your Solidity code using AI.

Code Conversion Tools

Convert Solidity to Other Languages

Easily translate your Solidity code to other programming languages with our AI-powered converters

Related resources

Stop memorizing. Start shipping.

Generate Solidity Docs with AI

DocuWriter.ai automatically generates comments, docstrings, and README files for your code.

Auto-generate comments
Create README files
Explain complex code
API documentation
Start Free - No Credit Card

Join 33,700+ developers saving hours every week