abstract_cw3/
helpers.rs

1use cosmwasm_schema::cw_serde;
2use cosmwasm_std::{to_json_binary, Addr, CosmosMsg, StdResult, WasmMsg};
3
4use crate::msg::{Cw3ExecuteMsg, Vote};
5use cw_utils::Expiration;
6
7/// Cw3Contract is a wrapper around Addr that provides a lot of helpers
8/// for working with this.
9///
10/// If you wish to persist this, convert to Cw3CanonicalContract via .canonical()
11///
12/// FIXME: Cw3Contract currently only supports CosmosMsg<Empty>. When we actually
13/// use this in some consuming code, we should make it generic over CosmosMsg<T>.
14#[cw_serde]
15pub struct Cw3Contract(pub Addr);
16
17impl Cw3Contract {
18    pub fn addr(&self) -> Addr {
19        self.0.clone()
20    }
21
22    pub fn encode_msg(&self, msg: Cw3ExecuteMsg) -> StdResult<CosmosMsg> {
23        Ok(WasmMsg::Execute {
24            contract_addr: self.addr().into(),
25            msg: to_json_binary(&msg)?,
26            funds: vec![],
27        }
28        .into())
29    }
30
31    /// helper doesn't support custom messages now
32    pub fn proposal<T: Into<String>, U: Into<String>>(
33        &self,
34        title: T,
35        description: U,
36        msgs: Vec<CosmosMsg>,
37        earliest: Option<Expiration>,
38        latest: Option<Expiration>,
39    ) -> StdResult<CosmosMsg> {
40        let msg = Cw3ExecuteMsg::Propose {
41            title: title.into(),
42            description: description.into(),
43            msgs,
44            earliest,
45            latest,
46        };
47        self.encode_msg(msg)
48    }
49
50    pub fn vote(&self, proposal_id: u64, vote: Vote) -> StdResult<CosmosMsg> {
51        let msg = Cw3ExecuteMsg::Vote { proposal_id, vote };
52        self.encode_msg(msg)
53    }
54
55    pub fn execute(&self, proposal_id: u64) -> StdResult<CosmosMsg> {
56        let msg = Cw3ExecuteMsg::Execute { proposal_id };
57        self.encode_msg(msg)
58    }
59
60    pub fn close(&self, proposal_id: u64) -> StdResult<CosmosMsg> {
61        let msg = Cw3ExecuteMsg::Close { proposal_id };
62        self.encode_msg(msg)
63    }
64}