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