Skip to content

Commit

Permalink
Add grpc query helper and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Apr 19, 2024
1 parent 1d58e80 commit d657d02
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/std/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ pub enum QueryRequest<C> {
/// The drawback of this query is that you have to handle the protobuf encoding and decoding yourself.
///
/// The returned data is protobuf encoded. The protobuf type depends on the query.
/// Because of this, using it with the [`query`](crate::QuerierWrapper::query) function will result
/// in a deserialization error.
/// Use [`raw_query`](crate::Querier::raw_query) or [`query_grpc`](crate::QuerierWrapper::query_grpc)
/// instead.
///
/// To find the path, as well as the request and response types,
/// you can query the chain's gRPC endpoint using a tool like
Expand Down
16 changes: 15 additions & 1 deletion packages/std/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,13 @@ impl<'a, C: CustomQuery> QuerierWrapper<'a, C> {
/// one level. Only use this if you don't need to check the SystemError
/// eg. If you don't differentiate between contract missing and contract returned error
pub fn query<U: DeserializeOwned>(&self, request: &QueryRequest<C>) -> StdResult<U> {
self.query_raw(request).and_then(|raw| from_json(raw))
}

/// Internal helper to avoid code duplication.
/// Performs a query and returns the binary result without deserializing it,
/// wrapping any errors that may occur into `StdError`.
fn query_raw(&self, request: &QueryRequest<C>) -> StdResult<Binary> {
let raw = to_json_vec(request).map_err(|serialize_err| {
StdError::generic_err(format!("Serializing QueryRequest: {serialize_err}"))
})?;
Expand All @@ -278,7 +285,7 @@ impl<'a, C: CustomQuery> QuerierWrapper<'a, C> {
SystemResult::Ok(ContractResult::Err(contract_err)) => Err(StdError::generic_err(
format!("Querier contract error: {contract_err}"),
)),
SystemResult::Ok(ContractResult::Ok(value)) => from_json(value),
SystemResult::Ok(ContractResult::Ok(value)) => Ok(value),
}
}

Expand Down Expand Up @@ -395,6 +402,13 @@ impl<'a, C: CustomQuery> QuerierWrapper<'a, C> {
Ok(res.validators)
}

/// See [`GrpcQuery`](crate::GrpcQuery) for more information.
#[cfg(feature = "cosmwasm_2_0")]
pub fn query_grpc(&self, path: String, data: Binary) -> StdResult<Binary> {
use crate::GrpcQuery;
self.query_raw(&QueryRequest::Grpc(GrpcQuery { path, data }))
}

/// Queries another wasm contract. You should know a priori the proper types for T and U
/// (response and request) based on the contract API
pub fn query_wasm_smart<T: DeserializeOwned>(
Expand Down

0 comments on commit d657d02

Please sign in to comment.