Solidity Control Flow, Events, Modifiers, Errors and Access Control: Writing Safe Contract Logic

Blockchain Developer 60 min min read Updated: Nov 17, 2026 Intermediate
Solidity Control Flow, Events, Modifiers, Errors and Access Control: Writing Safe Contract Logic
Intermediate Topic 3 of 4

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(); plus revert 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?

  • transfer and send pass only a small fixed gas amount and can break with some contracts
  • call{value: x}("") is the common modern way, but always check the result and protect against reentrancy (see the security section)

Test It in Remix

  1. Deploy with account 1 (the owner)
  2. Switch to account 2, enter a value in the Value box (for example 1 ether) and click deposit
  3. Call withdraw from account 2 and see the revert
  4. Call withdraw from account 1 and watch its balance grow

Practice

  1. Add a balance() view function returning the contract's ETH
  2. Add a modifier that blocks deposits below 0.01 ether
  3. 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.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators