Module 0x1::EventUtil
use 0x1::Errors;
use 0x1::Event;
use 0x1::Signer;
Resource EventHandleWrapper
struct EventHandleWrapper<EventT: drop, store> has key
Fields
-
handle: Event::EventHandle<EventT>
-
Constants
const ERR_INIT_REPEATE: u64 = 101;
const ERR_RESOURCE_NOT_EXISTS: u64 = 102;
Function init_event
public fun init_event<EventT: drop, store>(sender: &signer)
Implementation
public fun init_event<EventT: store + drop>(sender: &signer) {
let broker = Signer::address_of(sender);
assert!(!exists<EventHandleWrapper<EventT>>(broker), Errors::invalid_state(ERR_INIT_REPEATE));
move_to(sender, EventHandleWrapper<EventT> {
handle: Event::new_event_handle<EventT>(sender)
});
}
Function uninit_event
public fun uninit_event<EventT: drop, store>(sender: &signer)
Implementation
public fun uninit_event<EventT: store + drop>(sender: &signer) acquires EventHandleWrapper {
let broker = Signer::address_of(sender);
assert!(exists<EventHandleWrapper<EventT>>(broker), Errors::invalid_state(ERR_RESOURCE_NOT_EXISTS));
let EventHandleWrapper<EventT> { handle } = move_from<EventHandleWrapper<EventT>>(broker);
Event::destroy_handle<EventT>(handle);
}
Function emit_event
public fun emit_event<EventT: drop, store>(broker: address, event: EventT)
Implementation
public fun emit_event<EventT: store + drop>(broker: address, event: EventT) acquires EventHandleWrapper {
let event_handle = borrow_global_mut<EventHandleWrapper<EventT>>(broker);
Event::emit_event(&mut event_handle.handle, event);
}
Function exist_event
public fun exist_event<EventT: drop, store>(broker: address): bool
Implementation
public fun exist_event<EventT: store + drop>(broker: address): bool {
exists<EventHandleWrapper<EventT>>(broker)
}