Solidity Cheat Sheet
Contract skeleton
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract C { uint256 public x; constructor(uint256 _x) { x = _x; } }
Types and variables
bool b; uint256 n; int256 i; address a; address payable p; bytes32 h; string s;
uint256 constant MAX = 100; uint256 immutable start;
mapping(address => uint256) balances; uint256[] list; struct S { uint256 id; string name; }
enum Status { Open, Closed }
Functions
function f(uint256 x) external pure returns (uint256) { return x * 2; }
function g() public view returns (uint256) { return n; }
function pay() external payable { require(msg.value > 0, "No ETH"); }
modifier onlyOwner() { require(msg.sender == owner, "Not owner"); _; }
receive() external payable {} fallback() external payable {}
Globals and errors
msg.sender; msg.value; block.timestamp; block.number; address(this).balance;
error NotAllowed(address who); if (!ok) revert NotAllowed(msg.sender);
require(cond, "message"); assert(invariant);
event Paid(address indexed from, uint256 amount); emit Paid(msg.sender, msg.value);
(bool ok, ) = payable(to).call{value: amount}("");
keccak256(abi.encodePacked(a, b));
Hardhat
// compile, test, deploy
npx hardhat compile
npx hardhat test
npx hardhat node
npx hardhat run scripts/deploy.js --network sepolia
Full Solidity documentation