1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use cosmrs::proto::cosmos::base::abci::v1beta1::GasInfo as ProtoGasInfo;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::fmt;

use super::{coin::Coin, error::ChainError};
use crate::modules::auth::model::Address;

#[derive(Clone, Debug, Default, Serialize, Deserialize, Eq, PartialEq)]
pub struct Fee {
    pub amount: Vec<Coin>,

    pub gas_limit: Gas,

    pub payer: Option<Address>,

    pub granter: Option<Address>,
}

impl Fee {
    pub fn new(
        amount: Coin,
        gas_limit: impl Into<Gas>,
        payer: Option<Address>,
        granter: Option<Address>,
    ) -> Self {
        Self {
            amount: vec![amount],
            gas_limit: gas_limit.into(),
            payer,
            granter,
        }
    }
}

impl TryFrom<cosmrs::tx::Fee> for Fee {
    type Error = ChainError;

    fn try_from(fee: cosmrs::tx::Fee) -> Result<Self, Self::Error> {
        Ok(Fee {
            amount: fee
                .amount
                .into_iter()
                .map(TryInto::try_into)
                .collect::<Result<Vec<_>, _>>()?,
            gas_limit: fee.gas_limit.into(),
            payer: fee.payer.map(Into::into),
            granter: fee.granter.map(Into::into),
        })
    }
}

impl TryFrom<Fee> for cosmrs::tx::Fee {
    type Error = ChainError;

    fn try_from(fee: Fee) -> Result<Self, Self::Error> {
        Ok(Self {
            amount: fee
                .amount
                .into_iter()
                .map(TryInto::try_into)
                .collect::<Result<Vec<_>, _>>()?,
            gas_limit: fee.gas_limit.into(),
            payer: fee.payer.map(Into::into),
            granter: fee.granter.map(Into::into),
        })
    }
}

#[derive(
    Copy,
    Clone,
    Debug,
    Serialize,
    Deserialize,
    JsonSchema,
    Eq,
    PartialEq,
    PartialOrd,
    Ord,
    Default,
    Hash,
)]
pub struct Gas(u64);

impl Gas {
    pub fn value(self) -> u64 {
        self.0
    }
}

impl fmt::Display for Gas {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} gas", self.0)
    }
}

impl From<u64> for Gas {
    fn from(g: u64) -> Gas {
        Gas(g)
    }
}

impl From<Gas> for u64 {
    fn from(g: Gas) -> u64 {
        g.0
    }
}

impl From<u32> for Gas {
    fn from(g: u32) -> Gas {
        Gas(g.into())
    }
}

impl From<u16> for Gas {
    fn from(g: u16) -> Gas {
        Gas(g.into())
    }
}

impl From<u8> for Gas {
    fn from(g: u8) -> Gas {
        Gas(g.into())
    }
}

#[derive(
    Clone, Debug, Serialize, Deserialize, JsonSchema, Eq, PartialEq, PartialOrd, Ord, Default, Hash,
)]
pub struct GasInfo {
    pub gas_wanted: Gas,
    pub gas_used: Gas,
}

impl fmt::Display for GasInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "wanted: {} used: {}", self.gas_wanted, self.gas_used)
    }
}

impl GasInfo {
    pub fn new(gas_wanted: impl Into<Gas>, gas_used: impl Into<Gas>) -> GasInfo {
        GasInfo {
            gas_wanted: gas_wanted.into(),
            gas_used: gas_used.into(),
        }
    }
}

impl From<ProtoGasInfo> for GasInfo {
    fn from(info: ProtoGasInfo) -> GasInfo {
        GasInfo {
            gas_wanted: info.gas_wanted.into(),
            gas_used: info.gas_used.into(),
        }
    }
}

impl From<GasInfo> for ProtoGasInfo {
    fn from(info: GasInfo) -> ProtoGasInfo {
        ProtoGasInfo {
            gas_wanted: info.gas_wanted.into(),
            gas_used: info.gas_used.into(),
        }
    }
}