Events
Orbs contract events give you the ability to log information into the blockchain that is stored as part of the receipt block.
Here is a simple contract that emits an event. We will go over it in details:
1
package main
2
3
import (
4
"github.com/orbs-network/orbs-contract-sdk/go/sdk/v1"
5
"github.com/orbs-network/orbs-contract-sdk/go/sdk/v1/events"
6
"github.com/orbs-network/orbs-contract-sdk/go/sdk/v1/state"
7
)
8
9
var PUBLIC = sdk.Export(giveBirth)
10
var SYSTEM = sdk.Export(_init)
11
var EVENTS = sdk.Export(BabyBorn)
12
13
func BabyBorn(name string, weight uint32) {}
14
15
func _init() {
16
state.WriteUint64([]byte("some_data"), 1)
17
}
18
19
func giveBirth(name string) {
20
events.EmitEvent(BabyBorn, name, uint32(3))
21
}
Copied!
Using the Events API require you first to import it:
1
import (
2
"github.com/orbs-network/orbs-contract-sdk/go/sdk/v1/events"
3
)
Copied!
Next, we export the event:
1
var EVENTS = sdk.Export(BabyBorn)
Copied!
Then we declare it as an empty function. Note that the empty bracers are required ({}):
1
func BabyBorn(name string, weight uint32) {}
Copied!
When the function giveBirth(name string) is called, we want the event to be emitted, so we call the events.EmitEvent function:
1
events.EmitEvent(BabyBorn, name, uint32(3))
Copied!
The arguments are a pointer to the event function (which is also exported) and the event arguments.
Last modified 2yr ago
Copy link