1234567891011121314151617#![cfg_attr(not(feature = "std"), no_std)]1819use core::marker::PhantomData;20use fp_evm::WithdrawReason;21use frame_support::traits::{Currency, IsSubType};22pub use pallet::*;23use pallet_evm::{EVMCurrencyAdapter, EnsureAddressOrigin};24use sp_core::{H160, U256};25use sp_runtime::TransactionOutcome;26use up_sponsorship::SponsorshipHandler;27use up_evm_mapping::EvmBackwardsAddressMapping;28use pallet_evm::AddressMapping;2930#[frame_support::pallet]31pub mod pallet {32 use super::*;3334 use frame_support::traits::Currency;35 use sp_std::vec::Vec;3637 #[pallet::config]38 pub trait Config: frame_system::Config {39 type EvmSponsorshipHandler: SponsorshipHandler<H160, (H160, Vec<u8>)>;40 type Currency: Currency<Self::AccountId>;41 type EvmBackwardsAddressMapping: EvmBackwardsAddressMapping<Self::AccountId>;42 type EvmAddressMapping: AddressMapping<Self::AccountId>;43 }4445 #[pallet::pallet]46 #[pallet::generate_store(pub(super) trait Store)]47 pub struct Pallet<T>(_);48}4950type NegativeImbalanceOf<C, T> =51 <C as Currency<<T as frame_system::Config>::AccountId>>::NegativeImbalance;5253pub struct ChargeEvmLiquidityInfo<T>54where55 T: Config,56 T: pallet_evm::Config,57{58 who: H160,59 negative_imbalance: NegativeImbalanceOf<<T as Config>::Currency, T>,60}6162pub struct TransactionValidityHack<T: Config>(PhantomData<*const T>);63impl<T: Config> fp_evm::TransactionValidityHack for TransactionValidityHack<T> {64 fn who_pays_fee(origin: H160, reason: &WithdrawReason) -> Option<H160> {65 match reason {66 WithdrawReason::Call { target, input } => {67 68 frame_support::storage::with_transaction(|| {69 TransactionOutcome::Rollback(T::EvmSponsorshipHandler::get_sponsor(70 &origin,71 &(*target, input.clone()),72 ))73 })74 }75 _ => None,76 }77 }78}79pub struct OnChargeTransaction<T: Config>(PhantomData<*const T>);80impl<T> pallet_evm::OnChargeEVMTransaction<T> for OnChargeTransaction<T>81where82 T: Config,83 T: pallet_evm::Config,84{85 type LiquidityInfo = Option<ChargeEvmLiquidityInfo<T>>;8687 fn withdraw_fee(88 who: &H160,89 reason: WithdrawReason,90 fee: U256,91 ) -> core::result::Result<Self::LiquidityInfo, pallet_evm::Error<T>> {92 let mut who_pays_fee = *who;93 if let WithdrawReason::Call { target, input } = &reason {94 who_pays_fee = T::EvmSponsorshipHandler::get_sponsor(who, &(*target, input.clone()))95 .unwrap_or(who_pays_fee);96 }97 let negative_imbalance = EVMCurrencyAdapter::<<T as Config>::Currency, ()>::withdraw_fee(98 &who_pays_fee,99 reason,100 fee,101 )?;102 Ok(negative_imbalance.map(|i| ChargeEvmLiquidityInfo {103 who: who_pays_fee,104 negative_imbalance: i,105 }))106 }107108 fn correct_and_deposit_fee(109 who: &H160,110 corrected_fee: U256,111 already_withdrawn: Self::LiquidityInfo,112 ) {113 <EVMCurrencyAdapter<<T as Config>::Currency, ()> as pallet_evm::OnChargeEVMTransaction<T>>::correct_and_deposit_fee(114 &already_withdrawn.as_ref().map(|e| e.who).unwrap_or(*who),115 corrected_fee,116 already_withdrawn.map(|e| e.negative_imbalance),117 )118 }119120 fn pay_priority_fee(tip: U256) {121 <EVMCurrencyAdapter<<T as Config>::Currency, ()> as pallet_evm::OnChargeEVMTransaction<T>>::pay_priority_fee(tip)122 }123}124125126pub struct BridgeSponsorshipHandler<T>(PhantomData<T>);127impl<T, C> SponsorshipHandler<T::AccountId, C> for BridgeSponsorshipHandler<T>128where129 T: Config + pallet_evm::Config,130 C: IsSubType<pallet_evm::Call<T>>,131{132 fn get_sponsor(who: &T::AccountId, call: &C) -> Option<T::AccountId> {133 match call.is_sub_type()? {134 pallet_evm::Call::call {135 source,136 target,137 input,138 ..139 } => {140 let _ = T::CallOrigin::ensure_address_origin(141 source,142 <frame_system::RawOrigin<T::AccountId>>::Signed(who.clone()).into(),143 )144 .ok()?;145 let who = T::EvmBackwardsAddressMapping::from_account_id(who.clone());146 147 148 let sponsor = frame_support::storage::with_transaction(|| {149 TransactionOutcome::Rollback(T::EvmSponsorshipHandler::get_sponsor(150 &who,151 &(*target, input.clone()),152 ))153 })?;154 let sponsor = T::EvmAddressMapping::into_account_id(sponsor);155 Some(sponsor)156 }157 _ => None,158 }159 }160}