Skip to content

Commit

Permalink
Allow querying of matchmaking data
Browse files Browse the repository at this point in the history
  • Loading branch information
josefleventon committed Apr 2, 2023
1 parent 8765fdc commit 18ca93d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
36 changes: 34 additions & 2 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use crate::config::execute_update_params;
use crate::error::ContractError;
use crate::execute::{execute_cancel, execute_set_winner, execute_wager};
use crate::msg::{
ConfigResponse, ExecuteMsg, InstantiateMsg, QueryMsg, TokenStatusResponse, WagerResponse,
WagersResponse,
ConfigResponse, ExecuteMsg, InstantiateMsg, MatchmakingResponse, QueryMsg, TokenStatusResponse,
WagerResponse, WagersResponse,
};
use crate::state::{
wagers, Config, MatchmakingItem, MatchmakingItemExport, Token, TokenStatus, Wager, WagerExport,
Expand Down Expand Up @@ -149,6 +149,7 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::Wagers {} => to_binary(&query_wagers(deps)?),
QueryMsg::Wager { token } => to_binary(&query_wager(deps, token)?),
QueryMsg::Matchmaking {} => to_binary(&query_matchmaking(deps)?),
QueryMsg::TokenStatus { token } => to_binary(&query_token_status(deps, token)?),
QueryMsg::Config {} => to_binary(&query_config(deps)?),
}
Expand Down Expand Up @@ -190,6 +191,19 @@ pub fn query_wager(deps: Deps, token: Token) -> StdResult<WagerResponse> {
}
}

pub fn query_matchmaking(deps: Deps) -> StdResult<MatchmakingResponse> {
let config = CONFIG.load(deps.storage)?;

let matchmaking = MATCHMAKING
.range(deps.storage, None, None, Order::Ascending)
.collect::<StdResult<Vec<_>>>()?
.iter()
.map(|v| export_matchmaking(v.clone().0, v.clone().1, config.collection_address.clone()))
.collect();

Ok(MatchmakingResponse { matchmaking })
}

pub fn query_token_status(deps: Deps, token: Token) -> StdResult<TokenStatusResponse> {
// If there is a Wager for the token, return TokenStatus::Wager(Wager).
// If there is a MatchmakingItem for the token, return TokenStatus::Matchmaking(MatchmakingItem).
Expand Down Expand Up @@ -277,6 +291,24 @@ fn export_wager(v: Wager, collection: Addr) -> WagerExport {
}
}

fn export_matchmaking(
token_id: u64,
v: MatchmakingItem,
collection: Addr,
) -> MatchmakingItemExport {
MatchmakingItemExport {
token: NFT {
collection,
token_id,
},
currency: v.currency,
against_currencies: v.against_currencies,
expires_at: v.expires_at,
expiry: v.expiry,
amount: v.amount,
}
}

fn admin_only(deps: Deps, info: MessageInfo) -> Result<Empty, ContractError> {
let config = CONFIG.load(deps.storage)?;
if info.sender != config.fee_address {
Expand Down
9 changes: 8 additions & 1 deletion src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use cosmwasm_std::{Decimal, Uint128};

use crate::{
config::ParamInfo,
state::{Config, Currency, Token, TokenStatus, WagerExport},
state::{Config, Currency, MatchmakingItemExport, Token, TokenStatus, WagerExport},
};

#[cw_serde]
Expand Down Expand Up @@ -51,6 +51,8 @@ pub enum QueryMsg {
Wagers {},
#[returns(WagerResponse)]
Wager { token: Token },
#[returns(MatchmakingResponse)]
Matchmaking {},
#[returns(TokenStatusResponse)]
TokenStatus { token: Token },
#[returns(ConfigResponse)]
Expand All @@ -69,6 +71,11 @@ pub struct WagerResponse {
pub wager: WagerExport,
}

#[cw_serde]
pub struct MatchmakingResponse {
pub matchmaking: Vec<MatchmakingItemExport>,
}

#[cw_serde]
pub struct TokenStatusResponse {
pub token_status: TokenStatus,
Expand Down

0 comments on commit 18ca93d

Please sign in to comment.