git.delta.rocks / unique-network / refs/commits / 82bedeffe28d

difftreelog

source

pallets/evm-transaction-payment/src/lib.rs2.9 KiBsourcehistory
1#![cfg_attr(not(feature = "std"), no_std)]23pub use pallet::*;4use up_evm_mapping::EvmBackwardsAddressMapping;56#[frame_support::pallet]7pub mod pallet {8	use core::marker::PhantomData;9	use frame_support::traits::Currency;10	use pallet_evm::EVMCurrencyAdapter;11	use fp_evm::WithdrawReason;12	use sp_core::{H160, U256};13	use sp_runtime::TransactionOutcome;14	use up_sponsorship::SponsorshipHandler;15	use sp_std::vec::Vec;1617	type NegativeImbalanceOf<C, T> =18		<C as Currency<<T as frame_system::Config>::AccountId>>::NegativeImbalance;1920	#[pallet::config]21	pub trait Config: frame_system::Config {22		type SponsorshipHandler: SponsorshipHandler<H160, (H160, Vec<u8>)>;23		type Currency: Currency<Self::AccountId>;24		type EvmBackwardsAddressMapping: EvmBackwardsAddressMapping<Self::AccountId>;25	}2627	#[pallet::pallet]28	#[pallet::generate_store(pub(super) trait Store)]29	pub struct Pallet<T>(_);3031	pub struct ChargeEvmLiquidityInfo<T>32	where33		T: Config,34		T: pallet_evm::Config,35	{36		who: H160,37		negative_imbalance: NegativeImbalanceOf<<T as Config>::Currency, T>,38	}3940	pub struct TransactionValidityHack<T: Config>(PhantomData<*const T>);41	impl<T: Config> fp_evm::TransactionValidityHack for TransactionValidityHack<T> {42		fn who_pays_fee(origin: H160, reason: &WithdrawReason) -> Option<H160> {43			match reason {44				WithdrawReason::Call { target, input } => {45					// This method is only used for checking, we shouldn't touch storage in it46					frame_support::storage::with_transaction(|| {47						TransactionOutcome::Rollback(T::SponsorshipHandler::get_sponsor(48							&origin,49							&(*target, input.clone()),50						))51					})52				}53				_ => None,54			}55		}56	}5758	pub struct OnChargeTransaction<T: Config>(PhantomData<*const T>);59	impl<T> pallet_evm::OnChargeEVMTransaction<T> for OnChargeTransaction<T>60	where61		T: Config,62		T: pallet_evm::Config,63	{64		type LiquidityInfo = Option<ChargeEvmLiquidityInfo<T>>;6566		fn withdraw_fee(67			who: &H160,68			reason: WithdrawReason,69			fee: U256,70		) -> core::result::Result<Self::LiquidityInfo, pallet_evm::Error<T>> {71			let mut who_pays_fee = *who;72			if let WithdrawReason::Call { target, input } = &reason {73				who_pays_fee = T::SponsorshipHandler::get_sponsor(who, &(*target, input.clone()))74					.unwrap_or(who_pays_fee);75			}76			let negative_imbalance =77				EVMCurrencyAdapter::<<T as Config>::Currency, ()>::withdraw_fee(78					&who_pays_fee,79					reason,80					fee,81				)?;82			Ok(negative_imbalance.map(|i| ChargeEvmLiquidityInfo {83				who: who_pays_fee,84				negative_imbalance: i,85			}))86		}8788		fn correct_and_deposit_fee(89			who: &H160,90			corrected_fee: U256,91			already_withdrawn: Self::LiquidityInfo,92		) {93			<EVMCurrencyAdapter<<T as Config>::Currency, ()> as pallet_evm::OnChargeEVMTransaction<T>>::correct_and_deposit_fee(94				&already_withdrawn.as_ref().map(|e| e.who).unwrap_or(*who),95				corrected_fee,96				already_withdrawn.map(|e| e.negative_imbalance),97			)98		}99	}100}