open_edition_minter/
helpers.rs

1use cosmwasm_schema::cw_serde;
2use cosmwasm_std::{
3    to_json_binary, Addr, Coin, ContractInfoResponse, CustomQuery, Empty, Querier, QuerierWrapper,
4    StdError, StdResult, WasmMsg, WasmQuery,
5};
6use cw721_base::Extension;
7use sg721::ExecuteMsg as Sg721ExecuteMsg;
8use sg_metadata::Metadata;
9use sg_std::CosmosMsg;
10
11use crate::msg::{ConfigResponse, ExecuteMsg, QueryMsg};
12
13/// MinterContract is a wrapper around Addr that provides a lot of helpers
14/// for working with this.
15#[cw_serde]
16pub struct MinterContract(pub Addr);
17
18impl MinterContract {
19    pub fn addr(&self) -> Addr {
20        self.0.clone()
21    }
22
23    pub fn call<T: Into<ExecuteMsg>>(&self, msg: T) -> StdResult<CosmosMsg> {
24        let msg = to_json_binary(&msg.into())?;
25        Ok(WasmMsg::Execute {
26            contract_addr: self.addr().into(),
27            msg,
28            funds: vec![],
29        }
30        .into())
31    }
32
33    pub fn call_with_funds<T: Into<ExecuteMsg>>(
34        &self,
35        msg: T,
36        funds: Coin,
37    ) -> StdResult<CosmosMsg> {
38        let msg = to_json_binary(&msg.into())?;
39        Ok(WasmMsg::Execute {
40            contract_addr: self.addr().into(),
41            msg,
42            funds: vec![funds],
43        }
44        .into())
45    }
46
47    pub fn contract_info<Q, T, CQ>(&self, querier: &Q) -> StdResult<ContractInfoResponse>
48    where
49        Q: Querier,
50        T: Into<String>,
51        CQ: CustomQuery,
52    {
53        let query = WasmQuery::ContractInfo {
54            contract_addr: self.addr().into(),
55        }
56        .into();
57        let res: ContractInfoResponse = QuerierWrapper::<CQ>::new(querier).query(&query)?;
58        Ok(res)
59    }
60
61    pub fn config(&self, querier: &QuerierWrapper) -> StdResult<ConfigResponse> {
62        let res: ConfigResponse = querier.query_wasm_smart(self.addr(), &QueryMsg::Config {})?;
63        Ok(res)
64    }
65}
66
67pub fn mint_nft_msg(
68    sg721_address: Addr,
69    token_id: String,
70    recipient_addr: Addr,
71    extension: Option<Metadata>,
72    token_uri: Option<String>,
73) -> Result<CosmosMsg, StdError> {
74    let mint_msg = if let Some(extension) = extension {
75        CosmosMsg::Wasm(WasmMsg::Execute {
76            contract_addr: sg721_address.to_string(),
77            msg: to_json_binary(&Sg721ExecuteMsg::<Metadata, Empty>::Mint {
78                token_id,
79                owner: recipient_addr.to_string(),
80                token_uri: None,
81                extension,
82            })?,
83            funds: vec![],
84        })
85    } else {
86        CosmosMsg::Wasm(WasmMsg::Execute {
87            contract_addr: sg721_address.to_string(),
88            msg: to_json_binary(&Sg721ExecuteMsg::<Extension, Empty>::Mint {
89                token_id,
90                owner: recipient_addr.to_string(),
91                token_uri,
92                extension: None,
93            })?,
94            funds: vec![],
95        })
96    };
97    Ok(mint_msg)
98}