1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2024 Moonbeam foundation
// This file is part of Moonbeam.

// Moonbeam is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Moonbeam is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Moonbeam.  If not, see <http://www.gnu.org/licenses/>.

use frame_support::__private::Get;
use frame_support::pallet_prelude::TypedGet;
use frame_support::traits::fungible::Credit;
use frame_support::traits::tokens::imbalance::ResolveTo;
use frame_support::traits::Imbalance;
use frame_support::traits::OnUnbalanced;
use pallet_treasury::TreasuryAccountId;
use sp_runtime::Perbill;

/// Deal with substrate based fees and tip. This should be used with pallet_transaction_payment.
pub struct DealWithSubstrateFeesAndTip<R, FeesTreasuryProportion>(
	sp_std::marker::PhantomData<(R, FeesTreasuryProportion)>,
);
impl<R, FeesTreasuryProportion> DealWithSubstrateFeesAndTip<R, FeesTreasuryProportion>
where
	R: pallet_balances::Config + pallet_treasury::Config + pallet_author_inherent::Config,
	pallet_author_inherent::Pallet<R>: Get<R::AccountId>,
	FeesTreasuryProportion: Get<Perbill>,
{
	fn deal_with_fees(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
		// Balances pallet automatically burns dropped Credits by decreasing
		// total_supply accordingly
		let treasury_proportion = FeesTreasuryProportion::get();
		let treasury_part = treasury_proportion.deconstruct();
		let burn_part = Perbill::one().deconstruct() - treasury_part;
		let (_, to_treasury) = amount.ration(burn_part, treasury_part);
		ResolveTo::<TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(to_treasury);
	}

	fn deal_with_tip(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
		ResolveTo::<BlockAuthorAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(amount);
	}
}

impl<R, FeesTreasuryProportion> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>>
	for DealWithSubstrateFeesAndTip<R, FeesTreasuryProportion>
where
	R: pallet_balances::Config + pallet_treasury::Config + pallet_author_inherent::Config,
	pallet_author_inherent::Pallet<R>: Get<R::AccountId>,
	FeesTreasuryProportion: Get<Perbill>,
{
	fn on_unbalanceds(
		mut fees_then_tips: impl Iterator<Item = Credit<R::AccountId, pallet_balances::Pallet<R>>>,
	) {
		if let Some(fees) = fees_then_tips.next() {
			Self::deal_with_fees(fees);
			if let Some(tip) = fees_then_tips.next() {
				Self::deal_with_tip(tip);
			}
		}
	}
}

/// Deal with ethereum based fees. To handle tips/priority fees, use DealWithEthereumPriorityFees.
pub struct DealWithEthereumBaseFees<R, FeesTreasuryProportion>(
	sp_std::marker::PhantomData<(R, FeesTreasuryProportion)>,
);
impl<R, FeesTreasuryProportion> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>>
	for DealWithEthereumBaseFees<R, FeesTreasuryProportion>
where
	R: pallet_balances::Config + pallet_treasury::Config,
	FeesTreasuryProportion: Get<Perbill>,
{
	fn on_nonzero_unbalanced(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
		// Balances pallet automatically burns dropped Credits by decreasing
		// total_supply accordingly
		let treasury_proportion = FeesTreasuryProportion::get();
		let treasury_part = treasury_proportion.deconstruct();
		let burn_part = Perbill::one().deconstruct() - treasury_part;
		let (_, to_treasury) = amount.ration(burn_part, treasury_part);
		ResolveTo::<TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(to_treasury);
	}
}

pub struct BlockAuthorAccountId<R>(sp_std::marker::PhantomData<R>);
impl<R> TypedGet for BlockAuthorAccountId<R>
where
	R: frame_system::Config + pallet_author_inherent::Config,
	pallet_author_inherent::Pallet<R>: Get<R::AccountId>,
{
	type Type = R::AccountId;
	fn get() -> Self::Type {
		<pallet_author_inherent::Pallet<R> as Get<R::AccountId>>::get()
	}
}

/// Deal with ethereum based priority fees/tips. See DealWithEthereumBaseFees for base fees.
pub struct DealWithEthereumPriorityFees<R>(sp_std::marker::PhantomData<R>);
impl<R> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>>
	for DealWithEthereumPriorityFees<R>
where
	R: pallet_balances::Config + pallet_author_inherent::Config,
	pallet_author_inherent::Pallet<R>: Get<R::AccountId>,
{
	fn on_nonzero_unbalanced(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
		ResolveTo::<BlockAuthorAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(amount);
	}
}