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
Show file tree
Hide file tree
Changes from 2 commits
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
5 changes: 4 additions & 1 deletion build/StarcoinFramework/BuildInfo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ compiled_package_info:
? address: "0x00000000000000000000000000000001"
name: Event
: StarcoinFramework
? address: "0x00000000000000000000000000000001"
name: EventUtil
: StarcoinFramework
? address: "0x00000000000000000000000000000001"
name: FixedPoint32
: StarcoinFramework
Expand Down Expand Up @@ -318,7 +321,7 @@ compiled_package_info:
? address: "0x00000000000000000000000000000001"
name: YieldFarmingV2
: StarcoinFramework
source_digest: 5E08A2774FAADBD05F5E98290A4B05E6D4D5AA4DB5123B79F6015AC8574B75F8
source_digest: 4F1DB3D428C64D5748091F70FA9DFED7C6F0D4F0B4915662FCD87C7761DA34E0
build_flags:
dev_mode: false
test_mode: false
Expand Down
1 change: 1 addition & 0 deletions build/StarcoinFramework/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ This is the root document for the Move StarcoinFramework module documentation. T
- [`0x1::Epoch`](Epoch.md#0x1_Epoch)
- [`0x1::Errors`](Errors.md#0x1_Errors)
- [`0x1::Event`](Event.md#0x1_Event)
- [`0x1::EventUtil`](EventUtil.md#0x1_EventUtil)
- [`0x1::FixedPoint32`](FixedPoint32.md#0x1_FixedPoint32)
- [`0x1::Genesis`](Genesis.md#0x1_Genesis)
- [`0x1::GenesisNFT`](GenesisNFT.md#0x1_GenesisNFT)
Expand Down
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<EventT: store + drop>(sender: &signer) {
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<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<EventT: store + drop>(broker: address): bool {
exists<EventHandleWrapper<EventT>>(broker)
}
}
31 changes: 16 additions & 15 deletions sources/daospace/DAOSpace.move
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module StarcoinFramework::DAOSpace {
use StarcoinFramework::SnapshotUtil;
use StarcoinFramework::Block;
use StarcoinFramework::DAOPluginMarketplace;
use StarcoinFramework::EventUtil;

friend StarcoinFramework::StarcoinDAO;

Expand Down Expand Up @@ -779,24 +780,24 @@ module StarcoinFramework::DAOSpace {
}

/// Plugin event
public fun init_plugin_event<DAOT: store,
PluginT: store,
EventT: store + drop>(_cap: &DAOPluginEventCap<DAOT, PluginT>)
acquires DAOAccountCapHolder {
public fun init_plugin_event<
DAOT: store,
PluginT: store,
EventT: store + drop
>(_cap: &DAOPluginEventCap<DAOT, PluginT>) acquires DAOAccountCapHolder {
let dao_signer = dao_signer<DAOT>();
assert!(!exists<PluginEvent<DAOT, PluginT, EventT>>(dao_address<DAOT>()), Errors::invalid_state(ERR_ALREADY_INIT));
move_to(&dao_signer, PluginEvent<DAOT, PluginT, EventT> {
event_handle: Event::new_event_handle<EventT>(&dao_signer)
});
EventUtil::init_event<EventT>(&dao_signer);
}

public fun emit_plugin_event<DAOT: store,
PluginT: store,
EventT: store + drop>(_cap: &DAOPluginEventCap<DAOT, PluginT>,
event: EventT) acquires PluginEvent {
let dao_address = dao_address<DAOT>();
let plugin_event = borrow_global_mut<PluginEvent<DAOT, PluginT, EventT>>(dao_address);
Event::emit_event(&mut plugin_event.event_handle, event);
public fun emit_plugin_event<
DAOT: store,
PluginT: store,
EventT: store + drop
>(
_cap: &DAOPluginEventCap<DAOT, PluginT>,
event: EventT
) {
EventUtil::emit_event(dao_address<DAOT>(), event);
}


Expand Down