typestar

Events in Solidity

The log: indexed fields become searchable topics.

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

// Events are the log: indexed fields become searchable topics.
contract HarvestLog {
    event Picked(address indexed picker, string variety, uint256 kg);
    event CrateSealed(uint256 indexed crateId);

    uint256 public nextCrate = 1;

    function record(string calldata variety, uint256 kg) external {
        emit Picked(msg.sender, variety, kg);
    }

    function seal() external {
        emit CrateSealed(nextCrate);
        nextCrate += 1;
    }
}

How it works

  1. event Picked(address indexed picker, ...) declares the shape.
  2. emit writes to the chain's log, not to state.
  3. Indexed fields filter cheaply offchain; the rest is data.

Keywords and builtins used here

The run, in numbers

Lines
19
Characters to type
466
Tokens
79
Three-star pace
60 tpm

At the three-star pace of 60 tokens a minute, this run takes about 79 seconds.

Type this snippet

Step 1 of 3 in Ether & events, step 11 of 28 in Language basics.

← Previous Next →

Events in other languages