Getting Started
4 snippetsSmart 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; // RangeSPDX License
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: GPL-3.0
// SPDX-License-Identifier: UNLICENSEDComments
// Single line comment
/*
* Multi-line
* comment
*/
/// NatSpec documentationData Types
6 snippetsSolidity 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 127Address
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 snippetsContract 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 varsConstants
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.
Functions
6 snippetsFunction 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 snippetsConditionals 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 checkRevert
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 snippetsComplex 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 lengthEvents & Errors
4 snippetsLogging 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 snippetsInheritance, 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
}