Skip to content

Commit

Permalink
Add event util (#134)
Browse files Browse the repository at this point in the history
* Add event util

* Add calling that EventUtil in DAOSpace

* Add assert that event util init and uninit

* Fixed exp files
  • Loading branch information
welbon authored Sep 20, 2022
1 parent f1363a3 commit 545f191
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 37 deletions.
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: 988F0323FAFB181295E294FC2838DC0C7C8BC8C7801B8320594FBD82E3FC7C67
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
2 changes: 1 addition & 1 deletion integration-tests/daospace/dao_grant.exp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ task 5 'run'. lines 13-21:

task 8 'run'. lines 123-136:
{
"gas_used": 8205741,
"gas_used": 8209548,
"status": "Executed"
}

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/daospace/dao_proposal.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ processed 42 tasks

task 7 'run'. lines 169-182:
{
"gas_used": 8202944,
"gas_used": 8206751,
"status": "Executed"
}

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/daospace/dao_upgrade.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ processed 18 tasks

task 3 'run'. lines 7-15:
{
"gas_used": 6483371,
"gas_used": 6487178,
"status": "Executed"
}

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/daospace/dao_upgrade_incompatible.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ processed 19 tasks

task 3 'run'. lines 7-15:
{
"gas_used": 6483371,
"gas_used": 6487178,
"status": "Executed"
}

Expand Down
14 changes: 7 additions & 7 deletions integration-tests/daospace/stake_to_sbt_plugin.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ processed 18 tasks

task 4 'run'. lines 19-27:
{
"gas_used": 6483371,
"gas_used": 6487178,
"status": "Executed"
}

task 6 'run'. lines 71-79:
{
"gas_used": 1684266,
"gas_used": 1688073,
"status": "Executed"
}

task 7 'run'. lines 81-117:
{
"gas_used": 652577,
"gas_used": 655792,
"status": "Executed"
}

Expand Down Expand Up @@ -42,25 +42,25 @@ task 10 'run'. lines 134-147:

task 11 'run'. lines 149-165:
{
"gas_used": 419842,
"gas_used": 423057,
"status": "Executed"
}

task 13 'run'. lines 169-182:
{
"gas_used": 217386,
"gas_used": 220601,
"status": "Executed"
}

task 14 'run'. lines 184-200:
{
"gas_used": 541049,
"gas_used": 544264,
"status": "Executed"
}

task 16 'run'. lines 204-219:
{
"gas_used": 226351,
"gas_used": 229566,
"status": "Executed"
}

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/daospace/xdao.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ processed 8 tasks

task 2 'run'. lines 7-14:
{
"gas_used": 6483371,
"gas_used": 6487178,
"status": "Executed"
}

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/daospaceplugin/mint_proposal_plugin.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ processed 30 tasks

task 5 'run'. lines 11-18:
{
"gas_used": 6483371,
"gas_used": 6487178,
"status": "Executed"
}

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/daospaceplugin/salary_gov_plugin.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ processed 21 tasks

task 7 'run'. lines 252-266:
{
"gas_used": 8160906,
"gas_used": 8164713,
"status": "Executed"
}

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/starcoin_dao/starcoin_dao.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ processed 4 tasks

task 3 'run'. lines 8-16:
{
"gas_used": 6483371,
"gas_used": 6487178,
"status": "Executed"
}
4 changes: 2 additions & 2 deletions integration-tests/starcoin_dao/starcoin_onchain_config.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ processed 24 tasks

task 4 'run'. lines 9-17:
{
"gas_used": 6476171,
"gas_used": 6479978,
"status": "Executed"
}

task 5 'run'. lines 19-33:
{
"gas_used": 586891,
"gas_used": 590106,
"status": "Executed"
}

Expand Down
4 changes: 2 additions & 2 deletions integration-tests/starcoin_dao/starcoin_upgrade_module.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ processed 27 tasks

task 4 'run'. lines 9-17:
{
"gas_used": 6476171,
"gas_used": 6479978,
"status": "Executed"
}

task 5 'run'. lines 19-33:
{
"gas_used": 586891,
"gas_used": 590106,
"status": "Executed"
}

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;
use StarcoinFramework::Errors;

const ERR_INIT_REPEATE: u64 = 101;
const ERR_RESOURCE_NOT_EXISTS: u64 = 101;

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)
}
}
40 changes: 23 additions & 17 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 @@ -727,7 +728,12 @@ module StarcoinFramework::DAOSpace {
});
}

public fun set_member_image<DAOT: store, PluginT>(_cap: &DAOMemberCap<DAOT, PluginT>, member_addr: address, image_data:Option::Option<vector<u8>>, image_url:Option::Option<vector<u8>>) acquires DAONFTUpdateCapHolder{
public fun set_member_image<DAOT: store, PluginT>(
_cap: &DAOMemberCap<DAOT, PluginT>,
member_addr: address,
image_data: Option::Option<vector<u8>>,
image_url: Option::Option<vector<u8>>
) acquires DAONFTUpdateCapHolder {
ensure_member<DAOT>(member_addr);

let dao_address = dao_address<DAOT>();
Expand Down Expand Up @@ -779,24 +785,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 Expand Up @@ -874,7 +880,7 @@ module StarcoinFramework::DAOSpace {
let dao_address = dao_address<DAOT>();
let account_address = Signer::address_of(sender);

if(! exists<GrantEvent<PluginT>>(dao_address)){
if (!exists<GrantEvent<PluginT>>(dao_address)){
move_to(&dao_signer, GrantEvent<PluginT>{
create_grant_event_handler:Event::new_event_handle<GrantCreateEvent<PluginT>>(&dao_signer),
revoke_grant_event_handler:Event::new_event_handle<GrantRevokeEvent<PluginT>>(&dao_signer),
Expand Down

0 comments on commit 545f191

Please sign in to comment.