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

Read a complete small contract

Trace inputs, permission, state and logs in one self-contained example.

LET’S PICTURE IT

Read a small rulebook from start to finish instead of guessing its rules from the button names.

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.

Identify stored values

The sample remembers count and an immutable owner. The owner is set from deployment context, so the actual deployer matters. The public getters expose values without being state-changing setters.

Follow the permission path

Only set checks the owner. increment is intentionally open. Do not infer that the owner modifier protects functions where it is not applied.

Follow effects and records

Both successful actions can change count and emit a record. Logs help applications observe updates, while the persistent variable supplies state for later reads.

State the scope of the example

The contract has no token accounting, upgrade mechanism or external calls. Compiling it checks syntax and type consistency, not every property a production application might need.

Read the example

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

contract LearningExample {
    address public immutable owner = msg.sender;
    uint256 public count;
    event Changed(uint256 next);

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

    function increment() external {
        count += 1;
        emit Changed(count);
    }

    function set(uint256 next) external onlyOwner {
        count = next;
        emit Changed(next);
    }
}

A full toy example used by the local compiler checks. It does not manage funds.

QUICK CHECK · NO PRESSURE

In the sample, can any caller use increment while only the owner can use set?

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.