moonbeam_runtime_common/
deal_with_fees.rs

1// Copyright 2024 Moonbeam foundation
2// This file is part of Moonbeam.
3
4// Moonbeam is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Moonbeam is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Moonbeam.  If not, see <http://www.gnu.org/licenses/>.
16
17use core::marker::PhantomData;
18use cumulus_primitives_core::Weight;
19use frame_support::pallet_prelude::TypedGet;
20use frame_support::traits::fungible::Credit;
21use frame_support::traits::tokens::imbalance::ResolveTo;
22use frame_support::traits::OnUnbalanced;
23use frame_support::traits::{Get, Imbalance};
24use frame_support::weights::ConstantMultiplier;
25use moonbeam_core_primitives::Balance;
26use pallet_treasury::TreasuryAccountId;
27use sp_runtime::{Perbill, SaturatedConversion};
28
29/// Type alias for converting reference time weight to fee using a constant multiplier.
30///
31/// This maps computational weight (ref_time) to a fee amount by multiplying
32/// the weight by a constant factor `M`.
33pub type RefTimeToFee<M> = ConstantMultiplier<Balance, M>;
34
35/// Type alias for converting proof size weight to fee using a constant multiplier.
36///
37/// This maps the proof size (PoV size) component of weight to a fee amount
38/// by multiplying by a constant factor `M`.
39pub struct ProofSizeToFee<M>(PhantomData<M>);
40
41impl<M> frame_support::weights::WeightToFee for ProofSizeToFee<M>
42where
43	M: Get<Balance>,
44{
45	type Balance = Balance;
46
47	fn weight_to_fee(weight: &Weight) -> Self::Balance {
48		Self::Balance::saturated_from(weight.proof_size()).saturating_mul(M::get())
49	}
50}
51
52/// Combines reference time and proof size fees, charging by the more scarce resource.
53///
54/// This struct implements `WeightToFee` by computing fees for both the ref_time and
55/// proof_size components of a `Weight`, then returning the maximum of the two.
56/// This ensures transactions are charged based on whichever resource they consume
57/// more of relative to block limits.
58pub struct WeightToFee<RefTimeToFee, ProofSizeToFee>(PhantomData<(RefTimeToFee, ProofSizeToFee)>);
59impl<
60		RefTimeToFee: frame_support::weights::WeightToFee<Balance = Balance>,
61		ProofSizeToFee: frame_support::weights::WeightToFee<Balance = Balance>,
62	> frame_support::weights::WeightToFee for WeightToFee<RefTimeToFee, ProofSizeToFee>
63{
64	type Balance = Balance;
65
66	fn weight_to_fee(weight: &Weight) -> Self::Balance {
67		// Take the maximum instead of the sum to charge by the more scarce resource.
68		RefTimeToFee::weight_to_fee(weight).max(ProofSizeToFee::weight_to_fee(weight))
69	}
70}
71
72/// Deal with substrate based fees and tip. This should be used with pallet_transaction_payment.
73pub struct DealWithSubstrateFeesAndTip<R, FeesTreasuryProportion>(
74	sp_std::marker::PhantomData<(R, FeesTreasuryProportion)>,
75);
76impl<R, FeesTreasuryProportion> DealWithSubstrateFeesAndTip<R, FeesTreasuryProportion>
77where
78	R: pallet_balances::Config + pallet_treasury::Config + pallet_author_inherent::Config,
79	pallet_author_inherent::Pallet<R>: Get<R::AccountId>,
80	FeesTreasuryProportion: Get<Perbill>,
81{
82	fn deal_with_fees(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
83		// Balances pallet automatically burns dropped Credits by decreasing
84		// total_supply accordingly
85		let treasury_proportion = FeesTreasuryProportion::get();
86		let treasury_part = treasury_proportion.deconstruct();
87		let burn_part = Perbill::one().deconstruct() - treasury_part;
88		let (_, to_treasury) = amount.ration(burn_part, treasury_part);
89		ResolveTo::<TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(to_treasury);
90	}
91
92	fn deal_with_tip(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
93		ResolveTo::<BlockAuthorAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(amount);
94	}
95}
96
97impl<R, FeesTreasuryProportion> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>>
98	for DealWithSubstrateFeesAndTip<R, FeesTreasuryProportion>
99where
100	R: pallet_balances::Config + pallet_treasury::Config + pallet_author_inherent::Config,
101	pallet_author_inherent::Pallet<R>: Get<R::AccountId>,
102	FeesTreasuryProportion: Get<Perbill>,
103{
104	fn on_unbalanceds(
105		mut fees_then_tips: impl Iterator<Item = Credit<R::AccountId, pallet_balances::Pallet<R>>>,
106	) {
107		if let Some(fees) = fees_then_tips.next() {
108			Self::deal_with_fees(fees);
109			if let Some(tip) = fees_then_tips.next() {
110				Self::deal_with_tip(tip);
111			}
112		}
113	}
114}
115
116/// Deal with ethereum based fees. To handle tips/priority fees, use DealWithEthereumPriorityFees.
117pub struct DealWithEthereumBaseFees<R, FeesTreasuryProportion>(
118	sp_std::marker::PhantomData<(R, FeesTreasuryProportion)>,
119);
120impl<R, FeesTreasuryProportion> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>>
121	for DealWithEthereumBaseFees<R, FeesTreasuryProportion>
122where
123	R: pallet_balances::Config + pallet_treasury::Config,
124	FeesTreasuryProportion: Get<Perbill>,
125{
126	fn on_nonzero_unbalanced(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
127		// Balances pallet automatically burns dropped Credits by decreasing
128		// total_supply accordingly
129		let treasury_proportion = FeesTreasuryProportion::get();
130		let treasury_part = treasury_proportion.deconstruct();
131		let burn_part = Perbill::one().deconstruct() - treasury_part;
132		let (_, to_treasury) = amount.ration(burn_part, treasury_part);
133		ResolveTo::<TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(to_treasury);
134	}
135}
136
137pub struct BlockAuthorAccountId<R>(sp_std::marker::PhantomData<R>);
138impl<R> TypedGet for BlockAuthorAccountId<R>
139where
140	R: frame_system::Config + pallet_author_inherent::Config,
141	pallet_author_inherent::Pallet<R>: Get<R::AccountId>,
142{
143	type Type = R::AccountId;
144	fn get() -> Self::Type {
145		<pallet_author_inherent::Pallet<R> as Get<R::AccountId>>::get()
146	}
147}
148
149/// Deal with ethereum based priority fees/tips. See DealWithEthereumBaseFees for base fees.
150pub struct DealWithEthereumPriorityFees<R>(sp_std::marker::PhantomData<R>);
151impl<R> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>>
152	for DealWithEthereumPriorityFees<R>
153where
154	R: pallet_balances::Config + pallet_author_inherent::Config,
155	pallet_author_inherent::Pallet<R>: Get<R::AccountId>,
156{
157	fn on_nonzero_unbalanced(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
158		ResolveTo::<BlockAuthorAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(amount);
159	}
160}