dao_interface/
state.rs

1use cosmwasm_schema::cw_serde;
2use cosmwasm_std::{Addr, Binary, Coin, CosmosMsg, WasmMsg};
3
4/// Top level config type for core module.
5#[cw_serde]
6pub struct Config {
7    /// The name of the contract.
8    pub name: String,
9    /// A description of the contract.
10    pub description: String,
11    /// An optional image URL for displaying alongside the contract.
12    pub image_url: Option<String>,
13    /// If true the contract will automatically add received cw20
14    /// tokens to its treasury.
15    pub automatically_add_cw20s: bool,
16    /// If true the contract will automatically add received cw721
17    /// tokens to its treasury.
18    pub automatically_add_cw721s: bool,
19    /// The URI for the DAO as defined by the DAOstar standard
20    /// <https://daostar.one/EIP>
21    pub dao_uri: Option<String>,
22}
23
24/// Top level type describing a proposal module.
25#[cw_serde]
26pub struct ProposalModule {
27    /// The address of the proposal module.
28    pub address: Addr,
29    /// The URL prefix of this proposal module as derived from the module ID.
30    /// Prefixes are mapped to letters, e.g. 0 is 'A', and 26 is 'AA'.
31    pub prefix: String,
32    /// The status of the proposal module, e.g. 'Enabled' or 'Disabled.'
33    pub status: ProposalModuleStatus,
34}
35
36/// The status of a proposal module.
37#[cw_serde]
38pub enum ProposalModuleStatus {
39    Enabled,
40    Disabled,
41}
42
43/// Information about the CosmWasm level admin of a contract. Used in
44/// conjunction with `ModuleInstantiateInfo` to instantiate modules.
45#[cw_serde]
46pub enum Admin {
47    /// Set the admin to a specified address.
48    Address { addr: String },
49    /// Sets the admin as the core module address.
50    CoreModule {},
51}
52
53/// Information needed to instantiate a module.
54#[cw_serde]
55pub struct ModuleInstantiateInfo {
56    /// Code ID of the contract to be instantiated.
57    pub code_id: u64,
58    /// Instantiate message to be used to create the contract.
59    pub msg: Binary,
60    /// CosmWasm level admin of the instantiated contract. See:
61    /// <https://docs.cosmwasm.com/docs/1.0/smart-contracts/migration>
62    pub admin: Option<Admin>,
63    /// Funds to be sent to the instantiated contract.
64    pub funds: Vec<Coin>,
65    /// Label for the instantiated contract.
66    pub label: String,
67}
68
69impl ModuleInstantiateInfo {
70    pub fn into_wasm_msg(self, dao: Addr) -> WasmMsg {
71        WasmMsg::Instantiate {
72            admin: self.admin.map(|admin| match admin {
73                Admin::Address { addr } => addr,
74                Admin::CoreModule {} => dao.into_string(),
75            }),
76            code_id: self.code_id,
77            msg: self.msg,
78            funds: self.funds,
79            label: self.label,
80        }
81    }
82}
83
84/// Callbacks to be executed when a module is instantiated
85#[cw_serde]
86pub struct ModuleInstantiateCallback {
87    pub msgs: Vec<CosmosMsg>,
88}
89
90#[cfg(test)]
91mod tests {
92    use super::*;
93
94    use cosmwasm_std::{to_json_binary, Addr, WasmMsg};
95
96    #[test]
97    fn test_module_instantiate_admin_none() {
98        let no_admin = ModuleInstantiateInfo {
99            code_id: 42,
100            msg: to_json_binary("foo").unwrap(),
101            admin: None,
102            label: "bar".to_string(),
103            funds: vec![],
104        };
105        assert_eq!(
106            no_admin.into_wasm_msg(Addr::unchecked("ekez")),
107            WasmMsg::Instantiate {
108                admin: None,
109                code_id: 42,
110                msg: to_json_binary("foo").unwrap(),
111                funds: vec![],
112                label: "bar".to_string()
113            }
114        )
115    }
116
117    #[test]
118    fn test_module_instantiate_admin_addr() {
119        let no_admin = ModuleInstantiateInfo {
120            code_id: 42,
121            msg: to_json_binary("foo").unwrap(),
122            admin: Some(Admin::Address {
123                addr: "core".to_string(),
124            }),
125            label: "bar".to_string(),
126            funds: vec![],
127        };
128        assert_eq!(
129            no_admin.into_wasm_msg(Addr::unchecked("ekez")),
130            WasmMsg::Instantiate {
131                admin: Some("core".to_string()),
132                code_id: 42,
133                msg: to_json_binary("foo").unwrap(),
134                funds: vec![],
135                label: "bar".to_string()
136            }
137        )
138    }
139
140    #[test]
141    fn test_module_instantiate_instantiator_addr() {
142        let no_admin = ModuleInstantiateInfo {
143            code_id: 42,
144            msg: to_json_binary("foo").unwrap(),
145            admin: Some(Admin::CoreModule {}),
146            label: "bar".to_string(),
147            funds: vec![],
148        };
149        assert_eq!(
150            no_admin.into_wasm_msg(Addr::unchecked("ekez")),
151            WasmMsg::Instantiate {
152                admin: Some("ekez".to_string()),
153                code_id: 42,
154                msg: to_json_binary("foo").unwrap(),
155                funds: vec![],
156                label: "bar".to_string()
157            }
158        )
159    }
160}