1234567891011121314151617#![cfg_attr(not(feature = "std"), no_std)]1819pub use pallet::*;20use frame_support::pallet_prelude::*;21use frame_system::pallet_prelude::*;2223#[frame_support::pallet(dev_mode)]24pub mod pallet {25 use frame_support::{26 pallet_prelude::*,27 dispatch::{Dispatchable, GetDispatchInfo, PostDispatchInfo},28 traits::{UnfilteredDispatchable, IsSubType, OriginTrait},29 };30 use frame_system::pallet_prelude::*;31 use sp_std::vec::Vec;32 3334 #[pallet::config]35 pub trait Config: frame_system::Config {36 type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;3738 39 type RuntimeCall: Parameter40 + Dispatchable<41 RuntimeOrigin = <Self as frame_system::Config>::RuntimeOrigin,42 PostInfo = PostDispatchInfo,43 > + GetDispatchInfo44 + From<frame_system::Call<Self>>45 + UnfilteredDispatchable<RuntimeOrigin = <Self as frame_system::Config>::RuntimeOrigin>46 + IsSubType<Call<Self>>47 + IsType<<Self as frame_system::Config>::RuntimeCall>;48 }4950 #[pallet::event]51 #[pallet::generate_deposit(pub(super) fn deposit_event)]52 pub enum Event<T: Config> {53 ValueIsSet,54 ShouldRollback,55 BatchCompleted,56 }5758 #[pallet::pallet]59 pub struct Pallet<T>(_);6061 #[pallet::storage]62 #[pallet::getter(fn is_enabled)]63 pub type Enabled<T> = StorageValue<_, bool, ValueQuery>;6465 #[pallet::storage]66 #[pallet::getter(fn test_value)]67 pub type TestValue<T> = StorageValue<_, u32, ValueQuery>;6869 #[pallet::error]70 pub enum Error<T> {71 TestPalletDisabled,72 TriggerRollback,73 }7475 #[pallet::call]76 impl<T: Config> Pallet<T> {77 #[pallet::call_index(0)]78 #[pallet::weight(10_000)]79 pub fn enable(origin: OriginFor<T>) -> DispatchResult {80 ensure_root(origin)?;81 <Enabled<T>>::set(true);8283 Ok(())84 }8586 #[pallet::call_index(1)]87 #[pallet::weight(10_000)]88 pub fn set_test_value(origin: OriginFor<T>, value: u32) -> DispatchResult {89 Self::ensure_origin_and_enabled(origin)?;9091 <TestValue<T>>::put(value);9293 Self::deposit_event(Event::ValueIsSet);9495 Ok(())96 }9798 #[pallet::call_index(2)]99 #[pallet::weight(10_000)]100 pub fn set_test_value_and_rollback(origin: OriginFor<T>, value: u32) -> DispatchResult {101 Self::set_test_value(origin, value)?;102103 Self::deposit_event(Event::ShouldRollback);104105 Err(<Error<T>>::TriggerRollback.into())106 }107108 #[pallet::call_index(3)]109 #[pallet::weight(10_000)]110 pub fn inc_test_value(origin: OriginFor<T>) -> DispatchResult {111 Self::set_test_value(origin, <TestValue<T>>::get() + 1)112 }113114 115 116 117 118 119 120 121 122123 124 125 126127 128 129130 #[pallet::call_index(4)]131 #[pallet::weight(100_000_000)]132 pub fn just_take_fee(origin: OriginFor<T>) -> DispatchResult {133 Self::ensure_origin_and_enabled(origin)?;134 Ok(())135 }136137 #[pallet::call_index(5)]138 #[pallet::weight(10_000)]139 pub fn batch_all(140 origin: OriginFor<T>,141 calls: Vec<<T as Config>::RuntimeCall>,142 ) -> DispatchResultWithPostInfo {143 Self::ensure_origin_and_enabled(origin.clone())?;144145 let is_root = ensure_root(origin.clone()).is_ok();146147 for call in calls {148 if is_root {149 call.dispatch_bypass_filter(origin.clone())?;150 } else {151 let mut filtered_origin = origin.clone();152 153 filtered_origin.add_filter(154 move |c: &<T as frame_system::Config>::RuntimeCall| {155 let c = <T as Config>::RuntimeCall::from_ref(c);156 !matches!(c.is_sub_type(), Some(Call::batch_all { .. }))157 },158 );159 call.dispatch(filtered_origin)?;160 }161 }162163 Self::deposit_event(Event::BatchCompleted);164 Ok(None::<Weight>.into())165 }166 }167}168169impl<T: Config> Pallet<T> {170 fn ensure_origin_and_enabled(origin: OriginFor<T>) -> DispatchResult {171 ensure_signed(origin)?;172 <Enabled<T>>::get()173 .then_some(())174 .ok_or(<Error<T>>::TestPalletDisabled.into())175 }176}