1
// Copyright 2019-2022 PureStake Inc.
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

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

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

            
25
		pub struct OnChargeEVMTransaction<OU>(sp_std::marker::PhantomData<OU>);
26

            
27
		impl<T, OU> OnChargeEVMTransactionT<T> for OnChargeEVMTransaction<OU>
28
		where
29
			T: pallet_evm::Config,
30
			T::Currency: Balanced<T::AccountId>,
31
			OU: OnUnbalanced<Credit<T::AccountId, T::Currency>>,
32
			U256: UniqueSaturatedInto<BalanceFor<T>>
33
		{
34
			type LiquidityInfo = Option<Credit<T::AccountId, T::Currency>>;
35

            
36
412
			fn withdraw_fee(who: &H160, fee: U256) -> Result<Self::LiquidityInfo, pallet_evm::Error<T>> {
37
412
				EVMFungibleAdapter::<<T as pallet_evm::Config>::Currency, ()>::withdraw_fee(who, fee)
38
412
			}
39

            
40
44
			fn can_withdraw(who: &H160, amount: U256) -> Result<(), pallet_evm::Error<T>> {
41
44
				EVMFungibleAdapter::<<T as pallet_evm::Config>::Currency, ()>::can_withdraw(who, amount)
42
44
			}
43

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

            
55
412
			fn pay_priority_fee(tip: Self::LiquidityInfo) {
56
412
				if let Some(tip) = tip {
57
264
					OU::on_unbalanced(tip);
58
279
				}
59
			}
60
		}
61
	}
62
}