radix_engine/system/system_modules/costing/
fee_summary.rs

1use super::RoyaltyRecipient;
2use crate::internal_prelude::*;
3use radix_engine_interface::blueprints::resource::LiquidFungibleResource;
4use sbor::rust::collections::*;
5
6#[derive(Default, Debug, Clone, ScryptoSbor)]
7pub struct FeeReserveFinalizationSummary {
8    /// The total execution cost units consumed
9    pub total_execution_cost_units_consumed: u32,
10    /// The total finalization cost units consumed
11    pub total_finalization_cost_units_consumed: u32,
12
13    /// The total cost for execution
14    pub total_execution_cost_in_xrd: Decimal,
15    /// The total cost for finalization
16    pub total_finalization_cost_in_xrd: Decimal,
17    /// The total cost for tipping
18    pub total_tipping_cost_in_xrd: Decimal,
19    /// The total cost for storage
20    pub total_storage_cost_in_xrd: Decimal,
21    /// The total cost for royalty
22    pub total_royalty_cost_in_xrd: Decimal,
23
24    /// The (non-negative) amount of bad debt due to transaction unable to repay loan.
25    pub total_bad_debt_in_xrd: Decimal,
26    /// The vaults locked for XRD payment
27    pub locked_fees: Vec<(NodeId, LiquidFungibleResource, bool)>,
28    /// The royalty cost breakdown
29    pub royalty_cost_breakdown: IndexMap<RoyaltyRecipient, Decimal>,
30}
31
32impl FeeReserveFinalizationSummary {
33    pub fn loan_fully_repaid(&self) -> bool {
34        self.total_bad_debt_in_xrd == 0.into()
35    }
36
37    // NOTE: Decimal arithmetic operation safe unwrap.
38    // No chance to overflow considering current costing parameters
39
40    pub fn total_cost(&self) -> Decimal {
41        self.total_execution_cost_in_xrd
42            .checked_add(self.total_finalization_cost_in_xrd)
43            .unwrap()
44            .checked_add(self.total_tipping_cost_in_xrd)
45            .unwrap()
46            .checked_add(self.total_storage_cost_in_xrd)
47            .unwrap()
48            .checked_add(self.total_royalty_cost_in_xrd)
49            .unwrap()
50    }
51
52    pub fn network_fees(&self) -> Decimal {
53        self.total_execution_cost_in_xrd
54            .checked_add(self.total_finalization_cost_in_xrd)
55            .unwrap()
56            .checked_add(self.total_storage_cost_in_xrd)
57            .unwrap()
58    }
59
60    pub fn to_proposer_amount(&self) -> Decimal {
61        let one_percent = Decimal::ONE_HUNDREDTH;
62
63        self.total_tipping_cost_in_xrd
64            .checked_mul(
65                one_percent
66                    .checked_mul(TIPS_PROPOSER_SHARE_PERCENTAGE)
67                    .unwrap(),
68            )
69            .unwrap()
70            .checked_add(
71                self.network_fees()
72                    .checked_mul(
73                        one_percent
74                            .checked_mul(NETWORK_FEES_PROPOSER_SHARE_PERCENTAGE)
75                            .unwrap(),
76                    )
77                    .unwrap(),
78            )
79            .unwrap()
80    }
81
82    pub fn to_validator_set_amount(&self) -> Decimal {
83        let one_percent = Decimal::ONE_HUNDREDTH;
84
85        self.total_tipping_cost_in_xrd
86            .checked_mul(
87                one_percent
88                    .checked_mul(TIPS_VALIDATOR_SET_SHARE_PERCENTAGE)
89                    .unwrap(),
90            )
91            .unwrap()
92            .checked_add(
93                self.network_fees()
94                    .checked_mul(
95                        one_percent
96                            .checked_mul(NETWORK_FEES_VALIDATOR_SET_SHARE_PERCENTAGE)
97                            .unwrap(),
98                    )
99                    .unwrap(),
100            )
101            .unwrap()
102    }
103
104    pub fn to_burn_amount(&self) -> Decimal {
105        self.total_tipping_cost_in_xrd
106            .checked_add(self.network_fees())
107            .unwrap()
108            .checked_sub(self.to_proposer_amount())
109            .unwrap()
110            .checked_sub(self.to_validator_set_amount())
111            .unwrap()
112    }
113}