git.delta.rocks / unique-network / refs/commits / bd337d6ea58c

difftreelog

source

runtime/common/scheduler.rs4.7 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 codec::Encode;28use crate::{Runtime, Call, Origin, Balances};29use up_common::types::{AccountId, Balance};30use fp_self_contained::SelfContainedCall;31use pallet_unique_scheduler::DispatchCall;32use pallet_transaction_payment::ChargeTransactionPayment;3334type SponsorshipChargeTransactionPayment =35	pallet_charge_transaction::ChargeTransactionPayment<Runtime>;3637/// The SignedExtension to the basic transaction logic.38pub type SignedExtraScheduler = (39	frame_system::CheckSpecVersion<Runtime>,40	frame_system::CheckGenesis<Runtime>,41	frame_system::CheckEra<Runtime>,42	frame_system::CheckNonce<Runtime>,43	frame_system::CheckWeight<Runtime>,44	ChargeTransactionPayment<Runtime>,45);4647fn get_signed_extras(from: <Runtime as frame_system::Config>::AccountId) -> SignedExtraScheduler {48	(49		frame_system::CheckSpecVersion::<Runtime>::new(),50		frame_system::CheckGenesis::<Runtime>::new(),51		frame_system::CheckEra::<Runtime>::from(Era::Immortal),52		frame_system::CheckNonce::<Runtime>::from(frame_system::Pallet::<Runtime>::account_nonce(53			from,54		)),55		frame_system::CheckWeight::<Runtime>::new(),56		ChargeTransactionPayment::<Runtime>::from(0),57	)58}5960pub struct SchedulerPaymentExecutor;6162impl<T: frame_system::Config + pallet_unique_scheduler::Config, SelfContainedSignedInfo>63	DispatchCall<T, SelfContainedSignedInfo> for SchedulerPaymentExecutor64where65	<T as frame_system::Config>::Call: Member66		+ Dispatchable<Origin = Origin, Info = DispatchInfo>67		+ SelfContainedCall<SignedInfo = SelfContainedSignedInfo>68		+ GetDispatchInfo69		+ From<frame_system::Call<Runtime>>,70	SelfContainedSignedInfo: Send + Sync + 'static,71	Call: From<<T as frame_system::Config>::Call>72		+ From<<T as pallet_unique_scheduler::Config>::Call>73		+ SelfContainedCall<SignedInfo = SelfContainedSignedInfo>,74	sp_runtime::AccountId32: From<<T as frame_system::Config>::AccountId>,75{76	fn dispatch_call(77		signer: <T as frame_system::Config>::AccountId,78		call: <T as pallet_unique_scheduler::Config>::Call,79	) -> Result<80		Result<PostDispatchInfo, DispatchErrorWithPostInfo<PostDispatchInfo>>,81		TransactionValidityError,82	> {83		let dispatch_info = call.get_dispatch_info();84		let len = call.encoded_size();85		let extrinsic = fp_self_contained::CheckedExtrinsic::<86			AccountId,87			Call,88			SignedExtraScheduler,89			SelfContainedSignedInfo,90		> {91			signed: fp_self_contained::CheckedSignature::<92				AccountId,93				SignedExtraScheduler,94				SelfContainedSignedInfo,95			>::Signed(signer.clone().into(), get_signed_extras(signer.into())),96			function: call.into(),97		};9899		extrinsic.apply::<Runtime>(&dispatch_info, len)100	}101102	fn reserve_balance(103		id: [u8; 16],104		sponsor: <T as frame_system::Config>::AccountId,105		call: <T as pallet_unique_scheduler::Config>::Call,106		count: u32,107	) -> Result<(), DispatchError> {108		let dispatch_info = call.get_dispatch_info();109		let weight: Balance =110			SponsorshipChargeTransactionPayment::traditional_fee(0, &dispatch_info, 0)111				.saturating_mul(count.into());112113		<Balances as NamedReservableCurrency<AccountId>>::reserve_named(114			&id,115			&(sponsor.into()),116			weight,117		)118	}119120	fn pay_for_call(121		id: [u8; 16],122		sponsor: <T as frame_system::Config>::AccountId,123		call: <T as pallet_unique_scheduler::Config>::Call,124	) -> Result<u128, DispatchError> {125		let dispatch_info = call.get_dispatch_info();126		let weight: Balance =127			SponsorshipChargeTransactionPayment::traditional_fee(0, &dispatch_info, 0);128		Ok(129			<Balances as NamedReservableCurrency<AccountId>>::unreserve_named(130				&id,131				&(sponsor.into()),132				weight,133			),134		)135	}136137	fn cancel_reserve(138		id: [u8; 16],139		sponsor: <T as frame_system::Config>::AccountId,140	) -> Result<u128, DispatchError> {141		Ok(142			<Balances as NamedReservableCurrency<AccountId>>::unreserve_named(143				&id,144				&(sponsor.into()),145				u128::MAX,146			),147		)148	}149}