In this track: Write and read Solidity
advanced · 4 MIN READ

Modifiers and access checks

Reusable wrappers can enforce conditions, but the full code path matters.

LET’S PICTURE IT

A service desk can apply the same membership check before several operations.

SEE IT FOR YOURSELF
A valid counter call, step by stepStep 1 / 4
A valid counter call, step by stepFunction requestincrement()No argumentsBefore executionstored count0A local teaching example with a defined starting stateIncoming callCheck function dispatchCheck coded conditionsRules passConditions passContinue executionA failed check can revert insteadCalculation0 + 1Result: 1Storage writecount = 1Persists if call succeedsCalculating a local value and storing it are separate stepsPersistent statecount1Later readGetter reports 1No new incrementState, logs and browser display have different roles

This example begins with count zero and a request to call increment(). No real contract is deployed.

Animated local teaching model. No wallet, RPC or live blockchain data. Some labels and hashes are abbreviated for readability.

A modifier can wrap execution

Solidity modifiers insert behavior around a function body at the placeholder. This can reduce duplicated checks while making the effective execution sequence less obvious to a casual reader.

Inspect the condition and placement

A check must examine the intended caller and be applied to the relevant function. An unused modifier does not secure anything. Code before or after the placeholder can also have important effects.

Authorization depends on configuration

The owner or role value must be initialized and changeable only under appropriate rules. A correct comparison against the wrong configured address can still produce unwanted control.

Do not generalize the toy

The sample checks an immutable owner for a simple setting. Production authorization can involve upgrades, inheritance, external calls and recovery policies beyond this small demonstration.

Read the example

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract LearningExample {
    address public immutable owner = msg.sender;
    uint256 public value;

    modifier onlyOwner() {
        require(msg.sender == owner, "Owner only");
        _;
    }

    function set(uint256 next) external onlyOwner {
        value = next;
    }
}

The modifier is attached to set; its existence alone would not protect another unrestricted setter.

QUICK CHECK · NO PRESSURE

Does merely declaring onlyOwner secure every function automatically?

Your progress stays in this browser.
Keep exploring the original referenceSolidity documentation ↗

Original explanations by EtherTools. Examples are educational and source-linked; this site is independent of the Ethereum Foundation. Our approach.