From 5edd2c6e9995d3af4928991082c9a742796bfd9a Mon Sep 17 00:00:00 2001 From: Serkan Reis Date: Tue, 3 Sep 2024 17:46:10 +0300 Subject: [PATCH] Add stage related queries --- .../whitelist-tiered/src/contract.rs | 29 +++++++++++++++---- .../whitelists/whitelist-tiered/src/msg.rs | 16 ++++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/contracts/whitelists/whitelist-tiered/src/contract.rs b/contracts/whitelists/whitelist-tiered/src/contract.rs index c9784ed76..bd748ffec 100644 --- a/contracts/whitelists/whitelist-tiered/src/contract.rs +++ b/contracts/whitelists/whitelist-tiered/src/contract.rs @@ -4,15 +4,11 @@ use crate::admin::{ use crate::error::ContractError; use crate::helpers::validators::map_validate; use crate::helpers::{fetch_active_stage, fetch_active_stage_index, validate_stages}; -use crate::msg::{ - AddMembersMsg, ConfigResponse, ExecuteMsg, HasEndedResponse, HasMemberResponse, - HasStartedResponse, InstantiateMsg, IsActiveResponse, MembersResponse, QueryMsg, - RemoveMembersMsg, UpdateStageConfigMsg, -}; +use crate::msg::{AddMembersMsg, ConfigResponse, ExecuteMsg, HasEndedResponse, HasMemberResponse, HasStartedResponse, InstantiateMsg, IsActiveResponse, MembersResponse, QueryMsg, RemoveMembersMsg, StageResponse, StagesResponse, UpdateStageConfigMsg}; use crate::state::{AdminList, Config, Stage, ADMIN_LIST, CONFIG, WHITELIST_STAGES}; #[cfg(not(feature = "library"))] use cosmwasm_std::entry_point; -use cosmwasm_std::Order; +use cosmwasm_std::{Order, StdError}; use cosmwasm_std::{ ensure, to_json_binary, Binary, Coin, Deps, DepsMut, Env, MessageInfo, StdResult, Timestamp, Uint128, @@ -392,6 +388,8 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult { QueryMsg::ActiveStage {} => to_json_binary(&fetch_active_stage(deps.storage, &env)), QueryMsg::HasMember { member } => to_json_binary(&query_has_member(deps, env, member)?), QueryMsg::Config {} => to_json_binary(&query_config(deps, env)?), + QueryMsg::Stage { stage_id } => to_json_binary(&query_stage(deps, stage_id)?), + QueryMsg::Stages {} => to_json_binary(&query_stage_list(deps)?), QueryMsg::AdminList {} => to_json_binary(&query_admin_list(deps)?), QueryMsg::CanExecute { sender, .. } => to_json_binary(&query_can_execute(deps, &sender)?), } @@ -492,3 +490,22 @@ pub fn query_config(deps: Deps, env: Env) -> StdResult { }) } } + +pub fn query_stage(deps: Deps, stage_id: u32) -> StdResult { + let config = CONFIG.load(deps.storage)?; + ensure!( + stage_id < config.stages.len() as u32, + StdError::generic_err("Stage not found") + ); + Ok(StageResponse { + stage: config.stages[stage_id as usize].clone(), + }) +} + +pub fn query_stage_list(deps: Deps) -> StdResult { + let config = CONFIG.load(deps.storage)?; + ensure! (!config.stages.is_empty(), StdError::generic_err("No stages found")); + Ok(StagesResponse { + stages: config.stages.clone(), + }) +} diff --git a/contracts/whitelists/whitelist-tiered/src/msg.rs b/contracts/whitelists/whitelist-tiered/src/msg.rs index e94df6e57..b09314a38 100644 --- a/contracts/whitelists/whitelist-tiered/src/msg.rs +++ b/contracts/whitelists/whitelist-tiered/src/msg.rs @@ -76,6 +76,12 @@ pub enum QueryMsg { }, Config {}, + Stage { + stage_id: u32, + }, + + Stages {}, + AdminList {}, CanExecute { @@ -137,3 +143,13 @@ pub enum SudoMsg { pub struct CanExecuteResponse { pub can_execute: bool, } + +#[cw_serde] +pub struct StageResponse { + pub stage: Stage, +} + +#[cw_serde] +pub struct StagesResponse { + pub stages: Vec, +}