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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
use fraction::Fraction;
use hedera_proto::services;
use crate::{
AccountId,
FromProtobuf,
Hbar,
ToProtobuf,
TokenId,
};
#[cfg(test)]
mod tests;
pub type AnyCustomFee = CustomFee<Fee>;
pub type FixedFee = CustomFee<FixedFeeData>;
pub type FractionalFee = CustomFee<FractionalFeeData>;
pub type RoyaltyFee = CustomFee<RoyaltyFeeData>;
#[derive(Debug, Hash, PartialEq, Eq, Clone)]
pub struct CustomFee<Fee> {
pub fee: Fee,
pub fee_collector_account_id: Option<AccountId>,
pub all_collectors_are_exempt: bool,
}
impl AnyCustomFee {
pub fn from_bytes(bytes: &[u8]) -> crate::Result<Self> {
FromProtobuf::from_bytes(bytes)
}
pub fn to_bytes(&self) -> Vec<u8> {
ToProtobuf::to_bytes(self)
}
}
impl FromProtobuf<services::CustomFee> for AnyCustomFee {
fn from_protobuf(pb: services::CustomFee) -> crate::Result<Self>
where
Self: Sized,
{
let fee_collector_account_id = Option::from_protobuf(pb.fee_collector_account_id)?;
let fee = pb_getf!(pb, fee)?;
let fee: Fee = match fee {
services::custom_fee::Fee::FixedFee(pb) => FixedFeeData::from_protobuf(pb)?.into(),
services::custom_fee::Fee::FractionalFee(pb) => {
FractionalFeeData::from_protobuf(pb)?.into()
}
services::custom_fee::Fee::RoyaltyFee(pb) => RoyaltyFeeData::from_protobuf(pb)?.into(),
};
Ok(Self {
fee,
fee_collector_account_id,
all_collectors_are_exempt: pb.all_collectors_are_exempt,
})
}
}
impl ToProtobuf for AnyCustomFee {
type Protobuf = services::CustomFee;
fn to_protobuf(&self) -> Self::Protobuf {
services::CustomFee {
fee_collector_account_id: self.fee_collector_account_id.to_protobuf(),
fee: Some(self.fee.to_protobuf()),
all_collectors_are_exempt: self.all_collectors_are_exempt,
}
}
}
impl From<FixedFee> for AnyCustomFee {
fn from(v: FixedFee) -> Self {
Self {
fee: v.fee.into(),
fee_collector_account_id: v.fee_collector_account_id,
all_collectors_are_exempt: v.all_collectors_are_exempt,
}
}
}
impl From<FractionalFee> for AnyCustomFee {
fn from(v: FractionalFee) -> Self {
Self {
fee: v.fee.into(),
fee_collector_account_id: v.fee_collector_account_id,
all_collectors_are_exempt: v.all_collectors_are_exempt,
}
}
}
impl From<RoyaltyFee> for AnyCustomFee {
fn from(v: RoyaltyFee) -> Self {
Self {
fee: v.fee.into(),
fee_collector_account_id: v.fee_collector_account_id,
all_collectors_are_exempt: v.all_collectors_are_exempt,
}
}
}
#[derive(Debug, Hash, PartialEq, Eq, Clone)]
pub enum Fee {
Fixed(FixedFeeData),
Fractional(FractionalFeeData),
Royalty(RoyaltyFeeData),
}
impl FromProtobuf<services::custom_fee::Fee> for Fee {
fn from_protobuf(pb: services::custom_fee::Fee) -> crate::Result<Self>
where
Self: Sized,
{
use services::custom_fee::Fee as ProtoFee;
match pb {
ProtoFee::FixedFee(it) => Ok(FixedFeeData::from_protobuf(it)?.into()),
ProtoFee::FractionalFee(it) => Ok(FractionalFeeData::from_protobuf(it)?.into()),
ProtoFee::RoyaltyFee(it) => Ok(RoyaltyFeeData::from_protobuf(it)?.into()),
}
}
}
impl ToProtobuf for Fee {
type Protobuf = services::custom_fee::Fee;
fn to_protobuf(&self) -> Self::Protobuf {
use services::custom_fee::Fee as ProtoFee;
match self {
Self::Fixed(it) => ProtoFee::FixedFee(it.to_protobuf()),
Self::Fractional(it) => ProtoFee::FractionalFee(it.to_protobuf()),
Self::Royalty(it) => ProtoFee::RoyaltyFee(it.to_protobuf()),
}
}
}
impl From<FixedFeeData> for Fee {
fn from(v: FixedFeeData) -> Self {
Self::Fixed(v)
}
}
impl From<FractionalFeeData> for Fee {
fn from(v: FractionalFeeData) -> Self {
Self::Fractional(v)
}
}
impl From<RoyaltyFeeData> for Fee {
fn from(v: RoyaltyFeeData) -> Self {
Self::Royalty(v)
}
}
#[derive(Debug, Hash, PartialEq, Eq, Clone)]
pub struct FixedFeeData {
pub amount: i64,
pub denominating_token_id: TokenId,
}
impl FixedFeeData {
#[must_use]
pub fn from_hbar(amount: Hbar) -> Self {
Self {
amount: amount.to_tinybars(),
denominating_token_id: TokenId { shard: 0, realm: 0, num: 0, checksum: None },
}
}
#[must_use]
pub fn get_hbar(&self) -> Option<Hbar> {
(self.denominating_token_id == TokenId { shard: 0, realm: 0, num: 0, checksum: None })
.then(|| Hbar::from_tinybars(self.amount))
}
}
impl FromProtobuf<services::FixedFee> for FixedFeeData {
fn from_protobuf(pb: services::FixedFee) -> crate::Result<Self> {
Ok(Self {
amount: pb.amount,
denominating_token_id: TokenId::from_protobuf(pb_getf!(pb, denominating_token_id)?)?,
})
}
}
impl ToProtobuf for FixedFeeData {
type Protobuf = services::FixedFee;
fn to_protobuf(&self) -> Self::Protobuf {
Self::Protobuf {
amount: self.amount,
denominating_token_id: Some(self.denominating_token_id.to_protobuf()),
}
}
}
#[derive(Debug, Hash, PartialEq, Eq, Clone)]
pub struct FractionalFeeData {
pub denominator: u64,
pub numerator: u64,
pub minimum_amount: i64,
pub maximum_amount: i64,
pub assessment_method: FeeAssessmentMethod,
}
impl FromProtobuf<services::FractionalFee> for FractionalFeeData {
fn from_protobuf(pb: services::FractionalFee) -> crate::Result<Self> {
let amount = pb.fractional_amount.map(Fraction::from).unwrap_or_default();
Ok(Self {
denominator: *amount.denom().unwrap(),
numerator: *amount.numer().unwrap(),
assessment_method: match pb.net_of_transfers {
true => FeeAssessmentMethod::Exclusive,
false => FeeAssessmentMethod::Inclusive,
},
maximum_amount: pb.maximum_amount,
minimum_amount: pb.minimum_amount,
})
}
}
impl ToProtobuf for FractionalFeeData {
type Protobuf = services::FractionalFee;
fn to_protobuf(&self) -> Self::Protobuf {
Self::Protobuf {
fractional_amount: Some(Fraction::new(self.numerator, self.denominator).into()),
minimum_amount: self.minimum_amount,
maximum_amount: self.maximum_amount,
net_of_transfers: matches!(self.assessment_method, FeeAssessmentMethod::Exclusive),
}
}
}
#[derive(Debug, Hash, PartialEq, Eq, Clone)]
pub struct RoyaltyFeeData {
pub denominator: u64,
pub numerator: u64,
pub fallback_fee: Option<FixedFeeData>,
}
impl FromProtobuf<services::RoyaltyFee> for RoyaltyFeeData {
fn from_protobuf(pb: services::RoyaltyFee) -> crate::Result<Self> {
let amount = pb.exchange_value_fraction.unwrap_or_default();
Ok(Self {
denominator: amount.denominator as u64,
numerator: amount.numerator as u64,
fallback_fee: Option::from_protobuf(pb.fallback_fee)?,
})
}
}
impl ToProtobuf for RoyaltyFeeData {
type Protobuf = services::RoyaltyFee;
fn to_protobuf(&self) -> Self::Protobuf {
Self::Protobuf {
fallback_fee: self.fallback_fee.to_protobuf(),
exchange_value_fraction: Some(services::Fraction {
numerator: self.numerator as i64,
denominator: self.denominator as i64,
}),
}
}
}
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
pub enum FeeAssessmentMethod {
Inclusive,
Exclusive,
}