Emit Events
Each transaction processed by the Pontem Network can have events.
You can see events in the Polkadot JS UI when a transaction is processed or in the block explorer. To examine the block explorer, choose Network in the top header menu, and then click on Explorer in the dropdown and choose the block that contains the events.
You can also see events using the CLI when your transaction is processed.
To emit a new event use the Event module.
To emit events you have to create an event handle object that will be used one time and destroyed or can be used multiple times and stored under account resources for example.
Let's write a new script firing events with a temporary event handle:
1
script {
2
use 0x1::Signer;
3
use 0x1::Event;
4
5
fun emit_event(account: signer) {
6
let addr = Signer::address_of(&account);
7
8
// Create a new event handle.
9
let event_handle = Event::new_event_handle<address>(&account);
10
11
// Emit event.
12
Event::emit_event(
13
&mut event_handle,
14
addr
15
);
16
17
// Destroy event handle.
18
Event::destroy_handle(event_handle);
19
}
20
}
Copied!
The provided example is using a temporary event handle, that is created inside the script to fire the event and be destroyed later.
Created event handles can fire only events containing an account's addresses as data because during the creation of event handles, you provide a type that will be used for the created event handle. You can use any other types, even complex ones like structs.
Usually, you will create an event handle for each type of event and store it inside your account resource like in the following example:
1
address {{sender}} {
2
module MyEventEmitter {
3
use 0x1::Signer;
4
use 0x1::Event;
5
6
struct MyStruct<T: store + drop> has key, store {
7
event_handle: Event::EventHandle<T>,
8
}
9
10
public fun initialize<T: store + drop>(account: &signer) {
11
let event_handle = Event::new_event_handle<T>(account);
12
13
move_to(account, MyStruct<T> {
14
event_handle,
15
});
16
}
17
18
public fun emit<T: store + drop>(account: &signer, val: T) acquires MyStruct {
19
let my_struct = borrow_global_mut<MyStruct<T>>(Signer::address_of(account));
20
Event::emit_event(
21
&mut my_struct.event_handle,
22
val,
23
);
24
}
25
}
26
}
Copied!
Write a script for a new module:
1
script {
2
use {{sender}}::MyEventEmitter;
3
4
fun emit_via_emitter(account: signer, x: u64) {
5
// You call initialize ONLY ONE TIME. In the future smart contract you don't need to call it again for current signer.
6
MyEventEmitter::initialize<u64>(&account);
7
8
// Emit event with existing event handle.
9
MyEventEmitter::emit<u64>(&account, x);
10
}
11
}
Copied!
Build module and script:
1
dove build
2
dove tx 'emit_via_emitter(1000)'
Copied!
Afterwards, deploy module and execute new script (replace <seed> with your seed):
1
polkadot-js-api tx.mvm.publishModule @./artifacts/modules/0_MyEmitter.mv 100000 --seed <seed> --types ./types.json --ws <ws-endpoint>
2
polkadot-js-api tx.mvm.execute @./artifacts/transactions/emit.mvt 1000000 --seed <seed> --types ./types.json --ws <ws-endpoint>
Copied!
Example event output:
1
{
2
"method": "Event",
3
"section": "mvm",
4
"index": "0x3600",
5
"data": [
6
"0x040000000000000014ca430662463ce9a1a27dd6bb463595f3e3526d118e25541421b954893eca7d",
7
"U64",
8
"0xe803000000000000"
9
]
10
},
Copied!
See data array and parameters by indexes:
  • 0x040000000000000014ca430662463ce9a1a27dd6bb463595f3e3526d118e25541421b954893eca7d - account emitted event in hex and counter of created event handle.
  • U64 - is TypeTag value, describing the U64 number.
  • 0xe803000000000000 - event data is our u64 1000 value encoded in BCS.
The data placed in the events encoded in (Binary Canonical Serialization). You can reference this description from Diem on how it works.
You can use several libraries to parse events results:
Last modified 1mo ago
Copy link