Skip to content

Commit

Permalink
Add DelegationRewards query
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Jul 21, 2023
1 parent 5d07829 commit 981cb46
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ pub use crate::query::{
};
#[cfg(feature = "stargate")]
pub use crate::query::{ChannelResponse, IbcQuery, ListChannelsResponse, PortIdResponse};
#[cfg(feature = "cosmwasm_1_4")]
pub use crate::query::{DecCoin, DelegationRewardsResponse};
#[allow(deprecated)]
pub use crate::results::SubMsgExecutionResponse;
#[cfg(all(feature = "stargate", feature = "cosmwasm_1_2"))]
Expand Down
33 changes: 32 additions & 1 deletion packages/std/src/query/distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ use super::query_response::QueryResponseType;
#[serde(rename_all = "snake_case")]
pub enum DistributionQuery {
// https://github.com/cosmos/cosmos-sdk/blob/4f6f6c00021f4b5ee486bbb71ae2071a8ceb47c9/x/distribution/types/query.pb.go#L792-L795
DelegatorWithdrawAddress { delegator_address: String },
DelegatorWithdrawAddress {
delegator_address: String,
},
// https://github.com/cosmos/cosmos-sdk/blob/e3482f2d4142c55f9dc3f47a321b56610a11492c/x/distribution/types/query.pb.go#L525-L532
#[cfg(feature = "cosmwasm_1_4")]
DelegationRewards {
delegator_address: String,
validator_address: String,
},
}

// https://github.com/cosmos/cosmos-sdk/blob/4f6f6c00021f4b5ee486bbb71ae2071a8ceb47c9/x/distribution/types/query.pb.go#L832-L835
Expand All @@ -24,3 +32,26 @@ pub struct DelegatorWithdrawAddressResponse {
impl_response_constructor!(DelegatorWithdrawAddressResponse, withdraw_address: Addr);

impl QueryResponseType for DelegatorWithdrawAddressResponse {}

// https://github.com/cosmos/cosmos-sdk/blob/e3482f2d4142c55f9dc3f47a321b56610a11492c/x/distribution/types/query.pb.go#L567-L572
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
#[cfg(feature = "cosmwasm_1_4")]
pub struct DelegationRewardsResponse {
pub rewards: Vec<DecCoin>,
}

#[cfg(feature = "cosmwasm_1_4")]
impl_response_constructor!(DelegationRewardsResponse, rewards: Vec<DecCoin>);
#[cfg(feature = "cosmwasm_1_4")]
impl QueryResponseType for DelegationRewardsResponse {}

/// A coin type with decimal amount.
/// Modeled after the Cosmos SDK's `DecCoin` type
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
#[cfg(feature = "cosmwasm_1_4")]
pub struct DecCoin {
pub denom: String,
pub amount: crate::Decimal,
}
2 changes: 2 additions & 0 deletions packages/std/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub use bank::SupplyResponse;
pub use bank::{AllBalanceResponse, BalanceResponse, BankQuery};
#[cfg(feature = "cosmwasm_1_3")]
pub use bank::{AllDenomMetadataResponse, DenomMetadataResponse};
#[cfg(feature = "cosmwasm_1_4")]
pub use distribution::{DecCoin, DelegationRewardsResponse};
#[cfg(feature = "cosmwasm_1_3")]
pub use distribution::{DelegatorWithdrawAddressResponse, DistributionQuery};
#[cfg(feature = "stargate")]
Expand Down
34 changes: 33 additions & 1 deletion packages/std/src/testing/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ use crate::{
use crate::{Attribute, DenomMetadata};
#[cfg(feature = "stargate")]
use crate::{ChannelResponse, IbcQuery, ListChannelsResponse, PortIdResponse};
#[cfg(feature = "cosmwasm_1_4")]
use crate::{DecCoin, DelegationRewardsResponse};

use super::riffle_shuffle;

Expand Down Expand Up @@ -934,12 +936,17 @@ impl StakingQuerier {
#[derive(Clone, Default)]
pub struct DistributionQuerier {
withdraw_addresses: HashMap<String, String>,
#[cfg(feature = "cosmwasm_1_4")]
rewards: BTreeMap<(String, String), Vec<crate::DecCoin>>,
}

#[cfg(feature = "cosmwasm_1_3")]
impl DistributionQuerier {
pub fn new(withdraw_addresses: HashMap<String, String>) -> Self {
DistributionQuerier { withdraw_addresses }
DistributionQuerier {
withdraw_addresses,
..Default::default()
}
}

pub fn set_withdraw_address(
Expand Down Expand Up @@ -968,6 +975,17 @@ impl DistributionQuerier {
self.withdraw_addresses.clear();
}

/// Sets accumulated rewards for a given validator and delegator pair.
pub fn set_rewards(
&mut self,
validator: impl Into<String>,
delegator: impl Into<String>,
rewards: Vec<DecCoin>,
) {
self.rewards
.insert((validator.into(), delegator.into()), rewards);
}

pub fn query(&self, request: &DistributionQuery) -> QuerierResult {
let contract_result: ContractResult<Binary> = match request {
DistributionQuery::DelegatorWithdrawAddress { delegator_address } => {
Expand All @@ -980,6 +998,20 @@ impl DistributionQuerier {
};
to_binary(&res).into()
}
#[cfg(feature = "cosmwasm_1_4")]
DistributionQuery::DelegationRewards {
delegator_address,
validator_address,
} => {
let res = DelegationRewardsResponse {
rewards: self
.rewards
.get(&(validator_address.clone(), delegator_address.clone()))
.cloned()
.unwrap_or_default(),
};
to_binary(&res).into()
}
};
// system result is always ok in the mock implementation
SystemResult::Ok(contract_result)
Expand Down
18 changes: 18 additions & 0 deletions packages/std/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,24 @@ impl<'a, C: CustomQuery> QuerierWrapper<'a, C> {
self.query(&request)
}

#[cfg(feature = "cosmwasm_1_4")]
pub fn query_delegation_rewards(
&self,
delegator: impl Into<String>,
validator: impl Into<String>,
) -> StdResult<Vec<crate::DecCoin>> {
use crate::DelegationRewardsResponse;

let request = DistributionQuery::DelegationRewards {
delegator_address: delegator.into(),
validator_address: validator.into(),
}
.into();
let DelegationRewardsResponse { rewards } = self.query(&request)?;

Ok(rewards)
}

// this queries another wasm contract. You should know a priori the proper types for T and U
// (response and request) based on the contract API
pub fn query_wasm_smart<T: DeserializeOwned>(
Expand Down

0 comments on commit 981cb46

Please sign in to comment.