In this track: Write and read Solidity
Read a complete small contract
Trace inputs, permission, state and logs in one self-contained example.
Read a small rulebook from start to finish instead of guessing its rules from the button names.
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.
In the sample, can any caller use increment while only the owner can use set?
The owner modifier is attached only to set.
Original explanations by EtherTools. Examples are educational and source-linked; this site is independent of the Ethereum Foundation. Our approach.