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