difftreelog
feat sponsor evm transactions coming from substrate
in: master
2 files changed
pallets/evm-transaction-payment/src/lib.rsdiffbeforeafterboth1#![cfg_attr(not(feature = "std"), no_std)]1#![cfg_attr(not(feature = "std"), no_std)]223pub use pallet::*;4use up_evm_mapping::EvmBackwardsAddressMapping;56#[frame_support::pallet]7pub mod pallet {8 use core::marker::PhantomData;3use core::marker::PhantomData;4use fp_evm::WithdrawReason;9 use frame_support::traits::Currency;5use frame_support::traits::{Currency, IsSubType};10 use pallet_evm::EVMCurrencyAdapter;6pub use pallet::*;11 use fp_evm::WithdrawReason;7use pallet_evm::{EVMCurrencyAdapter, EnsureAddressOrigin};12 use sp_core::{H160, U256};8use sp_core::{H160, U256};13 use sp_runtime::TransactionOutcome;9use sp_runtime::TransactionOutcome;14 use up_sponsorship::SponsorshipHandler;10use up_sponsorship::SponsorshipHandler;11use up_evm_mapping::EvmBackwardsAddressMapping;12use pallet_evm::AddressMapping;1314#[frame_support::pallet]15pub mod pallet {16 use super::*;1715 use sp_std::vec::Vec;18 use frame_support::traits::Currency;1617 type NegativeImbalanceOf<C, T> =18 <C as Currency<<T as frame_system::Config>::AccountId>>::NegativeImbalance;19 use sp_std::vec::Vec;192020 #[pallet::config]21 #[pallet::config]21 pub trait Config: frame_system::Config {22 pub trait Config: frame_system::Config {22 type SponsorshipHandler: SponsorshipHandler<H160, (H160, Vec<u8>)>;23 type EvmSponsorshipHandler: SponsorshipHandler<H160, (H160, Vec<u8>)>;23 type Currency: Currency<Self::AccountId>;24 type Currency: Currency<Self::AccountId>;24 type EvmBackwardsAddressMapping: EvmBackwardsAddressMapping<Self::AccountId>;25 type EvmBackwardsAddressMapping: EvmBackwardsAddressMapping<Self::AccountId>;26 type EvmAddressMapping: AddressMapping<Self::AccountId>;25 }27 }262827 #[pallet::pallet]29 #[pallet::pallet]28 #[pallet::generate_store(pub(super) trait Store)]30 #[pallet::generate_store(pub(super) trait Store)]29 pub struct Pallet<T>(_);31 pub struct Pallet<T>(_);32}3334type NegativeImbalanceOf<C, T> =35 <C as Currency<<T as frame_system::Config>::AccountId>>::NegativeImbalance;303631 pub struct ChargeEvmLiquidityInfo<T>37pub struct ChargeEvmLiquidityInfo<T>32 where38where44 WithdrawReason::Call { target, input } => {50 WithdrawReason::Call { target, input } => {45 // This method is only used for checking, we shouldn't touch storage in it51 // This method is only used for checking, we shouldn't touch storage in it46 frame_support::storage::with_transaction(|| {52 frame_support::storage::with_transaction(|| {47 TransactionOutcome::Rollback(T::SponsorshipHandler::get_sponsor(53 TransactionOutcome::Rollback(T::EvmSponsorshipHandler::get_sponsor(48 &origin,54 &origin,49 &(*target, input.clone()),55 &(*target, input.clone()),50 ))56 ))70 ) -> core::result::Result<Self::LiquidityInfo, pallet_evm::Error<T>> {75 ) -> core::result::Result<Self::LiquidityInfo, pallet_evm::Error<T>> {71 let mut who_pays_fee = *who;76 let mut who_pays_fee = *who;72 if let WithdrawReason::Call { target, input } = &reason {77 if let WithdrawReason::Call { target, input } = &reason {73 who_pays_fee = T::SponsorshipHandler::get_sponsor(who, &(*target, input.clone()))78 who_pays_fee = T::EvmSponsorshipHandler::get_sponsor(who, &(*target, input.clone()))74 .unwrap_or(who_pays_fee);79 .unwrap_or(who_pays_fee);75 }80 }76 let negative_imbalance =81 let negative_imbalance = EVMCurrencyAdapter::<<T as Config>::Currency, ()>::withdraw_fee(97 )101 )98 }102 }99 }103}100}104105/// Implements sponsoring for evm calls performed from pallet-evm (via api.tx.ethereum.transact/api.tx.evm.call)106pub struct BridgeSponsorshipHandler<T>(PhantomData<T>);107impl<T, C> SponsorshipHandler<T::AccountId, C> for BridgeSponsorshipHandler<T>108where109 T: Config + pallet_evm::Config,110 C: IsSubType<pallet_evm::Call<T>>,111{112 fn get_sponsor(who: &T::AccountId, call: &C) -> Option<T::AccountId> {113 match call.is_sub_type()? {114 pallet_evm::Call::call {115 source,116 target,117 input,118 ..119 } => {120 let _ = T::CallOrigin::ensure_address_origin(121 source,122 <frame_system::RawOrigin<T::AccountId>>::Signed(who.clone()).into(),123 )124 .ok()?;125 let who = T::EvmBackwardsAddressMapping::from_account_id(who.clone());126 // Effects from EvmSponsorshipHandler are applied in OnChargeEvmTransaction by pallet_evm::runner127 // TODO: Should we implement simulation mode (test, but do not apply effects) in `up-sponsorship`?128 let sponsor = frame_support::storage::with_transaction(|| {129 TransactionOutcome::Rollback(T::EvmSponsorshipHandler::get_sponsor(130 &who,131 &(target.clone(), input.clone()),132 ))133 })?;134 let sponsor = T::EvmAddressMapping::into_account_id(sponsor);135 Some(sponsor)136 }137 _ => None,138 }139 }140}101141runtime/src/lib.rsdiffbeforeafterboth777 pub const MaxScheduledPerBlock: u32 = 50;777 pub const MaxScheduledPerBlock: u32 = 50;778}778}779779780pub struct Sponsoring;781impl SponsoringResolve<AccountId, Call> for Sponsoring {782 fn resolve(who: &AccountId, call: &Call) -> Option<AccountId>783 where784 Call: Dispatchable<Info = DispatchInfo>,780type EvmSponsorshipHandler = (785 AccountId: AsRef<[u8]>,781 pallet_nft::NftEthSponsorshipHandler<Runtime>,786 {787 pallet_nft_transaction_payment::Module::<Runtime>::withdraw_type(who, call)782 pallet_evm_contract_helpers::HelpersContractSponsoring<Runtime>,788 }783);789}790791type SponsorshipHandler = (784type SponsorshipHandler = (792 pallet_nft::NftSponsorshipHandler<Runtime>,785 pallet_nft::NftSponsorshipHandler<Runtime>,793 //pallet_contract_helpers::ContractSponsorshipHandler<Runtime>,786 //pallet_contract_helpers::ContractSponsorshipHandler<Runtime>,787 pallet_evm_transaction_payment::BridgeSponsorshipHandler<Runtime>,794);788);795789796impl pallet_unq_scheduler::Config for Runtime {790impl pallet_unq_scheduler::Config for Runtime {806}800}807801808impl pallet_evm_transaction_payment::Config for Runtime {802impl pallet_evm_transaction_payment::Config for Runtime {809 type SponsorshipHandler = (803 type EvmSponsorshipHandler = EvmSponsorshipHandler;810 pallet_nft::NftEthSponsorshipHandler<Self>,811 pallet_evm_contract_helpers::HelpersContractSponsoring<Self>,812 );813 type Currency = Balances;804 type Currency = Balances;805 type EvmAddressMapping = HashedAddressMapping<Self::Hashing>;814 type EvmBackwardsAddressMapping = up_evm_mapping::MapBackwardsAddressTruncated;806 type EvmBackwardsAddressMapping = up_evm_mapping::MapBackwardsAddressTruncated;815}807}816808817impl pallet_nft_charge_transaction::Config for Runtime {809impl pallet_nft_charge_transaction::Config for Runtime {818 type SponsorshipHandler = pallet_nft::NftSponsorshipHandler<Runtime>;810 type SponsorshipHandler = SponsorshipHandler;819}811}820812821// impl pallet_contract_helpers::Config for Runtime {813// impl pallet_contract_helpers::Config for Runtime {