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

Events in a contract

Logs help applications observe defined execution records.

LET’S PICTURE IT

A program can update its notebook and print a receipt describing the action.

SEE IT FOR YOURSELF
From an emitted Transfer event to a receipt logStep 1 / 4
From an emitted Transfer event to a receipt logToken executionApply coded rulesUpdate relevant balancesEmit Transferfrom · to · valueA structured logA reverted execution portion does not keep its emitted logsTransfer(address,address,uint256)Topic 0: 0xddf252ad…b3efFull digest · 32 bytesEvent topic zero and a function selector use different lengthsTopic 1from addressIndexedTopic 2to addressIndexedDataraw valueNot indexedThis diagram is specifically the common ERC-20 Transfer layoutReceipt logTopics + dataBlock contextApplication viewDecoded fieldsImplementation still mattersThe local event tool does not query or authenticate a deployed token

A token implementation may update balances and emit its standard Transfer event. State and the emitted record are related but distinct.

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

Declare a typed event

An event signature specifies its name and parameter types. Indexed fields occupy topic positions under ABI rules, while remaining fields are encoded into the data region.

Emit during execution

The emit statement records a log in the execution context. A revert removes logs from the reverted portion along with its state changes, so not every attempted emit survives to the final receipt.

A log is not persistent variable storage

Contracts do not ordinarily read arbitrary historical logs as though they were a mapping. Apps can index logs, while later contract logic often needs explicit stored state.

Match the layout

A standard ERC-20 Transfer decoder expects one particular indexed/non-indexed arrangement. Other events, including similar signatures with different indexed fields, need their actual ABI.

Read the example

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

contract LearningExample {
    event Counted(address indexed caller, uint256 value);
    uint256 public count;

    function increment() external {
        count += 1;
        emit Counted(msg.sender, count);
    }
}

The event reports a state update; the persistent count remains a separate variable.

QUICK CHECK · NO PRESSURE

Can a log emitted inside a reverted call remain as a successful surviving log of that reverted portion?

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.