vote_hooks/
lib.rs

1use cosmwasm_std::{to_binary, StdResult, Storage, SubMsg, WasmMsg};
2use indexable_hooks::Hooks;
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Debug)]
7#[serde(rename_all = "snake_case")]
8pub enum VoteHookMsg {
9    NewVote {
10        proposal_id: u64,
11        voter: String,
12        vote: String,
13    },
14}
15
16// This is just a helper to properly serialize the above message
17#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Debug)]
18#[serde(rename_all = "snake_case")]
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 tmp = SubMsg::reply_on_error(execute, index * 2 + 1);
46        index += 1;
47        Ok(tmp)
48    })
49}