# ADR 032: Typed Events
- 28-Sept-2020: Initial Draft
- Anil Kumar (@anilcse)
- Jack Zampolin (@jackzampolin)
- Adam Bozanich (@boz)
Currently in the Cosmos SDK, events are defined in the handlers for each message as well as
EndBlock. Each module doesn't have types defined for each event, they are implemented as
map[string]string. Above all else this makes these events difficult to consume as it requires a great deal of raw string matching and parsing. This proposal focuses on updating the events to use typed events defined in each module such that emiting and subscribing to events will be much easier. This workflow comes from the experience of the Akash Network team.
Currently in the Cosmos SDK, events are defined in the handlers for each message, meaning each module doesn't have a cannonical set of types for each event. Above all else this makes these events difficult to consume as it requires a great deal of raw string matching and parsing. This proposal focuses on updating the events to use typed events defined in each module such that emiting and subscribing to events will be much easier. This workflow comes from the experience of the Akash Network team.
Our platform (opens new window) requires a number of programatic on chain interactions both on the provider (datacenter - to bid on new orders and listen for leases created) and user (application developer - to send the app manifest to the provider) side. In addition the Akash team is now maintaining the IBC
relayer (opens new window), another very event driven process. In working on these core pieces of infrastructure, and integrating lessons learned from Kubernetes developement, our team has developed a standard method for defining and consuming typed events in Cosmos SDK modules. We have found that it is extremely useful in building this type of event driven application.
As the Cosmos SDK gets used more extensively for apps like
peggy, other peg zones, IBC, DeFi, etc... there will be an exploding demand for event driven applications to support new features desired by users. We propose upstreaming our findings into the Cosmos SDK to enable all Cosmos SDK applications to quickly and easily build event driven apps to aid their core application. Wallets, exchanges, explorers, and defi protocols all stand to benefit from this work.
If this proposal is accepted, users will be able to build event driven Cosmos SDK apps in go by just writing
EventHandlers for their specific event types and passing them to
EventEmitters that are defined in the Cosmos SDK.
The end of this proposal contains a detailed example of how to consume events after this refactor.
This proposal is specifically about how to consume these events as a client of the blockchain, not for intermodule communication.
Step-1: Implement additional functionality in the
EmitTypedEvent is a method on
EventManager which takes typed event as input and apply json serialization on it. Then it maps the JSON key/value pairs to
event.Attributes and emits it in form of
Event.Type will be the type URL of the proto message.
When we subscribe to emitted events on the tendermint websocket, they are emitted in the form of an
ParseTypedEvent parses the event back to it's original proto message.
Step-2: Add proto definitions for typed events for msgs in each module:
For example, let's take
gov module and implement this event's type.
Step-3: Refactor event emission to use the typed event created and emit using
# How to subscribe to these typed events in
NOTE: Full code example below
Users will be able to subscribe using
client.Context.Client.Subscribe and consume events which are emitted using
Please see the below code sample for more detail on this flow looks for clients.
- Improves consistency of implementation for the events currently in the Cosmos SDK
- Provides a much more ergonomic way to handle events and facilitates writing event driven applications
- This implementation will support a middleware ecosystem of
# Detailed code example of publishing events
This ADR also proposes adding affordances to emit and consume these events. This way developers will only need to write
EventHandlers which define the actions they desire to take.