Solidity Control Flow, Events, Modifiers, Errors and Access Control: Writing Safe Contract Logic
Conditions and Loops
solidity
function grade(uint256 marks) public pure returns (string memory) {
if (marks >= 75) return "A";
else if (marks >= 50) return "B";
return "C";
}
function sum(uint256[] memory arr) public pure returns (uint256 total) {
for (uint256 i = 0; i < arr.length; i++) {
total += arr[i];
}
}
Loops cost gas Looping over a list that can grow without limit can make a function too expensive to ever run. Avoid unbounded loops in write functions.
require, revert and Custom Errors
solidity
error NotOwner();
function withdrawAll() public {
if (msg.sender != owner) revert NotOwner();
require(address(this).balance > 0, "Nothing to withdraw");
// ...
}
require(condition, message)stops and undoes all changes if false- Custom errors (
error Name();plusrevert Name();) cost less gas than long strings - A failed transaction is reverted, but gas used is still charged
Events
Events write logs on the blockchain. Apps and block explorers listen to them. They are cheaper than storage.
solidity
event Deposited(address indexed user, uint256 amount);
function deposit() public payable {
emit Deposited(msg.sender, msg.value);
}
indexed lets you search logs by that value.
Modifiers
solidity
modifier onlyOwner() {
if (msg.sender != owner) revert NotOwner();
_; // the function body runs here
}
function setFee(uint256 f) public onlyOwner {
fee = f;
}
A Complete Example: Piggy Bank
solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract PiggyBank {
address public immutable owner;
mapping(address => uint256) public deposits;
event Deposited(address indexed user, uint256 amount);
event Withdrawn(address indexed owner, uint256 amount);
error NotOwner();
error NothingToWithdraw();
error TransferFailed();
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
if (msg.sender != owner) revert NotOwner();
_;
}
function deposit() external payable {
deposits[msg.sender] += msg.value;
emit Deposited(msg.sender, msg.value);
}
function withdraw() external onlyOwner {
uint256 amount = address(this).balance;
if (amount == 0) revert NothingToWithdraw();
emit Withdrawn(owner, amount);
(bool ok, ) = payable(owner).call{value: amount}("");
if (!ok) revert TransferFailed();
}
receive() external payable {
deposits[msg.sender] += msg.value;
emit Deposited(msg.sender, msg.value);
}
}
Why call for Sending ETH?
transferandsendpass only a small fixed gas amount and can break with some contractscall{value: x}("")is the common modern way, but always check the result and protect against reentrancy (see the security section)
Test It in Remix
- Deploy with account 1 (the owner)
- Switch to account 2, enter a value in the Value box (for example 1 ether) and click
deposit - Call
withdrawfrom account 2 and see the revert - Call
withdrawfrom account 1 and watch its balance grow
Practice
- Add a
balance()view function returning the contract's ETH - Add a modifier that blocks deposits below 0.01 ether
- Why use custom errors?
Conclusion
Use require or custom errors for checks, events for logs, modifiers for reusable rules and call with a success check to send ETH.
Quick Summary: flow; errors; events; modifiers; Piggy Bank; sending ETH; test.

