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, account::CrossAccountId};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 + pallet_evm::account::Config {39 type EvmSponsorshipHandler: SponsorshipHandler<Self::AccountId, (H160, Vec<u8>)>;40 type Currency: Currency<Self::AccountId>;41 }4243 #[pallet::pallet]44 #[pallet::generate_store(pub(super) trait Store)]45 pub struct Pallet<T>(_);46}4748type NegativeImbalanceOf<C, T> =49 <C as Currency<<T as frame_system::Config>::AccountId>>::NegativeImbalance;5051pub struct ChargeEvmLiquidityInfo<T>52where53 T: Config,54 T: pallet_evm::Config,55{56 who: H160,57 negative_imbalance: NegativeImbalanceOf<<T as Config>::Currency, T>,58}5960pub struct TransactionValidityHack<T: Config>(PhantomData<*const T>);61impl<T: Config> fp_evm::TransactionValidityHack<T::AccountId> for TransactionValidityHack<T> {62 fn who_pays_fee(origin: H160, reason: &WithdrawReason) -> Option<T::AccountId> {63 match reason {64 WithdrawReason::Call { target, input } => {65 66 frame_support::storage::with_transaction(|| {67 let origin_sub = T::EvmAddressMapping::into_account_id(origin);68 TransactionOutcome::Rollback(T::EvmSponsorshipHandler::get_sponsor(69 &origin_sub,70 &(*target, input.clone()),71 ))72 })73 }74 _ => None,75 }76 }77}78pub struct OnChargeTransaction<T: Config>(PhantomData<*const T>);79impl<T> pallet_evm::OnChargeEVMTransaction<T> for OnChargeTransaction<T>80where81 T: Config,82 T: pallet_evm::Config,83{84 type LiquidityInfo = Option<ChargeEvmLiquidityInfo<T>>;8586 fn withdraw_fee(87 who: &T::CrossAccountId,88 reason: WithdrawReason,89 fee: U256,90 ) -> core::result::Result<Self::LiquidityInfo, pallet_evm::Error<T>> {91 let who_pays_fee;92 if let WithdrawReason::Call { target, input } = &reason {93 who_pays_fee = <T as pallet_evm::account::Config>::CrossAccountId::from_sub(T::EvmSponsorshipHandler::get_sponsor(&who.as_sub(), &(*target, input.clone()))94 .unwrap_or(who.as_sub().clone()));95 } else {96 who_pays_fee = who.clone();97 }9899 let negative_imbalance = EVMCurrencyAdapter::<<T as Config>::Currency, ()>::withdraw_fee(100 &who_pays_fee,101 reason,102 fee,103 )?;104105 Ok(negative_imbalance.map(|i| ChargeEvmLiquidityInfo {106 who: who_pays_fee.as_eth().clone(),107 negative_imbalance: i,108 }))109 }110111 fn correct_and_deposit_fee(112 who: &T::CrossAccountId,113 corrected_fee: U256,114 already_withdrawn: Self::LiquidityInfo,115 ) {116 <EVMCurrencyAdapter<<T as Config>::Currency, ()> as pallet_evm::OnChargeEVMTransaction<T>>::correct_and_deposit_fee(117 &already_withdrawn.as_ref().map(|e| T::CrossAccountId::from_eth(e.who)).unwrap_or(who.clone()),118 corrected_fee,119 already_withdrawn.map(|e| e.negative_imbalance),120 )121 }122123 fn pay_priority_fee(tip: U256) {124 <EVMCurrencyAdapter<<T as Config>::Currency, ()> as pallet_evm::OnChargeEVMTransaction<T>>::pay_priority_fee(tip)125 }126}127128129pub struct BridgeSponsorshipHandler<T>(PhantomData<T>);130impl<T, C> SponsorshipHandler<T::AccountId, C> for BridgeSponsorshipHandler<T>131where132 T: Config + pallet_evm::Config,133 C: IsSubType<pallet_evm::Call<T>>,134{135 fn get_sponsor(who: &T::AccountId, call: &C) -> Option<T::AccountId> {136 match call.is_sub_type()? {137 pallet_evm::Call::call {138 source,139 target,140 input,141 ..142 } => {143 let _ = T::CallOrigin::ensure_address_origin(144 source,145 <frame_system::RawOrigin<T::AccountId>>::Signed(who.clone()).into(),146 )147 .ok()?;148 149 150 let sponsor = frame_support::storage::with_transaction(|| {151 TransactionOutcome::Rollback(T::EvmSponsorshipHandler::get_sponsor(152 &who,153 &(*target, input.clone()),154 ))155 })?;156 Some(sponsor)157 }158 _ => None,159 }160 }161}