From 89385f535124d4d32f14fe77ad857a1f94fbd4b4 Mon Sep 17 00:00:00 2001 From: Jake Hartnell Date: Sat, 2 Sep 2023 23:06:21 -0700 Subject: [PATCH] Add cw4::MemberChangedHookMsg and clean up dao-hooks pkg --- Cargo.lock | 1 + packages/dao-hooks/Cargo.toml | 1 + packages/dao-hooks/src/all_hooks.rs | 4 ++++ packages/dao-hooks/src/lib.rs | 3 +++ packages/dao-hooks/src/nft_stake.rs | 9 +++++++-- packages/dao-hooks/src/proposal.rs | 14 ++++++++------ packages/dao-hooks/src/stake.rs | 5 +++++ packages/dao-hooks/src/vote.rs | 11 ++++++----- 8 files changed, 35 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 00d2b40c3..25b31873b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1635,6 +1635,7 @@ dependencies = [ "cosmwasm-schema", "cosmwasm-std", "cw-hooks", + "cw4 1.1.0", "dao-voting 2.2.0", ] diff --git a/packages/dao-hooks/Cargo.toml b/packages/dao-hooks/Cargo.toml index d675dc65a..45ea0f640 100644 --- a/packages/dao-hooks/Cargo.toml +++ b/packages/dao-hooks/Cargo.toml @@ -10,5 +10,6 @@ version = { workspace = true } [dependencies] cosmwasm-std = { workspace = true } cosmwasm-schema = { workspace = true } +cw4 = { workspace = true } cw-hooks = { workspace = true } dao-voting = { workspace = true } diff --git a/packages/dao-hooks/src/all_hooks.rs b/packages/dao-hooks/src/all_hooks.rs index 361075e84..3dfe9ea4a 100644 --- a/packages/dao-hooks/src/all_hooks.rs +++ b/packages/dao-hooks/src/all_hooks.rs @@ -1,4 +1,5 @@ use cosmwasm_schema::cw_serde; +use cw4::MemberChangedHookMsg; use crate::nft_stake::NftStakeChangedHookMsg; use crate::proposal::ProposalHookMsg; @@ -8,6 +9,9 @@ use crate::vote::VoteHookMsg; /// An enum representing all possible DAO hooks. #[cw_serde] pub enum DaoHooks { + /// Called when a member is added or removed + /// to a cw4-groups or cw721-roles contract. + MemberChangedHook(MemberChangedHookMsg), /// Called when NFTs are staked or unstaked. NftStakeChangeHook(NftStakeChangedHookMsg), /// Called when a proposal status changes. diff --git a/packages/dao-hooks/src/lib.rs b/packages/dao-hooks/src/lib.rs index 3f9239868..41e5ef970 100644 --- a/packages/dao-hooks/src/lib.rs +++ b/packages/dao-hooks/src/lib.rs @@ -5,3 +5,6 @@ pub mod nft_stake; pub mod proposal; pub mod stake; pub mod vote; + +pub use all_hooks::DaoHooks; +pub use cw4::MemberChangedHookMsg; diff --git a/packages/dao-hooks/src/nft_stake.rs b/packages/dao-hooks/src/nft_stake.rs index fd7a221d0..9cebad29b 100644 --- a/packages/dao-hooks/src/nft_stake.rs +++ b/packages/dao-hooks/src/nft_stake.rs @@ -2,19 +2,22 @@ use cosmwasm_schema::cw_serde; use cosmwasm_std::{to_binary, Addr, StdResult, Storage, SubMsg, WasmMsg}; use cw_hooks::Hooks; +/// An enum representing NFT staking hooks. #[cw_serde] pub enum NftStakeChangedHookMsg { Stake { addr: Addr, token_id: String }, Unstake { addr: Addr, token_ids: Vec }, } +/// Prepares NftStakeChangedHookMsg::Stake hook SubMsgs, +/// containing the address and the token_id staked. pub fn stake_nft_hook_msgs( hooks: Hooks, storage: &dyn Storage, addr: Addr, token_id: String, ) -> StdResult> { - let msg = to_binary(&NftStakeChangedExecuteMsg::StakeChangeHook( + let msg = to_binary(&NftStakeChangedExecuteMsg::NftStakeChangeHook( NftStakeChangedHookMsg::Stake { addr, token_id }, ))?; hooks.prepare_hooks(storage, |a| { @@ -27,13 +30,15 @@ pub fn stake_nft_hook_msgs( }) } +/// Prepares NftStakeChangedHookMsg::Unstake hook SubMsgs, +/// containing the address and the token_ids unstaked. pub fn unstake_nft_hook_msgs( hooks: Hooks, storage: &dyn Storage, addr: Addr, token_ids: Vec, ) -> StdResult> { - let msg = to_binary(&NftStakeChangedExecuteMsg::StakeChangeHook( + let msg = to_binary(&NftStakeChangedExecuteMsg::NftStakeChangeHook( NftStakeChangedHookMsg::Unstake { addr, token_ids }, ))?; diff --git a/packages/dao-hooks/src/proposal.rs b/packages/dao-hooks/src/proposal.rs index 9db23fcdb..0b95758ad 100644 --- a/packages/dao-hooks/src/proposal.rs +++ b/packages/dao-hooks/src/proposal.rs @@ -3,6 +3,9 @@ use cosmwasm_std::{to_binary, StdResult, Storage, SubMsg, WasmMsg}; use cw_hooks::Hooks; use dao_voting::reply::mask_proposal_hook_index; +/// An enum representing proposal hook messages. +/// Either a new propsoal hook, fired when a new proposal is created, +/// or a proposal status hook, fired when a proposal changes status. #[cw_serde] pub enum ProposalHookMsg { NewProposal { @@ -16,12 +19,6 @@ pub enum ProposalHookMsg { }, } -// This is just a helper to properly serialize the above message -#[cw_serde] -pub enum ProposalHookExecuteMsg { - ProposalHook(ProposalHookMsg), -} - /// Prepares new proposal hook messages. These messages reply on error /// and have even reply IDs. /// IDs are set to even numbers to then be interleaved with the vote hooks. @@ -90,3 +87,8 @@ pub fn proposal_status_changed_hooks( Ok(messages) } + +#[cw_serde] +pub enum ProposalHookExecuteMsg { + ProposalHook(ProposalHookMsg), +} diff --git a/packages/dao-hooks/src/stake.rs b/packages/dao-hooks/src/stake.rs index 6f675071b..6161bf2c9 100644 --- a/packages/dao-hooks/src/stake.rs +++ b/packages/dao-hooks/src/stake.rs @@ -2,12 +2,15 @@ use cosmwasm_schema::cw_serde; use cosmwasm_std::{to_binary, Addr, StdResult, Storage, SubMsg, Uint128, WasmMsg}; use cw_hooks::Hooks; +/// An enum representing staking hooks. #[cw_serde] pub enum StakeChangedHookMsg { Stake { addr: Addr, amount: Uint128 }, Unstake { addr: Addr, amount: Uint128 }, } +/// Prepares StakeChangedHookMsg::Stake hook SubMsgs, +/// containing the address and the amount staked. pub fn stake_hook_msgs( hooks: Hooks, storage: &dyn Storage, @@ -27,6 +30,8 @@ pub fn stake_hook_msgs( }) } +/// Prepares StakeChangedHookMsg::Unstake hook SubMsgs, +/// containing the address and the amount unstaked. pub fn unstake_hook_msgs( hooks: Hooks, storage: &dyn Storage, diff --git a/packages/dao-hooks/src/vote.rs b/packages/dao-hooks/src/vote.rs index 7da7aa6c1..b8a0dd772 100644 --- a/packages/dao-hooks/src/vote.rs +++ b/packages/dao-hooks/src/vote.rs @@ -3,6 +3,7 @@ use cosmwasm_std::{to_binary, StdResult, Storage, SubMsg, WasmMsg}; use cw_hooks::Hooks; use dao_voting::reply::mask_vote_hook_index; +/// An enum representing vote hooks, fired when new votes are cast. #[cw_serde] pub enum VoteHookMsg { NewVote { @@ -12,11 +13,6 @@ pub enum VoteHookMsg { }, } -#[cw_serde] -pub enum VoteHookExecuteMsg { - VoteHook(VoteHookMsg), -} - /// Prepares new vote hook messages. These messages reply on error /// and have even reply IDs. /// IDs are set to odd numbers to then be interleaved with the proposal hooks. @@ -45,3 +41,8 @@ pub fn new_vote_hooks( Ok(tmp) }) } + +#[cw_serde] +pub enum VoteHookExecuteMsg { + VoteHook(VoteHookMsg), +}