Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add event util #134

Merged
merged 4 commits into from
Sep 20, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions sources/EventUtil.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module StarcoinFramework::EventUtil {
use StarcoinFramework::Event;
use StarcoinFramework::Signer;

struct EventHandleWrapper<phantom EventT: store + drop> has key {
handle: Event::EventHandle<EventT>,
}

public fun init_event_with_T<EventT: store + drop>(sender: &signer) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是不是可以把 _with_T 去掉? 另外这里不需要 drop 吧?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    /// Use EventHandleGenerator to generate a unique event handle for `sig`
    public fun new_event_handle<T: drop + store>(account: &signer): EventHandle<T>
    acquires EventHandleGenerator {
        let addr = Signer::address_of(account);
        assert!(exists<EventHandleGenerator>(addr), Errors::not_published(EEVENT_GENERATOR));
        EventHandle<T> {
            counter: 0,
            guid: fresh_guid(borrow_global_mut<EventHandleGenerator>(addr))
        }
    }

Evnet的新建函数里面有drop

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with_T可以去掉

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里在DAOSpace里面加上了对EventUtil的调用

let broker = Signer::address_of(sender);
if (exists<EventHandleWrapper<EventT>>(broker)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我觉得这里用 assert 好一点,因为 init_event 是只能调用一次的。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

return
};
move_to(sender, EventHandleWrapper<EventT> {
handle: Event::new_event_handle<EventT>(sender)
});
}

public fun uninit_event_with_T<EventT: store + drop>(sender: &signer) acquires EventHandleWrapper {
let broker = Signer::address_of(sender);
if (!exists<EventHandleWrapper<EventT>>(broker)) {
return
};
let EventHandleWrapper<EventT> { handle } = move_from<EventHandleWrapper<EventT>>(broker);
Event::destroy_handle<EventT>(handle);
}

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);
}

public fun exist_event_T<EventT: store + drop>(broker: address): bool {
exists<EventHandleWrapper<EventT>>(broker)
}
}