# ADR {ADR-NUMBER}: Typed Events ## Changelog - {date}: {changelog} ## Status Proposed ## Context Currently in SDK, events are not completely understandable when emitted and being tough for event listeners to handle them. Events need to be modified in a way which can be understandable and useful for event listeners to handle and work on them further if needed. ## Decision We need to modify each action in every module to typed event. Typed event means every event will have it's own type and some methods to modify it. And also we are going to use `BaseModuleEvent` struct for more information regarding event. It is of form: ``` type BaseModuleEvent{ Module string Action string } ``` Now for example, let's take `MsgTxSend` of bank module and modify this event. **Defining typed event for `MsgTxSend`:** ``` type EventTxSend struct { Context BaseModuleEvent FromAddress AccAddress ToAddress AccAddress Amount Coin } func NewEventTxSend(from, to AccAddress, amount Coin) EventTxSend { return EventTxSend{ Context: BaseModuleEvent{ Module: "bank", Action: "tx-send", }, FromAddress: from, ToAddress: to, Amount: amount, } } ``` Here we are defining typed event with module and action information along with attributes of event. We need to add `ToSDKEvent` function to this event to convert it to `types.Event` type and use when emitting Event. ``` func (ev EventTxSend) ToSDKEvent() types.Event { return types.NewEvent("cosmos-sdk-events", sdk.NewAttribute(sdk.AttributeKeyModule, "bank"), sdk.NewAttribute(sdk.AttributeKeyAction, "tx-send"), sdk.NewAttribute(evVersionKey, string(version)), sdk.NewAttribute("from", ev.FromAddress.String()), sdk.NewAttribute("to", ev.ToAddress.String()), sdk.NewAttribute("amount", ev.Amount.String()), ) } ``` **Emitting `EventTxSend`:** We need to use `ToSDKEvent` method of `TxSend` event to emit it. ``` types.Context.EventManager().EmitEvent( NewEventTxSend(fromAddress, toAddress, amount).toSDKEvent(), ) ``` We can subscribe to `EventsClient` of client Context and consume events which are emitted. Those events can be further parsed and handle accordingly. For reference on handling events, please check [this](https://github.com/ovrclk/deploy/blob/master/cmd/event-handlers.go#L57) ## Consequences ### Positive * Would be great for event listeners to handle events accordingly. ### Negative {negative consequences} ### Neutral {neutral consequences} ## References - {reference link}