dao_vote_hooks/
lib.rs

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