pallet_skip_feeless_payment/
lib.rs#![cfg_attr(not(feature = "std"), no_std)]
use codec::{Decode, Encode};
use frame_support::{
dispatch::{CheckIfFeeless, DispatchResult},
traits::{IsType, OriginTrait},
};
use scale_info::{StaticTypeInfo, TypeInfo};
use sp_runtime::{
traits::{DispatchInfoOf, PostDispatchInfoOf, SignedExtension},
transaction_validity::{TransactionValidity, TransactionValidityError, ValidTransaction},
};
#[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 { who: T::AccountId },
}
}
#[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)
}
}
impl<T: Config + Send + Sync, S: SignedExtension<AccountId = T::AccountId>> SignedExtension
for SkipCheckIfFeeless<T, S>
where
S::Call: CheckIfFeeless<Origin = frame_system::pallet_prelude::OriginFor<T>>,
{
type AccountId = T::AccountId;
type Call = S::Call;
type AdditionalSigned = S::AdditionalSigned;
type Pre = (Self::AccountId, Option<<S as SignedExtension>::Pre>);
const IDENTIFIER: &'static str = S::IDENTIFIER;
fn additional_signed(&self) -> Result<Self::AdditionalSigned, TransactionValidityError> {
self.0.additional_signed()
}
fn validate(
&self,
who: &Self::AccountId,
call: &Self::Call,
info: &DispatchInfoOf<Self::Call>,
len: usize,
) -> TransactionValidity {
if call.is_feeless(&<T as frame_system::Config>::RuntimeOrigin::signed(who.clone())) {
Ok(ValidTransaction::default())
} else {
self.0.validate(who, call, info, len)
}
}
fn pre_dispatch(
self,
who: &Self::AccountId,
call: &Self::Call,
info: &DispatchInfoOf<Self::Call>,
len: usize,
) -> Result<Self::Pre, TransactionValidityError> {
if call.is_feeless(&<T as frame_system::Config>::RuntimeOrigin::signed(who.clone())) {
Ok((who.clone(), None))
} else {
Ok((who.clone(), Some(self.0.pre_dispatch(who, call, info, len)?)))
}
}
fn post_dispatch(
pre: Option<Self::Pre>,
info: &DispatchInfoOf<Self::Call>,
post_info: &PostDispatchInfoOf<Self::Call>,
len: usize,
result: &DispatchResult,
) -> Result<(), TransactionValidityError> {
if let Some(pre) = pre {
if let Some(pre) = pre.1 {
S::post_dispatch(Some(pre), info, post_info, len, result)?;
} else {
Pallet::<T>::deposit_event(Event::<T>::FeeSkipped { who: pre.0 });
}
}
Ok(())
}
}