Skip to main content

hiero_sdk/
fee_estimate_types.rs

1// SPDX-License-Identifier: Apache-2.0
2
3use std::fmt;
4
5/// Represents an individual extra fee charged beyond the base amount.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct FeeExtra {
8    /// Unique name identifier from the fee schedule.
9    pub name: Option<String>,
10
11    /// Count of items included for free.
12    pub included: u64,
13
14    /// Actual count of items.
15    pub count: u64,
16
17    /// Billable item count: `max(0, count - included)`.
18    pub charged: u64,
19
20    /// Fee price per unit in tinycents.
21    pub fee_per_unit: u64,
22
23    /// Total fee in tinycents: `charged * fee_per_unit`.
24    pub subtotal: u64,
25}
26
27impl fmt::Display for FeeExtra {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        write!(
30            f,
31            "FeeExtra {{ name: {:?}, included: {}, count: {}, charged: {}, fee_per_unit: {}, subtotal: {} }}",
32            self.name, self.included, self.count, self.charged, self.fee_per_unit, self.subtotal
33        )
34    }
35}
36
37/// Represents a fee estimate component with a base fee and optional extras.
38#[derive(Debug, Clone, PartialEq, Eq)]
39pub struct FeeEstimate {
40    /// Base fee price in tinycents.
41    pub base: u64,
42
43    /// Additional fees beyond the base amount.
44    pub extras: Vec<FeeExtra>,
45}
46
47impl FeeEstimate {
48    /// Returns the subtotal: base + sum of all extras' subtotals.
49    #[must_use]
50    pub fn subtotal(&self) -> u64 {
51        self.base + self.extras.iter().map(|e| e.subtotal).sum::<u64>()
52    }
53}
54
55impl fmt::Display for FeeEstimate {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        write!(f, "FeeEstimate {{ base: {}, extras: {} }}", self.base, self.extras.len())
58    }
59}
60
61/// Represents the network fee component.
62#[derive(Debug, Clone, PartialEq, Eq)]
63pub struct NetworkFee {
64    /// Multiplier applied to the node fee subtotal to compute the network fee.
65    pub multiplier: u32,
66
67    /// Network fee subtotal in tinycents.
68    pub subtotal: u64,
69}
70
71impl fmt::Display for NetworkFee {
72    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73        write!(f, "NetworkFee {{ multiplier: {}, subtotal: {} }}", self.multiplier, self.subtotal)
74    }
75}
76
77/// The complete fee estimation response from the mirror node.
78#[derive(Debug, Clone, PartialEq, Eq)]
79pub struct FeeEstimateResponse {
80    /// High-volume pricing multiplier per HIP-1313. Defaults to 1 when not applicable.
81    pub high_volume_multiplier: u64,
82
83    /// Network fee component covering gossip, consensus, signature verification, and storage.
84    pub network: Option<NetworkFee>,
85
86    /// Node fee component for node compensation.
87    pub node: Option<FeeEstimate>,
88
89    /// Service fee component for execution and state persistence.
90    pub service: Option<FeeEstimate>,
91
92    /// Total combined fee in tinycents.
93    pub total: u64,
94}
95
96impl fmt::Display for FeeEstimateResponse {
97    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98        write!(f, "FeeEstimateResponse {{ total: {} }}", self.total)
99    }
100}