Skip to content

Commit 136601a

Browse files
committed
Added events chapter.
1 parent 3ccd053 commit 136601a

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

.vitepress/config.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export default withMermaid({
4848
items: [
4949
{text: 'Semantics', link: '/guide/cosmwasm-core/architecture/semantics'},
5050
{text: 'Actor model', link: '/guide/cosmwasm-core/architecture/actor-model'},
51+
{text: 'Events', link: '/guide/cosmwasm-core/architecture/events'},
5152
{text: 'Gas', link: '/guide/cosmwasm-core/architecture/gas'},
5253
{text: 'Transactions', link: '/guide/cosmwasm-core/architecture/transactions'},
5354
]
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
[Cosmos Events]: https://docs.cosmos.network/v0.50/learn/advanced/events
2+
3+
<SectionLabel chapter="core" section="architecture"></SectionLabel>
4+
5+
# Events
6+
7+
CosmWasm can emit [Cosmos Events]. These events are stored in the block execution result as
8+
metadata, allowing the contract to attach metadata to what exactly happened during execution.
9+
10+
::: tip :bulb: Tip
11+
12+
Some important details about the keys:
13+
- Whitespaces will be trimmed (i.e. removed from the beginning and end).
14+
- Empty keys (that include keys only consisting of whitespaces) are not allowed.
15+
- Keys _can not_ start with an underscore '**_**', such keys are reserved for **wasmd**.
16+
17+
:::
18+
19+
By default, CosmWasm emits the `wasm` event to which you can add attributes like so:
20+
21+
::: code-group
22+
23+
```Rust [wasm_event.rs]
24+
let response: Response<Empty> = Response::new()
25+
.add_attribute("custom_attribute", "value");
26+
```
27+
28+
:::
29+
30+
## Custom events
31+
32+
As mentioned above, CosmWasm only emits the event `wasm` by default. If you want to emit other
33+
events, such as domain-specific events like `user_added`, you can construct a fully custom event and
34+
attach it to the response.
35+
36+
::: tip :bulb: Tip
37+
38+
Note that those custom events will be prefixed with '**wasm-**' by the runtime.
39+
40+
:::
41+
42+
::: code-group
43+
44+
```Rust [custom_event.rs]
45+
let event = Event::new("custom_event")
46+
.add_attribute("custom_attribute", "value");
47+
48+
let response: Response<Empty> = Response::new()
49+
.add_event(event);
50+
```
51+
52+
:::

0 commit comments

Comments
 (0)