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

Add query_grpc and document gRPC querying #2120

Merged
merged 3 commits into from
Apr 19, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add grpc query helper and docs
chipshort committed Apr 19, 2024

Verified

This commit was signed with the committer’s verified signature.
chipshort Christoph Otter
commit d657d021c9334aa58a292d16ee66d8780b25f40d
4 changes: 4 additions & 0 deletions packages/std/src/query/mod.rs
Original file line number Diff line number Diff line change
@@ -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
16 changes: 15 additions & 1 deletion packages/std/src/traits.rs
Original file line number Diff line number Diff line change
@@ -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}"))
})?;
@@ -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),
}
}

@@ -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>(