moonbeam_runtime_common/
deal_with_fees.rs1use 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
26pub 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 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
70pub 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 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
103pub 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}