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 CanQueryStakingParams component #478

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
@@ -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<Chain, Encoding> StakingParamsQuerier<Chain> for QueryStakingParams
where
Chain: CanQueryAbci
+ HasHeightType<Height = Height>
+ HasCommitmentProofType<CommitmentProof = CosmosCommitmentProof>
+ HasDefaultEncoding<AsBytes, Encoding = Encoding>
+ CanRaiseError<Report>
+ CanRaiseError<Encoding::Error>
+ CanRaiseError<DecodeError>,
Encoding: Async + CanConvert<Any, QueryParamsResponse>,
{
async fn query_staking_params(chain: &Chain, height: &Height) -> Result<Params, Chain::Error> {
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))
}
}
1 change: 1 addition & 0 deletions crates/cosmos/cosmos-chain-components/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
@@ -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<Chain>)]
#[async_trait]
pub trait CanQueryStakingParams: HasHeightType + HasCommitmentProofType + HasErrorType {
async fn query_staking_params(&self, height: &Self::Height) -> Result<Params, Self::Error>;

async fn query_staking_params_with_proofs(
&self,
height: &Self::Height,
) -> Result<(Params, Self::CommitmentProof), Self::Error>;
}
Loading