Skip to content

Commit 977a3f4

Browse files
leohhhnnotJoon
andauthored
feat(docs): emit/event docs (#2047)
<!-- please provide a detailed description of the changes made in this pull request. --> ## Description This PR adds docs for emit/event. It adds reference docs, and a section in effective Gno. More practical examples will be added to Gno By Example. Closes: #2003 <details><summary>Contributors' checklist...</summary> - [ ] Added new tests, or not needed, or not feasible - [ ] Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory - [ ] Updated the official documentation or not needed - [ ] No breaking changes were made, or a `BREAKING CHANGE: xxx` message was included in the description - [ ] Added references to related issues and PRs - [ ] Provided any useful hints for running manual tests - [ ] Added new benchmarks to [generated graphs](https://gnoland.github.io/benchmarks), if any. More info [here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md). </details> --------- Co-authored-by: Lee ByeongJun <[email protected]>
1 parent 98c1d64 commit 977a3f4

File tree

4 files changed

+140
-3
lines changed

4 files changed

+140
-3
lines changed

docs/concepts/effective-gno.md

+67
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,73 @@ actual logic. This way, `privateMethod` can only be called from within the
413413
realm, and it can use the caller's address for authentication or authorization
414414
checks.
415415

416+
### Emit Gno events to make life off-chain easier
417+
418+
Gno provides users the ability to log specific occurrences that happened in their
419+
on-chain apps. An `event` log is stored in the ABCI results of each block, and
420+
these logs can be indexed, filtered, and searched by external services, allowing
421+
them to monitor the behaviour of on-chain apps.
422+
423+
It is good practice to emit events when any major action in your code is
424+
triggered. For example, good times to emit an event is after a balance transfer,
425+
ownership change, profile created, etc. Alternatively, you can view event emission
426+
as a way to include data for monitoring purposes, given the indexable nature of
427+
events.
428+
429+
Events consist of a type and a slice of strings representing `key:value` pairs.
430+
They are emitted with the `Emit()` function, contained in the `std` package in
431+
the Gno standard library:
432+
433+
```go
434+
package events
435+
436+
import (
437+
"std"
438+
)
439+
440+
var owner std.Address
441+
442+
func init() {
443+
owner = std.PrevRealm().Addr()
444+
}
445+
446+
func ChangeOwner(newOwner std.Address) {
447+
caller := std.PrevRealm().Addr()
448+
449+
if caller != owner {
450+
panic("access denied")
451+
}
452+
453+
owner = newOwner
454+
std.Emit("OwnershipChange", "newOwner", newOwner.String())
455+
}
456+
457+
```
458+
If `ChangeOwner()` was called in, for example, block #43, getting the `BlockResults`
459+
of block #43 will contain the following data:
460+
461+
```json
462+
{
463+
"Events": [
464+
{
465+
"@type": "/tm.gnoEvent",
466+
"type": "OwnershipChange",
467+
"pkg_path": "gno.",
468+
"func": "ChangeOwner",
469+
"attrs": [
470+
{
471+
"key": "newOwner",
472+
"value": "g1zzqd6phlfx0a809vhmykg5c6m44ap9756s7cjj"
473+
}
474+
]
475+
}
476+
// other events
477+
]
478+
}
479+
```
480+
481+
Read more about events [here](./stdlibs/events.md).
482+
416483
### Contract-level access control
417484

418485
In Gno, it's a good practice to design your contract as an application with its

docs/concepts/stdlibs/coin.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ The `Coins` slice can be included in a transaction made by a user addresses or a
3030
Coins in this set are then available for access by specific types of Bankers,
3131
which can manipulate them depending on access rights.
3232

33-
[//]: # (TODO ADD LINK TO Effective GNO)
34-
Read more about coins in the [Effective Gno](https://docs.gno.land/concepts/effective-gno/#native-tokens) section.
33+
Read more about coins in the [Effective Gno](../effective-gno.md#coins) section.
3534

3635
The Coin(s) API can be found in under the `std` package [reference](../../reference/stdlibs/std/coin.md).

docs/concepts/stdlibs/events.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
id: events
3+
---
4+
5+
# Gno Events
6+
7+
## Overview
8+
9+
Events in Gno are a fundamental aspect of interacting with and monitoring
10+
on-chain applications. They serve as a bridge between the on-chain environment
11+
and off-chain services, making it simpler for developers, analytics tools, and
12+
monitoring services to track and respond to activities happening in Gno.land.
13+
14+
Gno events are pieces of data that log specific activities or changes occurring
15+
within the state of an on-chain app. These activities are user-defined; they might
16+
be token transfers, changes in ownership, updates in user profiles, and more.
17+
Each event is recorded in the ABCI results of each block, ensuring that action
18+
that happened is verifiable and accessible to off-chain services.
19+
20+
## Emitting Events
21+
22+
To emit an event, you can use the `Emit()` function from the `std` package
23+
provided in the Gno standard library. The `Emit()` function takes in a string
24+
representing the type of event, and an even number of arguments after representing
25+
`key:value` pairs.
26+
27+
Read more about events & `Emit()` in
28+
[Effective Gno](../effective-gno.md#emit-gno-events-to-make-life-off-chain-easier),
29+
and the `Emit()` reference [here](../../reference/stdlibs/std/chain.md#emit).
30+
31+
## Data contained in a Gno Event
32+
33+
An event contained in an ABCI response of a block will include the following
34+
data:
35+
36+
``` json
37+
{
38+
"@type": "/tm.gnoEvent", // TM2 type
39+
"type": "OwnershipChange", // Type/name of event defined in Gno
40+
"pkg_path": "gno.land/r/demo/example", // Path of the emitter
41+
"func": "ChangeOwner", // Gno function that emitted the event
42+
"attrs": [ // Slice of key:value pairs emitted
43+
{
44+
"key": "oldOwner",
45+
"value": "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"
46+
},
47+
{
48+
"key": "newOwner",
49+
"value": "g1zzqd6phlfx0a809vhmykg5c6m44ap9756s7cjj"
50+
}
51+
]
52+
}
53+
```
54+
55+
You can fetch the ABCI response of a specific block by using the `/block_results`
56+
RPC endpoint.
57+

docs/reference/stdlibs/std/chain.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ std.AssertOriginCall()
2828
```
2929
---
3030

31+
## Emit
32+
```go
33+
func Emit(typ string, attrs ...string)
34+
```
35+
Emits a Gno event. Takes in a **string** type (event identifier), and an even number of string
36+
arguments acting as key-value pairs to be included in the emitted event.
37+
38+
#### Usage
39+
```go
40+
std.Emit("MyEvent", "myKey1", "myValue1", "myKey2", "myValue2")
41+
```
42+
---
43+
3144
## CurrentRealmPath
3245
```go
3346
func CurrentRealmPath() string
@@ -117,7 +130,8 @@ currentRealm := std.CurrentRealm()
117130
```go
118131
func PrevRealm() Realm
119132
```
120-
Returns the previous caller realm (can be realm or EOA). If caller is am EOA, `pkgpath` will be empty.
133+
Returns the previous caller realm (can be code or user realm). If caller is a
134+
user realm, `pkgpath` will be empty.
121135

122136
#### Usage
123137
```go

0 commit comments

Comments
 (0)