1234567891011121314151617use frame_support::{18 traits::NamedReservableCurrency,19 weights::{GetDispatchInfo, PostDispatchInfo, DispatchInfo},20};21use sp_runtime::{22 traits::{Dispatchable, Applyable, Member},23 generic::Era,24 transaction_validity::TransactionValidityError,25 DispatchErrorWithPostInfo, DispatchError,26};27use crate::{Runtime, Call, Origin, Balances, ChargeTransactionPayment};28use common_types::{AccountId, Balance};29use fp_self_contained::SelfContainedCall;30use pallet_unique_scheduler::DispatchCall;313233pub type SignedExtraScheduler = (34 frame_system::CheckSpecVersion<Runtime>,35 frame_system::CheckGenesis<Runtime>,36 frame_system::CheckEra<Runtime>,37 frame_system::CheckNonce<Runtime>,38 frame_system::CheckWeight<Runtime>,39);4041fn get_signed_extras(from: <Runtime as frame_system::Config>::AccountId) -> SignedExtraScheduler {42 (43 frame_system::CheckSpecVersion::<Runtime>::new(),44 frame_system::CheckGenesis::<Runtime>::new(),45 frame_system::CheckEra::<Runtime>::from(Era::Immortal),46 frame_system::CheckNonce::<Runtime>::from(frame_system::Pallet::<Runtime>::account_nonce(47 from,48 )),49 frame_system::CheckWeight::<Runtime>::new(),50 51 52 )53}5455pub struct SchedulerPaymentExecutor;5657impl<T: frame_system::Config + pallet_unique_scheduler::Config, SelfContainedSignedInfo>58 DispatchCall<T, SelfContainedSignedInfo> for SchedulerPaymentExecutor59where60 <T as frame_system::Config>::Call: Member61 + Dispatchable<Origin = Origin, Info = DispatchInfo>62 + SelfContainedCall<SignedInfo = SelfContainedSignedInfo>63 + GetDispatchInfo64 + From<frame_system::Call<Runtime>>,65 SelfContainedSignedInfo: Send + Sync + 'static,66 Call: From<<T as frame_system::Config>::Call>67 + From<<T as pallet_unique_scheduler::Config>::Call>68 + SelfContainedCall<SignedInfo = SelfContainedSignedInfo>,69 sp_runtime::AccountId32: From<<T as frame_system::Config>::AccountId>,70{71 fn dispatch_call(72 signer: <T as frame_system::Config>::AccountId,73 call: <T as pallet_unique_scheduler::Config>::Call,74 ) -> Result<75 Result<PostDispatchInfo, DispatchErrorWithPostInfo<PostDispatchInfo>>,76 TransactionValidityError,77 > {78 let dispatch_info = call.get_dispatch_info();79 let extrinsic = fp_self_contained::CheckedExtrinsic::<80 AccountId,81 Call,82 SignedExtraScheduler,83 SelfContainedSignedInfo,84 > {85 signed: fp_self_contained::CheckedSignature::<86 AccountId,87 SignedExtraScheduler,88 SelfContainedSignedInfo,89 >::Signed(signer.clone().into(), get_signed_extras(signer.into())),90 function: call.into(),91 };9293 extrinsic.apply::<Runtime>(&dispatch_info, 0)94 }9596 fn reserve_balance(97 id: [u8; 16],98 sponsor: <T as frame_system::Config>::AccountId,99 call: <T as pallet_unique_scheduler::Config>::Call,100 count: u32,101 ) -> Result<(), DispatchError> {102 let dispatch_info = call.get_dispatch_info();103 let weight: Balance = ChargeTransactionPayment::traditional_fee(0, &dispatch_info, 0)104 .saturating_mul(count.into());105106 <Balances as NamedReservableCurrency<AccountId>>::reserve_named(107 &id,108 &(sponsor.into()),109 weight,110 )111 }112113 fn pay_for_call(114 id: [u8; 16],115 sponsor: <T as frame_system::Config>::AccountId,116 call: <T as pallet_unique_scheduler::Config>::Call,117 ) -> Result<u128, DispatchError> {118 let dispatch_info = call.get_dispatch_info();119 let weight: Balance = ChargeTransactionPayment::traditional_fee(0, &dispatch_info, 0);120 Ok(121 <Balances as NamedReservableCurrency<AccountId>>::unreserve_named(122 &id,123 &(sponsor.into()),124 weight,125 ),126 )127 }128129 fn cancel_reserve(130 id: [u8; 16],131 sponsor: <T as frame_system::Config>::AccountId,132 ) -> Result<u128, DispatchError> {133 Ok(134 <Balances as NamedReservableCurrency<AccountId>>::unreserve_named(135 &id,136 &(sponsor.into()),137 u128::MAX,138 ),139 )140 }141}