moonbeam_runtime_common/
deal_with_fees.rs1use 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
29pub type RefTimeToFee<M> = ConstantMultiplier<Balance, M>;
34
35pub 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
52pub 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 RefTimeToFee::weight_to_fee(weight).max(ProofSizeToFee::weight_to_fee(weight))
69 }
70}
71
72pub 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 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
116pub 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 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
149pub 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}