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::{28 Runtime, Call, Origin, Balances,29 ChargeTransactionPayment,30};31use common_types::{AccountId, Balance};32use fp_self_contained::SelfContainedCall;33use pallet_unique_scheduler::DispatchCall;343536pub type SignedExtraScheduler = (37 frame_system::CheckSpecVersion<Runtime>,38 frame_system::CheckGenesis<Runtime>,39 frame_system::CheckEra<Runtime>,40 frame_system::CheckNonce<Runtime>,41 frame_system::CheckWeight<Runtime>,42);4344fn get_signed_extras(from: <Runtime as frame_system::Config>::AccountId) -> SignedExtraScheduler {45 (46 frame_system::CheckSpecVersion::<Runtime>::new(),47 frame_system::CheckGenesis::<Runtime>::new(),48 frame_system::CheckEra::<Runtime>::from(Era::Immortal),49 frame_system::CheckNonce::<Runtime>::from(frame_system::Pallet::<Runtime>::account_nonce(50 from,51 )),52 frame_system::CheckWeight::<Runtime>::new(),53 54 55 )56}5758pub struct SchedulerPaymentExecutor;5960impl<T: frame_system::Config + pallet_unique_scheduler::Config, SelfContainedSignedInfo>61 DispatchCall<T, SelfContainedSignedInfo> for SchedulerPaymentExecutor62where63 <T as frame_system::Config>::Call: Member64 + Dispatchable<Origin = Origin, Info = DispatchInfo>65 + SelfContainedCall<SignedInfo = SelfContainedSignedInfo>66 + GetDispatchInfo67 + From<frame_system::Call<Runtime>>,68 SelfContainedSignedInfo: Send + Sync + 'static,69 Call: From<<T as frame_system::Config>::Call>70 + From<<T as pallet_unique_scheduler::Config>::Call>71 + SelfContainedCall<SignedInfo = SelfContainedSignedInfo>,72 sp_runtime::AccountId32: From<<T as frame_system::Config>::AccountId>,73{74 fn dispatch_call(75 signer: <T as frame_system::Config>::AccountId,76 call: <T as pallet_unique_scheduler::Config>::Call,77 ) -> Result<78 Result<PostDispatchInfo, DispatchErrorWithPostInfo<PostDispatchInfo>>,79 TransactionValidityError,80 > {81 let dispatch_info = call.get_dispatch_info();82 let extrinsic = fp_self_contained::CheckedExtrinsic::<83 AccountId,84 Call,85 SignedExtraScheduler,86 SelfContainedSignedInfo,87 > {88 signed:89 fp_self_contained::CheckedSignature::<AccountId, SignedExtraScheduler, SelfContainedSignedInfo>::Signed(90 signer.clone().into(),91 get_signed_extras(signer.into()),92 ),93 function: call.into(),94 };9596 extrinsic.apply::<Runtime>(&dispatch_info, 0)97 }9899 fn reserve_balance(100 id: [u8; 16],101 sponsor: <T as frame_system::Config>::AccountId,102 call: <T as pallet_unique_scheduler::Config>::Call,103 count: u32,104 ) -> Result<(), DispatchError> {105 let dispatch_info = call.get_dispatch_info();106 let weight: Balance = ChargeTransactionPayment::traditional_fee(0, &dispatch_info, 0)107 .saturating_mul(count.into());108109 <Balances as NamedReservableCurrency<AccountId>>::reserve_named(110 &id,111 &(sponsor.into()),112 weight,113 )114 }115116 fn pay_for_call(117 id: [u8; 16],118 sponsor: <T as frame_system::Config>::AccountId,119 call: <T as pallet_unique_scheduler::Config>::Call,120 ) -> Result<u128, DispatchError> {121 let dispatch_info = call.get_dispatch_info();122 let weight: Balance = ChargeTransactionPayment::traditional_fee(0, &dispatch_info, 0);123 Ok(124 <Balances as NamedReservableCurrency<AccountId>>::unreserve_named(125 &id,126 &(sponsor.into()),127 weight,128 ),129 )130 }131132 fn cancel_reserve(133 id: [u8; 16],134 sponsor: <T as frame_system::Config>::AccountId,135 ) -> Result<u128, DispatchError> {136 Ok(137 <Balances as NamedReservableCurrency<AccountId>>::unreserve_named(138 &id,139 &(sponsor.into()),140 u128::MAX,141 ),142 )143 }144}