cosmrs/staking/
msg_delegate.rs

1use crate::{proto, tx::Msg, AccountId, Coin, Error, ErrorReport, Result};
2
3/// MsgDelegate represents a message to delegate coins to a validator.
4#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
5pub struct MsgDelegate {
6    /// Delegator's address.
7    pub delegator_address: AccountId,
8
9    /// Validator's address.
10    pub validator_address: AccountId,
11
12    /// Amount to send
13    pub amount: Coin,
14}
15
16impl Msg for MsgDelegate {
17    type Proto = proto::cosmos::staking::v1beta1::MsgDelegate;
18}
19
20impl TryFrom<proto::cosmos::staking::v1beta1::MsgDelegate> for MsgDelegate {
21    type Error = ErrorReport;
22
23    fn try_from(proto: proto::cosmos::staking::v1beta1::MsgDelegate) -> Result<MsgDelegate> {
24        MsgDelegate::try_from(&proto)
25    }
26}
27
28impl TryFrom<&proto::cosmos::staking::v1beta1::MsgDelegate> for MsgDelegate {
29    type Error = ErrorReport;
30
31    fn try_from(proto: &proto::cosmos::staking::v1beta1::MsgDelegate) -> Result<MsgDelegate> {
32        let amount = proto
33            .amount
34            .as_ref()
35            .ok_or(Error::MissingField { name: "amount" })?;
36
37        Ok(MsgDelegate {
38            delegator_address: proto.delegator_address.parse()?,
39            validator_address: proto.validator_address.parse()?,
40            amount: Coin {
41                denom: amount.denom.parse()?,
42                amount: amount.amount.parse()?,
43            },
44        })
45    }
46}
47
48impl From<MsgDelegate> for proto::cosmos::staking::v1beta1::MsgDelegate {
49    fn from(coin: MsgDelegate) -> proto::cosmos::staking::v1beta1::MsgDelegate {
50        proto::cosmos::staking::v1beta1::MsgDelegate::from(&coin)
51    }
52}
53
54impl From<&MsgDelegate> for proto::cosmos::staking::v1beta1::MsgDelegate {
55    fn from(msg: &MsgDelegate) -> proto::cosmos::staking::v1beta1::MsgDelegate {
56        let amount = proto::cosmos::base::v1beta1::Coin {
57            denom: msg.amount.denom.to_string(),
58            amount: msg.amount.amount.to_string(),
59        };
60
61        proto::cosmos::staking::v1beta1::MsgDelegate {
62            delegator_address: msg.delegator_address.to_string(),
63            validator_address: msg.validator_address.to_string(),
64            amount: Some(amount),
65        }
66    }
67}