events.rst 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. Events
  2. ======
  3. In Solidity, contracts can emit events that signal that changes have occurred. For example, a Solidity
  4. contract could emit a `Deposit` event, or `BetPlaced` in a poker game. These events are stored
  5. in the blockchain transaction log, so they become part of the permanent record. From Solidity's perspective,
  6. you can emit events but you cannot access events on the chain.
  7. Once those events are added to the chain, an off-chain application can listen for events. For example, the Web3.js
  8. interface has a `subscribe()` function. Another is example is
  9. `Hyperledger Burrow <https://hyperledger.github.io/burrow/#/reference/vent>`_
  10. which has a vent command that listens to events and inserts them into a database.
  11. An event has two parts. First, there is a limited set of topics. Usually there are no more than 3 topics,
  12. and each of those has a fixed length of 32 bytes. They are there so that an application listening for events
  13. can easily filter for particular types of events, without needing to do any decoding. There is also a data
  14. section of variable length bytes, which is ABI encoded. To decode this part, the ABI for the event must be known.
  15. From Solidity's perspective, an event has a name, and zero or more fields. The fields can either be ``indexed`` or
  16. not. ``indexed`` fields are stored as topics, so there can only be a limited number of ``indexed`` fields. The other
  17. fields are stored in the data section of the event. The event name does not need to be unique; just like
  18. functions, they can be overloaded as long as the fields are of different types, or the event has
  19. a different number of arguments.
  20. The first topic is used to identify the event, this is a hash of the event signature, also known
  21. as the selector. If the event is declared ``anonymous`` then this is omitted, and an additional
  22. ``indexed`` field is available. The selector is available in Solidity using the ``eventname.selector``
  23. syntax.
  24. .. warning::
  25. On Solana, the ``indexed`` event field attribute has no effect. All event attributes will be encoded as data to
  26. be passed for Solana's ``sol_log_data`` system call, regardless if the ``indexed`` keyword is present or not.
  27. This behavior follows what Solana's Anchor framework does.
  28. In Polkadot, field topics are culculated the same as in `ink! v5.0 <https://use.ink/basics/events/#topics>`_:
  29. Topic fields are either the encoded value of the field or its blake2b256 hash
  30. if the encoded value length exceeds 32 bytes.
  31. An event can be declared in a contract, or outside.
  32. .. include:: ../examples/events.sol
  33. :code: solidity
  34. Like function calls, the emit statement can have the fields specified by position, or by field name. Using
  35. field names rather than position may be useful in case the event name is overloaded, since the field names
  36. make it clearer which exact event is being emitted.
  37. .. include:: ../examples/event_positional_fields.sol
  38. :code: solidity
  39. In the transaction log, the topic of an event is the blake2b256 hash of the signature of the
  40. event. The signature is the event name, followed by the fields types in a comma separated list in parentheses,
  41. like event signatures in `Ethereum Solidity <https://docs.soliditylang.org/en/v0.8.25/abi-spec.html#events>`_. So
  42. the first topic for the second UserModified event would be the blake2b256 hash of ``UserModified(address,uint64)``.
  43. You can leave this topic out by declaring the event ``anonymous``. This makes the event slightly smaller (32 bytes
  44. less) and makes it possible to have 4 ``indexed`` fields rather than 3.