diff --git a/build/StarcoinFramework/BuildInfo.yaml b/build/StarcoinFramework/BuildInfo.yaml index 89da9a27..7dfd564e 100644 --- a/build/StarcoinFramework/BuildInfo.yaml +++ b/build/StarcoinFramework/BuildInfo.yaml @@ -84,6 +84,9 @@ compiled_package_info: ? address: "0x00000000000000000000000000000001" name: Event : StarcoinFramework + ? address: "0x00000000000000000000000000000001" + name: EventUtil + : StarcoinFramework ? address: "0x00000000000000000000000000000001" name: FixedPoint32 : StarcoinFramework @@ -264,7 +267,7 @@ compiled_package_info: ? address: "0x00000000000000000000000000000001" name: YieldFarmingV2 : StarcoinFramework - source_digest: 2317458930368CCF368CBFCFD2A2D84B74B03521306A30839C2CFFA9D0DCF89E + source_digest: 292D789D77E5CCC4DC7A710845C6751F5308EBD27AECB68680A2DF92F7A057F6 build_flags: dev_mode: false test_mode: false diff --git a/build/StarcoinFramework/bytecode_modules/EventUtil.mv b/build/StarcoinFramework/bytecode_modules/EventUtil.mv new file mode 100644 index 00000000..bcf5fb84 Binary files /dev/null and b/build/StarcoinFramework/bytecode_modules/EventUtil.mv differ diff --git a/build/StarcoinFramework/docs/EventUtil.md b/build/StarcoinFramework/docs/EventUtil.md new file mode 100644 index 00000000..420e3c6f --- /dev/null +++ b/build/StarcoinFramework/docs/EventUtil.md @@ -0,0 +1,175 @@ + + + +# Module `0x1::EventUtil` + + + +- [Resource `EventHandleWrapper`](#0x1_EventUtil_EventHandleWrapper) +- [Constants](#@Constants_0) +- [Function `init_event`](#0x1_EventUtil_init_event) +- [Function `uninit_event`](#0x1_EventUtil_uninit_event) +- [Function `emit_event`](#0x1_EventUtil_emit_event) +- [Function `exist_event`](#0x1_EventUtil_exist_event) + + +
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)
+}
+
+ + + +
diff --git a/build/StarcoinFramework/docs/README.md b/build/StarcoinFramework/docs/README.md index 524f358e..dfddb1cc 100644 --- a/build/StarcoinFramework/docs/README.md +++ b/build/StarcoinFramework/docs/README.md @@ -38,6 +38,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) diff --git a/build/StarcoinFramework/source_maps/EventUtil.mvsm b/build/StarcoinFramework/source_maps/EventUtil.mvsm new file mode 100644 index 00000000..91ef2539 Binary files /dev/null and b/build/StarcoinFramework/source_maps/EventUtil.mvsm differ diff --git a/sources/EventUtil.move b/sources/EventUtil.move new file mode 100644 index 00000000..1ecfceb5 --- /dev/null +++ b/sources/EventUtil.move @@ -0,0 +1,36 @@ +module StarcoinFramework::EventUtil { + use StarcoinFramework::Event; + use StarcoinFramework::Signer; + use StarcoinFramework::Errors; + + const ERR_INIT_REPEATE: u64 = 101; + const ERR_RESOURCE_NOT_EXISTS: u64 = 102; + + struct EventHandleWrapper has key { + handle: Event::EventHandle, + } + + public fun init_event(sender: &signer) { + let broker = Signer::address_of(sender); + assert!(!exists>(broker), Errors::invalid_state(ERR_INIT_REPEATE)); + move_to(sender, EventHandleWrapper { + handle: Event::new_event_handle(sender) + }); + } + + public fun uninit_event(sender: &signer) acquires EventHandleWrapper { + let broker = Signer::address_of(sender); + assert!(exists>(broker), Errors::invalid_state(ERR_RESOURCE_NOT_EXISTS)); + let EventHandleWrapper { handle } = move_from>(broker); + Event::destroy_handle(handle); + } + + public fun emit_event(broker: address, event: EventT) acquires EventHandleWrapper { + let event_handle = borrow_global_mut>(broker); + Event::emit_event(&mut event_handle.handle, event); + } + + public fun exist_event(broker: address): bool { + exists>(broker) + } +}