#![cfg_attr(not(feature = "std"), no_std)]
use codec::{Decode, Encode};
use frame_support::{
dispatch::{CheckIfFeeless, DispatchResult},
pallet_prelude::TransactionSource,
traits::{IsType, OriginTrait},
weights::Weight,
};
use scale_info::{StaticTypeInfo, TypeInfo};
use sp_runtime::{
traits::{
DispatchInfoOf, DispatchOriginOf, PostDispatchInfoOf, TransactionExtension, ValidateResult,
},
transaction_validity::TransactionValidityError,
};
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
pub use pallet::*;
#[frame_support::pallet]
pub mod pallet {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
}
#[pallet::pallet]
pub struct Pallet<T>(_);
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
FeeSkipped { origin: <T::RuntimeOrigin as OriginTrait>::PalletsOrigin },
}
}
#[derive(Encode, Decode, Clone, Eq, PartialEq)]
pub struct SkipCheckIfFeeless<T, S>(pub S, core::marker::PhantomData<T>);
impl<T, S: StaticTypeInfo> TypeInfo for SkipCheckIfFeeless<T, S> {
type Identity = S;
fn type_info() -> scale_info::Type {
S::type_info()
}
}
impl<T, S: Encode> core::fmt::Debug for SkipCheckIfFeeless<T, S> {
#[cfg(feature = "std")]
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "SkipCheckIfFeeless<{:?}>", self.0.encode())
}
#[cfg(not(feature = "std"))]
fn fmt(&self, _: &mut core::fmt::Formatter) -> core::fmt::Result {
Ok(())
}
}
impl<T, S> From<S> for SkipCheckIfFeeless<T, S> {
fn from(s: S) -> Self {
Self(s, core::marker::PhantomData)
}
}
pub enum Intermediate<T, O> {
Apply(T),
Skip(O),
}
use Intermediate::*;
impl<T: Config + Send + Sync, S: TransactionExtension<T::RuntimeCall>>
TransactionExtension<T::RuntimeCall> for SkipCheckIfFeeless<T, S>
where
T::RuntimeCall: CheckIfFeeless<Origin = frame_system::pallet_prelude::OriginFor<T>>,
{
const IDENTIFIER: &'static str = S::IDENTIFIER;
type Implicit = S::Implicit;
fn implicit(&self) -> Result<Self::Implicit, TransactionValidityError> {
self.0.implicit()
}
type Val =
Intermediate<S::Val, <DispatchOriginOf<T::RuntimeCall> as OriginTrait>::PalletsOrigin>;
type Pre =
Intermediate<S::Pre, <DispatchOriginOf<T::RuntimeCall> as OriginTrait>::PalletsOrigin>;
fn weight(&self, call: &T::RuntimeCall) -> frame_support::weights::Weight {
self.0.weight(call)
}
fn validate(
&self,
origin: DispatchOriginOf<T::RuntimeCall>,
call: &T::RuntimeCall,
info: &DispatchInfoOf<T::RuntimeCall>,
len: usize,
self_implicit: S::Implicit,
inherited_implication: &impl Encode,
source: TransactionSource,
) -> ValidateResult<Self::Val, T::RuntimeCall> {
if call.is_feeless(&origin) {
Ok((Default::default(), Skip(origin.caller().clone()), origin))
} else {
let (x, y, z) = self.0.validate(
origin,
call,
info,
len,
self_implicit,
inherited_implication,
source,
)?;
Ok((x, Apply(y), z))
}
}
fn prepare(
self,
val: Self::Val,
origin: &DispatchOriginOf<T::RuntimeCall>,
call: &T::RuntimeCall,
info: &DispatchInfoOf<T::RuntimeCall>,
len: usize,
) -> Result<Self::Pre, TransactionValidityError> {
match val {
Apply(val) => self.0.prepare(val, origin, call, info, len).map(Apply),
Skip(origin) => Ok(Skip(origin)),
}
}
fn post_dispatch_details(
pre: Self::Pre,
info: &DispatchInfoOf<T::RuntimeCall>,
post_info: &PostDispatchInfoOf<T::RuntimeCall>,
len: usize,
result: &DispatchResult,
) -> Result<Weight, TransactionValidityError> {
match pre {
Apply(pre) => S::post_dispatch_details(pre, info, post_info, len, result),
Skip(origin) => {
Pallet::<T>::deposit_event(Event::<T>::FeeSkipped { origin });
Ok(Weight::zero())
},
}
}
}