cosmwasm_std/query/
staking.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4use crate::prelude::*;
5use crate::{Addr, Coin, Decimal};
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 StakingQuery {
13    /// Returns the denomination that can be bonded (if there are multiple native tokens on the chain)
14    BondedDenom {},
15    /// AllDelegations will return all delegations by the delegator
16    AllDelegations { delegator: String },
17    /// Delegation will return more detailed info on a particular
18    /// delegation, defined by delegator/validator pair
19    Delegation {
20        delegator: String,
21        validator: String,
22    },
23    /// Returns all validators in the currently active validator set.
24    ///
25    /// The query response type is `AllValidatorsResponse`.
26    AllValidators {},
27    /// Returns the validator at the given address. Returns None if the validator is
28    /// not part of the currently active validator set.
29    ///
30    /// The query response type is `ValidatorResponse`.
31    Validator {
32        /// The validator's address (e.g. (e.g. cosmosvaloper1...))
33        address: String,
34    },
35}
36
37/// BondedDenomResponse is data format returned from StakingRequest::BondedDenom query
38#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
39#[serde(rename_all = "snake_case")]
40#[non_exhaustive]
41pub struct BondedDenomResponse {
42    pub denom: String,
43}
44
45impl QueryResponseType for BondedDenomResponse {}
46
47impl_response_constructor!(BondedDenomResponse, denom: String);
48
49/// DelegationsResponse is data format returned from StakingRequest::AllDelegations query
50#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
51#[serde(rename_all = "snake_case")]
52#[non_exhaustive]
53pub struct AllDelegationsResponse {
54    pub delegations: Vec<Delegation>,
55}
56
57impl QueryResponseType for AllDelegationsResponse {}
58
59impl_response_constructor!(AllDelegationsResponse, delegations: Vec<Delegation>);
60
61/// Delegation is basic (cheap to query) data about a delegation.
62///
63/// Instances are created in the querier.
64#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
65#[non_exhaustive]
66pub struct Delegation {
67    pub delegator: Addr,
68    /// A validator address (e.g. cosmosvaloper1...)
69    pub validator: String,
70    /// How much we have locked in the delegation
71    pub amount: Coin,
72}
73
74impl_response_constructor!(Delegation, delegator: Addr, validator: String, amount: Coin);
75
76impl From<FullDelegation> for Delegation {
77    fn from(full: FullDelegation) -> Self {
78        Delegation {
79            delegator: full.delegator,
80            validator: full.validator,
81            amount: full.amount,
82        }
83    }
84}
85
86/// DelegationResponse is data format returned from StakingRequest::Delegation query
87#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
88#[serde(rename_all = "snake_case")]
89#[non_exhaustive]
90pub struct DelegationResponse {
91    pub delegation: Option<FullDelegation>,
92}
93
94impl QueryResponseType for DelegationResponse {}
95
96impl_response_constructor!(DelegationResponse, delegation: Option<FullDelegation>);
97
98/// FullDelegation is all the info on the delegation, some (like accumulated_reward and can_redelegate)
99/// is expensive to query.
100///
101/// Instances are created in the querier.
102#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
103#[non_exhaustive]
104pub struct FullDelegation {
105    pub delegator: Addr,
106    /// A validator address (e.g. cosmosvaloper1...)
107    pub validator: String,
108    /// How much we have locked in the delegation
109    pub amount: Coin,
110    /// can_redelegate captures how much can be immediately redelegated.
111    /// 0 is no redelegation and can_redelegate == amount is redelegate all
112    /// but there are many places between the two
113    pub can_redelegate: Coin,
114    /// How much we can currently withdraw
115    pub accumulated_rewards: Vec<Coin>,
116}
117
118impl_response_constructor!(
119    FullDelegation,
120    delegator: Addr,
121    validator: String,
122    amount: Coin,
123    can_redelegate: Coin,
124    accumulated_rewards: Vec<Coin>
125);
126
127impl FullDelegation {
128    /// Creates a new delegation.
129    ///
130    /// If fields get added to the [`FullDelegation`] struct in the future, this constructor will
131    /// provide default values for them, but these default values may not be sensible.
132    pub fn create(
133        delegator: Addr,
134        validator: String,
135        amount: Coin,
136        can_redelegate: Coin,
137        accumulated_rewards: Vec<Coin>,
138    ) -> Self {
139        Self {
140            delegator,
141            validator,
142            amount,
143            can_redelegate,
144            accumulated_rewards,
145        }
146    }
147}
148
149/// The data format returned from StakingRequest::AllValidators query
150#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
151#[non_exhaustive]
152pub struct AllValidatorsResponse {
153    pub validators: Vec<Validator>,
154}
155
156impl QueryResponseType for AllValidatorsResponse {}
157
158impl_response_constructor!(AllValidatorsResponse, validators: Vec<Validator>);
159
160/// The data format returned from StakingRequest::Validator query
161#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
162#[non_exhaustive]
163pub struct ValidatorResponse {
164    pub validator: Option<Validator>,
165}
166
167impl QueryResponseType for ValidatorResponse {}
168
169impl_response_constructor!(ValidatorResponse, validator: Option<Validator>);
170
171/// Instances are created in the querier.
172#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
173#[non_exhaustive]
174pub struct Validator {
175    /// The operator address of the validator (e.g. cosmosvaloper1...).
176    /// See https://github.com/cosmos/cosmos-sdk/blob/v0.47.4/proto/cosmos/staking/v1beta1/staking.proto#L95-L96
177    /// for more information.
178    ///
179    /// This uses `String` instead of `Addr` since the bech32 address prefix is different from
180    /// the ones that regular user accounts use.
181    pub address: String,
182    pub commission: Decimal,
183    pub max_commission: Decimal,
184    /// The maximum daily increase of the commission
185    pub max_change_rate: Decimal,
186}
187
188impl_response_constructor!(
189    Validator,
190    address: String,
191    commission: Decimal,
192    max_commission: Decimal,
193    max_change_rate: Decimal
194);
195
196impl Validator {
197    /// Creates a new validator.
198    ///
199    /// If fields get added to the [`Validator`] struct in the future, this constructor will
200    /// provide default values for them, but these default values may not be sensible.
201    pub fn create(
202        address: String,
203        commission: Decimal,
204        max_commission: Decimal,
205        max_change_rate: Decimal,
206    ) -> Self {
207        Self {
208            address,
209            commission,
210            max_commission,
211            max_change_rate,
212        }
213    }
214}