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