cosm_tome_wasm_deploy_fork/chain/
fee.rs1use cosmrs::proto::cosmos::base::abci::v1beta1::GasInfo as ProtoGasInfo;
2use schemars::JsonSchema;
3use serde::{Deserialize, Serialize};
4use std::fmt;
5
6use super::{coin::Coin, error::ChainError};
7use crate::modules::auth::model::Address;
8
9#[derive(Clone, Debug, Default, Serialize, Deserialize, Eq, PartialEq)]
10pub struct Fee {
11    pub amount: Vec<Coin>,
12
13    pub gas_limit: Gas,
14
15    pub payer: Option<Address>,
16
17    pub granter: Option<Address>,
18}
19
20impl Fee {
21    pub fn new(
22        amount: Coin,
23        gas_limit: impl Into<Gas>,
24        payer: Option<Address>,
25        granter: Option<Address>,
26    ) -> Self {
27        Self {
28            amount: vec![amount],
29            gas_limit: gas_limit.into(),
30            payer,
31            granter,
32        }
33    }
34}
35
36impl TryFrom<cosmrs::tx::Fee> for Fee {
37    type Error = ChainError;
38
39    fn try_from(fee: cosmrs::tx::Fee) -> Result<Self, Self::Error> {
40        Ok(Fee {
41            amount: fee
42                .amount
43                .into_iter()
44                .map(TryInto::try_into)
45                .collect::<Result<Vec<_>, _>>()?,
46            gas_limit: fee.gas_limit.into(),
47            payer: fee.payer.map(Into::into),
48            granter: fee.granter.map(Into::into),
49        })
50    }
51}
52
53impl TryFrom<Fee> for cosmrs::tx::Fee {
54    type Error = ChainError;
55
56    fn try_from(fee: Fee) -> Result<Self, Self::Error> {
57        Ok(Self {
58            amount: fee
59                .amount
60                .into_iter()
61                .map(TryInto::try_into)
62                .collect::<Result<Vec<_>, _>>()?,
63            gas_limit: fee.gas_limit.into(),
64            payer: fee.payer.map(Into::into),
65            granter: fee.granter.map(Into::into),
66        })
67    }
68}
69
70#[derive(
71    Copy,
72    Clone,
73    Debug,
74    Serialize,
75    Deserialize,
76    JsonSchema,
77    Eq,
78    PartialEq,
79    PartialOrd,
80    Ord,
81    Default,
82    Hash,
83)]
84pub struct Gas(u64);
85
86impl Gas {
87    pub fn value(self) -> u64 {
88        self.0
89    }
90}
91
92impl fmt::Display for Gas {
93    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94        write!(f, "{} gas", self.0)
95    }
96}
97
98impl From<u64> for Gas {
99    fn from(g: u64) -> Gas {
100        Gas(g)
101    }
102}
103
104impl From<Gas> for u64 {
105    fn from(g: Gas) -> u64 {
106        g.0
107    }
108}
109
110impl From<u32> for Gas {
111    fn from(g: u32) -> Gas {
112        Gas(g.into())
113    }
114}
115
116impl From<u16> for Gas {
117    fn from(g: u16) -> Gas {
118        Gas(g.into())
119    }
120}
121
122impl From<u8> for Gas {
123    fn from(g: u8) -> Gas {
124        Gas(g.into())
125    }
126}
127
128#[derive(
129    Clone, Debug, Serialize, Deserialize, JsonSchema, Eq, PartialEq, PartialOrd, Ord, Default, Hash,
130)]
131pub struct GasInfo {
132    pub gas_wanted: Gas,
133    pub gas_used: Gas,
134}
135
136impl fmt::Display for GasInfo {
137    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138        write!(f, "wanted: {} used: {}", self.gas_wanted, self.gas_used)
139    }
140}
141
142impl GasInfo {
143    pub fn new(gas_wanted: impl Into<Gas>, gas_used: impl Into<Gas>) -> GasInfo {
144        GasInfo {
145            gas_wanted: gas_wanted.into(),
146            gas_used: gas_used.into(),
147        }
148    }
149}
150
151impl From<ProtoGasInfo> for GasInfo {
152    fn from(info: ProtoGasInfo) -> GasInfo {
153        GasInfo {
154            gas_wanted: info.gas_wanted.into(),
155            gas_used: info.gas_used.into(),
156        }
157    }
158}
159
160impl From<GasInfo> for ProtoGasInfo {
161    fn from(info: GasInfo) -> ProtoGasInfo {
162        ProtoGasInfo {
163            gas_wanted: info.gas_wanted.into(),
164            gas_used: info.gas_used.into(),
165        }
166    }
167}