|
| 1 | +//! Distribution module support |
| 2 | +//! |
| 3 | +//! <https://docs.cosmos.network/master/modules/distribution/> |
| 4 | +
|
| 5 | +use crate::{ |
| 6 | + proto, |
| 7 | + tx::{Msg, MsgType}, |
| 8 | + AccountId, Coin, Result, |
| 9 | +}; |
| 10 | +use std::convert::{TryFrom, TryInto}; |
| 11 | + |
| 12 | +/// MsgSetWithdrawAddress represents a message to set a withdraw address for staking rewards. |
| 13 | +#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] |
| 14 | +pub struct MsgSetWithdrawAddress { |
| 15 | + /// Delegator's address. |
| 16 | + pub delegator_address: AccountId, |
| 17 | + |
| 18 | + /// withdraw address. |
| 19 | + pub withdraw_address: AccountId, |
| 20 | +} |
| 21 | + |
| 22 | +impl MsgType for MsgSetWithdrawAddress { |
| 23 | + fn from_msg(msg: &Msg) -> Result<Self> { |
| 24 | + proto::cosmos::distribution::v1beta1::MsgSetWithdrawAddress::from_msg(msg) |
| 25 | + .and_then(TryInto::try_into) |
| 26 | + } |
| 27 | + |
| 28 | + fn to_msg(&self) -> Result<Msg> { |
| 29 | + proto::cosmos::distribution::v1beta1::MsgSetWithdrawAddress::from(self).to_msg() |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +impl TryFrom<proto::cosmos::distribution::v1beta1::MsgSetWithdrawAddress> |
| 34 | + for MsgSetWithdrawAddress |
| 35 | +{ |
| 36 | + type Error = eyre::Report; |
| 37 | + |
| 38 | + fn try_from( |
| 39 | + proto: proto::cosmos::distribution::v1beta1::MsgSetWithdrawAddress, |
| 40 | + ) -> Result<MsgSetWithdrawAddress> { |
| 41 | + MsgSetWithdrawAddress::try_from(&proto) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl TryFrom<&proto::cosmos::distribution::v1beta1::MsgSetWithdrawAddress> |
| 46 | + for MsgSetWithdrawAddress |
| 47 | +{ |
| 48 | + type Error = eyre::Report; |
| 49 | + |
| 50 | + fn try_from( |
| 51 | + proto: &proto::cosmos::distribution::v1beta1::MsgSetWithdrawAddress, |
| 52 | + ) -> Result<MsgSetWithdrawAddress> { |
| 53 | + Ok(MsgSetWithdrawAddress { |
| 54 | + delegator_address: proto.delegator_address.parse()?, |
| 55 | + withdraw_address: proto.withdraw_address.parse()?, |
| 56 | + }) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl From<MsgSetWithdrawAddress> for proto::cosmos::distribution::v1beta1::MsgSetWithdrawAddress { |
| 61 | + fn from( |
| 62 | + coin: MsgSetWithdrawAddress, |
| 63 | + ) -> proto::cosmos::distribution::v1beta1::MsgSetWithdrawAddress { |
| 64 | + proto::cosmos::distribution::v1beta1::MsgSetWithdrawAddress::from(&coin) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +impl From<&MsgSetWithdrawAddress> for proto::cosmos::distribution::v1beta1::MsgSetWithdrawAddress { |
| 69 | + fn from( |
| 70 | + msg: &MsgSetWithdrawAddress, |
| 71 | + ) -> proto::cosmos::distribution::v1beta1::MsgSetWithdrawAddress { |
| 72 | + proto::cosmos::distribution::v1beta1::MsgSetWithdrawAddress { |
| 73 | + delegator_address: msg.delegator_address.to_string(), |
| 74 | + withdraw_address: msg.withdraw_address.to_string(), |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/// MsgWithdrawDelegatorReward represents a message to withdraw a delegator's reward from a validator. |
| 80 | +#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] |
| 81 | +pub struct MsgWithdrawDelegatorReward { |
| 82 | + /// Delegator's address. |
| 83 | + pub delegator_address: AccountId, |
| 84 | + |
| 85 | + /// Validator's address. |
| 86 | + pub validator_address: AccountId, |
| 87 | +} |
| 88 | + |
| 89 | +impl MsgType for MsgWithdrawDelegatorReward { |
| 90 | + fn from_msg(msg: &Msg) -> Result<Self> { |
| 91 | + proto::cosmos::distribution::v1beta1::MsgWithdrawDelegatorReward::from_msg(msg) |
| 92 | + .and_then(TryInto::try_into) |
| 93 | + } |
| 94 | + |
| 95 | + fn to_msg(&self) -> Result<Msg> { |
| 96 | + proto::cosmos::distribution::v1beta1::MsgWithdrawDelegatorReward::from(self).to_msg() |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl TryFrom<proto::cosmos::distribution::v1beta1::MsgWithdrawDelegatorReward> |
| 101 | + for MsgWithdrawDelegatorReward |
| 102 | +{ |
| 103 | + type Error = eyre::Report; |
| 104 | + |
| 105 | + fn try_from( |
| 106 | + proto: proto::cosmos::distribution::v1beta1::MsgWithdrawDelegatorReward, |
| 107 | + ) -> Result<MsgWithdrawDelegatorReward> { |
| 108 | + MsgWithdrawDelegatorReward::try_from(&proto) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +impl TryFrom<&proto::cosmos::distribution::v1beta1::MsgWithdrawDelegatorReward> |
| 113 | + for MsgWithdrawDelegatorReward |
| 114 | +{ |
| 115 | + type Error = eyre::Report; |
| 116 | + |
| 117 | + fn try_from( |
| 118 | + proto: &proto::cosmos::distribution::v1beta1::MsgWithdrawDelegatorReward, |
| 119 | + ) -> Result<MsgWithdrawDelegatorReward> { |
| 120 | + Ok(MsgWithdrawDelegatorReward { |
| 121 | + delegator_address: proto.delegator_address.parse()?, |
| 122 | + validator_address: proto.validator_address.parse()?, |
| 123 | + }) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +impl From<MsgWithdrawDelegatorReward> |
| 128 | + for proto::cosmos::distribution::v1beta1::MsgWithdrawDelegatorReward |
| 129 | +{ |
| 130 | + fn from( |
| 131 | + coin: MsgWithdrawDelegatorReward, |
| 132 | + ) -> proto::cosmos::distribution::v1beta1::MsgWithdrawDelegatorReward { |
| 133 | + proto::cosmos::distribution::v1beta1::MsgWithdrawDelegatorReward::from(&coin) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +impl From<&MsgWithdrawDelegatorReward> |
| 138 | + for proto::cosmos::distribution::v1beta1::MsgWithdrawDelegatorReward |
| 139 | +{ |
| 140 | + fn from( |
| 141 | + msg: &MsgWithdrawDelegatorReward, |
| 142 | + ) -> proto::cosmos::distribution::v1beta1::MsgWithdrawDelegatorReward { |
| 143 | + proto::cosmos::distribution::v1beta1::MsgWithdrawDelegatorReward { |
| 144 | + delegator_address: msg.delegator_address.to_string(), |
| 145 | + validator_address: msg.validator_address.to_string(), |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +/// WithdrawValidatorCommission represents a message to withdraw a validator's staking commission. |
| 151 | +#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] |
| 152 | +pub struct MsgWithdrawValidatorCommission { |
| 153 | + /// Validator's address. |
| 154 | + pub validator_address: AccountId, |
| 155 | +} |
| 156 | + |
| 157 | +impl MsgType for MsgWithdrawValidatorCommission { |
| 158 | + fn from_msg(msg: &Msg) -> Result<Self> { |
| 159 | + proto::cosmos::distribution::v1beta1::MsgWithdrawValidatorCommission::from_msg(msg) |
| 160 | + .and_then(TryInto::try_into) |
| 161 | + } |
| 162 | + |
| 163 | + fn to_msg(&self) -> Result<Msg> { |
| 164 | + proto::cosmos::distribution::v1beta1::MsgWithdrawValidatorCommission::from(self).to_msg() |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +impl TryFrom<proto::cosmos::distribution::v1beta1::MsgWithdrawValidatorCommission> |
| 169 | + for MsgWithdrawValidatorCommission |
| 170 | +{ |
| 171 | + type Error = eyre::Report; |
| 172 | + |
| 173 | + fn try_from( |
| 174 | + proto: proto::cosmos::distribution::v1beta1::MsgWithdrawValidatorCommission, |
| 175 | + ) -> Result<MsgWithdrawValidatorCommission> { |
| 176 | + MsgWithdrawValidatorCommission::try_from(&proto) |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +impl TryFrom<&proto::cosmos::distribution::v1beta1::MsgWithdrawValidatorCommission> |
| 181 | + for MsgWithdrawValidatorCommission |
| 182 | +{ |
| 183 | + type Error = eyre::Report; |
| 184 | + |
| 185 | + fn try_from( |
| 186 | + proto: &proto::cosmos::distribution::v1beta1::MsgWithdrawValidatorCommission, |
| 187 | + ) -> Result<MsgWithdrawValidatorCommission> { |
| 188 | + Ok(MsgWithdrawValidatorCommission { |
| 189 | + validator_address: proto.validator_address.parse()?, |
| 190 | + }) |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +impl From<MsgWithdrawValidatorCommission> |
| 195 | + for proto::cosmos::distribution::v1beta1::MsgWithdrawValidatorCommission |
| 196 | +{ |
| 197 | + fn from( |
| 198 | + coin: MsgWithdrawValidatorCommission, |
| 199 | + ) -> proto::cosmos::distribution::v1beta1::MsgWithdrawValidatorCommission { |
| 200 | + proto::cosmos::distribution::v1beta1::MsgWithdrawValidatorCommission::from(&coin) |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +impl From<&MsgWithdrawValidatorCommission> |
| 205 | + for proto::cosmos::distribution::v1beta1::MsgWithdrawValidatorCommission |
| 206 | +{ |
| 207 | + fn from( |
| 208 | + msg: &MsgWithdrawValidatorCommission, |
| 209 | + ) -> proto::cosmos::distribution::v1beta1::MsgWithdrawValidatorCommission { |
| 210 | + proto::cosmos::distribution::v1beta1::MsgWithdrawValidatorCommission { |
| 211 | + validator_address: msg.validator_address.to_string(), |
| 212 | + } |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +/// MsgFundCommunityPool represents a message to send coins from depositor to the community pool. |
| 217 | +#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] |
| 218 | +pub struct MsgFundCommunityPool { |
| 219 | + /// Depositor's address. |
| 220 | + pub depositor: AccountId, |
| 221 | + |
| 222 | + /// Amount to deposit. |
| 223 | + pub amount: Vec<Coin>, |
| 224 | +} |
| 225 | + |
| 226 | +impl MsgType for MsgFundCommunityPool { |
| 227 | + fn from_msg(msg: &Msg) -> Result<Self> { |
| 228 | + proto::cosmos::distribution::v1beta1::MsgFundCommunityPool::from_msg(msg) |
| 229 | + .and_then(TryInto::try_into) |
| 230 | + } |
| 231 | + |
| 232 | + fn to_msg(&self) -> Result<Msg> { |
| 233 | + proto::cosmos::distribution::v1beta1::MsgFundCommunityPool::from(self).to_msg() |
| 234 | + } |
| 235 | +} |
| 236 | + |
| 237 | +impl TryFrom<proto::cosmos::distribution::v1beta1::MsgFundCommunityPool> for MsgFundCommunityPool { |
| 238 | + type Error = eyre::Report; |
| 239 | + |
| 240 | + fn try_from( |
| 241 | + proto: proto::cosmos::distribution::v1beta1::MsgFundCommunityPool, |
| 242 | + ) -> Result<MsgFundCommunityPool> { |
| 243 | + MsgFundCommunityPool::try_from(&proto) |
| 244 | + } |
| 245 | +} |
| 246 | + |
| 247 | +impl TryFrom<&proto::cosmos::distribution::v1beta1::MsgFundCommunityPool> for MsgFundCommunityPool { |
| 248 | + type Error = eyre::Report; |
| 249 | + |
| 250 | + fn try_from( |
| 251 | + proto: &proto::cosmos::distribution::v1beta1::MsgFundCommunityPool, |
| 252 | + ) -> Result<MsgFundCommunityPool> { |
| 253 | + let mut amounts = vec![]; |
| 254 | + for amount in proto.amount.iter() { |
| 255 | + amounts.push(Coin { |
| 256 | + denom: amount.denom.parse()?, |
| 257 | + amount: amount.amount.parse()?, |
| 258 | + }) |
| 259 | + } |
| 260 | + Ok(MsgFundCommunityPool { |
| 261 | + depositor: proto.depositor.parse()?, |
| 262 | + amount: amounts, |
| 263 | + }) |
| 264 | + } |
| 265 | +} |
| 266 | + |
| 267 | +impl From<MsgFundCommunityPool> for proto::cosmos::distribution::v1beta1::MsgFundCommunityPool { |
| 268 | + fn from( |
| 269 | + coin: MsgFundCommunityPool, |
| 270 | + ) -> proto::cosmos::distribution::v1beta1::MsgFundCommunityPool { |
| 271 | + proto::cosmos::distribution::v1beta1::MsgFundCommunityPool::from(&coin) |
| 272 | + } |
| 273 | +} |
| 274 | + |
| 275 | +impl From<&MsgFundCommunityPool> for proto::cosmos::distribution::v1beta1::MsgFundCommunityPool { |
| 276 | + fn from( |
| 277 | + msg: &MsgFundCommunityPool, |
| 278 | + ) -> proto::cosmos::distribution::v1beta1::MsgFundCommunityPool { |
| 279 | + let mut amounts = vec![]; |
| 280 | + for amount in msg.amount.iter() { |
| 281 | + amounts.push(proto::cosmos::base::v1beta1::Coin { |
| 282 | + denom: amount.denom.to_string(), |
| 283 | + amount: amount.amount.to_string(), |
| 284 | + }) |
| 285 | + } |
| 286 | + proto::cosmos::distribution::v1beta1::MsgFundCommunityPool { |
| 287 | + depositor: "".to_string(), |
| 288 | + amount: amounts, |
| 289 | + } |
| 290 | + } |
| 291 | +} |
0 commit comments