cosmwasm_std/query/
distribution.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4use crate::prelude::*;
5use crate::{Addr, Decimal256};
6
7use super::query_response::QueryResponseType;
8use crate::utils::impl_hidden_constructor;
9
10#[non_exhaustive]
11#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
12#[serde(rename_all = "snake_case")]
13pub enum DistributionQuery {
14    /// See <https://github.com/cosmos/cosmos-sdk/blob/c74e2887b0b73e81d48c2f33e6b1020090089ee0/proto/cosmos/distribution/v1beta1/query.proto#L222-L230>
15    DelegatorWithdrawAddress { delegator_address: String },
16    /// See <https://github.com/cosmos/cosmos-sdk/blob/c74e2887b0b73e81d48c2f33e6b1020090089ee0/proto/cosmos/distribution/v1beta1/query.proto#L157-L167>
17    #[cfg(feature = "cosmwasm_1_4")]
18    DelegationRewards {
19        delegator_address: String,
20        validator_address: String,
21    },
22    /// See <https://github.com/cosmos/cosmos-sdk/blob/c74e2887b0b73e81d48c2f33e6b1020090089ee0/proto/cosmos/distribution/v1beta1/query.proto#L180-L187>
23    #[cfg(feature = "cosmwasm_1_4")]
24    DelegationTotalRewards { delegator_address: String },
25    /// See <https://github.com/cosmos/cosmos-sdk/blob/b0acf60e6c39f7ab023841841fc0b751a12c13ff/proto/cosmos/distribution/v1beta1/query.proto#L202-L210>
26    #[cfg(feature = "cosmwasm_1_4")]
27    DelegatorValidators { delegator_address: String },
28}
29
30/// See <https://github.com/cosmos/cosmos-sdk/blob/c74e2887b0b73e81d48c2f33e6b1020090089ee0/proto/cosmos/distribution/v1beta1/query.proto#L232-L240>
31#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
32#[serde(rename_all = "snake_case")]
33#[non_exhaustive]
34pub struct DelegatorWithdrawAddressResponse {
35    pub withdraw_address: Addr,
36}
37
38impl_hidden_constructor!(DelegatorWithdrawAddressResponse, withdraw_address: Addr);
39impl QueryResponseType for DelegatorWithdrawAddressResponse {}
40
41/// See <https://github.com/cosmos/cosmos-sdk/blob/c74e2887b0b73e81d48c2f33e6b1020090089ee0/proto/cosmos/distribution/v1beta1/query.proto#L169-L178>
42#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
43#[serde(rename_all = "snake_case")]
44#[non_exhaustive]
45pub struct DelegationRewardsResponse {
46    pub rewards: Vec<DecCoin>,
47}
48
49impl_hidden_constructor!(DelegationRewardsResponse, rewards: Vec<DecCoin>);
50impl QueryResponseType for DelegationRewardsResponse {}
51
52/// A coin type with decimal amount.
53/// Modeled after the Cosmos SDK's [DecCoin] type.
54/// However, in contrast to the Cosmos SDK the `amount` string MUST always have a dot at JSON level,
55/// see <https://github.com/cosmos/cosmos-sdk/issues/10863>.
56/// Also if Cosmos SDK chooses to migrate away from fixed point decimals
57/// (as shown [here](https://github.com/cosmos/cosmos-sdk/blob/v0.47.4/x/group/internal/math/dec.go#L13-L21 and discussed [here](https://github.com/cosmos/cosmos-sdk/issues/11783)),
58/// wasmd needs to truncate the decimal places to 18.
59///
60/// [DecCoin]: (https://github.com/cosmos/cosmos-sdk/blob/v0.47.4/proto/cosmos/base/v1beta1/coin.proto#L28-L38)
61#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
62#[serde(rename_all = "snake_case")]
63pub struct DecCoin {
64    pub denom: String,
65    /// An amount in the base denom of the distributed token.
66    ///
67    /// Some chains have chosen atto (10^-18) for their token's base denomination. If we used `Decimal` here, we could only store
68    /// 340282366920938463463.374607431768211455atoken which is 340.28 TOKEN.
69    pub amount: Decimal256,
70}
71
72impl DecCoin {
73    pub fn new(amount: impl Into<Decimal256>, denom: impl Into<String>) -> Self {
74        Self {
75            denom: denom.into(),
76            amount: amount.into(),
77        }
78    }
79}
80
81/// See <https://github.com/cosmos/cosmos-sdk/blob/c74e2887b0b73e81d48c2f33e6b1020090089ee0/proto/cosmos/distribution/v1beta1/query.proto#L189-L200>
82#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
83#[non_exhaustive]
84pub struct DelegationTotalRewardsResponse {
85    pub rewards: Vec<DelegatorReward>,
86    pub total: Vec<DecCoin>,
87}
88
89impl_hidden_constructor!(
90    DelegationTotalRewardsResponse,
91    rewards: Vec<DelegatorReward>,
92    total: Vec<DecCoin>
93);
94impl QueryResponseType for DelegationTotalRewardsResponse {}
95
96#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
97#[non_exhaustive]
98pub struct DelegatorReward {
99    pub validator_address: String,
100    pub reward: Vec<DecCoin>,
101}
102impl_hidden_constructor!(
103    DelegatorReward,
104    validator_address: String,
105    reward: Vec<DecCoin>
106);
107
108/// See <https://github.com/cosmos/cosmos-sdk/blob/b0acf60e6c39f7ab023841841fc0b751a12c13ff/proto/cosmos/distribution/v1beta1/query.proto#L212-L220>
109#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
110#[non_exhaustive]
111pub struct DelegatorValidatorsResponse {
112    pub validators: Vec<String>,
113}
114
115impl_hidden_constructor!(DelegatorValidatorsResponse, validators: Vec<String>);
116impl QueryResponseType for DelegatorValidatorsResponse {}