terra_rust_api/messages/
staking.rs

1use crate::client::client_types::{terra_decimal_format, terra_opt_decimal_format};
2use crate::core_types::{Coin, MsgInternal};
3
4use crate::messages::Message;
5use rust_decimal::Decimal;
6use serde::{Deserialize, Serialize};
7
8#[derive(Deserialize, Serialize, Debug)]
9/// Description of the validator which appears in public
10/// on MsgEditValidator messages for fields that don't change you should put "[do-not-modify]"
11///
12pub struct ValidatorDescription {
13    pub details: String,
14    pub identity: String,
15    pub moniker: String,
16    pub security_contact: String,
17    pub website: String,
18}
19impl ValidatorDescription {
20    pub fn create_create(
21        details: Option<String>,
22        identity: Option<String>,
23        moniker: String,
24        security_contact: Option<String>,
25        website: Option<String>,
26    ) -> ValidatorDescription {
27        ValidatorDescription {
28            details: details.unwrap_or_else(|| "".into()),
29            identity: identity.unwrap_or_else(|| "".into()),
30            moniker,
31            security_contact: security_contact.unwrap_or_else(|| "".into()),
32            website: website.unwrap_or_else(|| "".into()),
33        }
34    }
35    pub fn create_edit(
36        details: Option<String>,
37        identity: Option<String>,
38        moniker: Option<String>,
39        security_contact: Option<String>,
40        website: Option<String>,
41    ) -> ValidatorDescription {
42        ValidatorDescription {
43            details: details.unwrap_or_else(|| "[do-not-modify]".into()),
44            identity: identity.unwrap_or_else(|| "[do-not-modify]".into()),
45            moniker: moniker.unwrap_or_else(|| "[do-not-modify]".into()),
46            security_contact: security_contact.unwrap_or_else(|| "[do-not-modify]".into()),
47            website: website.unwrap_or_else(|| "[do-not-modify]".into()),
48        }
49    }
50}
51/// Description of the validator commission structure
52#[derive(Deserialize, Serialize, Debug)]
53pub struct ValidatorCommission {
54    #[serde(with = "terra_decimal_format")]
55    pub max_change_rate: Decimal,
56    #[serde(with = "terra_decimal_format")]
57    pub max_rate: Decimal,
58    #[serde(with = "terra_decimal_format")]
59    pub rate: Decimal,
60}
61/// create validator message
62#[derive(Serialize, Debug)]
63pub struct MsgCreateValidator {
64    pub commission: ValidatorCommission,
65    pub delegator_address: String,
66    pub description: ValidatorDescription,
67    #[serde(with = "terra_decimal_format")]
68    pub min_self_delegation: Decimal,
69    pub pubkey: String,
70    pub value: Coin,
71    pub validator_address: String,
72}
73impl MsgInternal for MsgCreateValidator {}
74impl MsgCreateValidator {
75    pub fn create(
76        description: ValidatorDescription,
77        commission: ValidatorCommission,
78        min_self_delegation: Decimal,
79        delegator_address: String,
80        validator_address: String,
81        pubkey: String,
82        value: Coin,
83    ) -> Message {
84        let internal = MsgCreateValidator {
85            commission,
86            delegator_address,
87            description,
88            min_self_delegation,
89            pubkey,
90            value,
91            validator_address,
92        };
93        Message {
94            s_type: "staking/MsgCreateValidator".into(),
95            value: serde_json::to_value(internal).unwrap(),
96        }
97    }
98}
99/// edit validator message
100#[derive(Serialize, Debug)]
101pub struct MsgEditValidator {
102    pub address: String,
103    #[serde(with = "terra_opt_decimal_format")]
104    pub commission_rate: Option<Decimal>,
105    pub description: ValidatorDescription,
106    #[serde(with = "terra_opt_decimal_format")]
107    pub min_self_delegation: Option<Decimal>,
108}
109impl MsgInternal for MsgEditValidator {}
110impl MsgEditValidator {
111    pub fn create(
112        description: ValidatorDescription,
113        address: String,
114        commission_rate: Option<Decimal>,
115        min_self_delegation: Option<Decimal>,
116    ) -> anyhow::Result<Message> {
117        let internal = MsgEditValidator {
118            address,
119            commission_rate,
120            description,
121            min_self_delegation,
122        };
123        Ok(Message {
124            s_type: "staking/MsgEditValidator".into(),
125            value: serde_json::to_value(internal)?,
126        })
127    }
128}
129
130/// edit undelegate message
131#[derive(Serialize, Debug)]
132pub struct MsgUndelegate {
133    pub amount: Coin,
134    pub delegator_address: String,
135    pub validator_address: String,
136}
137impl MsgInternal for MsgUndelegate {}
138impl MsgUndelegate {
139    pub fn create(
140        delegator_address: String,
141        validator_address: String,
142        amount: Coin,
143    ) -> anyhow::Result<Message> {
144        let internal = MsgUndelegate {
145            amount,
146            delegator_address,
147            validator_address,
148        };
149        Ok(Message {
150            s_type: "staking/MsgUndelegate".into(),
151            value: serde_json::to_value(internal)?,
152        })
153    }
154}
155/// edit undelegate message
156#[derive(Serialize, Debug)]
157pub struct MsgDelegate {
158    pub amount: Coin,
159    pub delegator_address: String,
160    pub validator_address: String,
161}
162impl MsgInternal for MsgDelegate {}
163impl MsgDelegate {
164    pub fn create(
165        delegator_address: String,
166        validator_address: String,
167        amount: Coin,
168    ) -> anyhow::Result<Message> {
169        let internal = MsgDelegate {
170            amount,
171            delegator_address,
172            validator_address,
173        };
174        Ok(Message {
175            s_type: "staking/MsgDelegate".into(),
176            value: serde_json::to_value(internal)?,
177        })
178    }
179}
180/// edit undelegate message
181#[derive(Serialize, Debug)]
182pub struct MsgBeginRedelegate {
183    pub amount: Coin,
184    pub delegator_address: String,
185    pub validator_dst_address: String,
186    pub validator_src_address: String,
187}
188impl MsgInternal for MsgBeginRedelegate {}
189impl MsgBeginRedelegate {
190    pub fn create(
191        delegator_address: String,
192        validator_dst_address: String,
193        validator_src_address: String,
194        amount: Coin,
195    ) -> anyhow::Result<Message> {
196        let internal = MsgBeginRedelegate {
197            amount,
198            delegator_address,
199            validator_src_address,
200            validator_dst_address,
201        };
202        Ok(Message {
203            s_type: "staking/MsgBeginRedelegate".into(),
204            value: serde_json::to_value(internal)?,
205        })
206    }
207}