dao_hooks/
vote.rs

1use cosmwasm_schema::cw_serde;
2use cosmwasm_std::{to_json_binary, StdResult, Storage, SubMsg, WasmMsg};
3use cw_hooks::Hooks;
4use dao_voting::reply::mask_vote_hook_index;
5
6/// An enum representing vote hooks, fired when new votes are cast.
7#[cw_serde]
8pub enum VoteHookMsg {
9    NewVote {
10        proposal_id: u64,
11        voter: String,
12        vote: String,
13    },
14}
15
16/// Prepares new vote hook messages. These messages reply on error
17/// and have even reply IDs.
18/// IDs are set to odd numbers to then be interleaved with the proposal hooks.
19pub fn new_vote_hooks(
20    hooks: Hooks,
21    storage: &dyn Storage,
22    proposal_id: u64,
23    voter: String,
24    vote: String,
25) -> StdResult<Vec<SubMsg>> {
26    let msg = to_json_binary(&VoteHookExecuteMsg::VoteHook(VoteHookMsg::NewVote {
27        proposal_id,
28        voter,
29        vote,
30    }))?;
31    let mut index: u64 = 0;
32    hooks.prepare_hooks(storage, |a| {
33        let execute = WasmMsg::Execute {
34            contract_addr: a.to_string(),
35            msg: msg.clone(),
36            funds: vec![],
37        };
38        let masked_index = mask_vote_hook_index(index);
39        let tmp = SubMsg::reply_on_error(execute, masked_index);
40        index += 1;
41        Ok(tmp)
42    })
43}
44
45#[cw_serde]
46pub enum VoteHookExecuteMsg {
47    VoteHook(VoteHookMsg),
48}