Skip to content

Commit

Permalink
std: Update BankQuery::AllDenomMetadata query with pagination and add…
Browse files Browse the repository at this point in the history
… BankQuery::DenomMetadata also gated by cosmwasm_1_3 feature flag
  • Loading branch information
nik-suri authored and chipshort committed May 31, 2023
1 parent 4bac1e3 commit 71c5a07
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 16 deletions.
31 changes: 29 additions & 2 deletions MIGRATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,33 @@ This guide explains what is needed to upgrade contracts when migrating over
major releases of `cosmwasm`. Note that you can also view the
[complete CHANGELOG](./CHANGELOG.md) to understand the differences.

## 1.2.x -> 1.3.0

- Update `cosmwasm-*` dependencies in Cargo.toml (skip the ones you don't use):

```
[dependencies]
cosmwasm-std = "1.3.0"
cosmwasm-storage = "1.3.0"
# ...
[dev-dependencies]
cosmwasm-schema = "1.3.0"
cosmwasm-vm = "1.3.0"
# ...
```

- If you want to use a fewture that os only available on CosmWasm 1.3+ chains,
use this feature:

```diff
-cosmwasm-std = { version = "1.1.0", features = ["stargate"] }
+cosmwasm-std = { version = "1.1.0", features = ["stargate", "cosmwasm_1_3"] }
```

Please note that `cosmwasm_1_2` implies `cosmwasm_1_1`, and `cosmwasm_1_3`
implies `cosmwasm_1_2`, and so on, so there is no need to set multiple.

## 1.1.x -> 1.2.0

- Update `cosmwasm-*` dependencies in Cargo.toml (skip the ones you don't use):
Expand All @@ -28,8 +55,8 @@ major releases of `cosmwasm`. Note that you can also view the
+cosmwasm-std = { version = "1.1.0", features = ["stargate", "cosmwasm_1_2"] }
```

Please note that `cosmwasm_1_2` implies `cosmwasm_1_1`, and `cosmwasm_1_3`
implies `cosmwasm_1_2`, and so on, so there is no need to set multiple.
Please note that `cosmwasm_1_2` implies `cosmwasm_1_1`, so there is no need to
set both.

- If you use mixed type multiplication between `Uint{64,128,256}` and
`Decimal{,256}`, check out
Expand Down
2 changes: 2 additions & 0 deletions packages/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod iterator;
mod math;
mod metadata;
mod never;
mod pagination;
mod panic;
mod query;
mod results;
Expand Down Expand Up @@ -59,6 +60,7 @@ pub use crate::math::{
};
pub use crate::metadata::{DenomMetadata, DenomUnit};
pub use crate::never::Never;
pub use crate::pagination::PageRequest;
#[cfg(feature = "cosmwasm_1_2")]
pub use crate::query::CodeInfoResponse;
#[cfg(feature = "cosmwasm_1_1")]
Expand Down
6 changes: 3 additions & 3 deletions packages/std/src/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::math::Uint128;

/// Replicates the cosmos-sdk bank module Metadata type
#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, Eq, JsonSchema)]
pub struct DenomMetadata {
pub description: String,
Expand All @@ -15,9 +14,10 @@ pub struct DenomMetadata {
pub uri_hash: String,
}

/// Replicates the cosmos-sdk bank module DenomUnit type
#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, Eq, JsonSchema)]
pub struct DenomUnit {
pub denom: String,
pub exponent: Uint128,
pub exponent: u32,
pub aliases: Vec<String>,
}
14 changes: 14 additions & 0 deletions packages/std/src/pagination.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::{Binary, Uint64};

/// Replicates the PageRequest type for pagination from the cosmos-sdk
#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, Eq, JsonSchema)]
pub struct PageRequest {
pub key: Binary,
pub offset: Uint64,
pub limit: Uint64,
pub count_total: bool,
pub reverse: bool,
}
23 changes: 21 additions & 2 deletions packages/std/src/query/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use crate::Coin;
#[cfg(feature = "cosmwasm_1_3")]
use crate::DenomMetadata;

#[cfg(feature = "cosmwasm_1_3")]
use crate::PageRequest;

use super::query_response::QueryResponseType;

#[non_exhaustive]
Expand All @@ -24,10 +27,14 @@ pub enum BankQuery {
/// Note that this may be much more expensive than Balance and should be avoided if possible.
/// Return value is AllBalanceResponse.
AllBalances { address: String },
/// This calls into the native bank module for querying metadata for all bank tokens.
/// This calls into the native bank module for querying metadata for a specific bank token.
/// Return value is DenomMetadataResponse
#[cfg(feature = "cosmwasm_1_3")]
DenomMetadata { denom: String },
/// This calls into the native bank module for querying metadata for all bank tokens that have a metadata entry.
/// Return value is AllDenomMetadataResponse
#[cfg(feature = "cosmwasm_1_3")]
AllDenomMetadata {},
AllDenomMetadata { pagination: Option<PageRequest> },
}

#[cfg(feature = "cosmwasm_1_1")]
Expand Down Expand Up @@ -62,6 +69,18 @@ pub struct AllBalanceResponse {

impl QueryResponseType for AllBalanceResponse {}

#[cfg(feature = "cosmwasm_1_3")]
#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub struct DenomMetadataResponse {
/// Always returns metadata for all token denoms on the base chain.
pub metadata: DenomMetadata,
}

#[cfg(feature = "cosmwasm_1_3")]
impl QueryResponseType for DenomMetadataResponse {}

#[cfg(feature = "cosmwasm_1_3")]
#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
Expand Down
4 changes: 2 additions & 2 deletions packages/std/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ mod query_response;
mod staking;
mod wasm;

#[cfg(feature = "cosmwasm_1_3")]
pub use bank::AllDenomMetadataResponse;
#[cfg(feature = "cosmwasm_1_1")]
pub use bank::SupplyResponse;
pub use bank::{AllBalanceResponse, BalanceResponse, BankQuery};
#[cfg(feature = "cosmwasm_1_3")]
pub use bank::{AllDenomMetadataResponse, DenomMetadataResponse};
#[cfg(feature = "stargate")]
pub use ibc::{ChannelResponse, IbcQuery, ListChannelsResponse, PortIdResponse};
#[cfg(feature = "staking")]
Expand Down
19 changes: 16 additions & 3 deletions packages/std/src/testing/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ use crate::ibc::{
IbcTimeoutBlock,
};
use crate::math::Uint128;
#[cfg(feature = "cosmwasm_1_3")]
use crate::query::AllDenomMetadataResponse;
#[cfg(feature = "cosmwasm_1_1")]
use crate::query::SupplyResponse;
use crate::query::{
Expand All @@ -28,6 +26,8 @@ use crate::query::{
AllDelegationsResponse, AllValidatorsResponse, BondedDenomResponse, DelegationResponse,
FullDelegation, StakingQuery, Validator, ValidatorResponse,
};
#[cfg(feature = "cosmwasm_1_3")]
use crate::query::{AllDenomMetadataResponse, DenomMetadataResponse};
use crate::results::{ContractResult, Empty, SystemResult};
use crate::serde::{from_slice, to_binary};
use crate::storage::MemoryStorage;
Expand Down Expand Up @@ -688,7 +688,20 @@ impl BankQuerier {
to_binary(&bank_res).into()
}
#[cfg(feature = "cosmwasm_1_3")]
BankQuery::AllDenomMetadata {} => {
BankQuery::DenomMetadata { denom } => {
let denom_metadata = self.denom_metadata.iter().find(|m| &m.base == denom);
match denom_metadata {
Some(m) => {
let metadata_res = DenomMetadataResponse {
metadata: m.clone(),
};
to_binary(&metadata_res).into()
}
None => return SystemResult::Err(SystemError::Unknown {}),
}
}
#[cfg(feature = "cosmwasm_1_3")]
BankQuery::AllDenomMetadata { pagination: _ } => {
let metadata_res = AllDenomMetadataResponse {
metadata: self.denom_metadata.clone(),
};
Expand Down
23 changes: 19 additions & 4 deletions packages/std/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use crate::coin::Coin;
use crate::errors::{RecoverPubkeyError, StdError, StdResult, VerificationError};
#[cfg(feature = "iterator")]
use crate::iterator::{Order, Record};
#[cfg(feature = "cosmwasm_1_3")]
use crate::query::AllDenomMetadataResponse;
#[cfg(feature = "cosmwasm_1_2")]
use crate::query::CodeInfoResponse;
#[cfg(feature = "cosmwasm_1_1")]
Expand All @@ -22,11 +20,15 @@ use crate::query::{
AllDelegationsResponse, AllValidatorsResponse, BondedDenomResponse, Delegation,
DelegationResponse, FullDelegation, StakingQuery, Validator, ValidatorResponse,
};
#[cfg(feature = "cosmwasm_1_3")]
use crate::query::{AllDenomMetadataResponse, DenomMetadataResponse};
use crate::results::{ContractResult, Empty, SystemResult};
use crate::serde::{from_binary, to_binary, to_vec};
use crate::ContractInfoResponse;
#[cfg(feature = "cosmwasm_1_3")]
use crate::DenomMetadata;
#[cfg(feature = "cosmwasm_1_3")]
use crate::PageRequest;

/// Storage provides read and write access to a persistent storage.
/// If you only want to provide read access, provide `&Storage`
Expand Down Expand Up @@ -244,8 +246,21 @@ impl<'a, C: CustomQuery> QuerierWrapper<'a, C> {
}

#[cfg(feature = "cosmwasm_1_3")]
pub fn query_all_denom_metadata(&self) -> StdResult<Vec<DenomMetadata>> {
let request = BankQuery::AllDenomMetadata {}.into();
pub fn query_denom_metadata(&self, denom: impl Into<String>) -> StdResult<DenomMetadata> {
let request = BankQuery::DenomMetadata {
denom: denom.into(),
}
.into();
let res: DenomMetadataResponse = self.query(&request)?;
Ok(res.metadata)
}

#[cfg(feature = "cosmwasm_1_3")]
pub fn query_all_denom_metadata(
&self,
pagination: Option<PageRequest>,
) -> StdResult<Vec<DenomMetadata>> {
let request = BankQuery::AllDenomMetadata { pagination }.into();
let res: AllDenomMetadataResponse = self.query(&request)?;
Ok(res.metadata)
}
Expand Down

0 comments on commit 71c5a07

Please sign in to comment.