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
#[macro_export]
macro_rules! impl_on_charge_evm_transaction {
	{} => {
		type CurrencyAccountId<T> = <T as frame_system::Config>::AccountId;

		type BalanceFor<T> =
			<<T as pallet_evm::Config>::Currency as frame_support::traits::Currency<CurrencyAccountId<T>>>::Balance;

		type PositiveImbalanceFor<T> =
			<<T as pallet_evm::Config>::Currency as frame_support::traits::Currency<CurrencyAccountId<T>>>::PositiveImbalance;

		type NegativeImbalanceFor<T> =
			<<T as pallet_evm::Config>::Currency as frame_support::traits::Currency<CurrencyAccountId<T>>>::NegativeImbalance;

		pub struct OnChargeEVMTransaction<OU>(sp_std::marker::PhantomData<OU>);
		impl<T, OU> pallet_evm::OnChargeEVMTransaction<T> for OnChargeEVMTransaction<OU>
		where
			T: pallet_evm::Config,
			PositiveImbalanceFor<T>: frame_support::traits::Imbalance<BalanceFor<T>, Opposite = NegativeImbalanceFor<T>>,
			NegativeImbalanceFor<T>: frame_support::traits::Imbalance<BalanceFor<T>, Opposite = PositiveImbalanceFor<T>>,
			OU: OnUnbalanced<NegativeImbalanceFor<T>>,
			U256: sp_runtime::traits::UniqueSaturatedInto<BalanceFor<T>>
		{
			type LiquidityInfo = Option<NegativeImbalanceFor<T>>;

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

			// fn can_withdraw(who: &sp_core::H160, amount: sp_core::U256) -> Result<(), pallet_evm::Error<T>> {
			// 	pallet_evm::EVMCurrencyAdapter::<<T as pallet_evm::Config>::Currency, ()>::can_withdraw(who, amount)
			// }

			fn correct_and_deposit_fee(
				who: &sp_core::H160,
				corrected_fee: sp_core::U256,
				base_fee: sp_core::U256,
				already_withdrawn: Self::LiquidityInfo,
			) -> Self::LiquidityInfo {
				<pallet_evm::EVMCurrencyAdapter<<T as pallet_evm::Config>::Currency, OU> as OnChargeEVMTransactionT<
					T,
				>>::correct_and_deposit_fee(who, corrected_fee, base_fee, already_withdrawn)
			}

			fn pay_priority_fee(tip: Self::LiquidityInfo) {
				if let Some(tip) = tip {
					OU::on_unbalanced(tip);
				}
			}
		}
	}
}