dao_migrator/utils/
state_queries.rs

1use cosmwasm_std::{Addr, Deps, StdResult, Uint128};
2
3use crate::{types::SingleProposalData, ContractError};
4
5use super::query_helpers::{
6    v1_expiration_to_v2, v1_status_to_v2, v1_threshold_to_v2, v1_votes_to_v2,
7};
8
9pub fn query_proposal_count_v1(deps: Deps, proposals_addrs: Vec<Addr>) -> StdResult<Vec<u64>> {
10    proposals_addrs
11        .into_iter()
12        .map(|proposal_addr| {
13            deps.querier.query_wasm_smart(
14                proposal_addr,
15                &cw_proposal_single_v1::msg::QueryMsg::ProposalCount {},
16            )
17        })
18        .collect()
19}
20
21pub fn query_proposal_count_v2(deps: Deps, proposals_addrs: Vec<Addr>) -> StdResult<Vec<u64>> {
22    proposals_addrs
23        .into_iter()
24        .map(|proposal_addr| {
25            deps.querier.query_wasm_smart(
26                proposal_addr,
27                &dao_proposal_single::msg::QueryMsg::ProposalCount {},
28            )
29        })
30        .collect()
31}
32
33pub fn query_proposal_v1(
34    deps: Deps,
35    proposals_addrs: Vec<Addr>,
36) -> Result<
37    (
38        Vec<dao_proposal_single::proposal::SingleChoiceProposal>,
39        SingleProposalData,
40    ),
41    ContractError,
42> {
43    let mut sample_proposal = None;
44
45    let proposals = proposals_addrs
46        .into_iter()
47        .map(|proposal_addr| {
48            let proposals: cw_proposal_single_v1::query::ProposalListResponse =
49                deps.querier.query_wasm_smart(
50                    proposal_addr.clone(),
51                    &cw_proposal_single_v1::msg::QueryMsg::ReverseProposals {
52                        start_before: None,
53                        limit: None,
54                    },
55                )?;
56
57            // If we don't have a proposal in the module, we can't do tests, so bail out.
58            let proposal = if proposals.proposals.is_empty() {
59                Err(ContractError::NoProposalsOnModule {
60                    module_addr: proposal_addr.to_string(),
61                })
62            } else {
63                Ok(proposals.proposals[0].clone().proposal)
64            }?;
65
66            if sample_proposal.is_none() {
67                sample_proposal = Some(SingleProposalData {
68                    proposer: proposal.proposer.clone(),
69                    start_height: proposal.start_height,
70                });
71            }
72
73            Ok(dao_proposal_single::proposal::SingleChoiceProposal {
74                title: proposal.title,
75                description: proposal.description,
76                proposer: proposal.proposer,
77                start_height: proposal.start_height,
78                min_voting_period: proposal.min_voting_period.map(v1_expiration_to_v2),
79                expiration: v1_expiration_to_v2(proposal.expiration),
80                threshold: v1_threshold_to_v2(proposal.threshold),
81                total_power: proposal.total_power,
82                msgs: proposal.msgs,
83                status: v1_status_to_v2(proposal.status),
84                votes: v1_votes_to_v2(proposal.votes),
85                allow_revoting: proposal.allow_revoting,
86                veto: None,
87            })
88        })
89        .collect::<Result<Vec<dao_proposal_single::proposal::SingleChoiceProposal>, ContractError>>(
90        )?;
91
92    Ok((proposals, sample_proposal.unwrap()))
93}
94
95pub fn query_proposal_v2(
96    deps: Deps,
97    proposals_addrs: Vec<Addr>,
98) -> Result<
99    (
100        Vec<dao_proposal_single::proposal::SingleChoiceProposal>,
101        SingleProposalData,
102    ),
103    ContractError,
104> {
105    let mut sample_proposal = None;
106
107    let proposals = proposals_addrs
108        .into_iter()
109        .map(|proposal_addr| {
110            let proposals: dao_proposal_single::query::ProposalListResponse =
111                deps.querier.query_wasm_smart(
112                    proposal_addr.clone(),
113                    &dao_proposal_single::msg::QueryMsg::ReverseProposals {
114                        start_before: None,
115                        limit: None,
116                    },
117                )?;
118
119            let proposal = if proposals.proposals.is_empty() {
120                Err(ContractError::NoProposalsOnModule {
121                    module_addr: proposal_addr.to_string(),
122                })
123            } else {
124                Ok(proposals.proposals[0].clone().proposal)
125            }?;
126
127            if sample_proposal.is_none() {
128                sample_proposal = Some(SingleProposalData {
129                    proposer: proposal.proposer.clone(),
130                    start_height: proposal.start_height,
131                });
132            }
133
134            Ok(proposal)
135        })
136        .collect::<Result<Vec<dao_proposal_single::proposal::SingleChoiceProposal>, ContractError>>(
137        )?;
138
139    Ok((proposals, sample_proposal.unwrap()))
140}
141
142pub fn query_total_voting_power_v1(
143    deps: Deps,
144    voting_addr: Addr,
145    height: u64,
146) -> StdResult<Uint128> {
147    let res: cw_core_interface_v1::voting::TotalPowerAtHeightResponse =
148        deps.querier.query_wasm_smart(
149            voting_addr,
150            &cw20_staked_balance_voting_v1::msg::QueryMsg::TotalPowerAtHeight {
151                height: Some(height),
152            },
153        )?;
154    Ok(res.power)
155}
156
157pub fn query_total_voting_power_v2(
158    deps: Deps,
159    voting_addr: Addr,
160    height: u64,
161) -> StdResult<Uint128> {
162    let res: dao_interface::voting::TotalPowerAtHeightResponse = deps.querier.query_wasm_smart(
163        voting_addr,
164        &dao_voting_cw20_staked::msg::QueryMsg::TotalPowerAtHeight {
165            height: Some(height),
166        },
167    )?;
168    Ok(res.power)
169}
170
171pub fn query_single_voting_power_v1(
172    deps: Deps,
173    voting_addr: Addr,
174    address: Addr,
175    height: u64,
176) -> StdResult<Uint128> {
177    let res: cw_core_interface_v1::voting::VotingPowerAtHeightResponse =
178        deps.querier.query_wasm_smart(
179            voting_addr,
180            &cw20_staked_balance_voting_v1::msg::QueryMsg::VotingPowerAtHeight {
181                address: address.into(),
182                height: Some(height),
183            },
184        )?;
185    Ok(res.power)
186}
187
188pub fn query_single_voting_power_v2(
189    deps: Deps,
190    voting_addr: Addr,
191    address: Addr,
192    height: u64,
193) -> StdResult<Uint128> {
194    let res: dao_interface::voting::VotingPowerAtHeightResponse = deps.querier.query_wasm_smart(
195        voting_addr,
196        &dao_voting_cw20_staked::msg::QueryMsg::VotingPowerAtHeight {
197            address: address.into(),
198            height: Some(height),
199        },
200    )?;
201    Ok(res.power)
202}