Skip to content

Commit

Permalink
Merge pull request #839 from CosmWasm/querier-wrapper-in-helpers
Browse files Browse the repository at this point in the history
Use QuerierWrapper not Querier in cw20 helpers
  • Loading branch information
uint authored Nov 1, 2022
2 parents 1bf0b3c + 99afad0 commit 3610110
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 58 deletions.
2 changes: 1 addition & 1 deletion contracts/cw3-flex-multisig/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ mod tests {
.unwrap();

// Verify contract version set properly
let version = query_contract_info(&app, flex_addr.clone()).unwrap();
let version = query_contract_info(&app.wrap(), flex_addr.clone()).unwrap();
assert_eq!(
ContractVersion {
contract: CONTRACT_NAME.to_string(),
Expand Down
14 changes: 9 additions & 5 deletions packages/cw2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ For more information on this specification, please check out the
*/

use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Empty, Querier, QuerierWrapper, QueryRequest, StdResult, Storage, WasmQuery};
use cosmwasm_std::{CustomQuery, QuerierWrapper, QueryRequest, StdResult, Storage, WasmQuery};
use cw_storage_plus::Item;

pub const CONTRACT: Item<ContractVersion> = Item::new("contract_info");
Expand Down Expand Up @@ -59,15 +59,19 @@ pub fn set_contract_version<T: Into<String>, U: Into<String>>(
/// if the other contract exists and claims to be a cw20-base contract for example.
/// (Note: you usually want to require *interfaces* not *implementations* of the
/// contracts you compose with, so be careful of overuse)
pub fn query_contract_info<Q: Querier, T: Into<String>>(
querier: &Q,
pub fn query_contract_info<T, CQ>(
querier: &QuerierWrapper<CQ>,
contract_addr: T,
) -> StdResult<ContractVersion> {
) -> StdResult<ContractVersion>
where
T: Into<String>,
CQ: CustomQuery,
{
let req = QueryRequest::Wasm(WasmQuery::Raw {
contract_addr: contract_addr.into(),
key: CONTRACT.as_slice().into(),
});
QuerierWrapper::<Empty>::new(querier).query(&req)
querier.query(&req)
}

#[cfg(test)]
Expand Down
90 changes: 38 additions & 52 deletions packages/cw20/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{
to_binary, Addr, CosmosMsg, CustomQuery, Querier, QuerierWrapper, StdResult, Uint128, WasmMsg,
WasmQuery,
to_binary, Addr, CosmosMsg, CustomQuery, QuerierWrapper, QueryRequest, StdResult, Uint128,
WasmMsg, WasmQuery,
};

use crate::{
Expand Down Expand Up @@ -31,89 +31,75 @@ impl Cw20Contract {
.into())
}

fn encode_smart_query<CQ: CustomQuery>(
&self,
msg: Cw20QueryMsg,
) -> StdResult<QueryRequest<CQ>> {
Ok(WasmQuery::Smart {
contract_addr: self.addr().into(),
msg: to_binary(&msg)?,
}
.into())
}

/// Get token balance for the given address
pub fn balance<Q, T, CQ>(&self, querier: &Q, address: T) -> StdResult<Uint128>
pub fn balance<T, CQ>(&self, querier: &QuerierWrapper<CQ>, address: T) -> StdResult<Uint128>
where
Q: Querier,
T: Into<String>,
CQ: CustomQuery,
{
let msg = Cw20QueryMsg::Balance {
let query = self.encode_smart_query(Cw20QueryMsg::Balance {
address: address.into(),
};
let query = WasmQuery::Smart {
contract_addr: self.addr().into(),
msg: to_binary(&msg)?,
}
.into();
let res: BalanceResponse = QuerierWrapper::<CQ>::new(querier).query(&query)?;
})?;
let res: BalanceResponse = querier.query(&query)?;
Ok(res.balance)
}

/// Get metadata from the contract. This is a good check that the address
/// is a valid Cw20 contract.
pub fn meta<Q, CQ>(&self, querier: &Q) -> StdResult<TokenInfoResponse>
where
Q: Querier,
CQ: CustomQuery,
{
let msg = Cw20QueryMsg::TokenInfo {};
let query = WasmQuery::Smart {
contract_addr: self.addr().into(),
msg: to_binary(&msg)?,
}
.into();
QuerierWrapper::<CQ>::new(querier).query(&query)
pub fn meta<CQ: CustomQuery>(
&self,
querier: &QuerierWrapper<CQ>,
) -> StdResult<TokenInfoResponse> {
let query = self.encode_smart_query(Cw20QueryMsg::TokenInfo {})?;
querier.query(&query)
}

/// Get allowance of spender to use owner's account
pub fn allowance<Q, T, U, CQ>(
pub fn allowance<T, U, CQ>(
&self,
querier: &Q,
querier: &QuerierWrapper<CQ>,
owner: T,
spender: U,
) -> StdResult<AllowanceResponse>
where
Q: Querier,
T: Into<String>,
U: Into<String>,
CQ: CustomQuery,
{
let msg = Cw20QueryMsg::Allowance {
let query = self.encode_smart_query(Cw20QueryMsg::Allowance {
owner: owner.into(),
spender: spender.into(),
};
let query = WasmQuery::Smart {
contract_addr: self.addr().into(),
msg: to_binary(&msg)?,
}
.into();
QuerierWrapper::<CQ>::new(querier).query(&query)
})?;
querier.query(&query)
}

/// Find info on who can mint, and how much
pub fn minter<Q, CQ>(&self, querier: &Q) -> StdResult<Option<MinterResponse>>
where
Q: Querier,
CQ: CustomQuery,
{
let msg = Cw20QueryMsg::Minter {};
let query = WasmQuery::Smart {
contract_addr: self.addr().into(),
msg: to_binary(&msg)?,
}
.into();
QuerierWrapper::<CQ>::new(querier).query(&query)
pub fn minter<CQ: CustomQuery>(
&self,
querier: &QuerierWrapper<CQ>,
) -> StdResult<Option<MinterResponse>> {
let query = self.encode_smart_query(Cw20QueryMsg::Minter {})?;
querier.query(&query)
}

/// returns true if the contract supports the allowance extension
pub fn has_allowance<Q: Querier, CQ: CustomQuery>(&self, querier: &Q) -> bool {
self.allowance::<_, _, _, CQ>(querier, self.addr(), self.addr())
.is_ok()
pub fn has_allowance<CQ: CustomQuery>(&self, querier: &QuerierWrapper<CQ>) -> bool {
self.allowance(querier, self.addr(), self.addr()).is_ok()
}

/// returns true if the contract supports the mintable extension
pub fn is_mintable<Q: Querier, CQ: CustomQuery>(&self, querier: &Q) -> bool {
self.minter::<_, CQ>(querier).is_ok()
pub fn is_mintable<CQ: CustomQuery>(&self, querier: &QuerierWrapper<CQ>) -> bool {
self.minter(querier).is_ok()
}
}

0 comments on commit 3610110

Please sign in to comment.