cosmrs/feegrant/
periodic_allowance.rs

1use super::BasicAllowance;
2use crate::{proto, tx::Msg, Coin, ErrorReport, Result};
3use std::time::{Duration, SystemTime};
4
5/// PeriodicAllowance extends Allowance to allow for both a maximum cap,
6/// as well as a limit per time period.
7#[derive(Clone, Debug, Eq, PartialEq)]
8pub struct PeriodicAllowance {
9    /// basic specifies a struct of `BasicAllowance`
10    pub basic: Option<BasicAllowance>,
11
12    /// period specifies the time duration in which period_spend_limit coins can
13    /// be spent before that allowance is reset
14    pub period: Option<Duration>,
15
16    /// period_spend_limit specifies the maximum number of coins that can be spent
17    /// in the period
18    pub period_spend_limit: Vec<Coin>,
19
20    /// period_can_spend is the number of coins left to be spent before the period_reset time
21    pub period_can_spend: Vec<Coin>,
22
23    /// period_reset is the time at which this period resets and a new one begins,
24    /// it is calculated from the start time of the first transaction after the
25    /// last period ended
26    pub period_reset: Option<SystemTime>,
27}
28
29impl Msg for PeriodicAllowance {
30    type Proto = proto::cosmos::feegrant::v1beta1::PeriodicAllowance;
31}
32
33impl TryFrom<proto::cosmos::feegrant::v1beta1::PeriodicAllowance> for PeriodicAllowance {
34    type Error = ErrorReport;
35
36    fn try_from(
37        proto: proto::cosmos::feegrant::v1beta1::PeriodicAllowance,
38    ) -> Result<PeriodicAllowance> {
39        Ok(PeriodicAllowance {
40            basic: proto.basic.map(TryFrom::try_from).transpose()?,
41            period: proto.period.map(TryFrom::try_from).transpose()?,
42            period_spend_limit: proto
43                .period_spend_limit
44                .iter()
45                .map(TryFrom::try_from)
46                .collect::<Result<_, _>>()?,
47            period_can_spend: proto
48                .period_can_spend
49                .iter()
50                .map(TryFrom::try_from)
51                .collect::<Result<_, _>>()?,
52            period_reset: proto.period_reset.map(TryFrom::try_from).transpose()?,
53        })
54    }
55}
56
57impl From<PeriodicAllowance> for proto::cosmos::feegrant::v1beta1::PeriodicAllowance {
58    fn from(allowance: PeriodicAllowance) -> proto::cosmos::feegrant::v1beta1::PeriodicAllowance {
59        proto::cosmos::feegrant::v1beta1::PeriodicAllowance {
60            basic: allowance.basic.map(Into::into),
61            period: allowance
62                .period
63                .map(TryInto::try_into)
64                .transpose()
65                .expect("invalid allowance period"), // TODO(tarcieri): fallible serialization?
66            period_spend_limit: allowance
67                .period_spend_limit
68                .iter()
69                .map(Into::into)
70                .collect(),
71            period_can_spend: allowance.period_can_spend.iter().map(Into::into).collect(),
72            period_reset: allowance.period_reset.map(Into::into),
73        }
74    }
75}