1
// Copyright (C) Moondance Labs Ltd.
2
// This file is part of Tanssi.
3

            
4
// Tanssi 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
// Tanssi 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 Tanssi.  If not, see <http://www.gnu.org/licenses/>.
16

            
17
#[macro_export]
18
macro_rules! impl_on_charge_evm_transaction {
19
	{} => {
20
		type CurrencyAccountId<T> = <T as frame_system::Config>::AccountId;
21

            
22
		type BalanceFor<T> =
23
			<<T as pallet_evm::Config>::Currency as CurrencyT<CurrencyAccountId<T>>>::Balance;
24

            
25
		type PositiveImbalanceFor<T> =
26
			<<T as pallet_evm::Config>::Currency as CurrencyT<CurrencyAccountId<T>>>::PositiveImbalance;
27

            
28
		type NegativeImbalanceFor<T> =
29
			<<T as pallet_evm::Config>::Currency as CurrencyT<CurrencyAccountId<T>>>::NegativeImbalance;
30

            
31
		pub struct OnChargeEVMTransaction<OU>(sp_std::marker::PhantomData<OU>);
32
		impl<T, OU> OnChargeEVMTransactionT<T> for OnChargeEVMTransaction<OU>
33
		where
34
			T: pallet_evm::Config,
35
			PositiveImbalanceFor<T>: Imbalance<BalanceFor<T>, Opposite = NegativeImbalanceFor<T>>,
36
			NegativeImbalanceFor<T>: Imbalance<BalanceFor<T>, Opposite = PositiveImbalanceFor<T>>,
37
			OU: OnUnbalanced<NegativeImbalanceFor<T>>,
38
			U256: UniqueSaturatedInto<BalanceFor<T>>
39
		{
40
			type LiquidityInfo = Option<NegativeImbalanceFor<T>>;
41

            
42
			fn withdraw_fee(who: &H160, fee: U256) -> Result<Self::LiquidityInfo, pallet_evm::Error<T>> {
43
				EVMCurrencyAdapter::<<T as pallet_evm::Config>::Currency, ()>::withdraw_fee(who, fee)
44
			}
45

            
46
			fn correct_and_deposit_fee(
47
				who: &H160,
48
				corrected_fee: U256,
49
				base_fee: U256,
50
				already_withdrawn: Self::LiquidityInfo,
51
			) -> Self::LiquidityInfo {
52
				<EVMCurrencyAdapter<<T as pallet_evm::Config>::Currency, OU> as OnChargeEVMTransactionT<
53
					T,
54
				>>::correct_and_deposit_fee(who, corrected_fee, base_fee, already_withdrawn)
55
			}
56

            
57
			fn pay_priority_fee(tip: Self::LiquidityInfo) {
58
				if let Some(tip) = tip {
59
					OU::on_unbalanced(tip);
60
				}
61
			}
62
		}
63
	}
64
}