-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
216 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
|
||
<a name="0x1_EventUtil"></a> | ||
|
||
# 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) | ||
|
||
|
||
<pre><code><b>use</b> <a href="Errors.md#0x1_Errors">0x1::Errors</a>; | ||
<b>use</b> <a href="Event.md#0x1_Event">0x1::Event</a>; | ||
<b>use</b> <a href="Signer.md#0x1_Signer">0x1::Signer</a>; | ||
</code></pre> | ||
|
||
|
||
|
||
<a name="0x1_EventUtil_EventHandleWrapper"></a> | ||
|
||
## Resource `EventHandleWrapper` | ||
|
||
|
||
|
||
<pre><code><b>struct</b> <a href="EventUtil.md#0x1_EventUtil_EventHandleWrapper">EventHandleWrapper</a><EventT: drop, store> <b>has</b> key | ||
</code></pre> | ||
|
||
|
||
|
||
<details> | ||
<summary>Fields</summary> | ||
|
||
|
||
<dl> | ||
<dt> | ||
<code>handle: <a href="Event.md#0x1_Event_EventHandle">Event::EventHandle</a><EventT></code> | ||
</dt> | ||
<dd> | ||
|
||
</dd> | ||
</dl> | ||
|
||
|
||
</details> | ||
|
||
<a name="@Constants_0"></a> | ||
|
||
## Constants | ||
|
||
|
||
<a name="0x1_EventUtil_ERR_INIT_REPEATE"></a> | ||
|
||
|
||
|
||
<pre><code><b>const</b> <a href="EventUtil.md#0x1_EventUtil_ERR_INIT_REPEATE">ERR_INIT_REPEATE</a>: u64 = 101; | ||
</code></pre> | ||
|
||
|
||
|
||
<a name="0x1_EventUtil_ERR_RESOURCE_NOT_EXISTS"></a> | ||
|
||
|
||
|
||
<pre><code><b>const</b> <a href="EventUtil.md#0x1_EventUtil_ERR_RESOURCE_NOT_EXISTS">ERR_RESOURCE_NOT_EXISTS</a>: u64 = 102; | ||
</code></pre> | ||
|
||
|
||
|
||
<a name="0x1_EventUtil_init_event"></a> | ||
|
||
## Function `init_event` | ||
|
||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="EventUtil.md#0x1_EventUtil_init_event">init_event</a><EventT: drop, store>(sender: &signer) | ||
</code></pre> | ||
|
||
|
||
|
||
<details> | ||
<summary>Implementation</summary> | ||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="EventUtil.md#0x1_EventUtil_init_event">init_event</a><EventT: store + drop>(sender: &signer) { | ||
<b>let</b> broker = <a href="Signer.md#0x1_Signer_address_of">Signer::address_of</a>(sender); | ||
<b>assert</b>!(!<b>exists</b><<a href="EventUtil.md#0x1_EventUtil_EventHandleWrapper">EventHandleWrapper</a><EventT>>(broker), <a href="Errors.md#0x1_Errors_invalid_state">Errors::invalid_state</a>(<a href="EventUtil.md#0x1_EventUtil_ERR_INIT_REPEATE">ERR_INIT_REPEATE</a>)); | ||
<b>move_to</b>(sender, <a href="EventUtil.md#0x1_EventUtil_EventHandleWrapper">EventHandleWrapper</a><EventT> { | ||
handle: <a href="Event.md#0x1_Event_new_event_handle">Event::new_event_handle</a><EventT>(sender) | ||
}); | ||
} | ||
</code></pre> | ||
|
||
|
||
|
||
</details> | ||
|
||
<a name="0x1_EventUtil_uninit_event"></a> | ||
|
||
## Function `uninit_event` | ||
|
||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="EventUtil.md#0x1_EventUtil_uninit_event">uninit_event</a><EventT: drop, store>(sender: &signer) | ||
</code></pre> | ||
|
||
|
||
|
||
<details> | ||
<summary>Implementation</summary> | ||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="EventUtil.md#0x1_EventUtil_uninit_event">uninit_event</a><EventT: store + drop>(sender: &signer) <b>acquires</b> <a href="EventUtil.md#0x1_EventUtil_EventHandleWrapper">EventHandleWrapper</a> { | ||
<b>let</b> broker = <a href="Signer.md#0x1_Signer_address_of">Signer::address_of</a>(sender); | ||
<b>assert</b>!(<b>exists</b><<a href="EventUtil.md#0x1_EventUtil_EventHandleWrapper">EventHandleWrapper</a><EventT>>(broker), <a href="Errors.md#0x1_Errors_invalid_state">Errors::invalid_state</a>(<a href="EventUtil.md#0x1_EventUtil_ERR_RESOURCE_NOT_EXISTS">ERR_RESOURCE_NOT_EXISTS</a>)); | ||
<b>let</b> <a href="EventUtil.md#0x1_EventUtil_EventHandleWrapper">EventHandleWrapper</a><EventT> { handle } = <b>move_from</b><<a href="EventUtil.md#0x1_EventUtil_EventHandleWrapper">EventHandleWrapper</a><EventT>>(broker); | ||
<a href="Event.md#0x1_Event_destroy_handle">Event::destroy_handle</a><EventT>(handle); | ||
} | ||
</code></pre> | ||
|
||
|
||
|
||
</details> | ||
|
||
<a name="0x1_EventUtil_emit_event"></a> | ||
|
||
## Function `emit_event` | ||
|
||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="EventUtil.md#0x1_EventUtil_emit_event">emit_event</a><EventT: drop, store>(broker: <b>address</b>, event: EventT) | ||
</code></pre> | ||
|
||
|
||
|
||
<details> | ||
<summary>Implementation</summary> | ||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="EventUtil.md#0x1_EventUtil_emit_event">emit_event</a><EventT: store + drop>(broker: <b>address</b>, event: EventT) <b>acquires</b> <a href="EventUtil.md#0x1_EventUtil_EventHandleWrapper">EventHandleWrapper</a> { | ||
<b>let</b> event_handle = <b>borrow_global_mut</b><<a href="EventUtil.md#0x1_EventUtil_EventHandleWrapper">EventHandleWrapper</a><EventT>>(broker); | ||
<a href="Event.md#0x1_Event_emit_event">Event::emit_event</a>(&<b>mut</b> event_handle.handle, event); | ||
} | ||
</code></pre> | ||
|
||
|
||
|
||
</details> | ||
|
||
<a name="0x1_EventUtil_exist_event"></a> | ||
|
||
## Function `exist_event` | ||
|
||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="EventUtil.md#0x1_EventUtil_exist_event">exist_event</a><EventT: drop, store>(broker: <b>address</b>): bool | ||
</code></pre> | ||
|
||
|
||
|
||
<details> | ||
<summary>Implementation</summary> | ||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="EventUtil.md#0x1_EventUtil_exist_event">exist_event</a><EventT: store + drop>(broker: <b>address</b>): bool { | ||
<b>exists</b><<a href="EventUtil.md#0x1_EventUtil_EventHandleWrapper">EventHandleWrapper</a><EventT>>(broker) | ||
} | ||
</code></pre> | ||
|
||
|
||
|
||
</details> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<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); | ||
assert!(!exists<EventHandleWrapper<EventT>>(broker), Errors::invalid_state(ERR_INIT_REPEATE)); | ||
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); | ||
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); | ||
} | ||
|
||
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) | ||
} | ||
} |