proposal_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 ProposalHookMsg {
9    NewProposal {
10        id: u64,
11    },
12    ProposalStatusChanged {
13        id: u64,
14        old_status: String,
15        new_status: String,
16    },
17}
18
19// This is just a helper to properly serialize the above message
20#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Debug)]
21#[serde(rename_all = "snake_case")]
22pub enum ProposalHookExecuteMsg {
23    ProposalHook(ProposalHookMsg),
24}
25/// Prepares new proposal hook messages. These messages reply on error
26/// and have even reply IDs.
27/// IDs are set to even numbers to then be interleaved with the vote hooks.
28pub fn new_proposal_hooks(hooks: Hooks, storage: &dyn Storage, id: u64) -> StdResult<Vec<SubMsg>> {
29    let msg = to_binary(&ProposalHookExecuteMsg::ProposalHook(
30        ProposalHookMsg::NewProposal { id },
31    ))?;
32    let mut index: u64 = 0;
33    hooks.prepare_hooks(storage, |a| {
34        let execute = WasmMsg::Execute {
35            contract_addr: a.to_string(),
36            msg: msg.clone(),
37            funds: vec![],
38        };
39        let tmp = SubMsg::reply_on_error(execute, index * 2);
40        index += 1;
41        Ok(tmp)
42    })
43}
44
45/// Prepares proposal status hook messages. These messages reply on error
46/// and have even reply IDs.
47/// IDs are set to even numbers to then be interleaved with the vote hooks.
48pub fn proposal_status_changed_hooks(
49    hooks: Hooks,
50    storage: &dyn Storage,
51    id: u64,
52    old_status: String,
53    new_status: String,
54) -> StdResult<Vec<SubMsg>> {
55    if old_status == new_status {
56        return Ok(vec![]);
57    }
58
59    let msg = to_binary(&ProposalHookExecuteMsg::ProposalHook(
60        ProposalHookMsg::ProposalStatusChanged {
61            id,
62            old_status,
63            new_status,
64        },
65    ))?;
66    let mut index: u64 = 0;
67    hooks.prepare_hooks(storage, |a| {
68        let execute = WasmMsg::Execute {
69            contract_addr: a.to_string(),
70            msg: msg.clone(),
71            funds: vec![],
72        };
73        let tmp = SubMsg::reply_on_error(execute, index * 2);
74        index += 1;
75        Ok(tmp)
76    })
77}