git.delta.rocks / unique-network / refs/commits / 3cddb472af6f

difftreelog

source

runtime/common/scheduler.rs4.5 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use 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 up_common::types::{AccountId, Balance};29use fp_self_contained::SelfContainedCall;30use pallet_unique_scheduler::DispatchCall;3132/// The SignedExtension to the basic transaction logic.33pub 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		// sponsoring transaction logic51		// pallet_charge_transaction::ChargeTransactionPayment::<Runtime>::new(0),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}