Skip to content

Commit

Permalink
feat: add some query method
Browse files Browse the repository at this point in the history
  • Loading branch information
shenao78 committed Jul 12, 2024
1 parent cdff632 commit 6ca9d70
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 14 deletions.
32 changes: 27 additions & 5 deletions src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::error::ContractError;
use crate::msg::{ExecuteMsg, GetCountResponse, InstantiateMsg, QueryMsg};
use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
use crate::state::{State, STATE};
use std::collections::{BTreeMap, BTreeSet};

Expand Down Expand Up @@ -107,6 +107,7 @@ pub mod execute {
let sender = env.contract.address.to_string();
let denom = format!("factory/{}/{}", sender, name);
let token = Token {
name: name.clone(),
denom: denom.clone(),
settlement_chain,
};
Expand All @@ -118,7 +119,7 @@ pub mod execute {

let msg = MsgCreateDenom {
sender,
subdenom: name.clone(),
subdenom: name,
};
let cosmos_msg = CosmosMsg::Stargate {
type_url: "/osmosis.tokenfactory.v1beta1.MsgCreateDenom".into(),
Expand Down Expand Up @@ -274,14 +275,35 @@ pub mod execute {
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::GetCount {} => to_json_binary(&query::count(deps)?),
QueryMsg::GetTokenList => to_json_binary(&query::get_token_list(deps)?),
QueryMsg::GetFeeInfo => to_json_binary(&query::get_fee_info(deps)?),
}
}

pub mod query {
use crate::{
msg::{GetFeeResponse, GetTokenResponse},
state::read_state,
};

use super::*;

pub fn count(_deps: Deps) -> StdResult<GetCountResponse> {
Ok(GetCountResponse { count: 1 })
pub fn get_token_list(deps: Deps) -> StdResult<GetTokenResponse> {
let tokens = read_state(deps.storage, |state| {
state
.tokens
.iter()
.map(|(_, token)| token.clone())
.collect()
});
Ok(GetTokenResponse { tokens })
}

pub fn get_fee_info(deps: Deps) -> StdResult<GetFeeResponse> {
Ok(read_state(deps.storage, |state| GetFeeResponse {
fee_token: state.fee_token.clone(),
fee_token_factor: state.fee_token_factor.clone(),
target_chain_factor: state.target_chain_factor.clone(),
}))
}
}
8 changes: 4 additions & 4 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cosmwasm_std::{
WasmQuery,
};

use crate::msg::{ExecuteMsg, GetCountResponse, QueryMsg};
use crate::msg::{ExecuteMsg, GetTokenResponse, QueryMsg};

/// CwTemplateContract is a wrapper around Addr that provides a lot of helpers
/// for working with this.
Expand All @@ -29,19 +29,19 @@ impl CwTemplateContract {
}

/// Get Count
pub fn count<Q, T, CQ>(&self, querier: &Q) -> StdResult<GetCountResponse>
pub fn count<Q, T, CQ>(&self, querier: &Q) -> StdResult<GetTokenResponse>
where
Q: Querier,
T: Into<String>,
CQ: CustomQuery,
{
let msg = QueryMsg::GetCount {};
let msg = QueryMsg::GetTokenList;
let query = WasmQuery::Smart {
contract_addr: self.addr().into(),
msg: to_json_binary(&msg)?,
}
.into();
let res: GetCountResponse = QuerierWrapper::<CQ>::new(querier).query(&query)?;
let res: GetTokenResponse = QuerierWrapper::<CQ>::new(querier).query(&query)?;
Ok(res)
}
}
22 changes: 17 additions & 5 deletions src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::collections::BTreeMap;

use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::Addr;

use crate::state::Token;

#[cw_serde]
pub struct InstantiateMsg {
pub owner: Addr,
Expand Down Expand Up @@ -54,13 +58,21 @@ pub enum Factor {
#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
// GetCount returns the current count as a json-encoded number
#[returns(GetCountResponse)]
GetCount {},
#[returns(GetTokenResponse)]
GetTokenList,
#[returns(GetFeeResponse)]
GetFeeInfo,
}

// We define a custom struct for each query response
#[cw_serde]
pub struct GetCountResponse {
pub count: i32,
pub struct GetTokenResponse {
pub tokens: Vec<Token>,
}

#[cw_serde]
pub struct GetFeeResponse {
pub fee_token: Option<String>,
pub fee_token_factor: Option<u128>,
pub target_chain_factor: BTreeMap<String, u128>,
}
1 change: 1 addition & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct State {

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct Token {
pub name: String,
pub denom: String,
pub settlement_chain: String,
}
Expand Down

0 comments on commit 6ca9d70

Please sign in to comment.