Events: telling the outside world

Storage is what your contract knows. Events are what it announces. Indexers, wallets and explorers read events, not storage.

events.emit(name, fields) writes a log entry into the transaction receipt. The name is bytes; the fields are a dict.

from stdlib import events

events.emit(b"Transfer", {"from": sender, "to": recipient, "value": amount})

Events do not change state and cannot be read back by a contract — not by yours, not by anyone's. They exist for off-chain consumers: block explorers, wallets showing your balance, an indexer building a leaderboard.

If a call reverts, its events go with it. A receipt only ever contains events from calls that actually succeeded.

Emit after the state change, not before. The event describes something that happened; if you emit first and then hit a require that fails, the event is discarded anyway — but code that reads in the order things occur is easier to audit, and reviewers look for exactly this.

Your turn

Add an event to a transfer-like function. send(to, amount) should deduct from the caller's balance and emit Sent with fields to and amount. Reject overdrafts with b"insufficient".

Hints

Stuck? Ask

Answered by Animica's own free inference network. It is donated GPU capacity, so give it 20-30 seconds.