* encoding * working on baseapp doc * baseapp work * reorg * almost there * finish first draft * remove old files * module doc start * finish intro * working * workinnn * add transactions into core * hans comments * add transactions into core * working * gautier comments * clean * working * consolidate intro * querier * workiiiing * refactor for new module interface * karoly review * working on baseapp doc * baseapp work * reorg * almost there * finish first draft * remove old files * finish intro * workinnn * initial commit after rebase * query-lifecycle and started modules-interfaces * query-lifecycle first draft done * module interfaces first draft * rest and intro skeletons * rest and intro done * small edits and links * comments * revisions * cli.md comments * comments * minor edits * better flow for query lifecycle * add transactions into core * hans comments * add transactions into core * checkout master-docs files * deleted some * remove modules readme * cli.md comments * comments * module-interfaces comments * Merge PR #4857: Add Context concept doc * working * working * finish messages and queries * handler * querier * last comments! * punctuation * querier2 * consolidate intro * querier * workiiiing * refactor for new module interface * karoly review * working on baseapp doc * baseapp work * reorg * almost there * finish first draft * remove old files * finish intro * workinnn * initial commit after rebase * query-lifecycle and started modules-interfaces * query-lifecycle first draft done * module interfaces first draft * rest and intro skeletons * rest and intro done * small edits and links * comments * revisions * cli.md comments * comments * minor edits * better flow for query lifecycle * checkout master-docs files * deleted some * remove modules readme * cli.md comments * comments * module-interfaces comments * keeper * genesis * finish * Apply suggestions from code review Co-Authored-By: Hans Schoenburg <hschoenburg@users.noreply.github.com> * hans review * Update docs/core/baseapp.md Co-Authored-By: Hans Schoenburg <hschoenburg@users.noreply.github.com> * working * last comment * workin * Apply suggestions from code review * encoding and node * almost finish store * finish docs * fixes * fede comments + permalinks * hans review * add more permalinks * update docs theme version (#5239) * R4R: Docs Cleanup (#5246) * start * work * work * work * remove table of content * links intro * fix links * remove junk * cleanup * cleanup * work * finish cleanup * addback readmes * remove nft * fix links * remove dup * remove dup * remove dup * remove dup * remove dup * fix links * add subscribe events * refine rest * index page * sidebar * theme version * theme version * testing netlify * theme version * tooltip example * version * testing code embedding * reverting back * theme version * version * version * version * readme and version * cleanup * redo app anatomy * modules readme, theme version * theme version * fix modules list * theme version * new snippets * modules readme * update docs readme * modify synopsis * version * fix yaml * version * version * version * version * version * version * version * version * version * version * add hide banner * version * version * version * small fixes * modules readme, version * remove hotkeys dep, version * version * version * version * version * version * version * version * slight notice * fix links and hide * permalinks * small clean * version * resolve conflicts, add google analytics * fix merge remants * version * changelog 1/2 * Changelog: docs UI * version * remove merge conflicts * Code: Update link for Contributing to the docs to docs_readme * HTML/CSS: Update layout of homepage footer to match new layout in Figma * version * final modifs * modules, version * modules readme * link to module list from homepage * version * building modules link * version * version * fonts * version * version * fix link * fix package.json * links in explore sdk section * core concepts * version * change delimeters for frontmatter * frontmatter in comments * version * temp add tiny-cookie * fixed link issues * fixed styling issues, copy * hide frontmatter * hide frontmatter * layout fixes, padded ascii diagram * fira sans font for code
4.6 KiB
Events
Pre-Requisite Readings {hide}
- Anatomy of an SDK application {prereq}
Events
Events are implemented in the Cosmos SDK as an alias of the ABCI event type.
+++ bc572217c0/abci/types/types.pb.go (L2661-L2667)
They contain:
- A
typeof typestring, which can refer to the type of action that led to theevent's emission (e.g. a certain value going above a threshold), or to the type ofmessageif the event is triggered at the end of thatmessageprocessing. - A list of
attributes, which are key-value pairs that give more information about theevent. +++7d7821b9af/types/events.go (L51-L56)
Events are returned to the underlying consensus engine in the response of the following ABCI messages: CheckTx, DeliverTx, BeginBlock and EndBlock.
Typically, event types and attributes are defined on a per-module basis in the module's /internal/types/events.go file, and triggered from the module's handler via the EventManager.
EventManager
In Cosmos SDK applications, events are generally managed by an object called the EventManager. It is implemented as a simple wrapper around a slice of events:
+++ 7d7821b9af/types/events.go (L16-L20)
The EventManager comes with a set of useful methods to manage events. Among them, the one that is used the most by module and application developers is the EmitEvent method, which registers an event in the EventManager.
+++ 7d7821b9af/types/events.go (L29-L31)
Typically, module developers will implement event emission via the EventManager in the handler of modules, as well as in the BeginBlocker and/orEndBlocker functions. The EventManager is accessed via the context ctx, and event emission generally follows this pattern:
ctx.EventManager().EmitEvent(
sdk.NewEvent(
eventType, // e.g. sdk.EventTypeMessage for a message, types.CustomEventType for a custom event defined in the module
sdk.NewAttribute(attributeKey, attributeValue),
),
)
See the handler concept doc for a more detailed view on how to typically implement events and use the EventManager in modules.
Subscribing to events
It is possible to subscribe to events via Tendermint's Websocket. This is done by calling the subscribe RPC method via Websocket:
{
"jsonrpc": "2.0",
"method": "subscribe",
"id": "0",
"params": {
"query": "tm.event='eventCategory' AND type.attribute='attributeValue'"
}
}
The main eventCategory you can subscribe to are:
NewBlock: Containseventstriggered duringBeginBlockandEndBlock.Tx: Containseventstriggered duringDeliverTx(i.e. transaction processing).ValidatorSetUpdates: Contains validator set updates for the block.
These events are triggered from the state package after a block is committed. You can get the full list of event categories here.
The type and attribute value of the query allow you to filter the specific event you are looking for. For example, a transfer transaction triggers an event of type Transfer and has Recipient and Sender as attributes (as defined in the events file of the bank module). Subscribing to this event would be done like so:
{
"jsonrpc": "2.0",
"method": "subscribe",
"id": "0",
"params": {
"query": "tm.event='Tx' AND transfer.sender='senderAddress'"
}
}
where senderAddress is an address following the AccAddress format.
Next {hide}
Learn about object-capabilities {hide}