cw3_flex_multisig/
state.rs

1use cosmwasm_schema::cw_serde;
2use cosmwasm_std::{Addr, QuerierWrapper};
3use cw3::DepositInfo;
4use cw4::Cw4Contract;
5use cw_storage_plus::Item;
6use cw_utils::{Duration, Threshold};
7
8use crate::error::ContractError;
9
10/// Defines who is able to execute proposals once passed
11#[cw_serde]
12pub enum Executor {
13    /// Any member of the voting group, even with 0 points
14    Member,
15    /// Only the given address
16    Only(Addr),
17}
18
19#[cw_serde]
20pub struct Config {
21    pub threshold: Threshold,
22    pub max_voting_period: Duration,
23    // Total weight and voters are queried from this contract
24    pub group_addr: Cw4Contract,
25    // who is able to execute passed proposals
26    // None means that anyone can execute
27    pub executor: Option<Executor>,
28    /// The price, if any, of creating a new proposal.
29    pub proposal_deposit: Option<DepositInfo>,
30}
31
32impl Config {
33    // Executor can be set in 3 ways:
34    // - Member: any member of the voting group is authorized
35    // - Only: only passed address is authorized
36    // - None: Everyone are authorized
37    pub fn authorize(&self, querier: &QuerierWrapper, sender: &Addr) -> Result<(), ContractError> {
38        if let Some(executor) = &self.executor {
39            match executor {
40                Executor::Member => {
41                    self.group_addr
42                        .is_member(querier, sender, None)?
43                        .ok_or(ContractError::Unauthorized {})?;
44                }
45                Executor::Only(addr) => {
46                    if addr != sender {
47                        return Err(ContractError::Unauthorized {});
48                    }
49                }
50            }
51        }
52        Ok(())
53    }
54}
55
56// unique items
57pub const CONFIG: Item<Config> = Item::new("config");