Skip to main content

hiero_sdk/
custom_fixed_fee.rs

1// SPDX-License-Identifier: Apache-2.0
2
3use hiero_sdk_proto::services;
4
5use crate::{
6    AccountId,
7    FixedFee,
8    FixedFeeData,
9    FromProtobuf,
10    ToProtobuf,
11    TokenId,
12};
13
14/// A custom fee definition for a consensus topic.
15///
16/// This fee definition is specific to an Hiero Consensus Service (HCS) topic
17/// and SHOULD NOT be used in any other context.
18///
19/// All fields for this message are REQUIRED.
20///
21/// Only "fixed" fee definitions are supported because there is no basis for
22/// a fractional fee on a consensus submit transaction.
23#[derive(Debug, Hash, PartialEq, Eq, Clone, Default)]
24pub struct CustomFixedFee {
25    /// The amount of HBAR or other token described by this `FixedFee` SHALL
26    /// be charged to the transction payer for each message submitted to a
27    /// topic that assigns this consensus custom fee.
28    pub amount: u64,
29
30    /// The denomination of the fee; taken as hbar if left unset and, in a TokenCreate, taken as the id
31    /// of the newly created token if set to the sentinel value of 0.0.0
32    pub denominating_token_id: Option<TokenId>,
33
34    /// The account to receive the custom fee.
35    pub fee_collector_account_id: Option<AccountId>,
36}
37
38impl CustomFixedFee {
39    /// Creates a new `CustomFixedFee`
40    pub fn new(
41        amount: u64,
42        denominating_token_id: Option<TokenId>,
43        fee_collector_account_id: Option<AccountId>,
44    ) -> Self {
45        Self { amount, denominating_token_id, fee_collector_account_id }
46    }
47}
48
49impl CustomFixedFee {
50    pub(crate) fn to_fixed_fee_protobuf(&self) -> services::FixedFee {
51        services::FixedFee {
52            amount: self.amount as i64,
53            denominating_token_id: self.denominating_token_id.to_protobuf(),
54        }
55    }
56}
57
58impl From<CustomFixedFee> for FixedFee {
59    fn from(v: CustomFixedFee) -> Self {
60        Self {
61            fee: FixedFeeData {
62                amount: v.amount as i64,
63                denominating_token_id: v.denominating_token_id,
64            },
65            fee_collector_account_id: v.fee_collector_account_id,
66            all_collectors_are_exempt: false,
67        }
68    }
69}
70
71impl ToProtobuf for CustomFixedFee {
72    type Protobuf = services::FixedCustomFee;
73
74    fn to_protobuf(&self) -> Self::Protobuf {
75        Self::Protobuf {
76            fixed_fee: Some(services::FixedFee {
77                amount: self.amount as i64,
78                denominating_token_id: self.denominating_token_id.to_protobuf(),
79            }),
80            fee_collector_account_id: self.fee_collector_account_id.to_protobuf(),
81        }
82    }
83}
84
85impl FromProtobuf<services::FixedCustomFee> for CustomFixedFee {
86    fn from_protobuf(pb: services::FixedCustomFee) -> crate::Result<Self> {
87        let fee = FixedFeeData::from_protobuf(pb.fixed_fee.unwrap())?;
88
89        Ok(Self {
90            amount: fee.amount as u64,
91            denominating_token_id: fee.denominating_token_id,
92            fee_collector_account_id: Option::from_protobuf(pb.fee_collector_account_id)?,
93        })
94    }
95}
96
97impl FromProtobuf<services::FixedFee> for CustomFixedFee {
98    fn from_protobuf(pb: services::FixedFee) -> crate::Result<Self> {
99        let fee = FixedFeeData::from_protobuf(pb)?;
100
101        Ok(Self {
102            amount: fee.amount as u64,
103            denominating_token_id: fee.denominating_token_id,
104            fee_collector_account_id: None,
105        })
106    }
107}