Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use QuerierWrapper not Querier in cw20 helpers #839

Merged
merged 5 commits into from Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion contracts/cw3-flex-multisig/src/contract.rs
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
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
@@ -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()
}
}