cosmwasm_std/query/
staking.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4use crate::{Addr, Coin, Decimal};
5
6use super::query_response::QueryResponseType;
7
8#[non_exhaustive]
9#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
10#[serde(rename_all = "snake_case")]
11pub enum StakingQuery {
12    /// Returns the denomination that can be bonded (if there are multiple native tokens on the chain)
13    BondedDenom {},
14    /// AllDelegations will return all delegations by the delegator
15    AllDelegations { delegator: String },
16    /// Delegation will return more detailed info on a particular
17    /// delegation, defined by delegator/validator pair
18    Delegation {
19        delegator: String,
20        validator: String,
21    },
22    /// Returns all validators in the currently active validator set.
23    ///
24    /// The query response type is `AllValidatorsResponse`.
25    AllValidators {},
26    /// Returns the validator at the given address. Returns None if the validator is
27    /// not part of the currently active validator set.
28    ///
29    /// The query response type is `ValidatorResponse`.
30    Validator {
31        /// The validator's address (e.g. (e.g. cosmosvaloper1...))
32        address: String,
33    },
34}
35
36/// BondedDenomResponse is data format returned from StakingRequest::BondedDenom query
37#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
38#[serde(rename_all = "snake_case")]
39pub struct BondedDenomResponse {
40    pub denom: String,
41}
42
43impl QueryResponseType for BondedDenomResponse {}
44
45impl_response_constructor!(BondedDenomResponse, denom: String);
46
47/// DelegationsResponse is data format returned from StakingRequest::AllDelegations query
48#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
49#[serde(rename_all = "snake_case")]
50pub struct AllDelegationsResponse {
51    pub delegations: Vec<Delegation>,
52}
53
54impl QueryResponseType for AllDelegationsResponse {}
55
56impl_response_constructor!(AllDelegationsResponse, delegations: Vec<Delegation>);
57
58/// Delegation is basic (cheap to query) data about a delegation.
59///
60/// Instances are created in the querier.
61#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
62pub struct Delegation {
63    pub delegator: Addr,
64    /// A validator address (e.g. cosmosvaloper1...)
65    pub validator: String,
66    /// How much we have locked in the delegation
67    pub amount: Coin,
68}
69
70impl From<FullDelegation> for Delegation {
71    fn from(full: FullDelegation) -> Self {
72        Delegation {
73            delegator: full.delegator,
74            validator: full.validator,
75            amount: full.amount,
76        }
77    }
78}
79
80/// DelegationResponse is data format returned from StakingRequest::Delegation query
81#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
82#[serde(rename_all = "snake_case")]
83pub struct DelegationResponse {
84    pub delegation: Option<FullDelegation>,
85}
86
87impl QueryResponseType for DelegationResponse {}
88
89impl_response_constructor!(DelegationResponse, delegation: Option<FullDelegation>);
90
91/// FullDelegation is all the info on the delegation, some (like accumulated_reward and can_redelegate)
92/// is expensive to query.
93///
94/// Instances are created in the querier.
95#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
96pub struct FullDelegation {
97    pub delegator: Addr,
98    /// A validator address (e.g. cosmosvaloper1...)
99    pub validator: String,
100    /// How much we have locked in the delegation
101    pub amount: Coin,
102    /// can_redelegate captures how much can be immediately redelegated.
103    /// 0 is no redelegation and can_redelegate == amount is redelegate all
104    /// but there are many places between the two
105    pub can_redelegate: Coin,
106    /// How much we can currently withdraw
107    pub accumulated_rewards: Vec<Coin>,
108}
109
110/// The data format returned from StakingRequest::AllValidators query
111#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
112pub struct AllValidatorsResponse {
113    pub validators: Vec<Validator>,
114}
115
116impl QueryResponseType for AllValidatorsResponse {}
117
118impl_response_constructor!(AllValidatorsResponse, validators: Vec<Validator>);
119
120/// The data format returned from StakingRequest::Validator query
121#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
122pub struct ValidatorResponse {
123    pub validator: Option<Validator>,
124}
125
126impl QueryResponseType for ValidatorResponse {}
127
128impl_response_constructor!(ValidatorResponse, validator: Option<Validator>);
129
130/// Instances are created in the querier.
131#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
132pub struct Validator {
133    /// The operator address of the validator (e.g. cosmosvaloper1...).
134    /// See https://github.com/cosmos/cosmos-sdk/blob/v0.47.4/proto/cosmos/staking/v1beta1/staking.proto#L95-L96
135    /// for more information.
136    ///
137    /// This uses `String` instead of `Addr` since the bech32 address prefix is different from
138    /// the ones that regular user accounts use.
139    pub address: String,
140    pub commission: Decimal,
141    pub max_commission: Decimal,
142    /// The maximum daily increase of the commission
143    pub max_change_rate: Decimal,
144}