diff --git a/crates/cosmos/cosmos-chain-components/src/impls/queries/mod.rs b/crates/cosmos/cosmos-chain-components/src/impls/queries/mod.rs index 39775f933..f5f4aee9d 100644 --- a/crates/cosmos/cosmos-chain-components/src/impls/queries/mod.rs +++ b/crates/cosmos/cosmos-chain-components/src/impls/queries/mod.rs @@ -18,6 +18,7 @@ pub mod packet_receipt; pub mod received_packet; pub mod send_packet; pub mod send_packets; +pub mod staking_params; pub mod unreceived_acks; pub mod unreceived_packet; pub mod write_ack_event; diff --git a/crates/cosmos/cosmos-chain-components/src/impls/queries/staking_params.rs b/crates/cosmos/cosmos-chain-components/src/impls/queries/staking_params.rs new file mode 100644 index 000000000..5abc2538b --- /dev/null +++ b/crates/cosmos/cosmos-chain-components/src/impls/queries/staking_params.rs @@ -0,0 +1,83 @@ +use cgp::core::error::CanRaiseError; +use cgp::core::Async; +use eyre::Report; +use prost::{DecodeError, Message}; +use prost_types::Any; + +use ibc_proto::cosmos::staking::v1beta1::{Params, QueryParamsResponse}; +use ibc_relayer_types::Height; + +use hermes_chain_type_components::traits::types::commitment_proof::HasCommitmentProofType; +use hermes_encoding_components::traits::convert::CanConvert; +use hermes_encoding_components::traits::has_encoding::HasDefaultEncoding; +use hermes_encoding_components::types::AsBytes; +use hermes_relayer_components::chain::traits::types::height::HasHeightType; + +use crate::traits::abci_query::CanQueryAbci; +use crate::traits::staking_params_query::StakingParamsQuerier; +use crate::types::commitment_proof::CosmosCommitmentProof; + +pub struct QueryStakingParams; + +impl StakingParamsQuerier for QueryStakingParams +where + Chain: CanQueryAbci + + HasHeightType + + HasCommitmentProofType + + HasDefaultEncoding + + CanRaiseError + + CanRaiseError + + CanRaiseError, + Encoding: Async + CanConvert, +{ + async fn query_staking_params(chain: &Chain, height: &Height) -> Result { + let query_staking_params_bytes = chain + .query_abci( + "/cosmos.staking.v1beta1.Query/Params", + &"".to_owned().into_bytes(), + height, + ) + .await?; + + let query_params_any: Any = + Message::decode(query_staking_params_bytes.as_ref()).map_err(Chain::raise_error)?; + + let query_staking_params = Chain::default_encoding() + .convert(&query_params_any) + .map_err(Chain::raise_error)?; + + let staking_params = query_staking_params + .params + .ok_or_else(|| Report::msg("staking params is empty")) + .map_err(Chain::raise_error)?; + + Ok(staking_params) + } + + async fn query_staking_params_with_proofs( + chain: &Chain, + height: &Height, + ) -> Result<(Params, Chain::CommitmentProof), Chain::Error> { + let (query_staking_params_bytes, commitment_proof) = chain + .query_abci_with_proofs( + "/cosmos.staking.v1beta1.Query/Params", + &"".to_owned().into_bytes(), + height, + ) + .await?; + + let query_params_any: Any = + Message::decode(query_staking_params_bytes.as_ref()).map_err(Chain::raise_error)?; + + let query_staking_params = Chain::default_encoding() + .convert(&query_params_any) + .map_err(Chain::raise_error)?; + + let staking_params = query_staking_params + .params + .ok_or_else(|| Report::msg("staking params is empty")) + .map_err(Chain::raise_error)?; + + Ok((staking_params, commitment_proof)) + } +} diff --git a/crates/cosmos/cosmos-chain-components/src/traits/mod.rs b/crates/cosmos/cosmos-chain-components/src/traits/mod.rs index 1666f8231..e4c1a68c1 100644 --- a/crates/cosmos/cosmos-chain-components/src/traits/mod.rs +++ b/crates/cosmos/cosmos-chain-components/src/traits/mod.rs @@ -6,4 +6,5 @@ pub mod gas_config; pub mod grpc_address; pub mod message; pub mod rpc_client; +pub mod staking_params_query; pub mod tx_extension_options; diff --git a/crates/cosmos/cosmos-chain-components/src/traits/staking_params_query.rs b/crates/cosmos/cosmos-chain-components/src/traits/staking_params_query.rs new file mode 100644 index 000000000..1fbb14201 --- /dev/null +++ b/crates/cosmos/cosmos-chain-components/src/traits/staking_params_query.rs @@ -0,0 +1,15 @@ +use cgp::prelude::*; +use hermes_chain_type_components::traits::types::height::HasHeightType; +use hermes_relayer_components::chain::traits::types::proof::HasCommitmentProofType; +use ibc_proto::cosmos::staking::v1beta1::Params; + +#[derive_component(StakingParamsQuerierComponent, StakingParamsQuerier)] +#[async_trait] +pub trait CanQueryStakingParams: HasHeightType + HasCommitmentProofType + HasErrorType { + async fn query_staking_params(&self, height: &Self::Height) -> Result; + + async fn query_staking_params_with_proofs( + &self, + height: &Self::Height, + ) -> Result<(Params, Self::CommitmentProof), Self::Error>; +}