Solidity theorytheory 0/50 · 0%
Interfaces · easy

14. Events and logs

How contracts talk to the outside world.

Events write to the transaction log, which contracts cannot read but indexers, wallets and front-ends can. Up to three parameters may be `indexed`, becoming searchable topics.

event Staked(address indexed user, uint256 amount);

function stake(uint256 amount) external {
    bal[msg.sender] += amount;
    emit Staked(msg.sender, amount);
}

Logs are much cheaper than storage, so emit an event for every state change you would want to reconstruct later.

Check your understanding

  1. 1. Can a contract read past events?

  2. 2. How many parameters can be indexed?

  3. 3. Why emit events?