From 4e5dfcbfebd15aabef4b4cd1749219816fb6388c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Thu, 4 Jun 2026 15:44:03 +0100 Subject: [PATCH] added basic feegrant module allowances query --- .../module_traits/feegrant/mod.rs | 4 + .../module_traits/feegrant/query.rs | 153 ++++++++++++++++++ .../nyxd/cosmwasm_client/module_traits/mod.rs | 1 + 3 files changed, 158 insertions(+) create mode 100644 common/client-libs/validator-client/src/nyxd/cosmwasm_client/module_traits/feegrant/mod.rs create mode 100644 common/client-libs/validator-client/src/nyxd/cosmwasm_client/module_traits/feegrant/query.rs diff --git a/common/client-libs/validator-client/src/nyxd/cosmwasm_client/module_traits/feegrant/mod.rs b/common/client-libs/validator-client/src/nyxd/cosmwasm_client/module_traits/feegrant/mod.rs new file mode 100644 index 0000000000..172cb930d2 --- /dev/null +++ b/common/client-libs/validator-client/src/nyxd/cosmwasm_client/module_traits/feegrant/mod.rs @@ -0,0 +1,4 @@ +// Copyright 2026 - Nym Technologies SA +// SPDX-License-Identifier: GPL-3.0-only + +pub mod query; diff --git a/common/client-libs/validator-client/src/nyxd/cosmwasm_client/module_traits/feegrant/query.rs b/common/client-libs/validator-client/src/nyxd/cosmwasm_client/module_traits/feegrant/query.rs new file mode 100644 index 0000000000..233f56f8eb --- /dev/null +++ b/common/client-libs/validator-client/src/nyxd/cosmwasm_client/module_traits/feegrant/query.rs @@ -0,0 +1,153 @@ +// Copyright 2026 - Nym Technologies SA +// SPDX-License-Identifier: GPL-3.0-only + +// pub use cosmrs::feegrant::{Q} + +// unfortunately queries are not implemented within cosmrs + +use crate::nyxd::error::NyxdError; +use crate::nyxd::{Any, CosmWasmClient}; +use async_trait::async_trait; +use cosmrs::AccountId; + +use cosmrs::proto::cosmos::feegrant::v1beta1::{ + QueryAllowancesRequest as ProtoQueryAllowancesRequest, + QueryAllowancesResponse as ProtoQueryAllowancesResponse, +}; + +#[derive(Clone, Debug, PartialEq)] +pub struct QueryAllowancesRequest { + pub grantee: AccountId, + + pub pagination: Option, +} + +impl TryFrom + for QueryAllowancesRequest +{ + type Error = cosmrs::ErrorReport; + + fn try_from( + value: cosmrs::proto::cosmos::feegrant::v1beta1::QueryAllowancesRequest, + ) -> Result { + Ok(QueryAllowancesRequest { + grantee: value.grantee.parse()?, + pagination: value.pagination.map(Into::into), + }) + } +} + +impl From + for cosmrs::proto::cosmos::feegrant::v1beta1::QueryAllowancesRequest +{ + fn from(value: QueryAllowancesRequest) -> Self { + Self { + grantee: value.grantee.to_string(), + pagination: value.pagination.map(Into::into), + } + } +} + +#[derive(Clone, Debug, PartialEq)] +pub struct QueryAllowancesResponse { + pub allowances: Vec, + pub pagination: Option, +} + +impl TryFrom + for QueryAllowancesResponse +{ + type Error = cosmrs::ErrorReport; + + fn try_from( + value: cosmrs::proto::cosmos::feegrant::v1beta1::QueryAllowancesResponse, + ) -> Result { + Ok(QueryAllowancesResponse { + allowances: value + .allowances + .into_iter() + .map(TryInto::try_into) + .collect::>()?, + pagination: value.pagination.map(Into::into), + }) + } +} + +impl From + for cosmrs::proto::cosmos::feegrant::v1beta1::QueryAllowancesResponse +{ + fn from(value: QueryAllowancesResponse) -> Self { + Self { + allowances: value.allowances.into_iter().map(Into::into).collect(), + pagination: value.pagination.map(Into::into), + } + } +} + +/// Grant is stored in the KVStore to record a grant with full context +#[derive(Clone, Debug, PartialEq)] +pub struct Grant { + /// granter is the address of the user granting an allowance of their funds. + pub granter: AccountId, + + /// grantee is the address of the user being granted an allowance of another user's funds. + pub grantee: AccountId, + + /// allowance can be any of basic, periodic, allowed fee allowance. + pub allowance: Option, +} + +impl TryFrom for Grant { + type Error = cosmrs::ErrorReport; + + fn try_from( + value: cosmrs::proto::cosmos::feegrant::v1beta1::Grant, + ) -> Result { + Ok(Grant { + granter: value.granter.parse()?, + grantee: value.grantee.parse()?, + allowance: value.allowance, + }) + } +} + +impl From for cosmrs::proto::cosmos::feegrant::v1beta1::Grant { + fn from(value: Grant) -> Self { + Self { + granter: value.granter.to_string(), + grantee: value.grantee.to_string(), + allowance: value.allowance, + } + } +} + +// TODO: change trait restriction from `CosmWasmClient` to `TendermintRpcClient` +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +pub trait FeegrantQueryClient: CosmWasmClient { + async fn allowances( + &self, + grantee: AccountId, + pagination: Option, + ) -> Result { + let path = Some("/cosmos.feegrant.v1beta1.Query/Allowances".to_owned()); + + let req = QueryAllowancesRequest { + grantee, + pagination, + }; + + let res = self + .make_abci_query::( + path, + req.into(), + ) + .await?; + + Ok(res.try_into()?) + } +} + +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +impl FeegrantQueryClient for T where T: CosmWasmClient {} diff --git a/common/client-libs/validator-client/src/nyxd/cosmwasm_client/module_traits/mod.rs b/common/client-libs/validator-client/src/nyxd/cosmwasm_client/module_traits/mod.rs index e27ad09f98..3ace2c7591 100644 --- a/common/client-libs/validator-client/src/nyxd/cosmwasm_client/module_traits/mod.rs +++ b/common/client-libs/validator-client/src/nyxd/cosmwasm_client/module_traits/mod.rs @@ -1,6 +1,7 @@ // Copyright 2023 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 +pub mod feegrant; pub mod slashing; pub mod staking;