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 helpers to Item and Map and use them in cw4 helpers #412

Merged
merged 4 commits into from
Sep 13, 2021
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
52 changes: 31 additions & 21 deletions packages/storage-plus/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::path::Path;
#[cfg(feature = "iterator")]
use crate::prefix::{Bound, Prefix};
use cosmwasm_std::{
from_slice, to_vec, Addr, ContractResult, Empty, QuerierWrapper, QueryRequest, StdError,
StdResult, Storage, SystemResult, WasmQuery,
from_slice, to_vec, Addr, Binary, ContractResult, Empty, QuerierWrapper, QueryRequest,
StdError, StdResult, Storage, SystemResult, WasmQuery,
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -96,25 +96,7 @@ where
k: K,
) -> StdResult<Option<T>> {
let key = self.key(k).storage_key.into();
let request: QueryRequest<Empty> = WasmQuery::Raw {
contract_addr: remote_contract.into(),
key,
}
.into();

let raw = to_vec(&request).map_err(|serialize_err| {
StdError::generic_err(format!("Serializing QueryRequest: {}", serialize_err))
})?;
let result = match querier.raw_query(&raw) {
SystemResult::Err(system_err) => Err(StdError::generic_err(format!(
"Querier system error: {}",
system_err
))),
SystemResult::Ok(ContractResult::Err(contract_err)) => Err(StdError::generic_err(
format!("Querier contract error: {}", contract_err),
)),
SystemResult::Ok(ContractResult::Ok(value)) => Ok(value),
}?;
let result = query_raw(querier, remote_contract, key)?;
if result.is_empty() {
Ok(None)
} else {
Expand All @@ -123,6 +105,34 @@ where
}
}

// TODO: move this to a better helpers location
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have something specific in mind? Maybe create an issue along the way?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do this before merge.
Can't merge with a TODO (gotta make an issue for it first)

pub(crate) fn query_raw(
querier: &QuerierWrapper,
contract_addr: Addr,
key: Binary,
) -> StdResult<Binary> {
let request: QueryRequest<Empty> = WasmQuery::Raw {
contract_addr: contract_addr.into(),
key,
}
.into();

let raw = to_vec(&request).map_err(|serialize_err| {
StdError::generic_err(format!("Serializing QueryRequest: {}", serialize_err))
})?;
match querier.raw_query(&raw) {
SystemResult::Err(system_err) => Err(StdError::generic_err(format!(
"Querier system error: {}",
system_err
))),
SystemResult::Ok(ContractResult::Err(contract_err)) => Err(StdError::generic_err(format!(
"Querier contract error: {}",
contract_err
))),
SystemResult::Ok(ContractResult::Ok(value)) => Ok(value),
}
}

// short-cut for simple keys, rather than .prefix(()).range(...)
#[cfg(feature = "iterator")]
impl<'a, K, T> Map<'a, K, T>
Expand Down