Skip to content

Commit

Permalink
Add cw4::MemberChangedHookMsg and clean up dao-hooks pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeHartnell committed Sep 3, 2023
1 parent 1543647 commit 89385f5
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/dao-hooks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
4 changes: 4 additions & 0 deletions packages/dao-hooks/src/all_hooks.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cosmwasm_schema::cw_serde;
use cw4::MemberChangedHookMsg;

use crate::nft_stake::NftStakeChangedHookMsg;
use crate::proposal::ProposalHookMsg;
Expand All @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions packages/dao-hooks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
9 changes: 7 additions & 2 deletions packages/dao-hooks/src/nft_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> },
}

/// 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<Vec<SubMsg>> {
let msg = to_binary(&NftStakeChangedExecuteMsg::StakeChangeHook(
let msg = to_binary(&NftStakeChangedExecuteMsg::NftStakeChangeHook(
NftStakeChangedHookMsg::Stake { addr, token_id },
))?;
hooks.prepare_hooks(storage, |a| {
Expand All @@ -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<String>,
) -> StdResult<Vec<SubMsg>> {
let msg = to_binary(&NftStakeChangedExecuteMsg::StakeChangeHook(
let msg = to_binary(&NftStakeChangedExecuteMsg::NftStakeChangeHook(
NftStakeChangedHookMsg::Unstake { addr, token_ids },
))?;

Expand Down
14 changes: 8 additions & 6 deletions packages/dao-hooks/src/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand Down Expand Up @@ -90,3 +87,8 @@ pub fn proposal_status_changed_hooks(

Ok(messages)
}

#[cw_serde]
pub enum ProposalHookExecuteMsg {
ProposalHook(ProposalHookMsg),
}
5 changes: 5 additions & 0 deletions packages/dao-hooks/src/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
11 changes: 6 additions & 5 deletions packages/dao-hooks/src/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand Down Expand Up @@ -45,3 +41,8 @@ pub fn new_vote_hooks(
Ok(tmp)
})
}

#[cw_serde]
pub enum VoteHookExecuteMsg {
VoteHook(VoteHookMsg),
}

0 comments on commit 89385f5

Please sign in to comment.