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 frame_support::__private::Get;
18use frame_support::pallet_prelude::TypedGet;
19use frame_support::traits::fungible::Credit;
20use frame_support::traits::tokens::imbalance::ResolveTo;
21use frame_support::traits::Imbalance;
22use frame_support::traits::OnUnbalanced;
23use pallet_treasury::TreasuryAccountId;
24use sp_runtime::Perbill;
25
26/// Deal with substrate based fees and tip. This should be used with pallet_transaction_payment.
27pub struct DealWithSubstrateFeesAndTip<R, FeesTreasuryProportion>(
28	sp_std::marker::PhantomData<(R, FeesTreasuryProportion)>,
29);
30impl<R, FeesTreasuryProportion> DealWithSubstrateFeesAndTip<R, FeesTreasuryProportion>
31where
32	R: pallet_balances::Config + pallet_treasury::Config + pallet_author_inherent::Config,
33	pallet_author_inherent::Pallet<R>: Get<R::AccountId>,
34	FeesTreasuryProportion: Get<Perbill>,
35{
36	fn deal_with_fees(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
37		// Balances pallet automatically burns dropped Credits by decreasing
38		// total_supply accordingly
39		let treasury_proportion = FeesTreasuryProportion::get();
40		let treasury_part = treasury_proportion.deconstruct();
41		let burn_part = Perbill::one().deconstruct() - treasury_part;
42		let (_, to_treasury) = amount.ration(burn_part, treasury_part);
43		ResolveTo::<TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(to_treasury);
44	}
45
46	fn deal_with_tip(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
47		ResolveTo::<BlockAuthorAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(amount);
48	}
49}
50
51impl<R, FeesTreasuryProportion> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>>
52	for DealWithSubstrateFeesAndTip<R, FeesTreasuryProportion>
53where
54	R: pallet_balances::Config + pallet_treasury::Config + pallet_author_inherent::Config,
55	pallet_author_inherent::Pallet<R>: Get<R::AccountId>,
56	FeesTreasuryProportion: Get<Perbill>,
57{
58	fn on_unbalanceds(
59		mut fees_then_tips: impl Iterator<Item = Credit<R::AccountId, pallet_balances::Pallet<R>>>,
60	) {
61		if let Some(fees) = fees_then_tips.next() {
62			Self::deal_with_fees(fees);
63			if let Some(tip) = fees_then_tips.next() {
64				Self::deal_with_tip(tip);
65			}
66		}
67	}
68}
69
70/// Deal with ethereum based fees. To handle tips/priority fees, use DealWithEthereumPriorityFees.
71pub struct DealWithEthereumBaseFees<R, FeesTreasuryProportion>(
72	sp_std::marker::PhantomData<(R, FeesTreasuryProportion)>,
73);
74impl<R, FeesTreasuryProportion> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>>
75	for DealWithEthereumBaseFees<R, FeesTreasuryProportion>
76where
77	R: pallet_balances::Config + pallet_treasury::Config,
78	FeesTreasuryProportion: Get<Perbill>,
79{
80	fn on_nonzero_unbalanced(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
81		// Balances pallet automatically burns dropped Credits by decreasing
82		// total_supply accordingly
83		let treasury_proportion = FeesTreasuryProportion::get();
84		let treasury_part = treasury_proportion.deconstruct();
85		let burn_part = Perbill::one().deconstruct() - treasury_part;
86		let (_, to_treasury) = amount.ration(burn_part, treasury_part);
87		ResolveTo::<TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(to_treasury);
88	}
89}
90
91pub struct BlockAuthorAccountId<R>(sp_std::marker::PhantomData<R>);
92impl<R> TypedGet for BlockAuthorAccountId<R>
93where
94	R: frame_system::Config + pallet_author_inherent::Config,
95	pallet_author_inherent::Pallet<R>: Get<R::AccountId>,
96{
97	type Type = R::AccountId;
98	fn get() -> Self::Type {
99		<pallet_author_inherent::Pallet<R> as Get<R::AccountId>>::get()
100	}
101}
102
103/// Deal with ethereum based priority fees/tips. See DealWithEthereumBaseFees for base fees.
104pub struct DealWithEthereumPriorityFees<R>(sp_std::marker::PhantomData<R>);
105impl<R> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>>
106	for DealWithEthereumPriorityFees<R>
107where
108	R: pallet_balances::Config + pallet_author_inherent::Config,
109	pallet_author_inherent::Pallet<R>: Get<R::AccountId>,
110{
111	fn on_nonzero_unbalanced(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
112		ResolveTo::<BlockAuthorAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(amount);
113	}
114}