croncat_mod_nft/
contract.rs

1#[cfg(not(feature = "library"))]
2use cosmwasm_std::entry_point;
3use cosmwasm_std::{
4    to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdError, StdResult,
5};
6#[cfg(not(feature = "library"))]
7use cw2::set_contract_version;
8use cw721::Cw721QueryMsg::{OwnerOf, Tokens};
9use cw721::{OwnerOfResponse, TokensResponse};
10use mod_sdk::types::QueryResponse;
11
12use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
13use crate::types::OwnerOfNft;
14use crate::ContractError;
15
16// version info for migration info
17const CONTRACT_NAME: &str = "crate:croncat-mod-nft";
18const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
19
20#[cfg_attr(not(feature = "library"), entry_point)]
21pub fn instantiate(
22    deps: DepsMut,
23    _env: Env,
24    _info: MessageInfo,
25    msg: InstantiateMsg,
26) -> Result<Response, StdError> {
27    let contract_version = msg.version.unwrap_or_else(|| CONTRACT_VERSION.to_string());
28    set_contract_version(deps.storage, CONTRACT_NAME, &contract_version)?;
29
30    Ok(Response::new().add_attribute("action", "instantiate"))
31}
32
33#[cfg_attr(not(feature = "library"), entry_point)]
34pub fn execute(
35    _deps: DepsMut,
36    _env: Env,
37    _info: MessageInfo,
38    _msg: ExecuteMsg,
39) -> Result<Response, ContractError> {
40    Err(ContractError::Noop)
41}
42
43#[cfg_attr(not(feature = "library"), entry_point)]
44pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
45    match msg {
46        QueryMsg::OwnerOfNft(OwnerOfNft {
47            address,
48            nft_address,
49            token_id,
50        }) => to_binary(&query_nft_owner(deps, address, nft_address, token_id)?),
51        QueryMsg::AddrHasNft {
52            address,
53            nft_address,
54        } => to_binary(&query_addr_has_nft(deps, address, nft_address)?),
55    }
56}
57
58/// Query: OwnerOfNft
59/// Used as a helper method to check if address is the owner
60/// of the token with specific id in the nft_address contract
61///
62/// Response: QueryResponse
63/// Returns true if address owns the token
64/// Data contains information about the owner of this token
65/// Return error if token_id or nft_address are wrong
66fn query_nft_owner(
67    deps: Deps,
68    address: String,
69    nft_address: String,
70    token_id: String,
71) -> StdResult<QueryResponse> {
72    let valid_nft = deps.api.addr_validate(&nft_address)?;
73    let res: OwnerOfResponse = deps.querier.query_wasm_smart(
74        valid_nft,
75        &OwnerOf {
76            token_id,
77            include_expired: None,
78        },
79    )?;
80    Ok(QueryResponse {
81        result: address == res.owner,
82        data: to_binary(&res.owner)?,
83    })
84}
85
86/// Query: AddrHasNft
87/// Used as a helper method to check if address owns any nft tokens
88///
89/// Response: QueryResponse
90/// Returns true if address owns at least one token for this contracts
91/// Data contains the list of tokens which address owns
92/// Return error if nft_address is wrong
93fn query_addr_has_nft(
94    deps: Deps,
95    address: String,
96    nft_address: String,
97) -> StdResult<QueryResponse> {
98    let valid_nft = deps.api.addr_validate(&nft_address)?;
99    let res: TokensResponse = deps.querier.query_wasm_smart(
100        valid_nft,
101        &Tokens {
102            owner: address,
103            start_after: None,
104            limit: None,
105        },
106    )?;
107    Ok(QueryResponse {
108        result: !res.tokens.is_empty(),
109        data: to_binary(&res.tokens)?,
110    })
111}