moonbeam_runtime_common/
impl_on_charge_evm_transaction.rs

1// Copyright 2019-2025 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]
18macro_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<BaseFeesOU, PriorityFeesOU>(
26			sp_std::marker::PhantomData<(BaseFeesOU, PriorityFeesOU)>
27		);
28
29		impl<T, BaseFeesOU, PriorityFeesOU> OnChargeEVMTransactionT<T>
30			for OnChargeEVMTransaction<BaseFeesOU, PriorityFeesOU>
31		where
32			T: pallet_evm::Config,
33			T::Currency: Balanced<pallet_evm::AccountIdOf<T>>,
34			BaseFeesOU: OnUnbalanced<Credit<pallet_evm::AccountIdOf<T>, T::Currency>>,
35			PriorityFeesOU: OnUnbalanced<Credit<pallet_evm::AccountIdOf<T>, T::Currency>>,
36			U256: UniqueSaturatedInto<<T::Currency as Inspect<pallet_evm::AccountIdOf<T>>>::Balance>,
37			T::AddressMapping: pallet_evm::AddressMapping<T::AccountId>,
38		{
39			type LiquidityInfo = Option<Credit<pallet_evm::AccountIdOf<T>, T::Currency>>;
40
41			fn withdraw_fee(who: &H160, fee: U256) -> Result<Self::LiquidityInfo, pallet_evm::Error<T>> {
42				EVMFungibleAdapter::<<T as pallet_evm::Config>::Currency, ()>::withdraw_fee(who, fee)
43			}
44
45			fn can_withdraw(who: &H160, amount: U256) -> Result<(), pallet_evm::Error<T>> {
46				EVMFungibleAdapter::<<T as pallet_evm::Config>::Currency, ()>::can_withdraw(who, amount)
47			}
48
49			fn correct_and_deposit_fee(
50				who: &H160,
51				corrected_fee: U256,
52				base_fee: U256,
53				already_withdrawn: Self::LiquidityInfo,
54			) -> Self::LiquidityInfo {
55				<EVMFungibleAdapter<<T as pallet_evm::Config>::Currency, BaseFeesOU> as OnChargeEVMTransactionT<
56					T,
57				>>::correct_and_deposit_fee(who, corrected_fee, base_fee, already_withdrawn)
58			}
59
60			fn pay_priority_fee(tip: Self::LiquidityInfo) {
61				if let Some(tip) = tip {
62					PriorityFeesOU::on_unbalanced(tip);
63				}
64			}
65		}
66	}
67}