Loading...
Logic, modifiers, and logging
Solidity provides standard control flow along with blockchain-specific features.
require: Validates conditions. If it fails, the transaction reverts and gas is refunded.
require(msg.sender == owner, "Not the owner");
Modifiers: Reusable conditions attached to functions.
modifier onlyOwner() {
require(msg.sender == owner, "Not owner");
_; // Continue to function body
}
function withdraw() public onlyOwner { ... }
Events: Emit data from a contract that can be listened to by off-chain applications.
event Transfer(address indexed from, address indexed to, uint256 amount);
emit Transfer(msg.sender, recipient, amount);
Events are stored in transaction logs (not contract storage), making them much cheaper than storage writes. Indexed parameters allow efficient filtering.
Error handling:
• require() — Input validation, refunds remaining gas
• revert() — Custom error with a message
• assert() — Checks for conditions that should never be false (internal errors)